andrey-moura / db

A C++ wrapper around SQL based on ActiveRecord

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

database

Simple cross-platform library based in the ActiveRecord from Ruby, an wrapper around SQL.

Building

Linux

git clone https://github.com/Moonslate/database.git
mkdir build
cd build
cmake ..
make

Basic Usage

User user;
user.name = "Dummy";
user.password = "Some Password";
user.age = 21;
user.save();

The code above executes

INSERT INTO users(name,password,age) VALUES ('Dummy','Some Password',21) RETURNING id;

The same can be archivied by

User user = User::create({
    { "name", "Dummy" },
    { "password", "Some Password" }
    { "age", 21 }
}); 

Creating a new record and a table

database new-model user --migrate

new-model generates the following code

include/models/user.hpp

#include <core.hpp>
#include <database.hpp>

class User : public uva::database::basic_active_record
{    
    uva_database_declare(User);
};

src/models/user.cpp

#include <user.hpp>

uva_database_define(User);

The optional --migrate option generates the following migration

src\migrations\20230129161842_add_users_migration.cpp

#include <core.hpp>
#include <database.hpp>

#include <user.hpp>

class AddUsersMigration : public uva::database::basic_migration
{
uva_declare_migration(AddUsersMigration);
protected:
    virtual void change() override 
    { 
        add_table("users",
        {
            { "id",         "INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" },
            { "updated_at", "INTEGER NOT NULL DEFAULT (STRFTIME('%s'))" },
            { "created_at", "INTEGER NOT NULL DEFAULT (STRFTIME('%s'))" },
            { "removed",    "INTEGER NOT NULL DEFAULT 0" },
        });
    }
};

uva_define_migration(AddUsersMigration);

Columns are defined inside the map in second parameter of add_table. Those columns are accessed by User::operator[](std::string), they'll only be available with the User::operator. when exposing the column inside the class:

uva_database_expose_column(password);

Supported database engines

  • SQLite3

Todo For Next (First) Release

  • Before save, update πŸ‘Œ
  • Create database tool πŸ‘Œ
  • Move multiple_value_holder to uva::core (and rename to var) πŸ‘Œ
  • Create database_exception
  • Strongly typed var πŸ‘Œ
  • Complete Todo List of uva::string
  • Complete Todo List of uva::string
  • Complete Todo List of uva::cspec
  • Have 100% of tests coverage

Priority (For future releases)

  • 🐞 - Copy constructor of base class not works

Contributing

Just make a PR! :)

About

A C++ wrapper around SQL based on ActiveRecord


Languages

Language:C 99.1%Language:C++ 0.9%Language:CMake 0.0%