The database sub system now uses only my string.

This commit is contained in:
Relintai 2021-11-01 17:49:10 +01:00
parent faac12d3b1
commit 21558cb09a
21 changed files with 368 additions and 294 deletions

View File

@ -4,13 +4,13 @@
#include "table_builder.h"
#include "query_result.h"
void Database::connect(const std::string &connection_str) {
void Database::connect(const String &connection_str) {
}
Ref<QueryResult> Database::query(const std::string &query) {
Ref<QueryResult> Database::query(const String &query) {
return Ref<QueryResult>();
}
void Database::query_run(const std::string &query) {
void Database::query_run(const String &query) {
}
Ref<QueryBuilder> Database::get_query_builder() {
@ -21,11 +21,11 @@ Ref<TableBuilder> Database::get_table_builder() {
return Ref<TableBuilder>(new TableBuilder());
}
std::string Database::escape(const std::string str) {
return std::string();
String Database::escape(const String str) {
return String();
}
void Database::escape(const std::string str, std::string *to) {
void Database::escape(const String str, String *to) {
}

View File

@ -1,7 +1,7 @@
#ifndef DATABASE_H
#define DATABASE_H
#include <string>
#include "core/string.h"
#include <memory>
#include "core/reference.h"
@ -18,27 +18,27 @@ class QueryResult;
class Database {
public:
//probably needs refcount, depending on what db engines do, todo
//virtual QueryResult *query(const std::string &sql);
//virtual QueryResult *query_async(const std::string &sql);
//virtual QueryResult *query(const String &sql);
//virtual QueryResult *query_async(const String &sql);
//or
//virtual QueryErrorCode query(QueryResult *result, const std::string &sql);
//virtual QueryErrorCode query_async(QueryResult *result, const std::string &sql);
//virtual QueryErrorCode query(QueryResult *result, const String &sql);
//virtual QueryErrorCode query_async(QueryResult *result, const String &sql);
//also
//virtual QueryResult *query_prepared(const std::string &sql, param1, param2, ...);
//virtual QueryResult *query_prepared(const String &sql, param1, param2, ...);
//query interface (codeigniter 3 style)
//virtual void where(""); etc
virtual void connect(const std::string &connection_str);
virtual Ref<QueryResult> query(const std::string &query);
virtual void query_run(const std::string &query);
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 std::string escape(const std::string str);
virtual void escape(const std::string str, std::string *to);
virtual String escape(const String str);
virtual void escape(const String str, String *to);
Database();
~Database();

View File

@ -4,7 +4,7 @@ void DatabaseManager::load() {
//go thourgh settings, and create all the defined db backends
}
uint32_t DatabaseManager::create_database(const std::string &name) {
uint32_t DatabaseManager::create_database(const String &name) {
Database *db = _create_database(name);
if (!db) {
@ -28,7 +28,7 @@ DatabaseManager *DatabaseManager::get_singleton() {
return _instance;
}
void DatabaseManager::_register_db_creation_func(const std::string &name, std::function<Database *()> func) {
void DatabaseManager::_register_db_creation_func(const String &name, std::function<Database *()> func) {
if (!func) {
printf("_register_db_creation_func: %s, func is wrong!", name.c_str());
return;
@ -37,11 +37,11 @@ void DatabaseManager::_register_db_creation_func(const std::string &name, std::f
_db_creation_func_map[name] = func;
}
void DatabaseManager::_unregister_db_creation_func(const std::string &name) {
void DatabaseManager::_unregister_db_creation_func(const String &name) {
_db_creation_func_map.erase(name);
}
Database *DatabaseManager::_create_database(const std::string &name) {
Database *DatabaseManager::_create_database(const String &name) {
std::function<Database *()> func = _db_creation_func_map[name];
if (!func) {
@ -74,4 +74,4 @@ DatabaseManager::~DatabaseManager() {
DatabaseManager *DatabaseManager::_instance = nullptr;
std::map<std::string, std::function<Database *()> > DatabaseManager::_db_creation_func_map;
std::map<String, std::function<Database *()> > DatabaseManager::_db_creation_func_map;

View File

@ -1,6 +1,8 @@
#ifndef DATABASE_MANAGER_H
#define DATABASE_MANAGER_H
#include "core/string.h"
#include <vector>
#include <map>
#include <functional>
@ -17,12 +19,12 @@ public:
static DatabaseManager *get_singleton();
//note: not threadsafe, create these at the start of your program!
uint32_t create_database(const std::string &name);
uint32_t create_database(const String &name);
static void _register_db_creation_func(const std::string &name, std::function<Database*()> func);
static void _unregister_db_creation_func(const std::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 std::string &name);
static Database *_create_database(const String &name);
DatabaseManager();
~DatabaseManager();
@ -30,7 +32,7 @@ public:
private:
static DatabaseManager * _instance;
static std::map<std::string, std::function<Database *()> > _db_creation_func_map;
static std::map<String, std::function<Database *()> > _db_creation_func_map;
};
#endif

View File

@ -10,8 +10,8 @@ const char *QueryResult::get_cell(const int index) {
return "";
}
const std::string QueryResult::get_cell_str(const int index) {
return std::string(get_cell(index));
const String QueryResult::get_cell_str(const int index) {
return String(get_cell(index));
}
const bool QueryResult::get_cell_bool(const int index) {

View File

@ -1,7 +1,7 @@
#ifndef QUERY_RESULT_H
#define QUERY_RESULT_H
#include <string>
#include "core/string.h"
#include "core/reference.h"
@ -9,7 +9,7 @@ class QueryResult : public Reference {
public:
virtual bool next_row();
virtual const char *get_cell(const int index);
virtual const std::string get_cell_str(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);

View File

@ -2,19 +2,19 @@
#include "query_result.h"
TableBuilder *TableBuilder::create_table(const std::string &name) {
TableBuilder *TableBuilder::create_table(const String &name) {
return this;
}
TableBuilder *TableBuilder::integer(const std::string &name) {
TableBuilder *TableBuilder::integer(const String &name) {
return this;
}
TableBuilder *TableBuilder::date(const std::string &name) {
TableBuilder *TableBuilder::date(const String &name) {
return this;
}
TableBuilder *TableBuilder::varchar(const std::string &name, const int length) {
TableBuilder *TableBuilder::varchar(const String &name, const int length) {
return this;
}
@ -30,7 +30,7 @@ TableBuilder *TableBuilder::auto_increment() {
return this;
}
TableBuilder *TableBuilder::primary_key(const std::string &name) {
TableBuilder *TableBuilder::primary_key(const String &name) {
return this;
}
@ -52,20 +52,20 @@ TableBuilder *TableBuilder::drop_table() {
TableBuilder *TableBuilder::drop_table_if_exists() {
return this;
}
TableBuilder *TableBuilder::drop_table(const std::string &name) {
TableBuilder *TableBuilder::drop_table(const String &name) {
return this;
}
TableBuilder *TableBuilder::drop_table_if_exists(const std::string &name) {
TableBuilder *TableBuilder::drop_table_if_exists(const String &name) {
return this;
}
TableBuilder *TableBuilder::cdrop_table() {
return this;
}
TableBuilder *TableBuilder::foreign_key(const std::string &name) {
TableBuilder *TableBuilder::foreign_key(const String &name) {
return this;
}
TableBuilder *TableBuilder::references(const std::string &table, const std::string &name) {
TableBuilder *TableBuilder::references(const String &table, const String &name) {
return this;
}

View File

@ -1,7 +1,7 @@
#ifndef TABLE_BUILDER_H
#define TABLE_BUILDER_H
#include <string>
#include "core/string.h"
#include "core/reference.h"
@ -9,26 +9,26 @@ class QueryResult;
class TableBuilder : public Reference {
public:
virtual TableBuilder *create_table(const std::string &name);
virtual TableBuilder *integer(const std::string &name);
virtual TableBuilder *date(const std::string &name);
virtual TableBuilder *varchar(const std::string &name, const int length);
virtual TableBuilder *create_table(const String &name);
virtual TableBuilder *integer(const String &name);
virtual TableBuilder *date(const String &name);
virtual TableBuilder *varchar(const String &name, const int length);
virtual TableBuilder *not_null();
virtual TableBuilder *null();
virtual TableBuilder *auto_increment();
virtual TableBuilder *primary_key(const std::string &name);
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 std::string &name);
virtual TableBuilder *drop_table_if_exists(const std::string &name);
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 std::string &name);
virtual TableBuilder *references(const std::string &table, const std::string &name);
virtual TableBuilder *foreign_key(const String &name);
virtual TableBuilder *references(const String &table, const String &name);
virtual Ref<QueryResult> run();
virtual void run_query();
@ -38,7 +38,7 @@ public:
TableBuilder();
virtual ~TableBuilder();
std::string result;
String result;
};
#endif

View File

@ -2,8 +2,8 @@
#include "error_macros.h"
#include <stdlib.h>
#include <cstring>
#include <cstdio>
#include <cstring>
void String::push_back(const char element) {
ensure_capacity(_size + 1);
@ -76,11 +76,11 @@ void String::ensure_capacity(const int capacity) {
char *nd = new char[tsize];
if (_data) {
for (int i = 0; i < _size; ++i) {
nd[i] = _data[i];
}
for (int i = 0; i < _size; ++i) {
nd[i] = _data[i];
}
delete[] _data;
delete[] _data;
}
_data = nd;
@ -106,295 +106,295 @@ int String::find(const char val) const {
}
void String::get_substr(char *into_buf, const int start_index, const int len) {
ERR_FAIL_INDEX(start_index + len - 1, _size);
ERR_FAIL_INDEX(start_index + len - 1, _size);
int j = 0;
for (int i = start_index; i < start_index + len; ++i) {
into_buf[j++] = _data[i];
}
int j = 0;
for (int i = start_index; i < start_index + len; ++i) {
into_buf[j++] = _data[i];
}
}
void String::get_substr_nt(char *into_buf, const int start_index, const int len) {
ERR_FAIL_INDEX(start_index + len - 1, _size);
ERR_FAIL_INDEX(start_index + len - 1, _size);
int j = 0;
for (int i = start_index; i < start_index + len; ++i) {
into_buf[j++] = _data[i];
}
int j = 0;
for (int i = start_index; i < start_index + len; ++i) {
into_buf[j++] = _data[i];
}
into_buf[len + 1] = '\0';
into_buf[len + 1] = '\0';
}
uint8_t String::read_uint8_bytes_at(int &index, bool advance_index) {
ERR_FAIL_INDEX_V(index, _size, 0);
ERR_FAIL_INDEX_V(index, _size, 0);
if (advance_index) {
return static_cast<uint8_t>(_data[index++]);
} else {
return static_cast<uint8_t>(_data[index]);
}
if (advance_index) {
return static_cast<uint8_t>(_data[index++]);
} else {
return static_cast<uint8_t>(_data[index]);
}
}
uint16_t String::read_uint16_bytes_at(int &index, bool advance_index) {
ERR_FAIL_INDEX_V(index + 1, _size, 0);
ERR_FAIL_INDEX_V(index + 1, _size, 0);
char carr[3];
char* p = carr;
char carr[3];
char *p = carr;
//printf("%u %u\n", static_cast<uint8_t>(p[0]), static_cast<uint8_t>(p[1]));
//printf("%u %u\n", static_cast<uint8_t>(p[0]), static_cast<uint8_t>(p[1]));
get_substr_nt(p, index, 2);
get_substr_nt(p, index, 2);
const uint16_t *vp = static_cast<const uint16_t *>((void *)p);
const uint16_t *vp = static_cast<const uint16_t *>((void *)p);
if (advance_index) {
index += 2;
}
if (advance_index) {
index += 2;
}
return *vp;
return *vp;
}
uint32_t String::read_uint32_bytes_at(int &index, bool advance_index) {
ERR_FAIL_INDEX_V(index + 3, _size, 0);
ERR_FAIL_INDEX_V(index + 3, _size, 0);
char carr[5];
char* p = carr;
char carr[5];
char *p = carr;
get_substr_nt(p, index, 4);
get_substr_nt(p, index, 4);
const uint32_t *vp = static_cast<const uint32_t *>((void *)p);
const uint32_t *vp = static_cast<const uint32_t *>((void *)p);
if (advance_index) {
index += 4;
}
if (advance_index) {
index += 4;
}
return *vp;
return *vp;
}
uint64_t String::read_uint64_bytes_at(int &index, bool advance_index) {
ERR_FAIL_INDEX_V(index + 7, _size, 0);
ERR_FAIL_INDEX_V(index + 7, _size, 0);
char carr[9];
char* p = carr;
char carr[9];
char *p = carr;
get_substr_nt(p, index, 8);
get_substr_nt(p, index, 8);
const uint64_t *vp = static_cast<const uint64_t *>((void *)p);
const uint64_t *vp = static_cast<const uint64_t *>((void *)p);
if (advance_index) {
index += 8;
}
if (advance_index) {
index += 8;
}
return *vp;
return *vp;
}
int8_t String::read_int8_bytes_at(int &index, bool advance_index) {
ERR_FAIL_INDEX_V(index, _size, 0);
ERR_FAIL_INDEX_V(index, _size, 0);
if (advance_index) {
return static_cast<int8_t>(_data[index++]);
} else {
return static_cast<int8_t>(_data[index]);
}
if (advance_index) {
return static_cast<int8_t>(_data[index++]);
} else {
return static_cast<int8_t>(_data[index]);
}
}
int16_t String::read_int16_bytes_at(int &index, bool advance_index) {
ERR_FAIL_INDEX_V(index + 1, _size, 0);
ERR_FAIL_INDEX_V(index + 1, _size, 0);
char carr[3];
char* p = carr;
char carr[3];
char *p = carr;
//printf("%u %u\n", static_cast<uint8_t>(p[0]), static_cast<uint8_t>(p[1]));
//printf("%u %u\n", static_cast<uint8_t>(p[0]), static_cast<uint8_t>(p[1]));
get_substr_nt(p, index, 2);
get_substr_nt(p, index, 2);
const int16_t *vp = static_cast<const int16_t *>((void *)p);
const int16_t *vp = static_cast<const int16_t *>((void *)p);
if (advance_index) {
index += 2;
}
if (advance_index) {
index += 2;
}
return *vp;
return *vp;
}
int32_t String::read_int32_bytes_at(int &index, bool advance_index) {
ERR_FAIL_INDEX_V(index + 3, _size, 0);
ERR_FAIL_INDEX_V(index + 3, _size, 0);
char carr[5];
char* p = carr;
char carr[5];
char *p = carr;
get_substr_nt(p, index, 4);
get_substr_nt(p, index, 4);
const int32_t *vp = static_cast<const int32_t *>((void *)p);
const int32_t *vp = static_cast<const int32_t *>((void *)p);
if (advance_index) {
index += 4;
}
if (advance_index) {
index += 4;
}
return *vp;
return *vp;
}
int64_t String::read_int64_bytes_at(int &index, bool advance_index) {
ERR_FAIL_INDEX_V(index + 7, _size, 0);
ERR_FAIL_INDEX_V(index + 7, _size, 0);
char carr[9];
char* p = carr;
char carr[9];
char *p = carr;
get_substr_nt(p, index, 8);
get_substr_nt(p, index, 8);
const int64_t *vp = static_cast<const int64_t *>((void *)p);
const int64_t *vp = static_cast<const int64_t *>((void *)p);
if (advance_index) {
index += 8;
}
if (advance_index) {
index += 8;
}
return *vp;
return *vp;
}
void String::append_uint8_bytes(const uint8_t val) {
ensure_capacity(_size + 1);
ensure_capacity(_size + 1);
_data[_size++] = val;
_data[_size] = '\0';
_data[_size++] = val;
_data[_size] = '\0';
}
void String::append_uint16_bytes(const uint16_t val) {
ensure_capacity(_size + 2);
ensure_capacity(_size + 2);
const char *vp = static_cast<const char *>((void *)&val);
const char *vp = static_cast<const char *>((void *)&val);
//printf("a %u %u\n", static_cast<uint8_t>(vp[0]), static_cast<uint8_t>(vp[1]));
//printf("a %u %u\n", static_cast<uint8_t>(vp[0]), static_cast<uint8_t>(vp[1]));
memcpy(&_data[_size], vp, 2);
memcpy(&_data[_size], vp, 2);
_size += 2;
_size += 2;
_data[_size] = '\0';
_data[_size] = '\0';
}
void String::append_uint32_bytes(const uint32_t val) {
ensure_capacity(_size + 4);
ensure_capacity(_size + 4);
const char *vp = static_cast<const char *>((void *)&val);
const char *vp = static_cast<const char *>((void *)&val);
memcpy(&_data[_size], vp, 4);
memcpy(&_data[_size], vp, 4);
_size += 4;
_size += 4;
_data[_size] = '\0';
_data[_size] = '\0';
}
void String::append_uint64_bytes(const uint64_t val) {
ensure_capacity(_size + 8);
ensure_capacity(_size + 8);
const char *vp = static_cast<const char *>((void *)&val);
const char *vp = static_cast<const char *>((void *)&val);
memcpy(&_data[_size], vp, 8);
memcpy(&_data[_size], vp, 8);
_size += 8;
_size += 8;
_data[_size] = '\0';
_data[_size] = '\0';
}
void String::append_int8_bytes(const int8_t val) {
ensure_capacity(_size + 1);
ensure_capacity(_size + 1);
_data[_size++] = val;
_data[_size] = '\0';
_data[_size++] = val;
_data[_size] = '\0';
}
void String::append_int16_bytes(const int16_t val) {
ensure_capacity(_size + 2);
ensure_capacity(_size + 2);
const char *vp = static_cast<const char *>((void *)&val);
const char *vp = static_cast<const char *>((void *)&val);
//printf("a %u %u\n", static_cast<uint8_t>(vp[0]), static_cast<uint8_t>(vp[1]));
//printf("a %u %u\n", static_cast<uint8_t>(vp[0]), static_cast<uint8_t>(vp[1]));
memcpy(&_data[_size], vp, 2);
memcpy(&_data[_size], vp, 2);
_size += 2;
_size += 2;
_data[_size] = '\0';
_data[_size] = '\0';
}
void String::append_int32_bytes(const int32_t val) {
ensure_capacity(_size + 4);
ensure_capacity(_size + 4);
const char *vp = static_cast<const char *>((void *)&val);
const char *vp = static_cast<const char *>((void *)&val);
memcpy(&_data[_size], vp, 4);
memcpy(&_data[_size], vp, 4);
_size += 4;
_size += 4;
_data[_size] = '\0';
_data[_size] = '\0';
}
void String::append_int64_bytes(const int64_t val) {
ensure_capacity(_size + 8);
ensure_capacity(_size + 8);
const char *vp = static_cast<const char *>((void *)&val);
const char *vp = static_cast<const char *>((void *)&val);
memcpy(&_data[_size], vp, 8);
memcpy(&_data[_size], vp, 8);
_size += 8;
_size += 8;
_data[_size] = '\0';
_data[_size] = '\0';
}
float String::read_float_bytes_at(int &index, bool advance_index) {
ERR_FAIL_INDEX_V(index + 3, _size, 0);
ERR_FAIL_INDEX_V(index + 3, _size, 0);
char carr[5];
char* p = carr;
char carr[5];
char *p = carr;
get_substr_nt(p, index, 4);
get_substr_nt(p, index, 4);
const float *vp = static_cast<const float *>((void *)p);
const float *vp = static_cast<const float *>((void *)p);
if (advance_index) {
index += 4;
}
if (advance_index) {
index += 4;
}
return *vp;
return *vp;
}
void String::append_float_bytes(const float val) {
ensure_capacity(_size + 4);
ensure_capacity(_size + 4);
const char *vp = static_cast<const char *>((void *)&val);
const char *vp = static_cast<const char *>((void *)&val);
memcpy(&_data[_size], vp, 4);
memcpy(&_data[_size], vp, 4);
_size += 4;
_size += 4;
_data[_size] = '\0';
_data[_size] = '\0';
}
double String::read_double_bytes_at(int &index, bool advance_index) {
ERR_FAIL_INDEX_V(index + 7, _size, 0);
ERR_FAIL_INDEX_V(index + 7, _size, 0);
char carr[9];
char* p = carr;
char carr[9];
char *p = carr;
get_substr_nt(p, index, 8);
get_substr_nt(p, index, 8);
const double *vp = static_cast<const double *>((void *)p);
const double *vp = static_cast<const double *>((void *)p);
if (advance_index) {
index += 8;
}
if (advance_index) {
index += 8;
}
return *vp;
return *vp;
}
void String::append_double_bytes(const double val) {
ensure_capacity(_size + 8);
ensure_capacity(_size + 8);
const char *vp = static_cast<const char *>((void *)&val);
const char *vp = static_cast<const char *>((void *)&val);
memcpy(&_data[_size], vp, 8);
memcpy(&_data[_size], vp, 8);
_size += 8;
_size += 8;
_data[_size] = '\0';
_data[_size] = '\0';
}
void String::append_str(const char* str) {
if (str == nullptr) {
return;
}
void String::append_str(const char *str) {
if (str == nullptr) {
return;
}
int i = 0;
int i = 0;
while (str[i] != '\0') {
push_back(str[i++]);
}
while (str[i] != '\0') {
push_back(str[i++]);
}
}
void String::append_str(const String &other) {
@ -434,7 +434,7 @@ uint32_t String::to_uint() {
}
std::string String::to_string() const {
return std::string(c_str());
return std::string(c_str());
}
void String::print() const {
@ -494,9 +494,9 @@ String &String::operator+=(const char *p_c_str) {
}
String &String::operator+=(const std::string &b) {
append_str(b);
append_str(b);
return *this;
return *this;
}
String operator+(String lhs, const String &rhs) {
lhs.append_str(rhs);
@ -517,7 +517,7 @@ String operator+(String lhs, const char rhs) {
}
String operator+(String lhs, const std::string &rhs) {
lhs.append_str(rhs);
lhs.append_str(rhs);
return lhs;
}
@ -587,7 +587,7 @@ bool operator==(const String &a, std::string &b) {
return false;
}
char *bp = &b[0];
char *bp = &b[0];
for (int i = 0; i < a._size; ++i) {
if (a[i] != bp[i]) {
@ -598,17 +598,78 @@ bool operator==(const String &a, std::string &b) {
return true;
}
bool operator!=(const String &a, std::string &b) {
return !(a == b);
return !(a == b);
}
bool operator==(std::string &b, const String &a) {
return (a == b);
return (a == b);
}
bool operator!=(std::string &b, const String &a) {
return !(a == b);
return !(a == b);
}
String& String::operator=(const String &other) {
bool operator<(const String &a, const String &b) {
if (a.size() < b.size()) {
return true;
} else if (a.size() > b.size()) {
return false;
} else {
for (int i = 0; i < a._size; ++i) {
if (a[i] < b[i]) {
return true;
}
}
return false;
}
}
bool operator>(const String &a, const String &b) {
if (a.size() > b.size()) {
return true;
} else if (a.size() < b.size()) {
return false;
} else {
for (int i = 0; i < a._size; ++i) {
if (a[i] > b[i]) {
return true;
}
}
return false;
}
}
bool operator<=(const String &a, const String &b) {
if (a.size() < b.size()) {
return true;
} else if (a.size() > b.size()) {
return false;
} else {
for (int i = 0; i < a._size; ++i) {
if (a[i] <= b[i]) {
return true;
}
}
return false;
}
}
bool operator>=(const String &a, const String &b) {
if (a.size() > b.size()) {
return true;
} else if (a.size() < b.size()) {
return false;
} else {
for (int i = 0; i < a._size; ++i) {
if (a[i] >= b[i]) {
return true;
}
}
return false;
}
}
String &String::operator=(const String &other) {
clear();
append_str(other);
@ -616,7 +677,7 @@ String& String::operator=(const String &other) {
return *this;
}
String& String::operator=(const std::string &other) {
String &String::operator=(const std::string &other) {
clear();
append_str(other);
@ -624,7 +685,7 @@ String& String::operator=(const std::string &other) {
return *this;
}
String& String::operator=(const char* other) {
String &String::operator=(const char *other) {
clear();
append_str(other);
@ -677,7 +738,7 @@ String::String(const String &other, int grow_by) {
_data[_size] = '\0';
}
String::String(const char* p_c_str) {
String::String(const char *p_c_str) {
_data = nullptr;
_actual_size = 0;
_size = 0;
@ -686,7 +747,7 @@ String::String(const char* p_c_str) {
append_str(p_c_str);
}
String::String(const char* p_c_str, const int grow_by) {
String::String(const char *p_c_str, const int grow_by) {
_data = nullptr;
_actual_size = 0;
_size = 0;
@ -723,12 +784,12 @@ String::String(const std::string &str) {
_size = 0;
_grow_by = 100;
append_str(str);
append_str(str);
}
String::~String() {
if (_data) {
delete[] _data;
delete[] _data;
_data = nullptr;
}
}
}

View File

@ -95,6 +95,11 @@ public:
friend bool operator==(std::string &b, const String &a);
friend bool operator!=(std::string &b, const String &a);
friend bool operator<(const String &a, const String &b);
friend bool operator>(const String &a, const String &b);
friend bool operator<=(const String &a, const String &b);
friend bool operator>=(const String &a, const String &b);
operator std::string() { return to_string(); }
operator std::string() const { return to_string(); }

View File

@ -8,14 +8,14 @@
#include "mysql_query_result.h"
#include "mysql_table_builder.h"
void MysqlDatabase::connect(const std::string &connection_str) {
void MysqlDatabase::connect(const String &connection_str) {
mysql = mysql_init(mysql);
mysql_options(mysql, MYSQL_OPT_NONBLOCK, 0);
std::string host = "127.0.0.1";
std::string user = "";
std::string password = "";
std::string dbname = "testappdb";
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);
@ -25,13 +25,13 @@ void MysqlDatabase::connect(const std::string &connection_str) {
}
}
Ref<QueryResult> MysqlDatabase::query(const std::string &query) {
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.length());
int error = mysql_real_query(mysql, query.c_str(), query.capacity());
if (error) {
const char *merr = mysql_error(mysql);
@ -51,13 +51,13 @@ Ref<QueryResult> MysqlDatabase::query(const std::string &query) {
return Ref<QueryResult>(res);
}
void MysqlDatabase::query_run(const std::string &query) {
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.length());
int error = mysql_real_query(mysql, query.c_str(), query.capacity());
if (error) {
const char *merr = mysql_error(mysql);
@ -98,18 +98,22 @@ Ref<TableBuilder> MysqlDatabase::get_table_builder() {
return Ref<TableBuilder>(new MysqlTableBuilder());
}
std::string MysqlDatabase::escape(const std::string str) {
std::string res;
res.reserve(str.size() + 100);
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.data(), str.c_str(), str.size());
mysql_real_escape_string(mysql, res.dataw(), str.c_str(), str.size());
return res;
}
void MysqlDatabase::escape(const std::string str, std::string *to) {
to->reserve(str.size() + 100);
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->data(), str.c_str(), str.size());
mysql_real_escape_string(mysql, to->dataw(), str.c_str(), str.size());
}
MysqlDatabase::MysqlDatabase() :

View File

@ -1,6 +1,8 @@
#ifndef MYSQL_CONNECTION
#define MYSQL_CONNECTION
#include "core/string.h"
#include "core/database/database.h"
#include <memory>
@ -15,15 +17,15 @@
class MysqlDatabase : public Database {
public:
void connect(const std::string &connection_str);
Ref<QueryResult> query(const std::string &query);
void query_run(const std::string &query);
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();
std::string escape(const std::string str);
void escape(const std::string str, std::string *to);
String escape(const String str);
void escape(const String str, String *to);
static Database *_creation_func();
static void _register();

View File

@ -2,7 +2,7 @@
#define MYSQL_QUERY_BUILDER_H
#include <memory>
#include <string>
#include "core/string.h"
#include "core/database/query_builder.h"

View File

@ -1,24 +1,24 @@
#include "mysql_table_builder.h"
TableBuilder *MysqlTableBuilder::create_table(const std::string &name) {
TableBuilder *MysqlTableBuilder::create_table(const String &name) {
result += "CREATE TABLE " + name + " ( ";
return this;
}
TableBuilder *MysqlTableBuilder::integer(const std::string &name) {
TableBuilder *MysqlTableBuilder::integer(const String &name) {
result += name + " INTEGER ";
return this;
}
TableBuilder *MysqlTableBuilder::date(const std::string &name) {
TableBuilder *MysqlTableBuilder::date(const String &name) {
result += name + " DATE ";
return this;
}
TableBuilder *MysqlTableBuilder::varchar(const std::string &name, const int length) {
TableBuilder *MysqlTableBuilder::varchar(const String &name, const int length) {
result += name + " VARCHAR(" + std::to_string(length) + ")";
return this;
@ -42,7 +42,7 @@ TableBuilder *MysqlTableBuilder::auto_increment() {
return this;
}
TableBuilder *MysqlTableBuilder::primary_key(const std::string &name) {
TableBuilder *MysqlTableBuilder::primary_key(const String &name) {
result += "PRIMARY KEY (" + name + ") ";
return this;
@ -60,7 +60,7 @@ TableBuilder *MysqlTableBuilder::next_row() {
return this;
}
TableBuilder *MysqlTableBuilder::drop_table(const std::string &name) {
TableBuilder *MysqlTableBuilder::drop_table(const String &name) {
result += "DROP TABLE " + name + ";";
return this;

View File

@ -1,24 +1,24 @@
#ifndef MYSQL_TABLE_BUILDER_H
#define MYSQL_TABLE_BUILDER_H
#include <string>
#include "core/string.h"
#include "core/database/table_builder.h"
class MysqlTableBuilder : public TableBuilder {
public:
TableBuilder *create_table(const std::string &name);
TableBuilder *integer(const std::string &name);
TableBuilder *date(const std::string &name);
TableBuilder *varchar(const std::string &name, const int length);
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 std::string &name);
TableBuilder *primary_key(const String &name);
TableBuilder *primary_key();
TableBuilder *next_row();
TableBuilder *drop_table(const std::string &name);
TableBuilder *drop_table(const String &name);
MysqlTableBuilder();
virtual ~MysqlTableBuilder();

View File

@ -32,7 +32,7 @@ Ref<TableBuilder> SQLite3Database::get_table_builder() {
return b;
}
void SQLite3Database::connect(const std::string &connection_str) {
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");
@ -41,7 +41,7 @@ void SQLite3Database::connect(const std::string &connection_str) {
ret = sqlite3_open(connection_str.c_str(), &conn);
}
Ref<QueryResult> SQLite3Database::query(const std::string &query) {
Ref<QueryResult> SQLite3Database::query(const String &query) {
Sqlite3QueryResult *res = new Sqlite3QueryResult();
res->query(query, conn);
@ -49,7 +49,7 @@ Ref<QueryResult> SQLite3Database::query(const std::string &query) {
return res;
}
void SQLite3Database::query_run(const std::string &query) {
void SQLite3Database::query_run(const String &query) {
char *err_msg;
if (sqlite3_exec(conn, query.c_str(), NULL, NULL, &err_msg) != SQLITE_OK) {
@ -58,13 +58,13 @@ void SQLite3Database::query_run(const std::string &query) {
}
}
std::string SQLite3Database::escape(const std::string str) {
String SQLite3Database::escape(const String str) {
char *ret;
ret = sqlite3_mprintf("%q", str.c_str());
if (ret) {
std::string res(ret);
String res(ret);
sqlite3_free(ret);
@ -73,7 +73,7 @@ std::string SQLite3Database::escape(const std::string str) {
return "";
}
void SQLite3Database::escape(const std::string str, std::string *to) {
void SQLite3Database::escape(const String str, String *to) {
char *ret;
ret = sqlite3_mprintf("%q", str.c_str());

View File

@ -22,12 +22,12 @@ public:
Ref<QueryBuilder> get_query_builder();
Ref<TableBuilder> get_table_builder();
void connect(const std::string &connection_str);
Ref<QueryResult> query(const std::string &query);
void query_run(const std::string &query);
void connect(const String &connection_str);
Ref<QueryResult> query(const String &query);
void query_run(const String &query);
std::string escape(const std::string str);
void escape(const std::string str, std::string *to);
String escape(const String str);
void escape(const String str, String *to);
SQLite3Database();
~SQLite3Database();

View File

@ -18,7 +18,7 @@ int Sqlite3QueryResult::get_last_insert_rowid() {
return sqlite3_last_insert_rowid(_connection);
}
void Sqlite3QueryResult::query(const std::string &query, sqlite3 *conn) {
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) {

View File

@ -1,7 +1,7 @@
#ifndef MYSQL_QUERY_RESULT_H
#define MYSQL_QUERY_RESULT_H
#include <string>
#include "core/string.h"
#include <vector>
#include "core/database/query_result.h"
@ -17,7 +17,7 @@ public:
int get_last_insert_rowid();
void query(const std::string &query, sqlite3 *conn);
void query(const String &query, sqlite3 *conn);
static int run_query_finished(void *data, int argc, char **argv, char **col_names);
@ -29,7 +29,7 @@ public:
public:
struct Cell {
bool null;
std::string data;
String data;
Cell() {
null = false;

View File

@ -4,25 +4,25 @@
#include "sqlite3_database.h"
TableBuilder *SQLite3TableBuilder::create_table(const std::string &name) {
TableBuilder *SQLite3TableBuilder::create_table(const String &name) {
result += "CREATE TABLE " + name + " ( ";
return this;
}
TableBuilder *SQLite3TableBuilder::integer(const std::string &name) {
TableBuilder *SQLite3TableBuilder::integer(const String &name) {
result += name + " INTEGER ";
return this;
}
TableBuilder *SQLite3TableBuilder::date(const std::string &name) {
TableBuilder *SQLite3TableBuilder::date(const String &name) {
result += name + " DATE ";
return this;
}
TableBuilder *SQLite3TableBuilder::varchar(const std::string &name, const int length) {
TableBuilder *SQLite3TableBuilder::varchar(const String &name, const int length) {
result += name + " VARCHAR(" + std::to_string(length) + ") ";
return this;
@ -46,7 +46,7 @@ TableBuilder *SQLite3TableBuilder::auto_increment() {
return this;
}
TableBuilder *SQLite3TableBuilder::primary_key(const std::string &name) {
TableBuilder *SQLite3TableBuilder::primary_key(const String &name) {
result += "PRIMARY KEY (" + name + ") ";
return this;
@ -80,12 +80,12 @@ TableBuilder *SQLite3TableBuilder::drop_table_if_exists() {
return this;
}
TableBuilder *SQLite3TableBuilder::drop_table(const std::string &name) {
TableBuilder *SQLite3TableBuilder::drop_table(const String &name) {
result += "DROP TABLE " + name + ";";
return this;
}
TableBuilder *SQLite3TableBuilder::drop_table_if_exists(const std::string &name) {
TableBuilder *SQLite3TableBuilder::drop_table_if_exists(const String &name) {
result += "DROP TABLE IF EXISTS " + name + ";";
return this;
@ -96,12 +96,12 @@ TableBuilder *SQLite3TableBuilder::cdrop_table() {
return this;
}
TableBuilder *SQLite3TableBuilder::foreign_key(const std::string &name) {
TableBuilder *SQLite3TableBuilder::foreign_key(const String &name) {
result += "FOREIGN KEY (" + name + ") ";
return this;
}
TableBuilder *SQLite3TableBuilder::references(const std::string &table, const std::string &name) {
TableBuilder *SQLite3TableBuilder::references(const String &table, const String &name) {
result += "REFERENCES " + table + " (" + name + ") ";
return this;

View File

@ -1,7 +1,7 @@
#ifndef SQLITE3_TABLE_BUILDER_H
#define SQLITE3_TABLE_BUILDER_H
#include <string>
#include "core/string.h"
#include "core/database/table_builder.h"
@ -9,26 +9,26 @@ class SQLite3Database;
class SQLite3TableBuilder : public TableBuilder {
public:
TableBuilder *create_table(const std::string &name);
TableBuilder *integer(const std::string &name);
TableBuilder *date(const std::string &name);
TableBuilder *varchar(const std::string &name, const int length);
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 std::string &name);
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 std::string &name);
TableBuilder *drop_table_if_exists(const std::string &name);
TableBuilder *drop_table(const String &name);
TableBuilder *drop_table_if_exists(const String &name);
TableBuilder *cdrop_table();
TableBuilder *foreign_key(const std::string &name);
TableBuilder *references(const std::string &table, const std::string &name);
TableBuilder *foreign_key(const String &name);
TableBuilder *references(const String &table, const String &name);
virtual Ref<QueryResult> run();
virtual void run_query();