joeyates / cpp-active-record Goto Github PK
View Code? Open in Web Editor NEWA C++ Implementation of the Active Record Pattern
License: GNU General Public License v3.0
A C++ Implementation of the Active Record Pattern
License: GNU General Public License v3.0
See #8 for context.
Placeholders are different between SQLite ('?') and PostgreSQL ('$1')
Reference:
table_set.cpp:68
void TableSet::update_table( Table &required )
required.fields()
and existing.fields()
will always be different because of the automatic primary key assignment. Using the example table below:
td.fields() = fields
("code", ActiveRecord::text)
("title", ActiveRecord::text);
required.fields()
will contain: [code, title]
existing.fields()
will contain: [id, code, title]
Thus, the set difference required.fields() - existing.fields()
will always return the primary key id
and try to drop it.
Proposed solution:
void TableSet::update_table( Table &required ) {
log( "TableSet::update_table" );
log( required.table_name() );
Table existing = table_data( required.connection(), required.table_name() );
Fields missing = required.fields() - existing.fields();
Fields remove = existing.fields() - required.fields();
for( Fields::iterator it = missing.begin(); it != missing.end(); ++it )
existing.add_field( *it );
for( Fields::iterator it = remove.begin(); it != remove.end(); ++it ) {
if ((*it).name() == existing.primary_key()) /* fix */
continue;
throw ActiveRecordException( "Table::remove_field not yet implemented", __FILE__, __LINE__ );
//existing.remove_field( *it );
}
}
Better solution:
The fields()
method should always return the same Fields
no matter the context (ie. database fetch or class). The TableSet::table_data
function should not load the primary key as a Field
but instead only set the id_
variable of the Table
. Of course I say this without fully understanding the impact, but hopefully you agree with keeping everything deterministic.
When compiling, from the master branch, the unit tests fail when resolving and compiling the AR_HAS_MANY macro:
include/active_record/base.h:30:59: error: declaration of ‘std::vector<T1> ActiveRecord::Base<T>::has_many()’ outside of class is not definition [-fpermissive]
vector< other > ActiveRecord::Base< owner >::has_many(); \
^
test/base_associations_test.cpp:29:1: note: in expansion of macro ‘AR_HAS_MANY’
AR_HAS_MANY( Library, Book )
gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)
Only raise exceptions which are subclasses of ActiveRecordException.
If true, add 2 fields: created_at, updated_at
base.h
bool load();
stringstream is alive and used when throwing exception.
During throwing exception and memory deployment we have "Aborted".
This one works fine
template <class T>
bool Base<T>::load() {
{
std::stringstream ss;
ss << "SELECT * ";
ss << "FROM " << table_name_ << " ";
ss << "WHERE ";
ss << primary_key_ << " = ?";
AttributeList parameters;
parameters.push_back(id());
Row row = connection_->select_one(ss.str(), parameters);
if(row.has_data())
{
attributes_ = row.attributes();
state_ = loaded;
return true;
}
}
throw ActiveRecordException("Record not found", __FILE__, __LINE__);
return false;
}
Hello!
In your code of sqlite3_attribute.cpp
we can see something like this:
// TYPE_LIST
if(strcasecmp(type, "INTEGER") == 0) {
return static_cast<int64_t>(sqlite3_column_int(pStmt, i));
}
But if we ask https://www.sqlite.org we can get something like this:
SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
Of course we haven't int64_t
becouse we have int
We can use something like this:
SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
And finally:
// TYPE_LIST
if(strcasecmp(type, "INTEGER") == 0) {
return sqlite3_column_int64(pStmt, i);
}
And also in sqlite3_connection.cpp
case Type::integer: {
int64_t value = boost::get<int64_t>(parameter);
pa.param_values[i] = nullptr;
sqlite3_bind_int(ppStmt, i + 1, value);
break;
}
We must use sqlite3_bind_int64
Done)
This library is very useful for me. And I hope that we can make it even better.
Thanks)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.