Register | Login
Forum Index > Samples > MySQL / MariaDB
Author Message
Pages: 1
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[1385] MySQL / MariaDB - posted: 2013-11-28 00:41:13
Hello there,

I've had very little time these past months, i hope to get more time soon to implement a lot of new features i have plans for... meanwhile i spend 10 minutes putting together a sample include for mysql/mariadb and a sample app.

If anyone wants to extend and finish the include before i get around to it, please do reply:

Include File: "database/mysql.zir"
Code:
#include 'windows\\win_kernel.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);
}


sample usage:

Code:
program PE32_CUI 'mysql test';

#include 'win_def.zir';
#include 'zirutils.zir';
#include 'console.zir';
#include 'database/mysql.zir';

DWord mysql_libhandle;
mysql_libhandle = mysql_loadlib('libmariadb.dll');

const DB_HOST = 'localhost';
const DB_PORT = 3306;
const DB_USER = 'root';
const DB_PASS = '********';
const DB_NAME = 'DBNAME';

PMYSQL dbconn;
dbconn = mysql_init(nil);
mysql_real_connect(dbconn, DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT, nil, 0);

mysql_query(dbconn, 'SELECT * FROM items;');

PMYSQLRES dbres;
dbres = mysql_store_result(dbconn);

if (dbres != nil) {
  eax = mysql_fetch_row(dbres);

  while (eax != nil) {
    edi = [eax+25*4];  //25 = column you want info from
    print('Row Info: ', [char*]edi, '\r\n');
  
    eax = mysql_fetch_row(dbres);
  }

  mysql_free_result(dbres);
}

mysql_freelib(mysql_libhandle);

wait_key();

ExitProcess(0);


you'll need to fill in the database username, pass etc and edit the sql query and which column to grab data from for this sample.

Regards,

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[1392] - posted: 2014-02-07 05:43:54
I have updated the mysql.zir include a little further for playing around using OOP instead of plain functions.... this could be extended to provide a powerful mysql interface from Ziron later on down the road.

Include File: "database/mysql.zir":
Code:
//******************************************************//
//                                                      //
//    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];
}


sample app: (no error checking etc, still only for playing around)
Code:
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);


Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Pages: 1
create new reply


Quick reply:

Message:



Currently Active Users:
There are currently 2 user(s) online. 0 member(s) and 2 guest(s)
Most users ever online was 1046, January 28, 2022, 2:08 pm.


Statistics:
Threads: 225 | Posts: 1848 | Members: 51 | Active Members: 51
Welcome to our newest member, yecate
const Copyright = '2011-2024 © OverHertz Ltd. All rights reserved.';
Web development by OverHertz Ltd