a sqlite3 and mysql wrapper / active record (ish) implementation
OSX:
Download and install homebrew.
brew install premake
premake4 gmake
make
Windows:
- Download premake.
- run
premake4 vs2010
. - Open the generated project file in Visual Studio.
- class/struct/method names are all lower case with underscores separating words
- non public members are camel case with and underscore at end of the name
- macros, enums and constants are all upper case with underscores seperating words
- braces on a new line
/* database interfaces */ sqldb - interface for a specific database |- statement - interface for a prepared statement |- resultset - results of a statement |- row - an single result |- column - a field in a row containing a value /* implementations using the above*/ schema - a definition of a table schema_factory - cached schemas base_record - the active record (ish) implementation select_query - builds select queries modify_query - builds update/insert queries delete_query - builds delete queries sql_value - storage and conversion for basic sql types
arg3::db::sqlite3_db testdb("test.db");
//arg3::db::mysql_db testdb("database", "user", "password", "localhost", 3306);
class user : public arg3::db::base_record<user>
{
constexpr static const char *const ID_COLUMN = "id";
constexpr static const char *const TABLE_NAME = "users";
public:
/* default constructor */
user() : base_record(&testdb, TABLE_NAME, ID_COLUMN) {}
/* results constructor */
user(const row &values) : base_record(&testdb, TABLE_NAME, ID_COLUMN, values) {}
/* id constructor */
user(long id) : base_record(&testdb, TABLE_NAME, ID_COLUMN, id) {}
/* utility method showing how to get columns */
string to_string() const
{
ostringstream buf;
buf << id() << ": " << get("first_name") << " " << get("last_name");
return buf.str();
}
};
/* get all users */
auto results = user().find_all();
for (auto &user : results)
{
cout << "User: " << user.to_string() << endl;
}
results = user().find_by("first_name", "Joe");
for (auto &user : results)
{
cout << "Found user: " << user.to_string() << endl;
}
/* save a user */
user obj;
obj.set("first_name", "John");
obj.set("last_name", "Doe");
if(!obj.save())
cout << testdb.last_error() << endl;
user obj(1); // id constructor
if(!obj.de1ete())
cout << testdb.last_error() << endl;
/* upsert a user */
arg3::db::modify_query query(&testdb, "users", { "id", "first_name", "last_name" });
query.bind(1, 1234).bind(2, "happy").bind(3, "gilmour");
/* saves user { "id": 1, "first_name": "happy", "last_name": "gilmour" } */
if(!query.execute())
cout << testdb.last_error() << endl;
/* select some users */
arg3::db::select_query query(&testdb, "users");
query.where("last_name = ?");
query.bind(1, "Jenkins");
auto results = query.execute();
for(auto &row: results)
{
string lName = row["last_name"]; // "Jenkins"
...
}
- More tests
- More database implementations