{"id":29253517,"url":"https://github.com/krozark/cpp-orm","last_synced_at":"2025-07-04T02:06:45.566Z","repository":{"id":8593531,"uuid":"10228646","full_name":"Krozark/cpp-ORM","owner":"Krozark","description":"A project to create a simple ORM.","archived":false,"fork":false,"pushed_at":"2020-12-14T22:19:37.000Z","size":10595,"stargazers_count":7,"open_issues_count":12,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-02T18:04:40.655Z","etag":null,"topics":["cpp11","mysql","orm","sql","sqlite3"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Krozark.png","metadata":{"files":{"readme":"README.md","changelog":"changelog","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-05-22T20:06:40.000Z","updated_at":"2024-07-01T11:57:32.000Z","dependencies_parsed_at":"2022-08-27T16:50:15.869Z","dependency_job_id":null,"html_url":"https://github.com/Krozark/cpp-ORM","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/Krozark/cpp-ORM","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Krozark%2Fcpp-ORM","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Krozark%2Fcpp-ORM/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Krozark%2Fcpp-ORM/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Krozark%2Fcpp-ORM/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Krozark","download_url":"https://codeload.github.com/Krozark/cpp-ORM/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Krozark%2Fcpp-ORM/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263432207,"owners_count":23465572,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp11","mysql","orm","sql","sqlite3"],"created_at":"2025-07-04T02:04:16.330Z","updated_at":"2025-07-04T02:06:45.556Z","avatar_url":"https://github.com/Krozark.png","language":"C","readme":"cpp-ORM\n=========\n\nCurrent build status : \n[![Build Status](https://travis-ci.org/Krozark/cpp-ORM.svg?branch=master)](https://travis-ci.org/Krozark/cpp-ORM)\n\nAn ORM project.\n\nYou can simply create persistent objects using databases.\n\nThe object representation:\n    Each object have to be in a separate table with a pk column named 'id' as auto increment.\n    each attr is a column in this table.\n\nTable ares created automaticly.\n\n\nTested on\n----------\n\n* Windows x64 (sqlite3 only) + Mingw 4.9 [v 0.4+]\n* Ubuntu + Gcc 4.8+ [v 0.1+]\n* Ubuntu + clang 3.3+ [v 0.1+]\n\n\n\nfunctions\n---------\n\n* Persistent Object\n * print as json\n * save / update / load\n * polymorphic object [0.4.2]\n* Foreign key\n * save / update / load\n* ManyToMany\n * save / update / load / add / remove\n* filters (and, or, not)\n * WHERE statement\n * and, or, not \n * order by\n * limit\n* caching\n* debug output (set ORM_DEBUG to ORM_DEBUG_XXX in debug.hpp)\n* multi connection for a single object (select/save) [v 0.3]\n* Table creation, clearing and drop\n\nData bases supported\n=====================\n\n* MySql\n* Sqlite3\n\n\nRequirement\n===========\n\n* mysql-connector-c (if Mysql support is needed). Can be find here : https://dev.mysql.com/downloads/connector/c/ or libmysql++-dev (ubuntu)\n* ~~lib Sqlite3~~ [included since v 0.4]\n* pthread (for linux only) [v 0.4+]\n* doxygen (for user doc only)\n\n\nInstallation\n===========\n\nYou just have to use cmake:\n\n    mkdir build\n    cd build\n    cmake ..\n    make\n    make install\n\nYou can customise the backends supported using\n\n* ORM_BUILD_SUPPORT_SQLITE3\n* ORM_BUILD_SUPPORT_MYSQL\n\nExemple\n=======\n\nYou can see complet exemple in main.cpp\n\nTo build the exemple (the database is include)\n\n    set the BUILD_EXAMPLES to True with cmake\n\n\n```C++\n \n    //#include \u003cORM/backends/MySql.hpp\u003e\n    //create your default database\n    orm::MySqlDB def(\"root\",\"root\",\"test\");\n\n    //#include \u003cORM/backends/Sqlite3.hpp\u003e\n    //orm::Sqlite3DB def(\"./datas/test.db\"); //if you want to use sqlite3\n\n    //make it as default\n    orm::DB\u0026 orm::DB::Default = def;\n\n    \n    #include \u003cORM/fields.hpp\u003e\n    #include \u003cORM/models/SqlObject.hpp\u003e\n    //create you class\n    class Perso : public orm::SqlObject\u003cPerso\u003e\n    {\n        public:\n            Perso();\n            orm::CharField\u003c255\u003e name;\n            orm::IntegerField pv;\n            orm::IntegerField lvl;\n    \n            MAKE_STATIC_COLUM(name,pv,lvl)\n    };\n    REGISTER_AND_CONSTRUCT(Perso,\"perso\",name,\"nom\",pv,\"pv\",lvl,\"lvl\")\n\n    int main(int argc,char* argv[])\n    {\n        //connect to database\n        orm::DB::Default.connect();\n        \n        //orm::Tables::drop();  // delete all tables\n        //orm::Tables::clear();  // clear tables contents\n        orm::Tables::create(); //create all thes tables\n    \n        //get perso with pk=1\n        Perso::type_ptr p1 = Perso::get(1);\n        //see it\n        cout\u003c\u003c*p1\u003c\u003cendl;\n        //modify it\n        p1-\u003epv +=14;\n        //save it\n        p1-\u003esave();\n\n\n        //add one\n        Perso::type_ptr p2 = Perso::create();// = new Perso;\n        p2-\u003ename = \"test insert\";\n        p2-\u003elvl = 75;\n        p2-\u003esave(); //save it\n        cout\u003c\u003c*p2\u003c\u003cendl;\n\n        // all\n        cout\u003c\u003c\"All persos\"\u003c\u003cendl;\n        Perso::result_type lis= Perso::all();\n        for(auto u : lis)\n            cout\u003c\u003c*u\u003c\u003cendl;\n\n        list.clear();\n        //custom query\n        Perso::query()\\\n            .filter(Q\u003cPerso\u003e(4,\"gt\",Perso::$lvl)\n                and Q\u003cPerso\u003e(42,\"gte\",Perso::$lvl)\n                and Q\u003cPerso\u003e(\"test\",\"startswith\",Perso::$name)\n                and not Q\u003cPerso\u003e(4,\"lt\",Perso::$lvl)\n            ).orderBy(Perso::$name)\\\n            .limit(42)\\\n            .get(lis); //get take a list T::result_type\u0026 or a T\u0026 as param\n        for(auto u : lis)\n            cout\u003c\u003c*u\u003c\u003cendl;\n\n\n        orm::DB::Default.disconnect();\n        return 0;\n    }\n\n```\n\nClass\n=====\n\nAll class are under the orm namespace\n\n\norm::SqlObject\u003cT\u003e\n-----------------\n\nBase class for persistent T class. It add all static method / attribute to your class.\n\nmake some static method:\n* T::result_type get(int pk)\n* T::result_type all()\n* orm::QuerySet\\\u003cT\\\u003e T::query()  construct a queryset to make custom query\n\nstatic member:\n* DB* default_connection : database where the object is contain\n* std::string table : table name on the database\n\nmake member fonction:\n* bool save(bool recursive=false,DB\u0026=*default_connection)\n* bool del(bool recursive=false,DB\u0026=*default_connection)\n\nExemple: see main.cpp\n\nFields\n------\n\n* AutoField\n* BooleanField\n* CharField\\\u003cint\\\u003e\n* TextField\n* DateTimeField\n* AutoDateTimeField\n* AutoNowDateTimeField\n* DoubleField\n* FloatField\n* IntegerField\n\n\n* FK\\\u003cT\\\u003e\n* ManyToMany\\\u003cOWNER,T\\\u003e\n\norm::Attr\\\u003cT\\\u003e\n--------------\n\nMake a T persistent in the database.\n\nYou can access to the value using:\n* T value;\n\n\nAll operators are overloads to make a easy use of it.\n\nYou may prefer to use premake fields\n\norm::ManyToMany\\\u003cT,U\\\u003e\n======================\n\nT have to be the persistent class where the M2M is use, and U the persistent class.\nI create a link with the 2 class, and you can access to the U using:\n\naccessors:\n* U::result_type .all()\n\n\norm::DB\n---------\n\nBase classe to deal with your database. Use specialize database (MySqlDB, ...)\n\nConstructor:\nDB(const std::string\u0026 username,const std::string\u0026 pass,const std::string\u0026 db,const std::string\u0026 serveur,const std::string\u0026 port);\n\n\nYou can use some fonction:\n* bool connect()\n* bool disconnect()\n* Query* query()\n* Query* query(std::string\u0026)\n* Query* query(std::string\u0026\u0026)\n* Query* preparedQuery(std::string\u0026)\n* Query* preparedQuery(std::string\u0026\u0026)\n\n\nYou can custom the default database:\n\n    orm::MySqlDB def(\"root\",\"root\",\"test\");\n    orm::DB\u0026 orm::DB::Default = def;\n\nMacros\n======\n\nThere are some macos to help.\n\nMAKE_STATIC_COLUM(...)\n----------------------\n\nTo call in your class with all persistent attrs in params. It will construct the static std::string to use (on filter ...).\nORM_MAKE_NAME(colum) turn your colunm name to the on to use by adding ORM_COLUMN_CHAR ($ by default)\n\nexemple:\n\n```C++\n    class Spell : public orm::SqlObject\u003cSpell\u003e\n    {\n        public:\n            Spell();\n            orm::CharField\u003c255\u003e name;\n            orm::IntegerField element;\n\n            MAKE_STATIC_COLUMN(name,element);\n\n    };\n```\n\nthe colums $name and $element will be created for query.\n\n```C++\n    Spell::\n    auto results = Spell::query().filter(\"spell name\",orm::op::exact,Spell::$name);\n```\n\n\n\nREGISTER_AND_CONSTRUCT(klass,colum,[attr,colum]+)\n-------------------------------------------------\n\nMake the default constructor of your class, construct all comulm as static strings.\nThis macro simply call REGISTER_TABLE(klass,colum) and MAKE_CONSTRUCTOR(klass,__VA_ARGS__)\n\n\nMarcos II\n---------\n\nOnly to use if you want build a custum constructor\n\n\n\nREGISTER_TABLE(klass,colum)\n------------------------------\n\nIf you use REGISTER_AND_CONSTRUCT, you don't need to use it.\n\nThiis will create all the static variables needed by the library\n\n\nMAKE_CONSTRUCTOR(klass[,attr,colum]+)\n--------------------------------------\n\nIf you use REGISTER_AND_CONSTRUCT, you don't need to use it.\n\nMake the default constructor for the class klass, and register all Att in params. It also call _MAKE_STRING_N to create attrs colum names as static.\n\n     _MAKE_STRING_N(klass,NUM_ARGS(__VA_ARGS__),__VA_ARGS__)\n    klass::klass(): _MAKE_ATTRS_N(NUM_ARGS(__VA_ARGS__),__VA_ARGS__)\n    {\n         _MAKE_REGISTER_ATTRS(NUM_ARGS(__VA_ARGS__),__VA_ARGS__)\n    }\n\n\n _MAKE_STRING_N(klass,N,...)\n----------------------------\nIf you use REGISTER_AND_CONSTRUCT, you don't need to use it.\n\nN have to be the number of attrs (in ...)\n\nSimply construct all string as:\n    std::string klass::_attr = colum;\n\n\n_MAKE_ATTRS_N(N,...)\n--------------------\nIf you use REGISTER_AND_CONSTRUCT, you don't need to use it.\n\nN have to be lenght of ...\n\nsimply add attr(colum) to constuctor for const initialize\n\n\n_MAKE_REGISTER_ATTRS(N,...)\n---------------------------------------\nIf you use REGISTER_AND_CONSTRUCT, you don't need to use it.\n\nfor each att in ...\n\n    this-\u003eregisterAttr(this-\u003ename);\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrozark%2Fcpp-orm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrozark%2Fcpp-orm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrozark%2Fcpp-orm/lists"}