-
Notifications
You must be signed in to change notification settings - Fork 12
Add enum support #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add enum support #49
Conversation
@hos-b looks great, but why do we need the explicit sqlgen::Enum wrapper? I think we can easily figure out whether something is an enum using concepts. |
😅 good point. I guess I was biased towards the old PR (specialization of *: based on my understanding, it's not possible to infer the DB type when |
The way I would handle the string vs int situation is to introduce processors, just like we have in reflect-cpp. But that can be a different PR, it doesn't have to be part of this one. |
@hos-b yes, you are right. The library is deliberately designed in such a way that the parser is completely oblivious to the database. So for SQLite, we should just use TEXT for now and then introduce the processors at a later date. |
I update the code to now handle enums using type traits in #include <iostream>
#include <sqlgen.hpp>
#include <sqlgen/mysql.hpp>
#include <sqlgen/postgres.hpp>
#include <sqlgen/sqlite.hpp>
enum class TestEnum : int {
TEST_VALUE1 = 1,
TEST_VALUE2 = 2,
};
struct TestStruct {
sqlgen::PrimaryKey<int, true> id;
TestEnum type;
std::string name;
};
int main() {
TestStruct type_based;
type_based.id = 1;
type_based.type = TestEnum::TEST_VALUE2;
type_based.name = "Test";
const auto postgres_creds =
sqlgen::postgres::Credentials{.user = "username",
.password = "password",
.host = "172.17.0.1",
.dbname = "mydb",
.port = 5432};
auto postgres_conn = sqlgen::postgres::connect(postgres_creds);
const auto mysql_creds =
sqlgen::mysql::Credentials{.host = "172.17.0.1",
.user = "myuser",
.password = "mypassword",
.dbname = "mydatabase",
.port = 3306,
.unix_socket = "/var/run/mysqld/mysqld.sock"};
const auto sqlite_conn = sqlgen::sqlite::connect("database.db");
sqlgen::write(postgres_conn, type_based);
sqlgen::write(sqlite_conn, type_based);
const auto postgres_items =
sqlgen::read<std::vector<TestStruct>>(postgres_conn).value();
const auto sqlite_items =
sqlgen::read<std::vector<TestStruct>>(sqlite_conn).value();
auto print_rows = [](const auto& items, const std::string& db_name) {
for (const auto& it : items) {
std::cout << db_name << ": " << it.id.get() << ": name: " << it.name
<< ", uuid: " << "???"
<< ", type: " << rfl::enum_to_string<TestEnum>(it.type)
<< std::endl;
}
std::cout << "------------------------" << std::endl;
};
print_rows(postgres_items, "postgre:enum");
print_rows(sqlite_items, "sqlite:enum");
return 0;
} |
@hos-b looks great! If you could just add some tests, this would be ready to be merged. |
This PR adds
sqlgen::Enum
. It usesreflectcpp
to perform the string <-> int conversions to/from the DB interface. It uses the built-in enum types for mysql/postgres and string representation for sqlite by default. It allows using the underlying integer representation in all 3 cases.I consider this a WIP so please feel free to suggest improvements. I for the life of me could not get a mysql/maria server running to test that part. I kept getting SSL certificate errors when trying to start a connection. If you have a docker-compose yml ready, I'd gladly test the mysql implementation as well.
This is the code I used for testing:
One additional change I did was to switch the remote URL for vcpkg from
ssh
tohttps
. This makes cloning the repo easier, IMO. I could make a separate PR for that, if you'd prefer to keep this one clean.