{"id":18638410,"url":"https://github.com/ethiraric/mysql_orm","last_synced_at":"2025-08-27T15:09:15.605Z","repository":{"id":63794289,"uuid":"138312427","full_name":"Ethiraric/mysql_orm","owner":"Ethiraric","description":"A C++ ORM for MySQL","archived":false,"fork":false,"pushed_at":"2023-12-31T18:04:31.000Z","size":381,"stargazers_count":10,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-11T16:23:31.306Z","etag":null,"topics":["cpp","mysql","orm"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ethiraric.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-06-22T14:31:54.000Z","updated_at":"2024-06-05T15:43:21.000Z","dependencies_parsed_at":"2024-11-07T05:41:36.315Z","dependency_job_id":null,"html_url":"https://github.com/Ethiraric/mysql_orm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Ethiraric/mysql_orm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ethiraric%2Fmysql_orm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ethiraric%2Fmysql_orm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ethiraric%2Fmysql_orm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ethiraric%2Fmysql_orm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ethiraric","download_url":"https://codeload.github.com/Ethiraric/mysql_orm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ethiraric%2Fmysql_orm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272342823,"owners_count":24917707,"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","status":"online","status_checked_at":"2025-08-27T02:00:09.397Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["cpp","mysql","orm"],"created_at":"2024-11-07T05:41:26.602Z","updated_at":"2025-08-27T15:09:15.576Z","avatar_url":"https://github.com/Ethiraric.png","language":"C++","readme":"# mysql_orm\nA C++ ORM for MySQL, inspired by [sqlite_orm](https://github.com/fnc12/sqlite_orm).\n\n`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).\n\nConsider the following structure:\n\n```cpp\nstruct Record\n{\n  uint32_t id;\n  int i;\n  std::string s;\n};\n```\n\nWe can create a table from this structure the following way:\n\n```cpp\nauto table_records = make_table(\"records\",\n                                make_column\u003c\u0026Record::id\u003e(\"id\"),\n                                make_column\u003c\u0026Record::i\u003e(\"i\"),\n                                make_column\u003c\u0026Record::s\u003e(\"s\"));\n```\n\n`mysql_orm` automatically deduces the types of the fields and the one of the structure (which we call the _model_).\nIf the fields do not refer to the same model, an error is raised at compile-time.\n\nWe can now make a database connection from the table:\n\n```cpp\nauto connection = mysql_orm::Connection(\"localhost\", 3306, \"username\", \"password\", \"database\");\nauto database = make_database(connection, table_records);\n```\n\nAny number of tables may be supplied when constructing a database.\nAny model whose table is given to the database may be used when performing a query with the `database` object.\nNote that all tables must refer to distinct models.\n\n# Performing queries\n\nA `SELECT` query on a model is written the following way:\n\n```cpp\nstd::vector\u003cRecords\u003e records = database.getAll\u003cRecord\u003e()();\n```\n\nThe database automatically chooses the table for `Record` on which to perform the query.\nBy default all fields are selected.\nSome fields only may be selected by giving as template arguments the pointer to the attributes to select:\n\n```cpp\nstd::vector\u003cRecords\u003e records = database.getAll\u003c\u0026Record::id, \u0026Record::s\u003e()();\n```\n\nThis is the same as:\n\n```sql\nSELECT `id`, `s` FROM `records`\n```\n\n## Adding clauses\nSQL clauses are chained using `operator()`s:\n\n```cpp\ndatabase.getAll\u003cRecord\u003e()(Where{c\u003c\u0026Record::i\u003e{} = 3})(Limit\u003c1\u003e{})();\n```\n\nis equivalent to:\n\n```sql\nSELECT * FROM `records` WHERE `records`.`i`=3 LIMIT 1\n```\n\n## `c` and `ref`\nIn order to build conditions correctly for `WHERE` and assignments for `SET`, you need to user one of the `c` and `ref` classes.\n\n`c` is templated on a pointer to an attribute.\nWhen put in an operation, it will be substituted by the column corresponding to the attribute.\nIn the above example, `c\u003c\u0026Record::i\u003e{}` was changed to `records.i`.\n\n`ref` takes a reference to the variable it is given, as opposed to taking it by value.\n\nLet us look at the following:\n\n```cpp\nint i = 4;\n\nauto query = database.getAll\u003cRecord\u003e()(Where{c\u003c\u0026Record::i\u003e{}=ref{i});\nquery();\ni = 2;\nquery();\n```\n\nThe first call to `query` translates to\n```sql\nSELECT * FROM `records` WHERE `records`.`i`=4\n```\n\nWhile the second translates to\n```sql\nSELECT * FROM `records` WHERE `records`.`i`=2\n```\n\nHad we written `c\u003c\u0026Record::i\u003e{}=i`, both calls would have been evaluated with `i=4`.\n\n# Roadmap\n * Joins.\n * Constraints on multiple columns (`UNIQUE(a, b)`).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fethiraric%2Fmysql_orm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fethiraric%2Fmysql_orm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fethiraric%2Fmysql_orm/lists"}