mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2025-02-20 06:34:23 +01:00
Added the database classes from rcpp_framework.
This commit is contained in:
parent
121740f070
commit
aba918dd73
11
modules/database/database/SCsub
Normal file
11
modules/database/database/SCsub
Normal file
@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
Import("env")
|
||||
|
||||
env.core_sources = []
|
||||
|
||||
env.add_source_files(env.core_sources, "*.cpp")
|
||||
|
||||
# Build it all as a library
|
||||
lib = env.add_library("database", env.core_sources)
|
||||
env.Prepend(LIBS=[lib])
|
36
modules/database/database/database.cpp
Normal file
36
modules/database/database/database.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
#include "database.h"
|
||||
|
||||
#include "query_builder.h"
|
||||
#include "table_builder.h"
|
||||
#include "query_result.h"
|
||||
|
||||
void Database::connect(const String &connection_str) {
|
||||
}
|
||||
|
||||
Ref<QueryResult> Database::query(const String &query) {
|
||||
return Ref<QueryResult>();
|
||||
}
|
||||
void Database::query_run(const String &query) {
|
||||
}
|
||||
|
||||
Ref<QueryBuilder> Database::get_query_builder() {
|
||||
return Ref<QueryBuilder>(new QueryBuilder());
|
||||
}
|
||||
|
||||
Ref<TableBuilder> Database::get_table_builder() {
|
||||
return Ref<TableBuilder>(new TableBuilder());
|
||||
}
|
||||
|
||||
String Database::escape(const String str) {
|
||||
return String();
|
||||
}
|
||||
|
||||
void Database::escape(const String str, String *to) {
|
||||
|
||||
}
|
||||
|
||||
Database::Database() {
|
||||
}
|
||||
|
||||
Database::~Database() {
|
||||
}
|
45
modules/database/database/database.h
Normal file
45
modules/database/database/database.h
Normal file
@ -0,0 +1,45 @@
|
||||
#ifndef DATABASE_H
|
||||
#define DATABASE_H
|
||||
|
||||
#include "core/string.h"
|
||||
#include <memory>
|
||||
|
||||
#include "core/reference.h"
|
||||
|
||||
class QueryBuilder;
|
||||
class TableBuilder;
|
||||
class QueryResult;
|
||||
|
||||
class Database {
|
||||
public:
|
||||
//probably needs refcount, depending on what db engines do, todo
|
||||
//virtual QueryResult *query(const String &sql);
|
||||
//virtual QueryResult *query_async(const String &sql);
|
||||
//or
|
||||
//virtual QueryErrorCode query(QueryResult *result, const String &sql);
|
||||
//virtual QueryErrorCode query_async(QueryResult *result, const String &sql);
|
||||
|
||||
//also
|
||||
//virtual QueryResult *query_prepared(const String &sql, param1, param2, ...);
|
||||
|
||||
//query interface (codeigniter 3 style)
|
||||
//virtual void where(""); etc
|
||||
|
||||
virtual void connect(const String &connection_str);
|
||||
virtual Ref<QueryResult> query(const String &query);
|
||||
virtual void query_run(const String &query);
|
||||
|
||||
virtual Ref<QueryBuilder> get_query_builder();
|
||||
virtual Ref<TableBuilder> get_table_builder();
|
||||
|
||||
virtual String escape(const String str);
|
||||
virtual void escape(const String str, String *to);
|
||||
|
||||
Database();
|
||||
~Database();
|
||||
|
||||
private:
|
||||
//std::vector<QueryBuilder *> _builders;
|
||||
};
|
||||
|
||||
#endif
|
69
modules/database/database/database_manager.cpp
Normal file
69
modules/database/database/database_manager.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
#include "database_manager.h"
|
||||
|
||||
void DatabaseManager::load() {
|
||||
//go thourgh settings, and create all the defined db backends
|
||||
}
|
||||
|
||||
uint32_t DatabaseManager::create_database(const String &name) {
|
||||
Database *db = _create_database(name);
|
||||
|
||||
if (!db) {
|
||||
RLOG_MSG("(DatabaseManager) create_database: " + name + ", returned db is null!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
RLOG_MSG("(DatabaseManager) Database " + name + " successfully created!");
|
||||
|
||||
databases.push_back(db);
|
||||
|
||||
if (ddb == nullptr) {
|
||||
RLOG_MSG("(DatabaseManager) Database " + name + " has been set as the default database!");
|
||||
ddb = db;
|
||||
}
|
||||
|
||||
return databases.size() - 1;
|
||||
}
|
||||
|
||||
DatabaseManager *DatabaseManager::get_singleton() {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
void DatabaseManager::_register_db_creation_func(const String &name, std::function<Database *()> func) {
|
||||
ERR_FAIL_COND_MSG(!func, "_register_db_creation_func: " + name + ", func is wrong!");
|
||||
|
||||
_db_creation_func_map[name] = func;
|
||||
}
|
||||
|
||||
void DatabaseManager::_unregister_db_creation_func(const String &name) {
|
||||
_db_creation_func_map.erase(name);
|
||||
}
|
||||
|
||||
Database *DatabaseManager::_create_database(const String &name) {
|
||||
std::function<Database *()> func = _db_creation_func_map[name];
|
||||
|
||||
ERR_FAIL_COND_V_MSG(!func, nullptr, "_create_database: " + name + ", func is wrong!");
|
||||
|
||||
Database *db = func();
|
||||
|
||||
ERR_FAIL_COND_V_MSG(!db, nullptr, "_create_database: " + name + ", returned db is null!");
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
DatabaseManager::DatabaseManager() {
|
||||
_instance = this;
|
||||
|
||||
ddb = nullptr;
|
||||
}
|
||||
|
||||
DatabaseManager::~DatabaseManager() {
|
||||
_instance = nullptr;
|
||||
|
||||
for (uint32_t i = 0; i < databases.size(); ++i) {
|
||||
delete databases[i];
|
||||
}
|
||||
}
|
||||
|
||||
DatabaseManager *DatabaseManager::_instance = nullptr;
|
||||
|
||||
std::map<String, std::function<Database *()> > DatabaseManager::_db_creation_func_map;
|
42
modules/database/database/database_manager.h
Normal file
42
modules/database/database/database_manager.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef DATABASE_MANAGER_H
|
||||
#define DATABASE_MANAGER_H
|
||||
|
||||
#include "core/string.h"
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <functional>
|
||||
|
||||
#include "core/object.h"
|
||||
|
||||
#include "database.h"
|
||||
|
||||
class DatabaseManager : public Object {
|
||||
RCPP_OBJECT(DatabaseManager, Object);
|
||||
|
||||
public:
|
||||
std::vector<Database *> databases;
|
||||
Database *ddb;
|
||||
|
||||
void load();
|
||||
|
||||
static DatabaseManager *get_singleton();
|
||||
|
||||
//note: not threadsafe, create these at the start of your program!
|
||||
uint32_t create_database(const String &name);
|
||||
|
||||
static void _register_db_creation_func(const String &name, std::function<Database*()> func);
|
||||
static void _unregister_db_creation_func(const String &name);
|
||||
|
||||
static Database *_create_database(const String &name);
|
||||
|
||||
DatabaseManager();
|
||||
~DatabaseManager();
|
||||
|
||||
private:
|
||||
static DatabaseManager * _instance;
|
||||
|
||||
static std::map<String, std::function<Database *()> > _db_creation_func_map;
|
||||
};
|
||||
|
||||
#endif
|
320
modules/database/database/query_builder.cpp
Normal file
320
modules/database/database/query_builder.cpp
Normal file
@ -0,0 +1,320 @@
|
||||
#include "query_builder.h"
|
||||
|
||||
#include "query_result.h"
|
||||
|
||||
QueryBuilder *QueryBuilder::select() {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::update() {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::del() {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::where() {
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::from() {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::insert() {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::values() {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::cvalues() {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::next_value() {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::begin_transaction() {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::commit() {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::nl() {
|
||||
query_result += "\n";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::str() {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::cstr() {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::like() {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::select(const String ¶ms) {
|
||||
return nselect(escape(params));
|
||||
}
|
||||
QueryBuilder *QueryBuilder::update(const String ¶ms) {
|
||||
return nupdate(escape(params));
|
||||
}
|
||||
QueryBuilder *QueryBuilder::del(const String ¶ms) {
|
||||
return ndel(escape(params));
|
||||
}
|
||||
QueryBuilder *QueryBuilder::where(const String ¶ms) {
|
||||
return nwhere(escape(params));
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::from(const String ¶ms) {
|
||||
return nfrom(escape(params));
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::insert(const String &table_name) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::insert(const String &table_name, const String &columns) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::values(const String ¶ms_str) {
|
||||
return nvalues(escape(params_str));
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::val() {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::val(const String ¶m) {
|
||||
return nval(escape(param));
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::val(const char *param) {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::val(const int param) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::val(const bool param) {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::valf(const float param) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::vald(const double param) {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::like(const String &str) {
|
||||
return nlike(escape(str));
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::set() {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::cset() {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::setp(const String &col, const String ¶m) {
|
||||
return nsetp(col, escape(param));
|
||||
}
|
||||
QueryBuilder *QueryBuilder::setp(const String &col, const char *param) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::setp(const String &col, const int param) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::setp(const String &col, const bool param) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::setpf(const String &col, const float param) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::setpd(const String &col, const double param) {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::wp(const String &col, const String ¶m) {
|
||||
return nwp(col, escape(param));
|
||||
}
|
||||
QueryBuilder *QueryBuilder::wp(const String &col, const char *param) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::wp(const String &col, const int param) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::wp(const String &col, const bool param) {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::nselect(const String ¶ms) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::nupdate(const String ¶ms) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::ndel(const String ¶ms) {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::nwhere(const String ¶ms) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::nfrom(const String ¶ms) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::nlike(const String &str) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::nvalues(const String ¶ms_str) {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::nval(const String ¶m) {
|
||||
return val(escape(param));
|
||||
}
|
||||
QueryBuilder *QueryBuilder::nsetp(const String &col, const String &escape_param) {
|
||||
return setp(col, escape(escape_param));
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::nwp(const String &col, const String &escape_param) {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::limit(const int num) {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::offset(const int num) {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::order_by_asc(const String &col) {
|
||||
query_result += "ORDER BY " + col + " ASC, ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::order_by_desc(const String &col) {
|
||||
query_result += "ORDER BY " + col + " DESC, ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::order_by(const String &col) {
|
||||
query_result += "ORDER BY " + col + ", ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::order_by() {
|
||||
query_result += "ORDER BY ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::corder_by() {
|
||||
query_result[query_result.size() - 2] = ' ';
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::asc() {
|
||||
query_result += "ASC, ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::desc() {
|
||||
query_result += "DESC, ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::order_by_add_col(const String &col) {
|
||||
query_result += col + ", ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::asc(const String &col) {
|
||||
query_result += col + " ASC, ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::desc(const String &col) {
|
||||
query_result += col + " DESC, ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::land() {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::lor() {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::wildcard() {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::w(const String &str) {
|
||||
query_result += str;
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::ew(const String &str) {
|
||||
return w(escape(str));
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::select_last_insert_id() {
|
||||
return this;
|
||||
}
|
||||
|
||||
String QueryBuilder::escape(const String ¶ms) {
|
||||
return params;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::prepare() {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::set_param(const int index, const String &value) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::set_param(const int index, const int value) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *QueryBuilder::set_param(const int index, const float value) {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::end_command() {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *QueryBuilder::reset() {
|
||||
query_result = "";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
Ref<QueryResult> QueryBuilder::run() {
|
||||
return Ref<QueryResult>();
|
||||
}
|
||||
|
||||
void QueryBuilder::run_query() {
|
||||
}
|
||||
|
||||
String QueryBuilder::get_result() {
|
||||
end_command();
|
||||
|
||||
return query_result;
|
||||
}
|
||||
|
||||
void QueryBuilder::print() {
|
||||
printf("%s\n", query_result.c_str());
|
||||
}
|
||||
|
||||
QueryBuilder::QueryBuilder() {
|
||||
}
|
||||
|
||||
QueryBuilder::~QueryBuilder() {
|
||||
}
|
135
modules/database/database/query_builder.h
Normal file
135
modules/database/database/query_builder.h
Normal file
@ -0,0 +1,135 @@
|
||||
#ifndef QUERY_BUILDER_H
|
||||
#define QUERY_BUILDER_H
|
||||
|
||||
#include "core/string.h"
|
||||
|
||||
#include "core/reference.h"
|
||||
|
||||
class QueryResult;
|
||||
|
||||
//methods that start with an e escape their params.
|
||||
|
||||
class QueryBuilder : public Reference {
|
||||
RCPP_OBJECT(QueryBuilder, Reference);
|
||||
|
||||
public:
|
||||
virtual QueryBuilder *select();
|
||||
virtual QueryBuilder *update();
|
||||
virtual QueryBuilder *del();
|
||||
|
||||
virtual QueryBuilder *where();
|
||||
virtual QueryBuilder *from();
|
||||
virtual QueryBuilder *insert();
|
||||
virtual QueryBuilder *values();
|
||||
virtual QueryBuilder *cvalues();
|
||||
virtual QueryBuilder *next_value();
|
||||
|
||||
virtual QueryBuilder *begin_transaction();
|
||||
virtual QueryBuilder *commit();
|
||||
|
||||
virtual QueryBuilder *nl();
|
||||
|
||||
virtual QueryBuilder *str();
|
||||
virtual QueryBuilder *cstr();
|
||||
|
||||
virtual QueryBuilder *like();
|
||||
|
||||
virtual QueryBuilder *select(const String ¶ms);
|
||||
virtual QueryBuilder *update(const String ¶ms);
|
||||
virtual QueryBuilder *del(const String ¶ms);
|
||||
|
||||
virtual QueryBuilder *where(const String ¶ms);
|
||||
virtual QueryBuilder *from(const String ¶ms);
|
||||
virtual QueryBuilder *insert(const String &table_name);
|
||||
virtual QueryBuilder *insert(const String &table_name, const String &columns);
|
||||
virtual QueryBuilder *values(const String ¶ms_str);
|
||||
virtual QueryBuilder *val();
|
||||
virtual QueryBuilder *val(const String ¶m);
|
||||
virtual QueryBuilder *val(const char *param);
|
||||
virtual QueryBuilder *val(const int param);
|
||||
virtual QueryBuilder *val(const bool param);
|
||||
virtual QueryBuilder *valf(const float param);
|
||||
virtual QueryBuilder *vald(const double param);
|
||||
|
||||
virtual QueryBuilder *like(const String &str);
|
||||
|
||||
virtual QueryBuilder *set();
|
||||
virtual QueryBuilder *cset();
|
||||
|
||||
virtual QueryBuilder *setp(const String &col, const String ¶m);
|
||||
virtual QueryBuilder *setp(const String &col, const char *param);
|
||||
virtual QueryBuilder *setp(const String &col, const int param);
|
||||
virtual QueryBuilder *setp(const String &col, const bool param);
|
||||
virtual QueryBuilder *setpf(const String &col, const float param);
|
||||
virtual QueryBuilder *setpd(const String &col, const double param);
|
||||
|
||||
virtual QueryBuilder *wp(const String &col, const String ¶m);
|
||||
virtual QueryBuilder *wp(const String &col, const char *param);
|
||||
virtual QueryBuilder *wp(const String &col, const int param);
|
||||
virtual QueryBuilder *wp(const String &col, const bool param);
|
||||
|
||||
virtual QueryBuilder *nselect(const String ¶ms);
|
||||
virtual QueryBuilder *nupdate(const String ¶ms);
|
||||
virtual QueryBuilder *ndel(const String ¶ms);
|
||||
|
||||
virtual QueryBuilder *nwhere(const String ¶ms);
|
||||
virtual QueryBuilder *nfrom(const String ¶ms);
|
||||
virtual QueryBuilder *nlike(const String &str);
|
||||
virtual QueryBuilder *nvalues(const String ¶ms_str);
|
||||
virtual QueryBuilder *nval(const String ¶m);
|
||||
//note col is NOT escaped
|
||||
virtual QueryBuilder *nsetp(const String &col, const String &escape_param);
|
||||
//note col is NOT escaped
|
||||
virtual QueryBuilder *nwp(const String &col, const String &escape_param);
|
||||
|
||||
virtual QueryBuilder *limit(const int num);
|
||||
virtual QueryBuilder *offset(const int num);
|
||||
|
||||
virtual QueryBuilder *order_by_asc(const String &col);
|
||||
virtual QueryBuilder *order_by_desc(const String &col);
|
||||
virtual QueryBuilder *order_by(const String &col);
|
||||
|
||||
virtual QueryBuilder *order_by();
|
||||
virtual QueryBuilder *corder_by();
|
||||
virtual QueryBuilder *asc();
|
||||
virtual QueryBuilder *desc();
|
||||
virtual QueryBuilder *order_by_add_col(const String &col);
|
||||
virtual QueryBuilder *asc(const String &col);
|
||||
virtual QueryBuilder *desc(const String &col);
|
||||
|
||||
//l=logical (and, or are operators)
|
||||
virtual QueryBuilder *land();
|
||||
virtual QueryBuilder *lor();
|
||||
|
||||
virtual QueryBuilder *wildcard();
|
||||
|
||||
virtual QueryBuilder *w(const String &str);
|
||||
virtual QueryBuilder *ew(const String &str);
|
||||
|
||||
virtual QueryBuilder *select_last_insert_id();
|
||||
|
||||
virtual String escape(const String ¶ms);
|
||||
|
||||
virtual QueryBuilder *prepare();
|
||||
virtual QueryBuilder *set_param(const int index, const String &value);
|
||||
virtual QueryBuilder *set_param(const int index, const int value);
|
||||
virtual QueryBuilder *set_param(const int index, const float value);
|
||||
|
||||
virtual QueryBuilder *end_command();
|
||||
|
||||
virtual QueryBuilder *reset();
|
||||
|
||||
virtual Ref<QueryResult> run();
|
||||
virtual void run_query();
|
||||
|
||||
String get_result();
|
||||
|
||||
void print();
|
||||
|
||||
QueryBuilder();
|
||||
virtual ~QueryBuilder();
|
||||
|
||||
String query_result;
|
||||
};
|
||||
|
||||
#endif
|
77
modules/database/database/query_result.cpp
Normal file
77
modules/database/database/query_result.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
#include "query_result.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
bool QueryResult::next_row() {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *QueryResult::get_cell(const int index) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const String QueryResult::get_cell_str(const int index) {
|
||||
return String(get_cell(index));
|
||||
}
|
||||
|
||||
const bool QueryResult::get_cell_bool(const int index) {
|
||||
return get_cell_int(index);
|
||||
}
|
||||
|
||||
const int QueryResult::get_cell_int(const int index) {
|
||||
if (is_cell_null(index)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//todo better way
|
||||
std::stringstream ss;
|
||||
ss.str(get_cell(index));
|
||||
|
||||
int r;
|
||||
ss >> r;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
const float QueryResult::get_cell_float(const int index) {
|
||||
if (is_cell_null(index)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//todo better way
|
||||
std::stringstream ss;
|
||||
ss.str(get_cell(index));
|
||||
|
||||
float r;
|
||||
ss >> r;
|
||||
|
||||
return r;
|
||||
}
|
||||
const double QueryResult::get_cell_double(const int index) {
|
||||
if (is_cell_null(index)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//todo better way
|
||||
std::stringstream ss;
|
||||
ss.str(get_cell(index));
|
||||
|
||||
double r;
|
||||
ss >> r;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
bool QueryResult::is_cell_null(const int index) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int QueryResult::get_last_insert_rowid() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
QueryResult::QueryResult() {
|
||||
}
|
||||
|
||||
QueryResult::~QueryResult() {
|
||||
}
|
28
modules/database/database/query_result.h
Normal file
28
modules/database/database/query_result.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef QUERY_RESULT_H
|
||||
#define QUERY_RESULT_H
|
||||
|
||||
#include "core/string.h"
|
||||
|
||||
#include "core/reference.h"
|
||||
|
||||
class QueryResult : public Reference {
|
||||
RCPP_OBJECT(QueryResult, Reference);
|
||||
|
||||
public:
|
||||
virtual bool next_row();
|
||||
virtual const char *get_cell(const int index);
|
||||
virtual const String get_cell_str(const int index);
|
||||
virtual const bool get_cell_bool(const int index);
|
||||
virtual const int get_cell_int(const int index);
|
||||
virtual const float get_cell_float(const int index);
|
||||
virtual const double get_cell_double(const int index);
|
||||
|
||||
virtual bool is_cell_null(const int index);
|
||||
|
||||
virtual int get_last_insert_rowid();
|
||||
|
||||
QueryResult();
|
||||
virtual ~QueryResult();
|
||||
};
|
||||
|
||||
#endif
|
127
modules/database/database/table_builder.cpp
Normal file
127
modules/database/database/table_builder.cpp
Normal file
@ -0,0 +1,127 @@
|
||||
#include "table_builder.h"
|
||||
|
||||
#include "query_result.h"
|
||||
|
||||
TableBuilder *TableBuilder::create_table(const String &name) {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::integer(const String &name) {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::integer(const String &name, const int length) {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::tiny_integer(const String &name) {
|
||||
return this;
|
||||
}
|
||||
TableBuilder *TableBuilder::tiny_integer(const String &name, const int length) {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::small_integer(const String &name) {
|
||||
return this;
|
||||
}
|
||||
TableBuilder *TableBuilder::small_integer(const String &name, const int length) {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::real_float(const String &name) {
|
||||
return this;
|
||||
}
|
||||
TableBuilder *TableBuilder::real_float(const String &name, const int size, const int d) {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::real_double(const String &name) {
|
||||
return this;
|
||||
}
|
||||
TableBuilder *TableBuilder::real_double(const String &name, const int size, const int d) {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::date(const String &name) {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::varchar(const String &name, const int length) {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::text(const String &name) {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::not_null() {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::null() {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::defval(const String &val) {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::auto_increment() {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::primary_key(const String &name) {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::primary_key() {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::next_row() {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::ccreate_table() {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::drop_table() {
|
||||
return this;
|
||||
}
|
||||
TableBuilder *TableBuilder::drop_table_if_exists() {
|
||||
return this;
|
||||
}
|
||||
TableBuilder *TableBuilder::drop_table(const String &name) {
|
||||
return this;
|
||||
}
|
||||
TableBuilder *TableBuilder::drop_table_if_exists(const String &name) {
|
||||
return this;
|
||||
}
|
||||
TableBuilder *TableBuilder::cdrop_table() {
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *TableBuilder::foreign_key(const String &name) {
|
||||
return this;
|
||||
}
|
||||
TableBuilder *TableBuilder::references(const String &table, const String &name) {
|
||||
return this;
|
||||
}
|
||||
|
||||
Ref<QueryResult> TableBuilder::run() {
|
||||
return Ref<QueryResult>();
|
||||
}
|
||||
|
||||
void TableBuilder::run_query() {
|
||||
}
|
||||
|
||||
void TableBuilder::print() {
|
||||
printf("%s\n", result.c_str());
|
||||
}
|
||||
|
||||
TableBuilder::TableBuilder() {
|
||||
}
|
||||
|
||||
TableBuilder::~TableBuilder() {
|
||||
}
|
65
modules/database/database/table_builder.h
Normal file
65
modules/database/database/table_builder.h
Normal file
@ -0,0 +1,65 @@
|
||||
#ifndef TABLE_BUILDER_H
|
||||
#define TABLE_BUILDER_H
|
||||
|
||||
#include "core/string.h"
|
||||
|
||||
#include "core/reference.h"
|
||||
|
||||
class QueryResult;
|
||||
|
||||
class TableBuilder : public Reference {
|
||||
RCPP_OBJECT(TableBuilder, Reference);
|
||||
|
||||
public:
|
||||
virtual TableBuilder *create_table(const String &name);
|
||||
|
||||
virtual TableBuilder *integer(const String &name);
|
||||
virtual TableBuilder *integer(const String &name, const int length);
|
||||
|
||||
virtual TableBuilder *tiny_integer(const String &name);
|
||||
virtual TableBuilder *tiny_integer(const String &name, const int length);
|
||||
|
||||
virtual TableBuilder *small_integer(const String &name);
|
||||
virtual TableBuilder *small_integer(const String &name, const int length);
|
||||
|
||||
virtual TableBuilder *real_float(const String &name);
|
||||
virtual TableBuilder *real_float(const String &name, const int size, const int d);
|
||||
|
||||
virtual TableBuilder *real_double(const String &name);
|
||||
virtual TableBuilder *real_double(const String &name, const int size, const int d);
|
||||
|
||||
virtual TableBuilder *date(const String &name);
|
||||
|
||||
virtual TableBuilder *varchar(const String &name, const int length);
|
||||
virtual TableBuilder *text(const String &name);
|
||||
|
||||
virtual TableBuilder *not_null();
|
||||
virtual TableBuilder *null();
|
||||
virtual TableBuilder *defval(const String &val);
|
||||
virtual TableBuilder *auto_increment();
|
||||
virtual TableBuilder *primary_key(const String &name);
|
||||
virtual TableBuilder *primary_key();
|
||||
virtual TableBuilder *next_row();
|
||||
virtual TableBuilder *ccreate_table();
|
||||
|
||||
virtual TableBuilder *drop_table();
|
||||
virtual TableBuilder *drop_table_if_exists();
|
||||
virtual TableBuilder *drop_table(const String &name);
|
||||
virtual TableBuilder *drop_table_if_exists(const String &name);
|
||||
virtual TableBuilder *cdrop_table();
|
||||
|
||||
virtual TableBuilder *foreign_key(const String &name);
|
||||
virtual TableBuilder *references(const String &table, const String &name);
|
||||
|
||||
virtual Ref<QueryResult> run();
|
||||
virtual void run_query();
|
||||
|
||||
void print();
|
||||
|
||||
TableBuilder();
|
||||
virtual ~TableBuilder();
|
||||
|
||||
String result;
|
||||
};
|
||||
|
||||
#endif
|
30
modules/database/database_backends/db_init.h
Normal file
30
modules/database/database_backends/db_init.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef DB_INIT_H
|
||||
#define DB_INIT_H
|
||||
|
||||
#if MYSQL_PRESENT
|
||||
#include "mysql/mysql_database.h"
|
||||
#endif
|
||||
|
||||
#if PGSQL_PRESENT
|
||||
#include "postgres/pgsql_database.h"
|
||||
#endif
|
||||
|
||||
#if SQLITE_PRESENT
|
||||
#include "sqlite/sqlite3_database.h"
|
||||
#endif
|
||||
|
||||
void initialize_database_backends() {
|
||||
#if MYSQL_PRESENT
|
||||
MysqlDatabase::_register();
|
||||
#endif
|
||||
|
||||
#if PGSQL_PRESENT
|
||||
PGSQLDatabase::_register();
|
||||
#endif
|
||||
|
||||
#if SQLITE_PRESENT
|
||||
SQLite3Database::_register();
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
12
modules/database/database_backends/mysql/SCsub
Normal file
12
modules/database/database_backends/mysql/SCsub
Normal file
@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
Import("env_db")
|
||||
Import("env")
|
||||
|
||||
env_db.core_sources = []
|
||||
|
||||
env_db.add_source_files(env_db.core_sources, "*.cpp")
|
||||
|
||||
# Build it all as a library
|
||||
lib = env_db.add_library("database_mysql", env_db.core_sources)
|
||||
env.Prepend(LIBS=[lib])
|
71
modules/database/database_backends/mysql/detect.py
Normal file
71
modules/database/database_backends/mysql/detect.py
Normal file
@ -0,0 +1,71 @@
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
|
||||
|
||||
def is_active():
|
||||
return True
|
||||
|
||||
|
||||
def get_name():
|
||||
return "mysql"
|
||||
|
||||
|
||||
def can_build():
|
||||
|
||||
if os.name == "posix" or sys.platform == "darwin":
|
||||
x11_error = os.system("pkg-config --version > /dev/null")
|
||||
if x11_error:
|
||||
return False
|
||||
|
||||
mariadb_error = os.system("pkg-config mariadb --modversion --silence-errors > /dev/null ")
|
||||
mysql_error = os.system("pkg-config mysql --modversion --silence-errors > /dev/null ")
|
||||
|
||||
if mariadb_error and mysql_error:
|
||||
print("mysql and mariadb not found..")
|
||||
return False
|
||||
|
||||
if not mariadb_error:
|
||||
print("mariadb found!")
|
||||
|
||||
return True
|
||||
|
||||
if not mysql_error:
|
||||
print("mysql found!")
|
||||
|
||||
return True
|
||||
|
||||
#todo
|
||||
return False
|
||||
|
||||
|
||||
def get_opts():
|
||||
from SCons.Variables import BoolVariable, EnumVariable
|
||||
|
||||
return [
|
||||
EnumVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", "yes", ("yes", "no")),
|
||||
]
|
||||
|
||||
|
||||
def get_flags():
|
||||
|
||||
return []
|
||||
|
||||
|
||||
def configure(env):
|
||||
mariadb_error = os.system("pkg-config mariadb --modversion --silence-errors > /dev/null ")
|
||||
mysql_error = os.system("pkg-config mysql --modversion --silence-errors > /dev/null ")
|
||||
|
||||
if not mariadb_error:
|
||||
env.ParseConfig("pkg-config mariadb --cflags --libs")
|
||||
env.Append(CPPDEFINES=["MYSQL_PRESENT"])
|
||||
return
|
||||
|
||||
if not mysql_error:
|
||||
env.ParseConfig("pkg-config mysql --cflags --libs")
|
||||
|
||||
env.Append(CPPDEFINES=["MYSQL_PRESENT"])
|
||||
|
||||
# Link those statically for portability
|
||||
#if env["use_static_cpp"]:
|
||||
#env.Append(LINKFLAGS=["-static-libgcc", "-static-libstdc++"])
|
141
modules/database/database_backends/mysql/mysql_database.cpp
Normal file
141
modules/database/database_backends/mysql/mysql_database.cpp
Normal file
@ -0,0 +1,141 @@
|
||||
#include "mysql_database.h"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include "database/database_manager.h"
|
||||
|
||||
#include "mysql_query_builder.h"
|
||||
#include "mysql_query_result.h"
|
||||
#include "mysql_table_builder.h"
|
||||
|
||||
void MysqlDatabase::connect(const String &connection_str) {
|
||||
mysql = mysql_init(mysql);
|
||||
mysql_options(mysql, MYSQL_OPT_NONBLOCK, 0);
|
||||
|
||||
String host = "127.0.0.1";
|
||||
String user = "";
|
||||
String password = "";
|
||||
String dbname = "testappdb";
|
||||
int port = 3306;
|
||||
|
||||
mysql = mysql_real_connect(mysql, host.c_str(), user.c_str(), password.c_str(), dbname.c_str(), port, NULL, 0);
|
||||
|
||||
if (mysql) {
|
||||
printf("mysql connected\n");
|
||||
}
|
||||
}
|
||||
|
||||
Ref<QueryResult> MysqlDatabase::query(const String &query) {
|
||||
if (!mysql)
|
||||
return nullptr;
|
||||
|
||||
//printf("%s\n", query.c_str());
|
||||
|
||||
int error = mysql_real_query(mysql, query.c_str(), query.capacity());
|
||||
|
||||
if (error) {
|
||||
const char *merr = mysql_error(mysql);
|
||||
|
||||
printf("MySQL error: %s\n", merr);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
MYSQL_RES *result = mysql_use_result(mysql);
|
||||
|
||||
MysqlQueryResult *res = new MysqlQueryResult();
|
||||
|
||||
res->result = result;
|
||||
//res->next_row();
|
||||
|
||||
return Ref<QueryResult>(res);
|
||||
}
|
||||
|
||||
void MysqlDatabase::query_run(const String &query) {
|
||||
if (!mysql)
|
||||
return;
|
||||
|
||||
//printf("%s\n", query.c_str());
|
||||
|
||||
int error = mysql_real_query(mysql, query.c_str(), query.capacity());
|
||||
|
||||
if (error) {
|
||||
const char *merr = mysql_error(mysql);
|
||||
|
||||
printf("MySQL error: %s\n", merr);
|
||||
return;
|
||||
}
|
||||
|
||||
//printf("query OK\n");
|
||||
//printf("----------------\n");
|
||||
|
||||
/*
|
||||
printf("----------------\n");
|
||||
|
||||
MYSQL_RES *result = mysql_use_result(mysql);
|
||||
//MYSQL_RES *result = mysql_store_result(mysql);
|
||||
|
||||
MYSQL_ROW row;
|
||||
while (row = mysql_fetch_row(result)) {
|
||||
printf("%s\n", row[0]);
|
||||
//printf("%s\n", row[1]);
|
||||
}
|
||||
|
||||
printf("----------------\n");
|
||||
|
||||
mysql_free_result(result);
|
||||
*/
|
||||
}
|
||||
|
||||
Ref<QueryBuilder> MysqlDatabase::get_query_builder() {
|
||||
MysqlQueryBuilder *b = new MysqlQueryBuilder();
|
||||
b->_db = this;
|
||||
|
||||
return Ref<QueryBuilder>(b);
|
||||
}
|
||||
|
||||
Ref<TableBuilder> MysqlDatabase::get_table_builder() {
|
||||
return Ref<TableBuilder>(new MysqlTableBuilder());
|
||||
}
|
||||
|
||||
String MysqlDatabase::escape(const String str) {
|
||||
String res;
|
||||
//https://dev.mysql.com/doc/c-api/8.0/en/mysql-real-escape-string.html
|
||||
//You must allocate the to buffer to be at least length*2+1 bytes long.
|
||||
res.ensure_capacity(str.size() * 2 + 1);
|
||||
|
||||
mysql_real_escape_string(mysql, res.dataw(), str.c_str(), str.size());
|
||||
|
||||
return res;
|
||||
}
|
||||
void MysqlDatabase::escape(const String str, String *to) {
|
||||
//https://dev.mysql.com/doc/c-api/8.0/en/mysql-real-escape-string.html
|
||||
//You must allocate the to buffer to be at least length*2+1 bytes long.
|
||||
to->ensure_capacity(str.size() * 2 + 1);
|
||||
|
||||
mysql_real_escape_string(mysql, to->dataw(), str.c_str(), str.size());
|
||||
}
|
||||
|
||||
MysqlDatabase::MysqlDatabase() :
|
||||
Database() {
|
||||
|
||||
mysql = nullptr;
|
||||
}
|
||||
|
||||
MysqlDatabase::~MysqlDatabase() {
|
||||
mysql_close(mysql);
|
||||
|
||||
delete mysql;
|
||||
}
|
||||
|
||||
Database *MysqlDatabase::_creation_func() {
|
||||
return new MysqlDatabase();
|
||||
}
|
||||
|
||||
void MysqlDatabase::_register() {
|
||||
DatabaseManager::_register_db_creation_func("mysql", MysqlDatabase::_creation_func);
|
||||
}
|
||||
|
||||
void MysqlDatabase::_unregister() {
|
||||
DatabaseManager::_unregister_db_creation_func("mysql");
|
||||
}
|
42
modules/database/database_backends/mysql/mysql_database.h
Normal file
42
modules/database/database_backends/mysql/mysql_database.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef MYSQL_CONNECTION
|
||||
#define MYSQL_CONNECTION
|
||||
|
||||
#include "core/string.h"
|
||||
|
||||
#include "database/database.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
//Brynet has it aswell, and because of using namespace it is defined here aswell
|
||||
//later this will be fixed better
|
||||
#ifdef IS_NUM
|
||||
#undef IS_NUM
|
||||
#endif
|
||||
|
||||
#include <mysql.h>
|
||||
|
||||
class MysqlDatabase : public Database {
|
||||
public:
|
||||
void connect(const String &connection_str);
|
||||
Ref<QueryResult> query(const String &query);
|
||||
void query_run(const String &query);
|
||||
|
||||
Ref<QueryBuilder> get_query_builder();
|
||||
Ref<TableBuilder> get_table_builder();
|
||||
|
||||
String escape(const String str);
|
||||
void escape(const String str, String *to);
|
||||
|
||||
static Database *_creation_func();
|
||||
static void _register();
|
||||
static void _unregister();
|
||||
|
||||
MysqlDatabase();
|
||||
~MysqlDatabase();
|
||||
|
||||
MYSQL *mysql;
|
||||
};
|
||||
|
||||
#undef IS_NUM
|
||||
|
||||
#endif
|
136
modules/database/database_backends/mysql/mysql_query_builder.cpp
Normal file
136
modules/database/database_backends/mysql/mysql_query_builder.cpp
Normal file
@ -0,0 +1,136 @@
|
||||
#include "mysql_query_builder.h"
|
||||
|
||||
#include "mysql_database.h"
|
||||
#include "mysql_query_result.h"
|
||||
|
||||
QueryBuilder *MysqlQueryBuilder::select() {
|
||||
query_result += "SELECT ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *MysqlQueryBuilder::where() {
|
||||
query_result += "WHERE ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *MysqlQueryBuilder::from() {
|
||||
query_result += "FROM ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *MysqlQueryBuilder::insert() {
|
||||
query_result += "INSERT INTO ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *MysqlQueryBuilder::values() {
|
||||
query_result += "VALUES(";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *MysqlQueryBuilder::cvalues() {
|
||||
query_result += ") ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *MysqlQueryBuilder::nselect(const String ¶ms) {
|
||||
query_result += "SELECT " + params + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *MysqlQueryBuilder::nwhere(const String ¶ms) {
|
||||
query_result += "WHERE " + params + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *MysqlQueryBuilder::nfrom(const String ¶ms) {
|
||||
query_result += "FROM " + params + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *MysqlQueryBuilder::ninsert(const String &table_name) {
|
||||
query_result += "INSERT INTO " + table_name + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *MysqlQueryBuilder::nvalues(const String ¶ms_str) {
|
||||
query_result += "VALUES(" + params_str + ") ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
String MysqlQueryBuilder::escape(const String ¶ms) {
|
||||
if (!_db) {
|
||||
printf("MysqlQueryBuilder::escape !db!\n");
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
return _db->escape(params);
|
||||
}
|
||||
|
||||
QueryBuilder *MysqlQueryBuilder::prepare() {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *MysqlQueryBuilder::set_param(const int index, const String &value) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *MysqlQueryBuilder::set_param(const int index, const int value) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *MysqlQueryBuilder::set_param(const int index, const float value) {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *MysqlQueryBuilder::end_command() {
|
||||
query_result += ";";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
Ref<QueryResult> MysqlQueryBuilder::run() {
|
||||
end_command();
|
||||
|
||||
if (!_db) {
|
||||
printf("MysqlQueryBuilder::run !db!\n");
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return _db->query(query_result);
|
||||
}
|
||||
|
||||
void MysqlQueryBuilder::run_query() {
|
||||
end_command();
|
||||
|
||||
if (!_db) {
|
||||
printf("MysqlQueryBuilder::run_query !db!\n");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
_db->query_run(query_result);
|
||||
}
|
||||
|
||||
QueryBuilder *MysqlQueryBuilder::limit(const int num) {
|
||||
//query_result += "LIMIT " + num + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *MysqlQueryBuilder::offset(const int num) {
|
||||
//query_result += "OFFSET " + num + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
MysqlQueryBuilder::MysqlQueryBuilder() {
|
||||
}
|
||||
MysqlQueryBuilder::~MysqlQueryBuilder() {
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
#ifndef MYSQL_QUERY_BUILDER_H
|
||||
#define MYSQL_QUERY_BUILDER_H
|
||||
|
||||
#include <memory>
|
||||
#include "core/string.h"
|
||||
|
||||
#include "database/query_builder.h"
|
||||
|
||||
class MysqlDatabase;
|
||||
class MysqlQueryResult;
|
||||
|
||||
class MysqlQueryBuilder : public QueryBuilder {
|
||||
RCPP_OBJECT(MysqlQueryBuilder, QueryBuilder);
|
||||
|
||||
public:
|
||||
QueryBuilder *select();
|
||||
QueryBuilder *where();
|
||||
QueryBuilder *from();
|
||||
QueryBuilder *insert();
|
||||
QueryBuilder *values();
|
||||
QueryBuilder *cvalues();
|
||||
|
||||
QueryBuilder *nselect(const String ¶ms);
|
||||
QueryBuilder *nwhere(const String ¶ms);
|
||||
QueryBuilder *nfrom(const String ¶ms);
|
||||
QueryBuilder *ninsert(const String &table_name);
|
||||
QueryBuilder *nvalues(const String ¶ms_str);
|
||||
|
||||
QueryBuilder *limit(const int num);
|
||||
QueryBuilder *offset(const int num);
|
||||
|
||||
String escape(const String ¶ms);
|
||||
|
||||
QueryBuilder *prepare();
|
||||
QueryBuilder *set_param(const int index, const String &value);
|
||||
QueryBuilder *set_param(const int index, const int value);
|
||||
QueryBuilder *set_param(const int index, const float value);
|
||||
|
||||
QueryBuilder *end_command();
|
||||
|
||||
Ref<QueryResult> run();
|
||||
void run_query();
|
||||
|
||||
MysqlQueryBuilder();
|
||||
~MysqlQueryBuilder();
|
||||
|
||||
MysqlDatabase *_db;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,27 @@
|
||||
#include "mysql_query_result.h"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
bool MysqlQueryResult::next_row() {
|
||||
current_row = mysql_fetch_row(result);
|
||||
|
||||
//null if no result
|
||||
return current_row;
|
||||
}
|
||||
|
||||
const char* MysqlQueryResult::get_cell(const int index) {
|
||||
if (!current_row)
|
||||
return "";
|
||||
|
||||
return current_row[index];
|
||||
}
|
||||
|
||||
MysqlQueryResult::MysqlQueryResult() : QueryResult() {
|
||||
result = nullptr;
|
||||
}
|
||||
|
||||
MysqlQueryResult::~MysqlQueryResult() {
|
||||
if(result) {
|
||||
mysql_free_result(result);
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
#ifndef MYSQL_QUERY_RESULT_H
|
||||
#define MYSQL_QUERY_RESULT_H
|
||||
|
||||
#include "database/query_result.h"
|
||||
|
||||
#include <mysql.h>
|
||||
|
||||
class MysqlQueryResult : public QueryResult {
|
||||
RCPP_OBJECT(MysqlQueryResult, QueryResult);
|
||||
|
||||
public:
|
||||
bool next_row();
|
||||
const char* get_cell(const int index);
|
||||
|
||||
MysqlQueryResult();
|
||||
~MysqlQueryResult();
|
||||
|
||||
MYSQL_ROW current_row;
|
||||
MYSQL_RES *result;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,73 @@
|
||||
#include "mysql_table_builder.h"
|
||||
|
||||
TableBuilder *MysqlTableBuilder::create_table(const String &name) {
|
||||
result += "CREATE TABLE " + name + " ( ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *MysqlTableBuilder::integer(const String &name) {
|
||||
result += name + " INTEGER ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *MysqlTableBuilder::date(const String &name) {
|
||||
result += name + " DATE ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *MysqlTableBuilder::varchar(const String &name, const int length) {
|
||||
result += name + " VARCHAR(" + std::to_string(length) + ")";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *MysqlTableBuilder::not_null() {
|
||||
result += "NOT NULL ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *MysqlTableBuilder::null() {
|
||||
result += "NULL ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *MysqlTableBuilder::auto_increment() {
|
||||
result += "AUTO_INCREMENT ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *MysqlTableBuilder::primary_key(const String &name) {
|
||||
result += "PRIMARY KEY (" + name + ") ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *MysqlTableBuilder::primary_key() {
|
||||
result += "PRIMARY KEY ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *MysqlTableBuilder::next_row() {
|
||||
result += ", ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *MysqlTableBuilder::drop_table(const String &name) {
|
||||
result += "DROP TABLE " + name + ";";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
MysqlTableBuilder::MysqlTableBuilder() {
|
||||
}
|
||||
|
||||
MysqlTableBuilder::~MysqlTableBuilder() {
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
#ifndef MYSQL_TABLE_BUILDER_H
|
||||
#define MYSQL_TABLE_BUILDER_H
|
||||
|
||||
#include "core/string.h"
|
||||
|
||||
#include "database/table_builder.h"
|
||||
|
||||
class MysqlTableBuilder : public TableBuilder {
|
||||
RCPP_OBJECT(MysqlTableBuilder, TableBuilder);
|
||||
|
||||
public:
|
||||
TableBuilder *create_table(const String &name);
|
||||
TableBuilder *integer(const String &name);
|
||||
TableBuilder *date(const String &name);
|
||||
TableBuilder *varchar(const String &name, const int length);
|
||||
TableBuilder *not_null();
|
||||
TableBuilder *null();
|
||||
TableBuilder *auto_increment();
|
||||
TableBuilder *primary_key(const String &name);
|
||||
TableBuilder *primary_key();
|
||||
TableBuilder *next_row();
|
||||
|
||||
TableBuilder *drop_table(const String &name);
|
||||
|
||||
MysqlTableBuilder();
|
||||
virtual ~MysqlTableBuilder();
|
||||
};
|
||||
|
||||
#endif
|
12
modules/database/database_backends/postgres/SCsub
Normal file
12
modules/database/database_backends/postgres/SCsub
Normal file
@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
Import("env_db")
|
||||
Import("env")
|
||||
|
||||
env_db.core_sources = []
|
||||
|
||||
env_db.add_source_files(env_db.core_sources, "*.cpp")
|
||||
|
||||
# Build it all as a library
|
||||
lib = env_db.add_library("database_pgsql", env_db.core_sources)
|
||||
env.Prepend(LIBS=[lib])
|
55
modules/database/database_backends/postgres/detect.py
Normal file
55
modules/database/database_backends/postgres/detect.py
Normal file
@ -0,0 +1,55 @@
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
|
||||
|
||||
def is_active():
|
||||
return True
|
||||
|
||||
|
||||
def get_name():
|
||||
return "pgsql"
|
||||
|
||||
|
||||
def can_build():
|
||||
|
||||
if os.name == "posix" or sys.platform == "darwin":
|
||||
x11_error = os.system("pkg-config --version > /dev/null")
|
||||
if x11_error:
|
||||
return False
|
||||
|
||||
libpg_error = os.system("pkg-config libpq --modversion --silence-errors > /dev/null ")
|
||||
|
||||
if libpg_error:
|
||||
print("postgres not found!")
|
||||
return False
|
||||
|
||||
print("postgres found!")
|
||||
|
||||
return True
|
||||
|
||||
#todo
|
||||
return False
|
||||
|
||||
|
||||
def get_opts():
|
||||
from SCons.Variables import BoolVariable, EnumVariable
|
||||
|
||||
return [
|
||||
EnumVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", "yes", ("yes", "no")),
|
||||
]
|
||||
|
||||
|
||||
def get_flags():
|
||||
|
||||
return []
|
||||
|
||||
|
||||
def configure(env):
|
||||
env.ParseConfig("pkg-config libpq --cflags --libs")
|
||||
|
||||
env.Append(CPPDEFINES=["PGSQL_PRESENT"])
|
||||
|
||||
# Link those statically for portability
|
||||
#if env["use_static_cpp"]:
|
||||
#env.Append(LINKFLAGS=["-static-libgcc", "-static-libstdc++"])
|
@ -0,0 +1,15 @@
|
||||
#include "pgsql_database.h"
|
||||
|
||||
#include "database/database_manager.h"
|
||||
|
||||
Database *PGSQLDatabase::_creation_func() {
|
||||
return new PGSQLDatabase();
|
||||
}
|
||||
|
||||
void PGSQLDatabase::_register() {
|
||||
DatabaseManager::_register_db_creation_func("pgsql", PGSQLDatabase::_creation_func);
|
||||
}
|
||||
|
||||
void PGSQLDatabase::_unregister() {
|
||||
DatabaseManager::_unregister_db_creation_func("pgsql");
|
||||
}
|
33
modules/database/database_backends/postgres/pgsql_database.h
Normal file
33
modules/database/database_backends/postgres/pgsql_database.h
Normal file
@ -0,0 +1,33 @@
|
||||
#ifndef PGSQL_CONNECTION
|
||||
#define PGSQL_CONNECTION
|
||||
|
||||
#include "database/database.h"
|
||||
|
||||
//Brynet has it aswell, and because of using namespace it is defined here aswell
|
||||
//later this will be fixed better
|
||||
//#ifdef IS_NUM
|
||||
//#undef IS_NUM
|
||||
//#endif
|
||||
|
||||
#include <libpq-fe.h>
|
||||
|
||||
class PGSQLDatabase : public Database {
|
||||
public:
|
||||
static Database *_creation_func();
|
||||
static void _register();
|
||||
static void _unregister();
|
||||
|
||||
PGSQLDatabase() :
|
||||
Database() {
|
||||
conn = PQconnectStart("");
|
||||
}
|
||||
~PGSQLDatabase() {
|
||||
PQfinish(conn);
|
||||
}
|
||||
|
||||
PGconn *conn;
|
||||
};
|
||||
|
||||
//#undef IS_NUM
|
||||
|
||||
#endif
|
13
modules/database/database_backends/sqlite/SCsub
Normal file
13
modules/database/database_backends/sqlite/SCsub
Normal file
@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
Import("env_db")
|
||||
Import("env")
|
||||
|
||||
env_db.core_sources = []
|
||||
|
||||
env_db.add_source_files(env_db.core_sources, "*.cpp")
|
||||
env_db.core_sources.append("./sqlite/sqlite3.c")
|
||||
|
||||
# Build it all as a library
|
||||
lib = env_db.add_library("database_sqlite", env_db.core_sources)
|
||||
env.Prepend(LIBS=[lib])
|
62
modules/database/database_backends/sqlite/detect.py
Normal file
62
modules/database/database_backends/sqlite/detect.py
Normal file
@ -0,0 +1,62 @@
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
|
||||
|
||||
def is_active():
|
||||
return True
|
||||
|
||||
|
||||
def get_name():
|
||||
return "sqlite3"
|
||||
|
||||
|
||||
def can_build():
|
||||
|
||||
# if os.name == "posix" or sys.platform == "darwin":
|
||||
# x11_error = os.system("pkg-config --version > /dev/null")
|
||||
# if x11_error:
|
||||
# return False
|
||||
|
||||
# sqlite_error = os.system("pkg-config sqlite3 --modversion --silence-errors > /dev/null ")
|
||||
|
||||
# if sqlite_error:
|
||||
# print("sqlite3 not found!")
|
||||
# return False
|
||||
|
||||
# print("sqlite3 found!")
|
||||
|
||||
# return True
|
||||
|
||||
# #todo
|
||||
# return False
|
||||
|
||||
print("sqlite3 built in!")
|
||||
|
||||
return True
|
||||
|
||||
def get_opts():
|
||||
from SCons.Variables import BoolVariable, EnumVariable
|
||||
|
||||
return [
|
||||
# EnumVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", "yes", ("yes", "no")),
|
||||
]
|
||||
|
||||
|
||||
def get_flags():
|
||||
|
||||
return []
|
||||
|
||||
|
||||
def configure(env):
|
||||
#env.ParseConfig("pkg-config sqlite3 --cflags --libs")
|
||||
|
||||
env.Append(CPPDEFINES=["SQLITE_PRESENT"])
|
||||
|
||||
# Link those statically for portability
|
||||
#if env["use_static_cpp"]:
|
||||
#env.Append(LINKFLAGS=["-static-libgcc", "-static-libstdc++"])
|
||||
|
||||
env.Append(LINKFLAGS=["-ldl"])
|
||||
|
||||
|
2635
modules/database/database_backends/sqlite/sqlite/shell.c
Normal file
2635
modules/database/database_backends/sqlite/sqlite/shell.c
Normal file
File diff suppressed because it is too large
Load Diff
118686
modules/database/database_backends/sqlite/sqlite/sqlite3.c
Normal file
118686
modules/database/database_backends/sqlite/sqlite/sqlite3.c
Normal file
File diff suppressed because it is too large
Load Diff
186
modules/database/database_backends/sqlite/sqlite/sqlite3.def
Normal file
186
modules/database/database_backends/sqlite/sqlite/sqlite3.def
Normal file
@ -0,0 +1,186 @@
|
||||
EXPORTS
|
||||
sqlite3_aggregate_context
|
||||
sqlite3_aggregate_count
|
||||
sqlite3_auto_extension
|
||||
sqlite3_backup_finish
|
||||
sqlite3_backup_init
|
||||
sqlite3_backup_pagecount
|
||||
sqlite3_backup_remaining
|
||||
sqlite3_backup_step
|
||||
sqlite3_bind_blob
|
||||
sqlite3_bind_double
|
||||
sqlite3_bind_int
|
||||
sqlite3_bind_int64
|
||||
sqlite3_bind_null
|
||||
sqlite3_bind_parameter_count
|
||||
sqlite3_bind_parameter_index
|
||||
sqlite3_bind_parameter_name
|
||||
sqlite3_bind_text
|
||||
sqlite3_bind_text16
|
||||
sqlite3_bind_value
|
||||
sqlite3_bind_zeroblob
|
||||
sqlite3_blob_bytes
|
||||
sqlite3_blob_close
|
||||
sqlite3_blob_open
|
||||
sqlite3_blob_read
|
||||
sqlite3_blob_write
|
||||
sqlite3_busy_handler
|
||||
sqlite3_busy_timeout
|
||||
sqlite3_changes
|
||||
sqlite3_clear_bindings
|
||||
sqlite3_close
|
||||
sqlite3_collation_needed
|
||||
sqlite3_collation_needed16
|
||||
sqlite3_column_blob
|
||||
sqlite3_column_bytes
|
||||
sqlite3_column_bytes16
|
||||
sqlite3_column_count
|
||||
sqlite3_column_database_name
|
||||
sqlite3_column_database_name16
|
||||
sqlite3_column_decltype
|
||||
sqlite3_column_decltype16
|
||||
sqlite3_column_double
|
||||
sqlite3_column_int
|
||||
sqlite3_column_int64
|
||||
sqlite3_column_name
|
||||
sqlite3_column_name16
|
||||
sqlite3_column_origin_name
|
||||
sqlite3_column_origin_name16
|
||||
sqlite3_column_table_name
|
||||
sqlite3_column_table_name16
|
||||
sqlite3_column_text
|
||||
sqlite3_column_text16
|
||||
sqlite3_column_type
|
||||
sqlite3_column_value
|
||||
sqlite3_commit_hook
|
||||
sqlite3_compileoption_get
|
||||
sqlite3_compileoption_used
|
||||
sqlite3_complete
|
||||
sqlite3_complete16
|
||||
sqlite3_config
|
||||
sqlite3_context_db_handle
|
||||
sqlite3_create_collation
|
||||
sqlite3_create_collation16
|
||||
sqlite3_create_collation_v2
|
||||
sqlite3_create_function
|
||||
sqlite3_create_function16
|
||||
sqlite3_create_module
|
||||
sqlite3_create_module_v2
|
||||
sqlite3_data_count
|
||||
sqlite3_db_config
|
||||
sqlite3_db_handle
|
||||
sqlite3_db_mutex
|
||||
sqlite3_db_status
|
||||
sqlite3_declare_vtab
|
||||
sqlite3_enable_load_extension
|
||||
sqlite3_enable_shared_cache
|
||||
sqlite3_errcode
|
||||
sqlite3_errmsg
|
||||
sqlite3_errmsg16
|
||||
sqlite3_exec
|
||||
sqlite3_expired
|
||||
sqlite3_extended_errcode
|
||||
sqlite3_extended_result_codes
|
||||
sqlite3_file_control
|
||||
sqlite3_finalize
|
||||
sqlite3_free
|
||||
sqlite3_free_table
|
||||
sqlite3_get_autocommit
|
||||
sqlite3_get_auxdata
|
||||
sqlite3_get_table
|
||||
sqlite3_global_recover
|
||||
sqlite3_initialize
|
||||
sqlite3_interrupt
|
||||
sqlite3_last_insert_rowid
|
||||
sqlite3_libversion
|
||||
sqlite3_libversion_number
|
||||
sqlite3_limit
|
||||
sqlite3_load_extension
|
||||
sqlite3_log
|
||||
sqlite3_malloc
|
||||
sqlite3_memory_alarm
|
||||
sqlite3_memory_highwater
|
||||
sqlite3_memory_used
|
||||
sqlite3_mprintf
|
||||
sqlite3_mutex_alloc
|
||||
sqlite3_mutex_enter
|
||||
sqlite3_mutex_free
|
||||
sqlite3_mutex_leave
|
||||
sqlite3_mutex_try
|
||||
sqlite3_next_stmt
|
||||
sqlite3_open
|
||||
sqlite3_open16
|
||||
sqlite3_open_v2
|
||||
sqlite3_os_end
|
||||
sqlite3_os_init
|
||||
sqlite3_overload_function
|
||||
sqlite3_prepare
|
||||
sqlite3_prepare16
|
||||
sqlite3_prepare16_v2
|
||||
sqlite3_prepare_v2
|
||||
sqlite3_profile
|
||||
sqlite3_progress_handler
|
||||
sqlite3_randomness
|
||||
sqlite3_realloc
|
||||
sqlite3_release_memory
|
||||
sqlite3_reset
|
||||
sqlite3_reset_auto_extension
|
||||
sqlite3_result_blob
|
||||
sqlite3_result_double
|
||||
sqlite3_result_error
|
||||
sqlite3_result_error16
|
||||
sqlite3_result_error_code
|
||||
sqlite3_result_error_nomem
|
||||
sqlite3_result_error_toobig
|
||||
sqlite3_result_int
|
||||
sqlite3_result_int64
|
||||
sqlite3_result_null
|
||||
sqlite3_result_text
|
||||
sqlite3_result_text16
|
||||
sqlite3_result_text16be
|
||||
sqlite3_result_text16le
|
||||
sqlite3_result_value
|
||||
sqlite3_result_zeroblob
|
||||
sqlite3_rollback_hook
|
||||
sqlite3_set_authorizer
|
||||
sqlite3_set_auxdata
|
||||
sqlite3_shutdown
|
||||
sqlite3_sleep
|
||||
sqlite3_snprintf
|
||||
sqlite3_soft_heap_limit
|
||||
sqlite3_sourceid
|
||||
sqlite3_sql
|
||||
sqlite3_status
|
||||
sqlite3_step
|
||||
sqlite3_stmt_status
|
||||
sqlite3_strnicmp
|
||||
sqlite3_table_column_metadata
|
||||
sqlite3_test_control
|
||||
sqlite3_thread_cleanup
|
||||
sqlite3_threadsafe
|
||||
sqlite3_total_changes
|
||||
sqlite3_trace
|
||||
sqlite3_transfer_bindings
|
||||
sqlite3_update_hook
|
||||
sqlite3_user_data
|
||||
sqlite3_value_blob
|
||||
sqlite3_value_bytes
|
||||
sqlite3_value_bytes16
|
||||
sqlite3_value_double
|
||||
sqlite3_value_int
|
||||
sqlite3_value_int64
|
||||
sqlite3_value_numeric_type
|
||||
sqlite3_value_text
|
||||
sqlite3_value_text16
|
||||
sqlite3_value_text16be
|
||||
sqlite3_value_text16le
|
||||
sqlite3_value_type
|
||||
sqlite3_version
|
||||
sqlite3_vfs_find
|
||||
sqlite3_vfs_register
|
||||
sqlite3_vfs_unregister
|
||||
sqlite3_vmprintf
|
||||
sqlite3_wal_autocheckpoint
|
||||
sqlite3_wal_checkpoint
|
||||
sqlite3_wal_hook
|
||||
sqlite3_win32_mbcs_to_utf8
|
5915
modules/database/database_backends/sqlite/sqlite/sqlite3.h
Normal file
5915
modules/database/database_backends/sqlite/sqlite/sqlite3.h
Normal file
File diff suppressed because it is too large
Load Diff
378
modules/database/database_backends/sqlite/sqlite/sqlite3ext.h
Normal file
378
modules/database/database_backends/sqlite/sqlite/sqlite3ext.h
Normal file
@ -0,0 +1,378 @@
|
||||
/*
|
||||
** 2006 June 7
|
||||
**
|
||||
** The author disclaims copyright to this source code. In place of
|
||||
** a legal notice, here is a blessing:
|
||||
**
|
||||
** May you do good and not evil.
|
||||
** May you find forgiveness for yourself and forgive others.
|
||||
** May you share freely, never taking more than you give.
|
||||
**
|
||||
*************************************************************************
|
||||
** This header file defines the SQLite interface for use by
|
||||
** shared libraries that want to be imported as extensions into
|
||||
** an SQLite instance. Shared libraries that intend to be loaded
|
||||
** as extensions by SQLite should #include this file instead of
|
||||
** sqlite3.h.
|
||||
*/
|
||||
#ifndef _SQLITE3EXT_H_
|
||||
#define _SQLITE3EXT_H_
|
||||
#include "sqlite3.h"
|
||||
|
||||
typedef struct sqlite3_api_routines sqlite3_api_routines;
|
||||
|
||||
/*
|
||||
** The following structure holds pointers to all of the SQLite API
|
||||
** routines.
|
||||
**
|
||||
** WARNING: In order to maintain backwards compatibility, add new
|
||||
** interfaces to the end of this structure only. If you insert new
|
||||
** interfaces in the middle of this structure, then older different
|
||||
** versions of SQLite will not be able to load each others' shared
|
||||
** libraries!
|
||||
*/
|
||||
struct sqlite3_api_routines {
|
||||
void * (*aggregate_context)(sqlite3_context*,int nBytes);
|
||||
int (*aggregate_count)(sqlite3_context*);
|
||||
int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
|
||||
int (*bind_double)(sqlite3_stmt*,int,double);
|
||||
int (*bind_int)(sqlite3_stmt*,int,int);
|
||||
int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
|
||||
int (*bind_null)(sqlite3_stmt*,int);
|
||||
int (*bind_parameter_count)(sqlite3_stmt*);
|
||||
int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
|
||||
const char * (*bind_parameter_name)(sqlite3_stmt*,int);
|
||||
int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
|
||||
int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
|
||||
int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
|
||||
int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
|
||||
int (*busy_timeout)(sqlite3*,int ms);
|
||||
int (*changes)(sqlite3*);
|
||||
int (*close)(sqlite3*);
|
||||
int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const char*));
|
||||
int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const void*));
|
||||
const void * (*column_blob)(sqlite3_stmt*,int iCol);
|
||||
int (*column_bytes)(sqlite3_stmt*,int iCol);
|
||||
int (*column_bytes16)(sqlite3_stmt*,int iCol);
|
||||
int (*column_count)(sqlite3_stmt*pStmt);
|
||||
const char * (*column_database_name)(sqlite3_stmt*,int);
|
||||
const void * (*column_database_name16)(sqlite3_stmt*,int);
|
||||
const char * (*column_decltype)(sqlite3_stmt*,int i);
|
||||
const void * (*column_decltype16)(sqlite3_stmt*,int);
|
||||
double (*column_double)(sqlite3_stmt*,int iCol);
|
||||
int (*column_int)(sqlite3_stmt*,int iCol);
|
||||
sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
|
||||
const char * (*column_name)(sqlite3_stmt*,int);
|
||||
const void * (*column_name16)(sqlite3_stmt*,int);
|
||||
const char * (*column_origin_name)(sqlite3_stmt*,int);
|
||||
const void * (*column_origin_name16)(sqlite3_stmt*,int);
|
||||
const char * (*column_table_name)(sqlite3_stmt*,int);
|
||||
const void * (*column_table_name16)(sqlite3_stmt*,int);
|
||||
const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
|
||||
const void * (*column_text16)(sqlite3_stmt*,int iCol);
|
||||
int (*column_type)(sqlite3_stmt*,int iCol);
|
||||
sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
|
||||
void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
|
||||
int (*complete)(const char*sql);
|
||||
int (*complete16)(const void*sql);
|
||||
int (*create_collation)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*));
|
||||
int (*create_collation16)(sqlite3*,const void*,int,void*,int(*)(void*,int,const void*,int,const void*));
|
||||
int (*create_function)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*));
|
||||
int (*create_function16)(sqlite3*,const void*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*));
|
||||
int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
|
||||
int (*data_count)(sqlite3_stmt*pStmt);
|
||||
sqlite3 * (*db_handle)(sqlite3_stmt*);
|
||||
int (*declare_vtab)(sqlite3*,const char*);
|
||||
int (*enable_shared_cache)(int);
|
||||
int (*errcode)(sqlite3*db);
|
||||
const char * (*errmsg)(sqlite3*);
|
||||
const void * (*errmsg16)(sqlite3*);
|
||||
int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
|
||||
int (*expired)(sqlite3_stmt*);
|
||||
int (*finalize)(sqlite3_stmt*pStmt);
|
||||
void (*free)(void*);
|
||||
void (*free_table)(char**result);
|
||||
int (*get_autocommit)(sqlite3*);
|
||||
void * (*get_auxdata)(sqlite3_context*,int);
|
||||
int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
|
||||
int (*global_recover)(void);
|
||||
void (*interruptx)(sqlite3*);
|
||||
sqlite_int64 (*last_insert_rowid)(sqlite3*);
|
||||
const char * (*libversion)(void);
|
||||
int (*libversion_number)(void);
|
||||
void *(*malloc)(int);
|
||||
char * (*mprintf)(const char*,...);
|
||||
int (*open)(const char*,sqlite3**);
|
||||
int (*open16)(const void*,sqlite3**);
|
||||
int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
|
||||
int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
|
||||
void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
|
||||
void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
|
||||
void *(*realloc)(void*,int);
|
||||
int (*reset)(sqlite3_stmt*pStmt);
|
||||
void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
|
||||
void (*result_double)(sqlite3_context*,double);
|
||||
void (*result_error)(sqlite3_context*,const char*,int);
|
||||
void (*result_error16)(sqlite3_context*,const void*,int);
|
||||
void (*result_int)(sqlite3_context*,int);
|
||||
void (*result_int64)(sqlite3_context*,sqlite_int64);
|
||||
void (*result_null)(sqlite3_context*);
|
||||
void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
|
||||
void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
|
||||
void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
|
||||
void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
|
||||
void (*result_value)(sqlite3_context*,sqlite3_value*);
|
||||
void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
|
||||
int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,const char*,const char*),void*);
|
||||
void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
|
||||
char * (*snprintf)(int,char*,const char*,...);
|
||||
int (*step)(sqlite3_stmt*);
|
||||
int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,char const**,char const**,int*,int*,int*);
|
||||
void (*thread_cleanup)(void);
|
||||
int (*total_changes)(sqlite3*);
|
||||
void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
|
||||
int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
|
||||
void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,sqlite_int64),void*);
|
||||
void * (*user_data)(sqlite3_context*);
|
||||
const void * (*value_blob)(sqlite3_value*);
|
||||
int (*value_bytes)(sqlite3_value*);
|
||||
int (*value_bytes16)(sqlite3_value*);
|
||||
double (*value_double)(sqlite3_value*);
|
||||
int (*value_int)(sqlite3_value*);
|
||||
sqlite_int64 (*value_int64)(sqlite3_value*);
|
||||
int (*value_numeric_type)(sqlite3_value*);
|
||||
const unsigned char * (*value_text)(sqlite3_value*);
|
||||
const void * (*value_text16)(sqlite3_value*);
|
||||
const void * (*value_text16be)(sqlite3_value*);
|
||||
const void * (*value_text16le)(sqlite3_value*);
|
||||
int (*value_type)(sqlite3_value*);
|
||||
char *(*vmprintf)(const char*,va_list);
|
||||
/* Added ??? */
|
||||
int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
|
||||
/* Added by 3.3.13 */
|
||||
int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
|
||||
int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
|
||||
int (*clear_bindings)(sqlite3_stmt*);
|
||||
/* Added by 3.4.1 */
|
||||
int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,void (*xDestroy)(void *));
|
||||
/* Added by 3.5.0 */
|
||||
int (*bind_zeroblob)(sqlite3_stmt*,int,int);
|
||||
int (*blob_bytes)(sqlite3_blob*);
|
||||
int (*blob_close)(sqlite3_blob*);
|
||||
int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,int,sqlite3_blob**);
|
||||
int (*blob_read)(sqlite3_blob*,void*,int,int);
|
||||
int (*blob_write)(sqlite3_blob*,const void*,int,int);
|
||||
int (*create_collation_v2)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*),void(*)(void*));
|
||||
int (*file_control)(sqlite3*,const char*,int,void*);
|
||||
sqlite3_int64 (*memory_highwater)(int);
|
||||
sqlite3_int64 (*memory_used)(void);
|
||||
sqlite3_mutex *(*mutex_alloc)(int);
|
||||
void (*mutex_enter)(sqlite3_mutex*);
|
||||
void (*mutex_free)(sqlite3_mutex*);
|
||||
void (*mutex_leave)(sqlite3_mutex*);
|
||||
int (*mutex_try)(sqlite3_mutex*);
|
||||
int (*open_v2)(const char*,sqlite3**,int,const char*);
|
||||
int (*release_memory)(int);
|
||||
void (*result_error_nomem)(sqlite3_context*);
|
||||
void (*result_error_toobig)(sqlite3_context*);
|
||||
int (*sleep)(int);
|
||||
void (*soft_heap_limit)(int);
|
||||
sqlite3_vfs *(*vfs_find)(const char*);
|
||||
int (*vfs_register)(sqlite3_vfs*,int);
|
||||
int (*vfs_unregister)(sqlite3_vfs*);
|
||||
int (*xthreadsafe)(void);
|
||||
void (*result_zeroblob)(sqlite3_context*,int);
|
||||
void (*result_error_code)(sqlite3_context*,int);
|
||||
int (*test_control)(int, ...);
|
||||
void (*randomness)(int,void*);
|
||||
sqlite3 *(*context_db_handle)(sqlite3_context*);
|
||||
int (*extended_result_codes)(sqlite3*,int);
|
||||
int (*limit)(sqlite3*,int,int);
|
||||
sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
|
||||
const char *(*sql)(sqlite3_stmt*);
|
||||
int (*status)(int,int*,int*,int);
|
||||
};
|
||||
|
||||
/*
|
||||
** The following macros redefine the API routines so that they are
|
||||
** redirected throught the global sqlite3_api structure.
|
||||
**
|
||||
** This header file is also used by the loadext.c source file
|
||||
** (part of the main SQLite library - not an extension) so that
|
||||
** it can get access to the sqlite3_api_routines structure
|
||||
** definition. But the main library does not want to redefine
|
||||
** the API. So the redefinition macros are only valid if the
|
||||
** SQLITE_CORE macros is undefined.
|
||||
*/
|
||||
#ifndef SQLITE_CORE
|
||||
#define sqlite3_aggregate_context sqlite3_api->aggregate_context
|
||||
#ifndef SQLITE_OMIT_DEPRECATED
|
||||
#define sqlite3_aggregate_count sqlite3_api->aggregate_count
|
||||
#endif
|
||||
#define sqlite3_bind_blob sqlite3_api->bind_blob
|
||||
#define sqlite3_bind_double sqlite3_api->bind_double
|
||||
#define sqlite3_bind_int sqlite3_api->bind_int
|
||||
#define sqlite3_bind_int64 sqlite3_api->bind_int64
|
||||
#define sqlite3_bind_null sqlite3_api->bind_null
|
||||
#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
|
||||
#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
|
||||
#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
|
||||
#define sqlite3_bind_text sqlite3_api->bind_text
|
||||
#define sqlite3_bind_text16 sqlite3_api->bind_text16
|
||||
#define sqlite3_bind_value sqlite3_api->bind_value
|
||||
#define sqlite3_busy_handler sqlite3_api->busy_handler
|
||||
#define sqlite3_busy_timeout sqlite3_api->busy_timeout
|
||||
#define sqlite3_changes sqlite3_api->changes
|
||||
#define sqlite3_close sqlite3_api->close
|
||||
#define sqlite3_collation_needed sqlite3_api->collation_needed
|
||||
#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
|
||||
#define sqlite3_column_blob sqlite3_api->column_blob
|
||||
#define sqlite3_column_bytes sqlite3_api->column_bytes
|
||||
#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
|
||||
#define sqlite3_column_count sqlite3_api->column_count
|
||||
#define sqlite3_column_database_name sqlite3_api->column_database_name
|
||||
#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
|
||||
#define sqlite3_column_decltype sqlite3_api->column_decltype
|
||||
#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
|
||||
#define sqlite3_column_double sqlite3_api->column_double
|
||||
#define sqlite3_column_int sqlite3_api->column_int
|
||||
#define sqlite3_column_int64 sqlite3_api->column_int64
|
||||
#define sqlite3_column_name sqlite3_api->column_name
|
||||
#define sqlite3_column_name16 sqlite3_api->column_name16
|
||||
#define sqlite3_column_origin_name sqlite3_api->column_origin_name
|
||||
#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
|
||||
#define sqlite3_column_table_name sqlite3_api->column_table_name
|
||||
#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
|
||||
#define sqlite3_column_text sqlite3_api->column_text
|
||||
#define sqlite3_column_text16 sqlite3_api->column_text16
|
||||
#define sqlite3_column_type sqlite3_api->column_type
|
||||
#define sqlite3_column_value sqlite3_api->column_value
|
||||
#define sqlite3_commit_hook sqlite3_api->commit_hook
|
||||
#define sqlite3_complete sqlite3_api->complete
|
||||
#define sqlite3_complete16 sqlite3_api->complete16
|
||||
#define sqlite3_create_collation sqlite3_api->create_collation
|
||||
#define sqlite3_create_collation16 sqlite3_api->create_collation16
|
||||
#define sqlite3_create_function sqlite3_api->create_function
|
||||
#define sqlite3_create_function16 sqlite3_api->create_function16
|
||||
#define sqlite3_create_module sqlite3_api->create_module
|
||||
#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
|
||||
#define sqlite3_data_count sqlite3_api->data_count
|
||||
#define sqlite3_db_handle sqlite3_api->db_handle
|
||||
#define sqlite3_declare_vtab sqlite3_api->declare_vtab
|
||||
#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
|
||||
#define sqlite3_errcode sqlite3_api->errcode
|
||||
#define sqlite3_errmsg sqlite3_api->errmsg
|
||||
#define sqlite3_errmsg16 sqlite3_api->errmsg16
|
||||
#define sqlite3_exec sqlite3_api->exec
|
||||
#ifndef SQLITE_OMIT_DEPRECATED
|
||||
#define sqlite3_expired sqlite3_api->expired
|
||||
#endif
|
||||
#define sqlite3_finalize sqlite3_api->finalize
|
||||
#define sqlite3_free sqlite3_api->free
|
||||
#define sqlite3_free_table sqlite3_api->free_table
|
||||
#define sqlite3_get_autocommit sqlite3_api->get_autocommit
|
||||
#define sqlite3_get_auxdata sqlite3_api->get_auxdata
|
||||
#define sqlite3_get_table sqlite3_api->get_table
|
||||
#ifndef SQLITE_OMIT_DEPRECATED
|
||||
#define sqlite3_global_recover sqlite3_api->global_recover
|
||||
#endif
|
||||
#define sqlite3_interrupt sqlite3_api->interruptx
|
||||
#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
|
||||
#define sqlite3_libversion sqlite3_api->libversion
|
||||
#define sqlite3_libversion_number sqlite3_api->libversion_number
|
||||
#define sqlite3_malloc sqlite3_api->malloc
|
||||
#define sqlite3_mprintf sqlite3_api->mprintf
|
||||
#define sqlite3_open sqlite3_api->open
|
||||
#define sqlite3_open16 sqlite3_api->open16
|
||||
#define sqlite3_prepare sqlite3_api->prepare
|
||||
#define sqlite3_prepare16 sqlite3_api->prepare16
|
||||
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
|
||||
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
|
||||
#define sqlite3_profile sqlite3_api->profile
|
||||
#define sqlite3_progress_handler sqlite3_api->progress_handler
|
||||
#define sqlite3_realloc sqlite3_api->realloc
|
||||
#define sqlite3_reset sqlite3_api->reset
|
||||
#define sqlite3_result_blob sqlite3_api->result_blob
|
||||
#define sqlite3_result_double sqlite3_api->result_double
|
||||
#define sqlite3_result_error sqlite3_api->result_error
|
||||
#define sqlite3_result_error16 sqlite3_api->result_error16
|
||||
#define sqlite3_result_int sqlite3_api->result_int
|
||||
#define sqlite3_result_int64 sqlite3_api->result_int64
|
||||
#define sqlite3_result_null sqlite3_api->result_null
|
||||
#define sqlite3_result_text sqlite3_api->result_text
|
||||
#define sqlite3_result_text16 sqlite3_api->result_text16
|
||||
#define sqlite3_result_text16be sqlite3_api->result_text16be
|
||||
#define sqlite3_result_text16le sqlite3_api->result_text16le
|
||||
#define sqlite3_result_value sqlite3_api->result_value
|
||||
#define sqlite3_rollback_hook sqlite3_api->rollback_hook
|
||||
#define sqlite3_set_authorizer sqlite3_api->set_authorizer
|
||||
#define sqlite3_set_auxdata sqlite3_api->set_auxdata
|
||||
#define sqlite3_snprintf sqlite3_api->snprintf
|
||||
#define sqlite3_step sqlite3_api->step
|
||||
#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
|
||||
#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
|
||||
#define sqlite3_total_changes sqlite3_api->total_changes
|
||||
#define sqlite3_trace sqlite3_api->trace
|
||||
#ifndef SQLITE_OMIT_DEPRECATED
|
||||
#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
|
||||
#endif
|
||||
#define sqlite3_update_hook sqlite3_api->update_hook
|
||||
#define sqlite3_user_data sqlite3_api->user_data
|
||||
#define sqlite3_value_blob sqlite3_api->value_blob
|
||||
#define sqlite3_value_bytes sqlite3_api->value_bytes
|
||||
#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
|
||||
#define sqlite3_value_double sqlite3_api->value_double
|
||||
#define sqlite3_value_int sqlite3_api->value_int
|
||||
#define sqlite3_value_int64 sqlite3_api->value_int64
|
||||
#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
|
||||
#define sqlite3_value_text sqlite3_api->value_text
|
||||
#define sqlite3_value_text16 sqlite3_api->value_text16
|
||||
#define sqlite3_value_text16be sqlite3_api->value_text16be
|
||||
#define sqlite3_value_text16le sqlite3_api->value_text16le
|
||||
#define sqlite3_value_type sqlite3_api->value_type
|
||||
#define sqlite3_vmprintf sqlite3_api->vmprintf
|
||||
#define sqlite3_overload_function sqlite3_api->overload_function
|
||||
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
|
||||
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
|
||||
#define sqlite3_clear_bindings sqlite3_api->clear_bindings
|
||||
#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
|
||||
#define sqlite3_blob_bytes sqlite3_api->blob_bytes
|
||||
#define sqlite3_blob_close sqlite3_api->blob_close
|
||||
#define sqlite3_blob_open sqlite3_api->blob_open
|
||||
#define sqlite3_blob_read sqlite3_api->blob_read
|
||||
#define sqlite3_blob_write sqlite3_api->blob_write
|
||||
#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
|
||||
#define sqlite3_file_control sqlite3_api->file_control
|
||||
#define sqlite3_memory_highwater sqlite3_api->memory_highwater
|
||||
#define sqlite3_memory_used sqlite3_api->memory_used
|
||||
#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
|
||||
#define sqlite3_mutex_enter sqlite3_api->mutex_enter
|
||||
#define sqlite3_mutex_free sqlite3_api->mutex_free
|
||||
#define sqlite3_mutex_leave sqlite3_api->mutex_leave
|
||||
#define sqlite3_mutex_try sqlite3_api->mutex_try
|
||||
#define sqlite3_open_v2 sqlite3_api->open_v2
|
||||
#define sqlite3_release_memory sqlite3_api->release_memory
|
||||
#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
|
||||
#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
|
||||
#define sqlite3_sleep sqlite3_api->sleep
|
||||
#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
|
||||
#define sqlite3_vfs_find sqlite3_api->vfs_find
|
||||
#define sqlite3_vfs_register sqlite3_api->vfs_register
|
||||
#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
|
||||
#define sqlite3_threadsafe sqlite3_api->xthreadsafe
|
||||
#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
|
||||
#define sqlite3_result_error_code sqlite3_api->result_error_code
|
||||
#define sqlite3_test_control sqlite3_api->test_control
|
||||
#define sqlite3_randomness sqlite3_api->randomness
|
||||
#define sqlite3_context_db_handle sqlite3_api->context_db_handle
|
||||
#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
|
||||
#define sqlite3_limit sqlite3_api->limit
|
||||
#define sqlite3_next_stmt sqlite3_api->next_stmt
|
||||
#define sqlite3_sql sqlite3_api->sql
|
||||
#define sqlite3_status sqlite3_api->status
|
||||
#endif /* SQLITE_CORE */
|
||||
|
||||
#define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api = 0;
|
||||
#define SQLITE_EXTENSION_INIT2(v) sqlite3_api = v;
|
||||
|
||||
#endif /* _SQLITE3EXT_H_ */
|
@ -0,0 +1,95 @@
|
||||
#include "sqlite3_database.h"
|
||||
|
||||
#include "database/database_manager.h"
|
||||
|
||||
#include "sqlite3_query_builder.h"
|
||||
#include "sqlite3_query_result.h"
|
||||
#include "sqlite3_table_builder.h"
|
||||
|
||||
Database *SQLite3Database::_creation_func() {
|
||||
return new SQLite3Database();
|
||||
}
|
||||
|
||||
void SQLite3Database::_register() {
|
||||
DatabaseManager::_register_db_creation_func("sqlite", SQLite3Database::_creation_func);
|
||||
}
|
||||
|
||||
void SQLite3Database::_unregister() {
|
||||
DatabaseManager::_unregister_db_creation_func("sqlite");
|
||||
}
|
||||
|
||||
Ref<QueryBuilder> SQLite3Database::get_query_builder() {
|
||||
SQLite3QueryBuilder *b = new SQLite3QueryBuilder();
|
||||
b->_db = this;
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
Ref<TableBuilder> SQLite3Database::get_table_builder() {
|
||||
SQLite3TableBuilder *b = new SQLite3TableBuilder();
|
||||
b->_db = this;
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
void SQLite3Database::connect(const String &connection_str) {
|
||||
int ret = sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
|
||||
if (ret != SQLITE_OK) {
|
||||
printf("SQLITE3 multithreading is not supported!\n");
|
||||
}
|
||||
|
||||
ret = sqlite3_open(connection_str.c_str(), &conn);
|
||||
}
|
||||
|
||||
Ref<QueryResult> SQLite3Database::query(const String &query) {
|
||||
Sqlite3QueryResult *res = new Sqlite3QueryResult();
|
||||
|
||||
res->query(query, conn);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void SQLite3Database::query_run(const String &query) {
|
||||
char *err_msg;
|
||||
|
||||
if (sqlite3_exec(conn, query.c_str(), NULL, NULL, &err_msg) != SQLITE_OK) {
|
||||
printf("SQLite3Database::query_run error: \nQuery: %s \n Error:\n %s\n", query.c_str(), err_msg);
|
||||
sqlite3_free(err_msg);
|
||||
}
|
||||
}
|
||||
|
||||
String SQLite3Database::escape(const String str) {
|
||||
char *ret;
|
||||
|
||||
ret = sqlite3_mprintf("%q", str.c_str());
|
||||
|
||||
if (ret) {
|
||||
String res(ret);
|
||||
|
||||
sqlite3_free(ret);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
void SQLite3Database::escape(const String str, String *to) {
|
||||
char *ret;
|
||||
|
||||
ret = sqlite3_mprintf("%q", str.c_str());
|
||||
|
||||
if (ret) {
|
||||
to->operator=(ret);
|
||||
|
||||
sqlite3_free(ret);
|
||||
}
|
||||
}
|
||||
|
||||
SQLite3Database::SQLite3Database() :
|
||||
Database() {
|
||||
}
|
||||
|
||||
SQLite3Database::~SQLite3Database() {
|
||||
if (conn)
|
||||
sqlite3_close(conn);
|
||||
}
|
40
modules/database/database_backends/sqlite/sqlite3_database.h
Normal file
40
modules/database/database_backends/sqlite/sqlite3_database.h
Normal file
@ -0,0 +1,40 @@
|
||||
#ifndef SQLITE3_CONNECTION
|
||||
#define SQLITE3_CONNECTION
|
||||
|
||||
#include "database/database.h"
|
||||
|
||||
//Brynet has it aswell, and because of using namespace it is defined here aswell
|
||||
//later this will be fixed better
|
||||
//#ifdef IS_NUM
|
||||
//#undef IS_NUM
|
||||
//#endif
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include "./sqlite/sqlite3.h"
|
||||
|
||||
class SQLite3Database : public Database {
|
||||
public:
|
||||
static Database *_creation_func();
|
||||
static void _register();
|
||||
static void _unregister();
|
||||
|
||||
Ref<QueryBuilder> get_query_builder();
|
||||
Ref<TableBuilder> get_table_builder();
|
||||
|
||||
void connect(const String &connection_str);
|
||||
Ref<QueryResult> query(const String &query);
|
||||
void query_run(const String &query);
|
||||
|
||||
String escape(const String str);
|
||||
void escape(const String str, String *to);
|
||||
|
||||
SQLite3Database();
|
||||
~SQLite3Database();
|
||||
|
||||
sqlite3 *conn;
|
||||
};
|
||||
|
||||
//#undef IS_NUM
|
||||
|
||||
#endif
|
@ -0,0 +1,368 @@
|
||||
#include "sqlite3_query_builder.h"
|
||||
|
||||
#include "sqlite3_database.h"
|
||||
#include "sqlite3_query_result.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::select() {
|
||||
query_result += "SELECT ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::update() {
|
||||
query_result += "UPDATE ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::del() {
|
||||
query_result += "DELETE FROM ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::where() {
|
||||
query_result += "WHERE ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::from() {
|
||||
query_result += "FROM ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::insert() {
|
||||
query_result += "INSERT INTO ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::values() {
|
||||
query_result += "VALUES(";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::cvalues() {
|
||||
query_result[query_result.size() - 2] = ' ';
|
||||
query_result += ") ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::next_value() {
|
||||
query_result[query_result.size() - 2] = ' ';
|
||||
query_result += "), (";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::begin_transaction() {
|
||||
query_result += "BEGIN TRANSACTION;";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::commit() {
|
||||
query_result += "COMMIT;";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::str() {
|
||||
query_result += "'";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::cstr() {
|
||||
query_result += "'";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::like() {
|
||||
query_result += "LIKE ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::nselect(const String ¶ms) {
|
||||
query_result += "SELECT " + params + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::nupdate(const String ¶ms) {
|
||||
query_result += "UPDATE " + params + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::ndel(const String ¶ms) {
|
||||
query_result += "DELETE FROM " + params + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::nwhere(const String ¶ms) {
|
||||
query_result += "WHERE " + params + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::nfrom(const String ¶ms) {
|
||||
query_result += "FROM " + params + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::insert(const String &table_name) {
|
||||
query_result += "INSERT INTO " + table_name + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::insert(const String &table_name, const String &columns) {
|
||||
query_result += "INSERT INTO " + table_name + "(" + columns + ") ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::nvalues(const String ¶ms_str) {
|
||||
query_result += "VALUES(" + params_str + ") ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::val() {
|
||||
//query_result += "DEFAULT, ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::nval(const String ¶m) {
|
||||
query_result += "'" + param + "', ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::val(const char *param) {
|
||||
query_result += "'" + String(param) + "', ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::val(const int param) {
|
||||
//todo add a better way
|
||||
std::stringstream ss;
|
||||
ss << param;
|
||||
|
||||
query_result += ss.str() + ", ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::val(const bool param) {
|
||||
if (param)
|
||||
query_result += "1, ";
|
||||
else
|
||||
query_result += "0, ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::valf(const float param) {
|
||||
//todo add a better way
|
||||
std::stringstream ss;
|
||||
ss << param;
|
||||
|
||||
query_result += ss.str() + ", ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::vald(const double param) {
|
||||
//todo add a better way
|
||||
std::stringstream ss;
|
||||
ss << param;
|
||||
|
||||
query_result += ss.str() + ", ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::nlike(const String &str) {
|
||||
query_result += "LIKE '" + str + "' ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::set() {
|
||||
query_result += "SET ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::cset() {
|
||||
query_result[query_result.size() - 2] = ' ';
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::nsetp(const String &col, const String ¶m) {
|
||||
query_result += col + "='" + param + "', ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::setp(const String &col, const char *param) {
|
||||
query_result += col + "='" + String(param) + "', ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::setp(const String &col, const int param) {
|
||||
//todo add a better way
|
||||
std::stringstream ss;
|
||||
ss << param;
|
||||
|
||||
query_result += col + "=" + ss.str() + ", ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::setp(const String &col, const bool param) {
|
||||
if (param)
|
||||
query_result += col + "=1, ";
|
||||
else
|
||||
query_result += col + "=0, ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::setpf(const String &col, const float param) {
|
||||
//todo add a better way
|
||||
std::stringstream ss;
|
||||
ss << param;
|
||||
|
||||
query_result += col + "=" + ss.str() + ", ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::setpd(const String &col, const double param) {
|
||||
//todo add a better way
|
||||
std::stringstream ss;
|
||||
ss << param;
|
||||
|
||||
query_result += col + "=" + ss.str() + ", ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::nwp(const String &col, const String ¶m) {
|
||||
query_result += col + "='" + param + "' ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::wp(const String &col, const char *param) {
|
||||
query_result += col + "='" + String(param) + "' ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::wp(const String &col, const int param) {
|
||||
//todo add a better way
|
||||
std::stringstream ss;
|
||||
ss << param;
|
||||
|
||||
query_result += col + "=" + ss.str() + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::wp(const String &col, const bool param) {
|
||||
if (param)
|
||||
query_result += col + "=1 ";
|
||||
else
|
||||
query_result += col + "=0 ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::limit(const int num) {
|
||||
//todo better way
|
||||
std::stringstream ss;
|
||||
ss << num;
|
||||
|
||||
query_result += "LIMIT " + ss.str() + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::offset(const int num) {
|
||||
//todo better way
|
||||
std::stringstream ss;
|
||||
ss << num;
|
||||
|
||||
query_result += "OFFSET " + ss.str() + " ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::land() {
|
||||
query_result += "AND ";
|
||||
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::lor() {
|
||||
query_result += "OR ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::wildcard() {
|
||||
query_result += "%";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
String SQLite3QueryBuilder::escape(const String ¶ms) {
|
||||
if (!_db) {
|
||||
printf("SQLite3QueryBuilder::escape !db!\n");
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
return _db->escape(params);
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::prepare() {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::set_param(const int index, const String &value) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::set_param(const int index, const int value) {
|
||||
return this;
|
||||
}
|
||||
QueryBuilder *SQLite3QueryBuilder::set_param(const int index, const float value) {
|
||||
return this;
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::end_command() {
|
||||
query_result += ";";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
Ref<QueryResult> SQLite3QueryBuilder::run() {
|
||||
if (!_db) {
|
||||
printf("SQLite3QueryBuilder::run !db!\n");
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return _db->query(query_result);
|
||||
}
|
||||
|
||||
void SQLite3QueryBuilder::run_query() {
|
||||
if (!_db) {
|
||||
printf("SQLite3QueryBuilder::run_query !db!\n");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
_db->query_run(query_result);
|
||||
}
|
||||
|
||||
QueryBuilder *SQLite3QueryBuilder::select_last_insert_id() {
|
||||
return this;
|
||||
}
|
||||
|
||||
SQLite3QueryBuilder::SQLite3QueryBuilder() {
|
||||
}
|
||||
SQLite3QueryBuilder::~SQLite3QueryBuilder() {
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
#ifndef SQLITE3_QUERY_BUILDER_H
|
||||
#define SQLITE3_QUERY_BUILDER_H
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "database/query_builder.h"
|
||||
|
||||
class SQLite3Database;
|
||||
|
||||
class SQLite3QueryBuilder : public QueryBuilder {
|
||||
RCPP_OBJECT(SQLite3QueryBuilder, QueryBuilder);
|
||||
|
||||
public:
|
||||
QueryBuilder *select();
|
||||
QueryBuilder *update();
|
||||
QueryBuilder *del();
|
||||
|
||||
QueryBuilder *where();
|
||||
QueryBuilder *from();
|
||||
QueryBuilder *insert();
|
||||
QueryBuilder *values();
|
||||
QueryBuilder *cvalues();
|
||||
QueryBuilder *next_value();
|
||||
|
||||
QueryBuilder *begin_transaction();
|
||||
QueryBuilder *commit();
|
||||
|
||||
QueryBuilder *str();
|
||||
QueryBuilder *cstr();
|
||||
|
||||
QueryBuilder *like();
|
||||
|
||||
QueryBuilder *nselect(const String ¶ms);
|
||||
QueryBuilder *nupdate(const String ¶ms);
|
||||
QueryBuilder *ndel(const String ¶ms);
|
||||
|
||||
QueryBuilder *nwhere(const String ¶ms);
|
||||
QueryBuilder *nfrom(const String ¶ms);
|
||||
QueryBuilder *insert(const String &table_name);
|
||||
QueryBuilder *insert(const String &table_name, const String &columns);
|
||||
QueryBuilder *nvalues(const String ¶ms_str);
|
||||
QueryBuilder *val();
|
||||
QueryBuilder *nval(const String ¶m);
|
||||
QueryBuilder *val(const char *param);
|
||||
QueryBuilder *val(const int param);
|
||||
QueryBuilder *val(const bool param);
|
||||
QueryBuilder *valf(const float param);
|
||||
QueryBuilder *vald(const double param);
|
||||
|
||||
QueryBuilder *nlike(const String &str);
|
||||
|
||||
QueryBuilder *set();
|
||||
QueryBuilder *cset();
|
||||
QueryBuilder *nsetp(const String &col, const String ¶m);
|
||||
QueryBuilder *setp(const String &col, const char *param);
|
||||
QueryBuilder *setp(const String &col, const int param);
|
||||
QueryBuilder *setp(const String &col, const bool param);
|
||||
QueryBuilder *setpf(const String &col, const float param);
|
||||
QueryBuilder *setpd(const String &col, const double param);
|
||||
|
||||
QueryBuilder *nwp(const String &col, const String ¶m);
|
||||
QueryBuilder *wp(const String &col, const char *param);
|
||||
QueryBuilder *wp(const String &col, const int param);
|
||||
QueryBuilder *wp(const String &col, const bool param);
|
||||
|
||||
QueryBuilder *limit(const int num);
|
||||
QueryBuilder *offset(const int num);
|
||||
|
||||
//l=logical (and, or are operators)
|
||||
QueryBuilder *land();
|
||||
QueryBuilder *lor();
|
||||
|
||||
QueryBuilder *wildcard();
|
||||
|
||||
QueryBuilder *select_last_insert_id();
|
||||
|
||||
String escape(const String ¶ms);
|
||||
|
||||
QueryBuilder *prepare();
|
||||
QueryBuilder *set_param(const int index, const String &value);
|
||||
QueryBuilder *set_param(const int index, const int value);
|
||||
QueryBuilder *set_param(const int index, const float value);
|
||||
|
||||
QueryBuilder *end_command();
|
||||
|
||||
Ref<QueryResult> run();
|
||||
void run_query();
|
||||
|
||||
SQLite3QueryBuilder();
|
||||
~SQLite3QueryBuilder();
|
||||
|
||||
SQLite3Database *_db;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,64 @@
|
||||
#include "sqlite3_query_result.h"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
bool Sqlite3QueryResult::next_row() {
|
||||
return ++current_row < rows.size();
|
||||
}
|
||||
|
||||
const char *Sqlite3QueryResult::get_cell(const int index) {
|
||||
return rows[current_row]->cells[index].data.c_str();
|
||||
}
|
||||
|
||||
bool Sqlite3QueryResult::is_cell_null(const int index) {
|
||||
return rows[current_row]->cells[index].null;
|
||||
}
|
||||
|
||||
int Sqlite3QueryResult::get_last_insert_rowid() {
|
||||
return sqlite3_last_insert_rowid(_connection);
|
||||
}
|
||||
|
||||
void Sqlite3QueryResult::query(const String &query, sqlite3 *conn) {
|
||||
_connection = conn;
|
||||
|
||||
if (sqlite3_exec(conn, query.c_str(), Sqlite3QueryResult::run_query_finished, this, &err_msg) != SQLITE_OK) {
|
||||
printf("SQLite3Database::query error: \nQuery: %s \n Error:\n %s\n", query.c_str(), err_msg);
|
||||
sqlite3_free(err_msg);
|
||||
}
|
||||
}
|
||||
|
||||
int Sqlite3QueryResult::run_query_finished(void *data, int argc, char **argv, char **col_names) {
|
||||
Sqlite3QueryResult *res = reinterpret_cast<Sqlite3QueryResult *>(data);
|
||||
|
||||
//res->col_names = col_names;
|
||||
|
||||
Sqlite3QueryResultRow *r = new Sqlite3QueryResultRow();
|
||||
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
Cell c;
|
||||
|
||||
if (argv[i]) {
|
||||
c.data = argv[i];
|
||||
} else {
|
||||
c.null = true;
|
||||
}
|
||||
|
||||
r->cells.push_back(c);
|
||||
}
|
||||
|
||||
res->rows.push_back(r);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Sqlite3QueryResult::Sqlite3QueryResult() :
|
||||
QueryResult() {
|
||||
err_msg = nullptr;
|
||||
current_row = -1;
|
||||
}
|
||||
|
||||
Sqlite3QueryResult::~Sqlite3QueryResult() {
|
||||
for (int i = 0; i < rows.size(); ++i) {
|
||||
delete rows[i];
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
#ifndef MYSQL_QUERY_RESULT_H
|
||||
#define MYSQL_QUERY_RESULT_H
|
||||
|
||||
#include "core/string.h"
|
||||
#include <vector>
|
||||
|
||||
#include "database/query_result.h"
|
||||
|
||||
#include "./sqlite/sqlite3.h"
|
||||
|
||||
class Sqlite3QueryResult : public QueryResult {
|
||||
RCPP_OBJECT(Sqlite3QueryResult, QueryResult);
|
||||
|
||||
public:
|
||||
bool next_row();
|
||||
const char* get_cell(const int index);
|
||||
|
||||
bool is_cell_null(const int index);
|
||||
|
||||
int get_last_insert_rowid();
|
||||
|
||||
void query(const String &query, sqlite3 *conn);
|
||||
|
||||
static int run_query_finished(void *data, int argc, char **argv, char **col_names);
|
||||
|
||||
Sqlite3QueryResult();
|
||||
~Sqlite3QueryResult();
|
||||
|
||||
char* err_msg;
|
||||
|
||||
public:
|
||||
struct Cell {
|
||||
bool null;
|
||||
String data;
|
||||
|
||||
Cell() {
|
||||
null = false;
|
||||
}
|
||||
};
|
||||
|
||||
struct Sqlite3QueryResultRow {
|
||||
std::vector<Cell> cells;
|
||||
};
|
||||
|
||||
char **col_names;
|
||||
std::vector<Sqlite3QueryResultRow *> rows;
|
||||
int current_row;
|
||||
|
||||
sqlite3 *_connection;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,203 @@
|
||||
#include "sqlite3_table_builder.h"
|
||||
|
||||
#include "database/query_result.h"
|
||||
|
||||
#include "sqlite3_database.h"
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::create_table(const String &name) {
|
||||
result += "CREATE TABLE " + name + " ( ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::integer(const String &name) {
|
||||
result += name + " INTEGER ";
|
||||
|
||||
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(4) ";
|
||||
|
||||
return this;
|
||||
}
|
||||
TableBuilder *SQLite3TableBuilder::tiny_integer(const String &name, const int length) {
|
||||
result += name + " INTEGER(";
|
||||
result += String::num(length);
|
||||
result += ") ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::small_integer(const String &name) {
|
||||
result += name + " INTEGER(6) ";
|
||||
|
||||
return this;
|
||||
}
|
||||
TableBuilder *SQLite3TableBuilder::small_integer(const String &name, const int length) {
|
||||
result += name + " INTEGER(";
|
||||
result += String::num(length);
|
||||
result += ") ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::real_float(const String &name) {
|
||||
result += name + " FLOAT ";
|
||||
|
||||
return this;
|
||||
}
|
||||
TableBuilder *SQLite3TableBuilder::real_float(const String &name, const int size, const int d) {
|
||||
result += name + " FLOAT ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::real_double(const String &name) {
|
||||
result += name + " DOUBLE ";
|
||||
|
||||
return this;
|
||||
}
|
||||
TableBuilder *SQLite3TableBuilder::real_double(const String &name, const int size, const int d) {
|
||||
result += name + " DOUBLE ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::date(const String &name) {
|
||||
result += name + " DATE ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::varchar(const String &name, const int length) {
|
||||
result += name + " VARCHAR(" + std::to_string(length) + ") ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::text(const String &name) {
|
||||
result += name + " TEXT ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::not_null() {
|
||||
//result += "NOT NULL ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::null() {
|
||||
result += "NULL ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::defval(const String &val) {
|
||||
result += "DEFAULT '";
|
||||
result += val;
|
||||
result += "'";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::auto_increment() {
|
||||
//result += "AUTO_INCREMENT ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::primary_key(const String &name) {
|
||||
result += "PRIMARY KEY (" + name + ") ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::primary_key() {
|
||||
result += "PRIMARY KEY ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::next_row() {
|
||||
result += ", ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::ccreate_table() {
|
||||
result += ");";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::drop_table() {
|
||||
result += "DROP TABLE ";
|
||||
|
||||
return this;
|
||||
}
|
||||
TableBuilder *SQLite3TableBuilder::drop_table_if_exists() {
|
||||
result += "DROP TABLE IF EXISTS ";
|
||||
|
||||
return this;
|
||||
}
|
||||
TableBuilder *SQLite3TableBuilder::drop_table(const String &name) {
|
||||
result += "DROP TABLE " + name + ";";
|
||||
|
||||
return this;
|
||||
}
|
||||
TableBuilder *SQLite3TableBuilder::drop_table_if_exists(const String &name) {
|
||||
result += "DROP TABLE IF EXISTS " + name + ";";
|
||||
|
||||
return this;
|
||||
}
|
||||
TableBuilder *SQLite3TableBuilder::cdrop_table() {
|
||||
result += ";";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
TableBuilder *SQLite3TableBuilder::foreign_key(const String &name) {
|
||||
result += "FOREIGN KEY (" + name + ") ";
|
||||
|
||||
return this;
|
||||
}
|
||||
TableBuilder *SQLite3TableBuilder::references(const String &table, const String &name) {
|
||||
result += "REFERENCES " + table + " (" + name + ") ";
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
Ref<QueryResult> SQLite3TableBuilder::run() {
|
||||
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);
|
||||
}
|
||||
|
||||
SQLite3TableBuilder::SQLite3TableBuilder() {
|
||||
}
|
||||
|
||||
SQLite3TableBuilder::~SQLite3TableBuilder() {
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
#ifndef SQLITE3_TABLE_BUILDER_H
|
||||
#define SQLITE3_TABLE_BUILDER_H
|
||||
|
||||
#include "core/string.h"
|
||||
|
||||
#include "database/table_builder.h"
|
||||
|
||||
class SQLite3Database;
|
||||
|
||||
class SQLite3TableBuilder : public TableBuilder {
|
||||
RCPP_OBJECT(SQLite3TableBuilder, TableBuilder);
|
||||
|
||||
public:
|
||||
TableBuilder *create_table(const String &name);
|
||||
|
||||
TableBuilder *integer(const String &name);
|
||||
TableBuilder *integer(const String &name, const int length);
|
||||
|
||||
TableBuilder *tiny_integer(const String &name);
|
||||
TableBuilder *tiny_integer(const String &name, const int length);
|
||||
|
||||
TableBuilder *small_integer(const String &name);
|
||||
TableBuilder *small_integer(const String &name, const int length);
|
||||
|
||||
TableBuilder *real_float(const String &name);
|
||||
TableBuilder *real_float(const String &name, const int size, const int d);
|
||||
|
||||
TableBuilder *real_double(const String &name);
|
||||
TableBuilder *real_double(const String &name, const int size, const int d);
|
||||
|
||||
TableBuilder *date(const String &name);
|
||||
|
||||
TableBuilder *varchar(const String &name, const int length);
|
||||
TableBuilder *text(const String &name);
|
||||
|
||||
TableBuilder *not_null();
|
||||
TableBuilder *null();
|
||||
TableBuilder *defval(const String &val);
|
||||
TableBuilder *auto_increment();
|
||||
TableBuilder *primary_key(const String &name);
|
||||
TableBuilder *primary_key();
|
||||
TableBuilder *next_row();
|
||||
TableBuilder *ccreate_table();
|
||||
|
||||
TableBuilder *drop_table();
|
||||
TableBuilder *drop_table_if_exists();
|
||||
TableBuilder *drop_table(const String &name);
|
||||
TableBuilder *drop_table_if_exists(const String &name);
|
||||
TableBuilder *cdrop_table();
|
||||
|
||||
TableBuilder *foreign_key(const String &name);
|
||||
TableBuilder *references(const String &table, const String &name);
|
||||
|
||||
virtual Ref<QueryResult> run();
|
||||
virtual void run_query();
|
||||
|
||||
SQLite3TableBuilder();
|
||||
virtual ~SQLite3TableBuilder();
|
||||
|
||||
SQLite3Database *_db;
|
||||
};
|
||||
|
||||
#endif
|
12
modules/database/db_settings/SCsub
Normal file
12
modules/database/db_settings/SCsub
Normal file
@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
Import("env_mod")
|
||||
Import("env")
|
||||
|
||||
env_mod.core_sources = []
|
||||
|
||||
env_mod.add_source_files(env_mod.core_sources, "*.cpp")
|
||||
|
||||
# Build it all as a library
|
||||
lib = env_mod.add_library("db_settings", env_mod.core_sources)
|
||||
env.Prepend(LIBS=[lib])
|
90
modules/database/db_settings/db_settings.cpp
Normal file
90
modules/database/db_settings/db_settings.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
#include "db_settings.h"
|
||||
|
||||
#include "database/database.h"
|
||||
#include "database/database_manager.h"
|
||||
#include "database/query_builder.h"
|
||||
#include "database/query_result.h"
|
||||
#include "database/table_builder.h"
|
||||
|
||||
void DBSettings::set_value(const String &key, const Variant &value) {
|
||||
const Variant &v = _data[key];
|
||||
|
||||
int id = 0;
|
||||
|
||||
if (!v.is_null()) {
|
||||
id = _key_map[key];
|
||||
}
|
||||
|
||||
_data[key] = value;
|
||||
|
||||
Ref<QueryBuilder> qb = DatabaseManager::get_singleton()->ddb->get_query_builder();
|
||||
|
||||
if (id == 0) {
|
||||
qb->insert(_table, "key,value")->values()->val(key)->val(value.to_string())->cvalues();
|
||||
qb->select_last_insert_id();
|
||||
Ref<QueryResult> res = qb->run();
|
||||
|
||||
id = res->get_last_insert_rowid();
|
||||
|
||||
_key_map[key] = id;
|
||||
} else {
|
||||
qb->update(_table)->set()->setp("key", key)->setp("value", value.to_string())->cset()->where()->wp("id", id);
|
||||
qb->run_query();
|
||||
}
|
||||
}
|
||||
|
||||
void DBSettings::load() {
|
||||
_data.clear();
|
||||
_key_map.clear();
|
||||
|
||||
Ref<QueryBuilder> qb = DatabaseManager::get_singleton()->ddb->get_query_builder();
|
||||
qb->select("id,key,value")->from(_table);
|
||||
|
||||
Ref<QueryResult> res = qb->run();
|
||||
|
||||
while (res->next_row()) {
|
||||
int id = res->get_cell_int(0);
|
||||
String key = res->get_cell_str(1);
|
||||
String value = res->get_cell_str(2);
|
||||
|
||||
_data[key] = Variant::parse_string(value);
|
||||
_key_map[key] = id;
|
||||
}
|
||||
}
|
||||
|
||||
void DBSettings::migrate() {
|
||||
Ref<TableBuilder> tb = DatabaseManager::get_singleton()->ddb->get_table_builder();
|
||||
tb->drop_table_if_exists(_table);
|
||||
|
||||
tb->create_table(_table);
|
||||
tb->integer("id")->auto_increment()->next_row();
|
||||
tb->varchar("key", 300)->not_null()->next_row();
|
||||
tb->text("value")->not_null()->next_row();
|
||||
tb->primary_key("id");
|
||||
tb->ccreate_table();
|
||||
|
||||
tb->run_query();
|
||||
}
|
||||
|
||||
void DBSettings::set_table(const String &table) {
|
||||
_table = table;
|
||||
}
|
||||
|
||||
DBSettings *DBSettings::get_singleton() {
|
||||
return _db_settings_singleton;
|
||||
}
|
||||
|
||||
DBSettings::DBSettings(const bool singleton) :
|
||||
Settings(singleton) {
|
||||
|
||||
_table = "settings";
|
||||
|
||||
if (singleton) {
|
||||
_db_settings_singleton = this;
|
||||
}
|
||||
}
|
||||
|
||||
DBSettings::~DBSettings() {
|
||||
}
|
||||
|
||||
DBSettings *DBSettings::_db_settings_singleton = nullptr;
|
32
modules/database/db_settings/db_settings.h
Normal file
32
modules/database/db_settings/db_settings.h
Normal file
@ -0,0 +1,32 @@
|
||||
#ifndef DB_SETTINGS_H
|
||||
#define DB_SETTINGS_H
|
||||
|
||||
#include "core/settings/settings.h"
|
||||
|
||||
class DBSettings : public Settings {
|
||||
RCPP_OBJECT(DBSettings, Settings);
|
||||
|
||||
public:
|
||||
virtual void set_value(const String &key, const Variant &value);
|
||||
|
||||
void load();
|
||||
|
||||
void migrate();
|
||||
|
||||
void set_table(const String &table);
|
||||
|
||||
static DBSettings *get_singleton();
|
||||
|
||||
DBSettings(const bool singleton = false);
|
||||
virtual ~DBSettings();
|
||||
|
||||
protected:
|
||||
String _table;
|
||||
|
||||
std::map<String, int> _key_map;
|
||||
|
||||
private:
|
||||
static DBSettings *_db_settings_singleton;
|
||||
};
|
||||
|
||||
#endif
|
26
modules/database/db_settings/detect.py
Normal file
26
modules/database/db_settings/detect.py
Normal file
@ -0,0 +1,26 @@
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
|
||||
|
||||
def is_active():
|
||||
return True
|
||||
|
||||
|
||||
def get_name():
|
||||
return "db_settings"
|
||||
|
||||
|
||||
def can_build():
|
||||
return True
|
||||
|
||||
|
||||
def get_opts():
|
||||
return []
|
||||
|
||||
def get_flags():
|
||||
|
||||
return []
|
||||
|
||||
def configure(env):
|
||||
pass
|
132
modules/database/settings/settings.cpp
Normal file
132
modules/database/settings/settings.cpp
Normal file
@ -0,0 +1,132 @@
|
||||
#include "settings.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Variant Settings::get_value(const String &key, const Variant &def) {
|
||||
std::map<String, Variant>::iterator e = _data.find(key);
|
||||
|
||||
if (e != _data.end()) {
|
||||
return e->second;
|
||||
} else {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
String Settings::get_value_string(const String &key, const String &def) {
|
||||
std::map<String, Variant>::iterator e = _data.find(key);
|
||||
|
||||
if (e != _data.end()) {
|
||||
return e->second;
|
||||
} else {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
int Settings::get_value_int(const String &key, const int def) {
|
||||
std::map<String, Variant>::iterator e = _data.find(key);
|
||||
|
||||
if (e != _data.end()) {
|
||||
return e->second.to_int();
|
||||
} else {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
float Settings::get_value_float(const String &key, const float def) {
|
||||
std::map<String, Variant>::iterator e = _data.find(key);
|
||||
|
||||
if (e != _data.end()) {
|
||||
return e->second.to_float();
|
||||
} else {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
double Settings::get_value_double(const String &key, const double def) {
|
||||
std::map<String, Variant>::iterator e = _data.find(key);
|
||||
|
||||
if (e != _data.end()) {
|
||||
return e->second.to_float();
|
||||
} else {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
bool Settings::get_value_bool(const String &key, const bool def) {
|
||||
std::map<String, Variant>::iterator e = _data.find(key);
|
||||
|
||||
if (e != _data.end()) {
|
||||
return e->second.to_bool();
|
||||
} else {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
void Settings::set_value(const String &key, const Variant &value) {
|
||||
_data[key] = value;
|
||||
}
|
||||
|
||||
void Settings::parse_ini_file(const String &path) {
|
||||
FILE *f = fopen(path.c_str(), "r");
|
||||
|
||||
if (!f) {
|
||||
printf("Settings::parse_file: Error opening file!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
long fsize = ftell(f);
|
||||
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
|
||||
|
||||
String config_str;
|
||||
config_str.resize(fsize);
|
||||
|
||||
fread(config_str.dataw(), 1, fsize, f);
|
||||
fclose(f);
|
||||
|
||||
config_str.replace('\r', ' ');
|
||||
Vector<String> ns = config_str.split('\n');
|
||||
|
||||
for (int i = 0; i < ns.size(); ++i) {
|
||||
String s = ns[i];
|
||||
|
||||
s.trim();
|
||||
|
||||
if (s.size() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (s[0] == '#') {
|
||||
continue;
|
||||
}
|
||||
|
||||
int eindex = s.find('=');
|
||||
|
||||
if (eindex == -1 || eindex == s.size() - 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String k = s.substr_index(0, eindex - 1);
|
||||
String v = s.substr_index(eindex + 1, s.size() - 1);
|
||||
|
||||
_data[k] = Variant::parse_string(v);
|
||||
}
|
||||
}
|
||||
|
||||
Settings *Settings::get_singleton() {
|
||||
return _singleton;
|
||||
}
|
||||
|
||||
Settings::Settings(const bool singleton) {
|
||||
if (singleton) {
|
||||
if (_singleton) {
|
||||
printf("Settings singleton overridden!\n");
|
||||
}
|
||||
|
||||
_singleton = this;
|
||||
}
|
||||
}
|
||||
|
||||
Settings::~Settings() {
|
||||
if (_singleton == this) {
|
||||
_singleton = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Settings *Settings::_singleton = nullptr;
|
38
modules/database/settings/settings.h
Normal file
38
modules/database/settings/settings.h
Normal file
@ -0,0 +1,38 @@
|
||||
#ifndef SETTINGS_H
|
||||
#define SETTINGS_H
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "core/object.h"
|
||||
|
||||
#include "core/string.h"
|
||||
#include "core/variant.h"
|
||||
|
||||
class Settings : public Object {
|
||||
RCPP_OBJECT(Settings, Object);
|
||||
|
||||
public:
|
||||
Variant get_value(const String &key, const Variant &def = Variant());
|
||||
String get_value_string(const String &key, const String &def = "");
|
||||
int get_value_int(const String &key, const int def = 0);
|
||||
float get_value_float(const String &key, const float def = 0);
|
||||
double get_value_double(const String &key, const double def = 0);
|
||||
bool get_value_bool(const String &key, const bool def = false);
|
||||
|
||||
virtual void set_value(const String &key, const Variant &value);
|
||||
|
||||
void parse_ini_file(const String &path);
|
||||
|
||||
static Settings *get_singleton();
|
||||
|
||||
Settings(const bool singleton = false);
|
||||
virtual ~Settings();
|
||||
|
||||
protected:
|
||||
std::map<String, Variant> _data;
|
||||
|
||||
private:
|
||||
static Settings *_singleton;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user