//******************************************************//
// //
// Ziron Run Time Library //
// MySQL include (mysql.zir) //
// //
// Include by OverHertz Ltd //
// Still under development, unfinished //
// //
//******************************************************//
#include 'windows\\win_kernel.zir';
#include 'sockets.zir';
type PMYSQL = pointer;
type PMYSQLRES = pointer;
type PMYSQLROW = char*;
// function refs
function* WINAPI mysql_init(PMYSQL _mysql): PMYSQL;
function* WINAPI mysql_real_connect(PMYSQL _mysql; char* host, user, passwd, db; DWord port; char* unix_socket; DWord clientflag): PMYSQL;
function* WINAPI mysql_query(PMYSQL _mysql; char* q): Int32;
function* WINAPI mysql_store_result(PMYSQL _mysql): PMYSQLRES;
function* WINAPI mysql_num_rows(PMYSQLRES res): Int64;
procedure* WINAPI mysql_free_result(PMYSQLRES result);
function* WINAPI mysql_get_server_info(PMYSQL _mysql): char*;
function* WINAPI mysql_fetch_row(PMYSQLRES result): PMYSQLROW;
////
function mysql_loadlib(char* libfile) {
DWord libmysql_handle;
libmysql_handle = LoadLibraryA(libfile);
if (libmysql_handle != 0) {
mysql_init = GetProcAddress(libmysql_handle, 'mysql_init');
mysql_real_connect = GetProcAddress(libmysql_handle, 'mysql_real_connect');
mysql_query = GetProcAddress(libmysql_handle, 'mysql_query');
mysql_store_result = GetProcAddress(libmysql_handle, 'mysql_store_result');
mysql_num_rows = GetProcAddress(libmysql_handle, 'mysql_num_rows');
mysql_free_result = GetProcAddress(libmysql_handle, 'mysql_free_result');
mysql_get_server_info = GetProcAddress(libmysql_handle, 'mysql_get_server_info');
mysql_fetch_row = GetProcAddress(libmysql_handle, 'mysql_fetch_row');
}
return libmysql_handle;
}
function mysql_freelib(Dword libhand) {
FreeLibrary(libhand);
}
//////////////////////
// OOP style
block ZMySQL {
DWord handle;
PMYSQL dbconn;
function create(char* filename): ZMySQL*;
procedure free();
function connect(char* host, user, pass, name; dword port): boolean;
function query_result(char* sql): PMYSQLRES;
function fetch_row(PMYSQLRES dbres): pointer;
procedure free_result(PMYSQLRES dbres);
}
block ZMySQLRow {
pointer data; //must be first....
function getColumnChar(DWord col): char*;
}
function ZMySQL:create(char* filename) {
uses ebx;
ebx = this as ZMySQL;
ebx.handle = mysql_loadlib(filename);
ebx.dbconn = mysql_init(nil);
return ebx;
}
procedure ZMySQL:free() {
eax = this as ZMySQL;
mysql_freelib(eax.handle);
}
function ZMySQL:connect(char* host, user, pass, name; dword port) {
uses ebx;
ebx = this as ZMySQL;
mysql_real_connect(ebx.dbconn, host, user, pass, name, port, nil, 0);
}
function ZMySQL:query_result(char* sql) {
uses ebx;
ebx = this as ZMySQL;
mysql_query(ebx.dbconn, sql);
eax = mysql_store_result(ebx.dbconn);
}
function ZMySQL:fetch_row(PMYSQLRES dbres) {
eax = mysql_fetch_row(dbres);
}
procedure ZMySQL:free_result(PMYSQLRES dbres) {
mysql_free_result(dbres);
}
///////
function ZMySQLRow:getColumnChar(DWord col) {
eax = this;
ecx = col;
eax = [eax+ecx*4];
}
program PE32_CUI 'mysql test';
#include 'win_def.zir';
#include 'zirutils.zir';
#include 'console.zir';
#include 'database/mysql.zir';
/////////
const DB_HOST = 'localhost';
const DB_PORT = 3306;
const DB_USER = 'user';
const DB_PASS = '*****';
const DB_NAME = 'dbname';
PMYSQLRES res;
ZMySQL conn;
conn.create('libmariadb.dll');
conn.connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);
res = conn.query_result('SELECT * FROM items;');
if (res != nil) {
ebx = conn.fetch_row(res) as ZMySQLRow;
while (ebx != nil) {
edi = ebx.getColumnChar(25);
print('Row Info: ', [char*]edi, '\r\n');
ebx = conn.fetch_row(res);
}
conn.free_result(res);
}
conn.free();
wait_key();
ExitProcess(0);