GithubHelp home page GithubHelp 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)).

mysql_orm's People

Contributors

ethiraric avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mysql_orm's Issues

Project Status

This project looks promising and could prove to be an easy migration from sqlote_orm.

I'm wondering if you plan on updating the project any time soon? Unique constraints would be one feature I'm particularly interested in.

I encountered these bugs while compiling the project, please help me, thank you. I really like this project.

I'm unable to successfully compile this project. I'm using Ubuntu 22.04 with g++ 13.1. I really want to use this project and hope you can help me. Below is the process I've tried to resolve the errors.

First, there is this error. I found the value corresponding to SIGSTKSZ and directly replaced it, which I consider as solving this error.

/home/yyx/mysql_orm/tests/catch.hpp:7441:45: error: size of array ‘altStackMem’ is not an integral constant-expression
 7441 |     char FatalConditionHandler::altStackMem[SIGSTKSZ] = {};
      |                                             ^~~~~~~~

Then there is this error. I don't understand why you are using <> to include your own project's header files, so I

$ ln -s ../include/mysql_orm/ ./tests/
$ ln -s ../deps/include/ ./tests/
/home/yyx/mysql_orm/tests/test_Column.cpp:1:10: fatal error: mysql_orm/Column.hpp: No such file or directory
    1 | #include <mysql_orm/Column.hpp>

This is the third error. I searched the entire project and found no definition for my_bool, so I had to replace all instances with bool.

/home/yyx/mysql_orm/tests/./mysql_orm/Connection.hpp:36:22: error: ‘my_bool’ was not declared in this scope; did you mean ‘bool’?
   36 |     auto reconnect = my_bool{1};

This is the fourth error, I really have no idea how to solve it.

$ make
Consolidate compiler generated dependencies of target mysql_orm_tests
[  6%] Building CXX object CMakeFiles/mysql_orm_tests.dir/test_Database.cpp.o
[ 13%] Building CXX object CMakeFiles/mysql_orm_tests.dir/test_Delete.cpp.o
[ 20%] Building CXX object CMakeFiles/mysql_orm_tests.dir/test_Insert.cpp.o
[ 26%] Building CXX object CMakeFiles/mysql_orm_tests.dir/test_Limit.cpp.o
[ 33%] Building CXX object CMakeFiles/mysql_orm_tests.dir/test_Pack.cpp.o
[ 40%] Building CXX object CMakeFiles/mysql_orm_tests.dir/test_RemoveOccurences.cpp.o
[ 46%] Building CXX object CMakeFiles/mysql_orm_tests.dir/test_GetAll.cpp.o
[ 53%] Building CXX object CMakeFiles/mysql_orm_tests.dir/test_Table.cpp.o
[ 60%] Building CXX object CMakeFiles/mysql_orm_tests.dir/test_Update.cpp.o
[ 66%] Building CXX object CMakeFiles/mysql_orm_tests.dir/test_Varchar.cpp.o
[ 73%] Building CXX object CMakeFiles/mysql_orm_tests.dir/test_Where.cpp.o
[ 80%] Linking CXX executable mysql_orm_tests
/usr/bin/ld: cannot find -lmysql_orm: No such file or directory
collect2: error: ld returned 1 exit status

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.