Skip to content

respu/arg3db

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libarg3db

a sqlite3 and mysql wrapper / active record (ish) implementation

Building

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.

Coding Style

  • 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

Model

/* 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

Records

Base Record

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();
    }
};

Query records

    /* 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 record

    /* save a user */
    user obj;

    obj.set("first_name", "John");
    obj.set("last_name", "Doe");

    if(!obj.save())
    	cout << testdb.last_error() << endl;

Delete a record

    user obj(1); // id constructor

    if(!obj.de1ete())
        cout << testdb.last_error() << endl;

Basic Queries

Modify Query

/* 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 Query

/* 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"
    ...
}

TODO

  • More tests
  • More database implementations

About

a sqlite3 wrapper for c++11.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published