-
-
Notifications
You must be signed in to change notification settings - Fork 321
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
Reverse engineering complex databases - automatically generate storage definition and data structures #450
Comments
Hi. Thanks for using the lib. How many tables do you have? I am just curious. CREATE TABLE contacts (
contact_id INTEGER PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
phone TEXT NOT NULL UNIQUE
); will generate struct Contact {
int id = 0;
std::string firstName;
std::string lastName;
std::string email;
std::string phone;
};
auto storage = make_storage("db.sqlite",
make_table("contacts",
make_column("contact_id", &Contact::id, primary_key()),
make_column("first_name", &Contact::firstName),
make_column("last_name", &Contact::lastName),
make_column("email", &Contact::email, unique()),
make_column("phone", &Contact::phone, unique()))); or if you type SELECT first_name, salary
FROM emp_master; you will get auto rows = storage.select(columns(&EmpMaster::firstName, &EmpMaster::salary)); What do you think? |
There should be somewhere around 70 tables (2 different schema definitions). Many of them have several dozens of columns. The results you describe are exactly what is needed (although it seems that NOT NULL is ignored in the example). That would make sqlite_orm even more efficient to use and attractive! I suppose that when it comes to details (e.g. the determining the right order of the make_table entities in the presence of foreign keys), one needs to be careful. May I ask how you are planning to implement this transformation? |
|
Sounds good! What priority does this code generation thing have for you? |
|
I will need to have the tables converted in about 2 weeks. Do you think the code generator might be ready then? If not, no problem, then I'll write a simple object oriented parser just for 'create table'. |
Probably it will be ready for |
Awesome, thank you so much! |
Quick question: How are types translated into sqlite_orm, e.g. UNSIGNED INT(4), CHARACTER(1), VARCHAR(64) etc.? |
And another Question:
...unique_ptr means that imageUrl = nullptr is allowed. Correct? |
|
Thanks! |
This is cool! But it is not required cause I have already started making injections in sqlite source to make available statement analyzing. I thought about creating custom parser but native parser is always better cause it will parse 100% of queries but non-native will parse <100% queries and must be regularly supported. Anyway parser creation is a good exercise to get amazing experience! |
Generated Code: The compiler says I must use the command line option /bigobj in order to get it to compile. |
Ok, I put /bigobj into the linker command line options instead of the compiler. Obviously I need a break but it seems that I am almost there. |
Ok, I can create an sqlite3 db using sqlite_orm and the generated definition (103 KB of code, I am happy that I did not have to write this manually). Now I need to have a look at the foreign keys. |
I am stuck with the data type mapping. Seems like it is not possible to create a db definition for sqlite_orm without losing the detailed data type specification of the SQLite db to be modelled. I need to exactly recreate the existing SQLite db. Is this possible? |
How does sqlite_orm deal with circular foreign key references? I just discovered that the schema I am currently working with has 7 circular references... |
@prospectumdev what will be lost? |
The data type definitions as specified in the original databases. For instance, 'UNSIGNED INT(4)' is mapped to 'INT', so that databases created with sqlite_orm will always differ from original ones. However, contrary to my expectations it seems that the host software I am using does not care. So it is not a big problem.
In my tables there are self references and circular references. is it possible / are you planning to support these references? As for timing, is this considered a minor or a major feature? Plus it seems that it is not possibly to specify validity checks via the check constraint, like From my perspective, my assessment of these three issues is the following:
So you are planning to add support for circular references and self references? That is great news! What about check constraints? If sqlite_orm is supposed to do the checking itself, this might be a lot of work. However, why not simply add the check constraints to the database and have SQLite itself do the checking? In that case it should be much easier to add this feature. As for the data type names: It seems that SQLite allows for data type name aliases to be stored. While I am not sure if it is a good practice to use those at all, SQLite does it and so, for completeness, it might be nice to represent that, too. Or sqlite_orm could explicitly refuse to use data types other than those specified by the 5 storage classes and rely on checks in order to ensure validity. While this is a plausible approach, my experience from product development tells me that for psychological reasons this is likely to have a (small) detrimental effect on the user acceptance of sqlite_orm. |
|
Thanks a lot, I'll find a way to get something going that I can work with at the moment. If the host software accepts databases without foreign keys and checks, that's ok for now. I think you are doing an excellent job with sqlite_orm, and I think it can become very popular once the features are there and it is convenient to use (example: code generation). |
Feedback: It kind of works: I can now read an existing database, generate the sqlite_orm header file (no foreign keys, no check() constraints) and make sqlite_orm produce a working database. 'working means' that I can insert all the data of the preexisting database into the new one and the new one is accepted by the host software! |
Data is lost cause something in schema differs. Please show me the results of |
My quick and dirty code: https://github.com/prospectumdev/200111_sqlite_orm_codegen I'll post the results of sync_schema shortly. |
sync_schema() returns "old table dropped and recreated" for all tables I found the reason: It is happening because of the data type names. Maybe custom data type name support isn't a 'nice to have' feature at all... |
Optional data type aliases could solve the problem. Then
turns into
If available, the aliases can be used to set table_info[columnid].type when tables are created. |
what type has |
original db : |
what C++ type has |
if all tables are bound with each other with foreign keys than the only way is to drop foreign keys. If some tables can be separated from others that you can split your storage without dropping anything. |
The problem is the complexity of So that turns into
After all, the larger the schema, the more useful orm gets and thus the more desirable it is to use it. Another problem is compilation time and memory usage. Do you think extern template declarations could help decrease compilation time? |
Yeah, I know this. There is no way to divide |
Assuming that a schema is given, what is the type returned by |
Thanks! When I remove all the |
Almost there! Could you please tell me all the constraints that can be put after decltype(...), ? Do I have to use them in a specific order? Thanks! |
|
This works when the primary key definitions are removed...
but does not when they are used. How can I represent a primary key that is defined as in make_storage in the storage type? Thanks! |
This combination works...
but, in order to support multi column primary keys, I want something like
but that does not work. |
I just figured it out constraints::primary_key_t<decltype(&__ValidFixedCodes_ReverbTailTruncationMode::Code)> |
Unfortunately, the combination
causes an error in sqlite_orm.h, line 9134: |
Column< _General, decltype(_General::CustomDisplay_EnableStdPages_Controls), constraints::default_t<const char *>>,
make_column("CustomDisplay_EnableStdPages_Controls", &_General::CustomDisplay_EnableStdPages_Controls, default_value("Y")), |
But I'll try it. |
field can be |
So for an unsigned int it is Then, just for completeness, how is it done for real and blob types? Thanks! |
No. struct User{
int id = 0;
float floatValue = 0;
};
auto storage = make_storage({},
make_table("users",
make_column("id", &User::id, primary_key()),
make_column("value", &User::floatValue, default_value(0))));
// or
auto storage = make_storage({},
make_table("users",
make_column("id", &User::id, primary_key()),
make_column("value", &User::floatValue, default_value(0.0))));
// or
auto storage = make_storage({},
make_table("users",
make_column("id", &User::id, primary_key()),
make_column("value", &User::floatValue, default_value(0.0f))));
// or
auto storage = make_storage({},
make_table("users",
make_column("id", &User::id, primary_key()),
make_column("value", &User::floatValue, default_value(0L))));
// or
auto storage = make_storage({},
make_table("users",
make_column("id", &User::id, primary_key()),
make_column("value", &User::floatValue, default_value("0")))); everything will work cause all this just is serialized to SQL queries which are compiled by SQLite3 engine. Next is SQLite's responsibility to assign 0.0 to int column and it will be assigned well cause SQLite has weak types. You can insert string into int column and reverse. Also you can omit columns' types during table creation: CREATE TABLE users (id, name) |
Thanks, after ~12 hours it is finally working. 353 kB of generated code... Still takes long to compile but it works! Anyway, thank you very much again! I'm done for today, 12 hours of coding should be enough ;-) |
I am glad that everything works. Your idea about a dedicated library is the best solution. Also please beware that changing your schema requires storage type changing also. |
My working code generator: https://github.com/prospectumdev/200111_sqlite_orm_codegen Thank you so much for your help! |
|
is the issue actual? |
I'll check tomorrow |
Before I update my generator: Is there any 'official' functionality now for generating storage definitions from SQLite dbs? If not: This is worth considering becauses such a generator increases the efficiency of use of sqlite_orm a lot, especially for more complex databases. If updated, my generator code could be a starting point for that. |
Ok, it was easier than I thought to update my generator (https://github.com/prospectumdev/200111_sqlite_orm_codegen). Still: There should be an 'official' generator, I think. |
hi @prospectumdev . There is no ready generator. I was working on it but it takes a lot of time to make an universal SQLite parser. You can help me if you give me some useful test cases which I can start with. Thanks |
Sure - I have already sent you a database by email. |
I tried to develop a parser of SQL by myself to make a code generator like you want. By I faced an issue that it is not a trivial task. E.g. does your generator support |
I have databases with many complex tables (lots of columns, foreign keys, range checks etc). Manually creating the storage definition and all the structs would take maybe a week. It is also likely that errors are made in the process.
Is there some kind of parser that generates the structs and the storage definition from an existing SQLite db?
If not, I am tempted to write a SQLite3 -> sqlite_orm definition parser that reads sqlite_master and outputs a .h file with all you need in order to use the db with sqlite_orm.
The text was updated successfully, but these errors were encountered: