2021-08-19 22:24:16 +02:00
|
|
|
#include "sqlite3_table_builder.h"
|
|
|
|
|
2021-11-01 16:57:22 +01:00
|
|
|
#include "core/database/query_result.h"
|
|
|
|
|
2021-08-19 23:55:16 +02:00
|
|
|
#include "sqlite3_database.h"
|
|
|
|
|
2021-11-01 17:49:10 +01:00
|
|
|
TableBuilder *SQLite3TableBuilder::create_table(const String &name) {
|
2021-08-19 22:24:16 +02:00
|
|
|
result += "CREATE TABLE " + name + " ( ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:49:10 +01:00
|
|
|
TableBuilder *SQLite3TableBuilder::integer(const String &name) {
|
2021-08-19 22:24:16 +02:00
|
|
|
result += name + " INTEGER ";
|
|
|
|
|
2021-11-14 13:56:25 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
TableBuilder *SQLite3TableBuilder::integer(const String &name, const int length) {
|
|
|
|
result += name + " INTEGER(";
|
|
|
|
result += String::num(length);
|
|
|
|
result += ") ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TableBuilder *SQLite3TableBuilder::tiny_integer(const String &name) {
|
|
|
|
result += name + " INTEGER ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
TableBuilder *SQLite3TableBuilder::tiny_integer(const String &name, const int length) {
|
|
|
|
result += name + " INTEGER(";
|
|
|
|
result += String::num(length);
|
|
|
|
result += ") ";
|
|
|
|
|
|
|
|
|
2021-08-19 22:24:16 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:49:10 +01:00
|
|
|
TableBuilder *SQLite3TableBuilder::date(const String &name) {
|
2021-08-19 22:24:16 +02:00
|
|
|
result += name + " DATE ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:49:10 +01:00
|
|
|
TableBuilder *SQLite3TableBuilder::varchar(const String &name, const int length) {
|
2021-08-19 23:55:16 +02:00
|
|
|
result += name + " VARCHAR(" + std::to_string(length) + ") ";
|
2021-08-19 22:24:16 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TableBuilder *SQLite3TableBuilder::not_null() {
|
2021-08-19 23:55:16 +02:00
|
|
|
//result += "NOT NULL ";
|
2021-08-19 22:24:16 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TableBuilder *SQLite3TableBuilder::null() {
|
|
|
|
result += "NULL ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-14 13:56:25 +01:00
|
|
|
TableBuilder *SQLite3TableBuilder::defval(const String &val) {
|
|
|
|
result += "DEFAULT '";
|
|
|
|
result += val;
|
|
|
|
result += "'";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:24:16 +02:00
|
|
|
TableBuilder *SQLite3TableBuilder::auto_increment() {
|
2021-08-20 01:32:43 +02:00
|
|
|
//result += "AUTO_INCREMENT ";
|
2021-08-19 22:24:16 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:49:10 +01:00
|
|
|
TableBuilder *SQLite3TableBuilder::primary_key(const String &name) {
|
2021-08-19 22:24:16 +02:00
|
|
|
result += "PRIMARY KEY (" + name + ") ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TableBuilder *SQLite3TableBuilder::primary_key() {
|
|
|
|
result += "PRIMARY KEY ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TableBuilder *SQLite3TableBuilder::next_row() {
|
|
|
|
result += ", ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-19 23:55:16 +02:00
|
|
|
TableBuilder *SQLite3TableBuilder::ccreate_table() {
|
2021-08-19 22:24:16 +02:00
|
|
|
result += ");";
|
2021-08-19 23:55:16 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TableBuilder *SQLite3TableBuilder::drop_table() {
|
|
|
|
result += "DROP TABLE ";
|
|
|
|
|
|
|
|
return this;
|
2021-08-19 22:24:16 +02:00
|
|
|
}
|
2021-08-19 23:55:16 +02:00
|
|
|
TableBuilder *SQLite3TableBuilder::drop_table_if_exists() {
|
|
|
|
result += "DROP TABLE IF EXISTS ";
|
2021-08-19 22:24:16 +02:00
|
|
|
|
2021-08-19 23:55:16 +02:00
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:49:10 +01:00
|
|
|
TableBuilder *SQLite3TableBuilder::drop_table(const String &name) {
|
2021-08-19 22:24:16 +02:00
|
|
|
result += "DROP TABLE " + name + ";";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:49:10 +01:00
|
|
|
TableBuilder *SQLite3TableBuilder::drop_table_if_exists(const String &name) {
|
2021-08-19 23:55:16 +02:00
|
|
|
result += "DROP TABLE IF EXISTS " + name + ";";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
TableBuilder *SQLite3TableBuilder::cdrop_table() {
|
|
|
|
result += ";";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:49:10 +01:00
|
|
|
TableBuilder *SQLite3TableBuilder::foreign_key(const String &name) {
|
2021-08-19 23:55:16 +02:00
|
|
|
result += "FOREIGN KEY (" + name + ") ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:49:10 +01:00
|
|
|
TableBuilder *SQLite3TableBuilder::references(const String &table, const String &name) {
|
2021-08-19 23:55:16 +02:00
|
|
|
result += "REFERENCES " + table + " (" + name + ") ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 16:57:22 +01:00
|
|
|
Ref<QueryResult> SQLite3TableBuilder::run() {
|
2021-08-19 23:55:16 +02:00
|
|
|
if (!_db) {
|
|
|
|
printf("SQLite3TableBuilder::run !db!\n");
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _db->query(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SQLite3TableBuilder::run_query() {
|
|
|
|
if (!_db) {
|
|
|
|
printf("SQLite3TableBuilder::run_query !db!\n");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_db->query_run(result);
|
|
|
|
}
|
2021-08-19 22:24:16 +02:00
|
|
|
|
|
|
|
SQLite3TableBuilder::SQLite3TableBuilder() {
|
|
|
|
}
|
|
|
|
|
|
|
|
SQLite3TableBuilder::~SQLite3TableBuilder() {
|
|
|
|
}
|