mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2025-05-17 10:43:23 +02:00
Implemented table version support for SQLite3DatabaseConnection.
This commit is contained in:
parent
882d1903a4
commit
e69fcf5656
@ -73,16 +73,11 @@ Ref<Database> DatabaseConnection::get_owner() {
|
|||||||
int DatabaseConnection::get_table_version(const String &table) {
|
int DatabaseConnection::get_table_version(const String &table) {
|
||||||
ensure_version_table_exists();
|
ensure_version_table_exists();
|
||||||
|
|
||||||
//get_query_builder()
|
|
||||||
//TODO
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
void DatabaseConnection::set_table_version(const String &table, const int version) {
|
void DatabaseConnection::set_table_version(const String &table, const int version) {
|
||||||
//TODO
|
|
||||||
}
|
}
|
||||||
void DatabaseConnection::ensure_version_table_exists() {
|
void DatabaseConnection::ensure_version_table_exists() {
|
||||||
//TODO
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DatabaseConnection::set_owner(Database *owner) {
|
void DatabaseConnection::set_owner(Database *owner) {
|
||||||
|
@ -106,6 +106,98 @@ void SQLite3DatabaseConnection::escape_to(const String &str, String *to) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int SQLite3DatabaseConnection::get_table_version(const String &table) {
|
||||||
|
ensure_version_table_exists();
|
||||||
|
|
||||||
|
Ref<QueryBuilder> qb = get_query_builder();
|
||||||
|
|
||||||
|
qb->select("version");
|
||||||
|
qb->from("table_versions");
|
||||||
|
qb->where();
|
||||||
|
qb->wps("table_name", table);
|
||||||
|
qb->end_command();
|
||||||
|
Ref<QueryResult> res = qb->run();
|
||||||
|
|
||||||
|
if (!res->next_row()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res->get_next_cell_int();
|
||||||
|
}
|
||||||
|
void SQLite3DatabaseConnection::set_table_version(const String &table, const int version) {
|
||||||
|
ensure_version_table_exists();
|
||||||
|
|
||||||
|
Ref<QueryBuilder> qb = get_query_builder();
|
||||||
|
|
||||||
|
qb->select("id");
|
||||||
|
qb->from("table_versions");
|
||||||
|
qb->where();
|
||||||
|
qb->wps("table_name", table);
|
||||||
|
qb->end_command();
|
||||||
|
Ref<QueryResult> res = qb->run();
|
||||||
|
|
||||||
|
qb->reset();
|
||||||
|
|
||||||
|
if (!res->next_row()) {
|
||||||
|
if (version == -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
qb->insert("table_versions", "table_name,version");
|
||||||
|
qb->values();
|
||||||
|
qb->vals(table);
|
||||||
|
qb->vali(version);
|
||||||
|
qb->cvalues();
|
||||||
|
qb->end_command();
|
||||||
|
qb->run_query();
|
||||||
|
} else {
|
||||||
|
int id = res->get_next_cell_int();
|
||||||
|
|
||||||
|
if (version != -1) {
|
||||||
|
qb->update("table_versions");
|
||||||
|
qb->sets();
|
||||||
|
qb->setpi("version", version);
|
||||||
|
qb->cset();
|
||||||
|
qb->where()->wpi("id", id);
|
||||||
|
qb->run_query();
|
||||||
|
} else {
|
||||||
|
qb->del("table_versions");
|
||||||
|
qb->where()->wpi("id", id);
|
||||||
|
qb->run_query();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void SQLite3DatabaseConnection::ensure_version_table_exists() {
|
||||||
|
// https://www.sqlite.org/fileformat2.html#storage_of_the_sql_database_schema
|
||||||
|
Ref<QueryBuilder> qb = get_query_builder();
|
||||||
|
|
||||||
|
qb->select("count(*)");
|
||||||
|
qb->from("sqlite_master");
|
||||||
|
qb->where();
|
||||||
|
qb->wps("type", "table");
|
||||||
|
qb->land();
|
||||||
|
qb->wps("name", "table_versions");
|
||||||
|
qb->end_command();
|
||||||
|
Ref<QueryResult> res = qb->run();
|
||||||
|
|
||||||
|
ERR_FAIL_COND(!res->next_row());
|
||||||
|
|
||||||
|
int c = res->get_next_cell_int();
|
||||||
|
|
||||||
|
if (c == 0) {
|
||||||
|
Ref<TableBuilder> tb = get_table_builder();
|
||||||
|
|
||||||
|
tb->create_table("table_versions");
|
||||||
|
tb->integer("id")->auto_increment()->next_row();
|
||||||
|
tb->varchar("table_name", 256)->not_null()->next_row();
|
||||||
|
tb->integer("version")->next_row();
|
||||||
|
tb->primary_key("id");
|
||||||
|
tb->ccreate_table();
|
||||||
|
tb->run_query();
|
||||||
|
// tb->print();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SQLite3DatabaseConnection::SQLite3DatabaseConnection() {
|
SQLite3DatabaseConnection::SQLite3DatabaseConnection() {
|
||||||
conn = nullptr;
|
conn = nullptr;
|
||||||
}
|
}
|
||||||
|
@ -15,16 +15,20 @@ class SQLite3DatabaseConnection : public DatabaseConnection {
|
|||||||
public:
|
public:
|
||||||
friend class SQLite3PreparedStatement;
|
friend class SQLite3PreparedStatement;
|
||||||
|
|
||||||
Error database_connect(const String &connection_str);
|
virtual Error database_connect(const String &connection_str);
|
||||||
Ref<QueryResult> query(const String &query);
|
virtual Ref<QueryResult> query(const String &query);
|
||||||
void query_run(const String &query);
|
virtual void query_run(const String &query);
|
||||||
|
|
||||||
Ref<QueryBuilder> get_query_builder();
|
virtual Ref<QueryBuilder> get_query_builder();
|
||||||
Ref<TableBuilder> get_table_builder();
|
virtual Ref<TableBuilder> get_table_builder();
|
||||||
Ref<PreparedStatement> create_prepared_statement();
|
virtual Ref<PreparedStatement> create_prepared_statement();
|
||||||
|
|
||||||
String escape(const String &str);
|
virtual String escape(const String &str);
|
||||||
void escape_to(const String &str, String *to);
|
virtual void escape_to(const String &str, String *to);
|
||||||
|
|
||||||
|
virtual int get_table_version(const String &table);
|
||||||
|
virtual void set_table_version(const String &table, const int version);
|
||||||
|
virtual void ensure_version_table_exists();
|
||||||
|
|
||||||
SQLite3DatabaseConnection();
|
SQLite3DatabaseConnection();
|
||||||
~SQLite3DatabaseConnection();
|
~SQLite3DatabaseConnection();
|
||||||
|
Loading…
Reference in New Issue
Block a user