Giter Club home page Giter Club logo

mysql_orm's Introduction

mysql_orm

A C++ ORM for MySQL, inspired by sqlite_orm.

mysql_orm manages your queries to a MySQL database with an intuitive syntax and as much type-checking as possible at compile-time (hence reducing risks that a query is ill-formed).

Consider the following structure:

struct Record
{
  uint32_t id;
  int i;
  std::string s;
};

We can create a table from this structure the following way:

auto table_records = make_table("records",
                                make_column<&Record::id>("id"),
                                make_column<&Record::i>("i"),
                                make_column<&Record::s>("s"));

mysql_orm automatically deduces the types of the fields and the one of the structure (which we call the model). If the fields do not refer to the same model, an error is raised at compile-time.

We can now make a database connection from the table:

auto connection = mysql_orm::Connection("localhost", 3306, "username", "password", "database");
auto database = make_database(connection, table_records);

Any number of tables may be supplied when constructing a database. Any model whose table is given to the database may be used when performing a query with the database object. Note that all tables must refer to distinct models.

Performing queries

A SELECT query on a model is written the following way:

std::vector<Records> records = database.getAll<Record>()();

The database automatically chooses the table for Record on which to perform the query. By default all fields are selected. Some fields only may be selected by giving as template arguments the pointer to the attributes to select:

std::vector<Records> records = database.getAll<&Record::id, &Record::s>()();

This is the same as:

SELECT `id`, `s` FROM `records`

Adding clauses

SQL clauses are chained using operator()s:

database.getAll<Record>()(Where{c<&Record::i>{} = 3})(Limit<1>{})();

is equivalent to:

SELECT * FROM `records` WHERE `records`.`i`=3 LIMIT 1

c and ref

In order to build conditions correctly for WHERE and assignments for SET, you need to user one of the c and ref classes.

c is templated on a pointer to an attribute. When put in an operation, it will be substituted by the column corresponding to the attribute. In the above example, c<&Record::i>{} was changed to records.i.

ref takes a reference to the variable it is given, as opposed to taking it by value.

Let us look at the following:

int i = 4;

auto query = database.getAll<Record>()(Where{c<&Record::i>{}=ref{i});
query();
i = 2;
query();

The first call to query translates to

SELECT * FROM `records` WHERE `records`.`i`=4

While the second translates to

SELECT * FROM `records` WHERE `records`.`i`=2

Had we written c<&Record::i>{}=i, both calls would have been evaluated with i=4.

Roadmap

  • Joins.
  • Constraints on multiple columns (UNIQUE(a, b)).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.