/examples/Mysql.winxed
http://github.com/NotFound/winxed · Unknown · 358 lines · 325 code · 33 blank · 0 comment · 0 complexity · 8b9c11e1923364d76659ecbe2fa074cc MD5 · raw file
- #! winxed
- // Test Mysql usage via NCI
- // Note:
- // null results from dlfuncs are checked in a convoluted way
- // to be able to work with buggy versions of parrot.
- namespace WinxedMysql
- {
- $include_const 'datatypes.pasm';
- // Severity and type of the exceptions explicitly thrown.
- const int ERROR = 2;
- const int TYPE= 99;
- namespace __private
- {
- function findlib()
- {
- for (string lib in [
- 'libmysqlclient',
- 'libmysqlclient.so.16',
- 'libmysqlclient.so.15'
- ] ) {
- var l = loadlib(lib);
- if (l)
- return l;
- }
- return null;
- }
- function getlib()
- {
- var l = findlib();
- if (! l)
- throw Error('Cannot load Mysql lib', ERROR, TYPE);
- for (;;)
- yield l;
- }
- function str_to_cstring(string s)
- {
- var cstring = new ['ByteBuffer'];
- cstring =: s;
- push(cstring, 0);
- return cstring;
- }
- } // namespace __private;
- using namespace __private;
- class Row
- {
- var mrow;
- var desc;
- var nfields;
- var encoding;
- function Row(var myrow, var desc, int n, string encoding[optional])
- {
- self.mrow = myrow;
- self.desc = desc;
- self.nfields = n;
- if (encoding != null)
- self.encoding = encoding;
- }
- function get(int i)
- {
- string encoding;
- if (self.encoding != null)
- encoding = self.encoding;
- var p = self.desc[self.mrow, i];
- string result;
- if (p != null) {
- result = p.as_string(encoding);
- }
- else
- result = '';
- return result;
- }
- }
- class Result
- {
- var mysql;
- var myresult;
- var nfields;
- var desc;
- function Result(var my, var r)
- {
- using WinxedMysql.getlib;
- self.mysql = my;
- self.myresult = r;
- var f = dlfunc(getlib(), 'mysql_field_count', 'ip');
- int count = f(my.mysql);
- self.nfields = count;
- int viewtype[count + 2] = [ DATATYPE_STRUCT, count ];
- for (int i = 2; i < count + 2; ++i)
- viewtype[i] = DATATYPE_PTR;
- var desc = new ['StructView'] (viewtype);
- self.desc = desc;
- }
- function field_count()
- {
- return self.nfields;
- }
- function fetch_row()
- {
- using WinxedMysql.getlib;
- var f = dlfunc(getlib(), 'mysql_fetch_row', 'pp');
- var frow = f(self.myresult);
- if (frow == null)
- return null;
- var none = new 'UnManagedStruct';
- if (frow == none)
- return null;
- string encoding;
- if (self.mysql.encoding != null)
- encoding = self.mysql.encoding;
- return new WinxedMysql.Row(frow, self.desc, self.nfields, encoding);
- }
- function close()
- {
- if (self.myresult != null)
- {
- using WinxedMysql.getlib;
- var f = dlfunc(getlib(), 'mysql_free_result', 'vp');
- f(self.myresult);
- self.myresult = null;
- }
- }
- }
- class Connection
- {
- var mysql;
- var encoding;
- function Connection()
- {
- using WinxedMysql.getlib;
- var minit = dlfunc(getlib(), 'mysql_init', 'pp');
- string nothing;
- var my = minit(null);
- self.mysql = my;
- }
- function close()
- {
- using WinxedMysql.getlib;
- var mclose = dlfunc(getlib(), 'mysql_close', 'vp');
- mclose(self.mysql);
- var n = new 'Undef';
- self.mysql = n;
- }
- function get_client_info()
- {
- using WinxedMysql.getlib;
- var f = dlfunc(getlib(), 'mysql_get_client_info', 'p');
- return f().as_string('utf8');
- }
- function error()
- {
- using WinxedMysql.getlib;
- var f = dlfunc(getlib(), 'mysql_error', 'pp');
- return f(self.mysql).as_string('utf8');
- }
- function connect(string host, string user, string pass, string database,
- string encoding)
- {
- using WinxedMysql.getlib;
- var f = dlfunc(getlib(), 'mysql_real_connect', 'ppppppipi');
- string snull;
- var phost = str_to_cstring(host);
- var puser = str_to_cstring(user);
- var ppass = str_to_cstring(pass);
- var pdatabase = str_to_cstring(database);
- var p = f(self.mysql, phost, puser, ppass, pdatabase, 0, null, 0);
- if (p == null)
- throw Error(self.error(), ERROR, TYPE);
- var none = new 'UnManagedStruct';
- if (p == none)
- throw Error(self.error(), ERROR, TYPE);
- var setcharset = dlfunc(getlib(), 'mysql_set_character_set', 'ipp');
- if (encoding != null) {
- int i = setcharset(self.mysql, str_to_cstring(encoding));
- if (i != 0)
- cry('Failed to set character set.');
- self.encoding = encoding;
- }
- return p;
- }
- function query(string stmt)
- {
- using WinxedMysql.getlib;
- var f = dlfunc(getlib(), 'mysql_query', 'ipp');
- var pstmt = str_to_cstring(stmt);
- int q = f(self.mysql, pstmt);
- if (q != 0)
- throw Error(self.error(), ERROR, TYPE);
- }
- function use_result()
- {
- using WinxedMysql.getlib;
- var f = dlfunc(getlib(), 'mysql_use_result', 'pp');
- var r = f(self.mysql);
- if (r == null)
- throw Error(self.error(), ERROR, TYPE);
- var none = new 'UnManagedStruct';
- if (r == none)
- throw Error(self.error(), ERROR, TYPE);
- return new WinxedMysql.Result(self, r);
- }
- }
- } // namespace WinxedMysql
- $load 'Getopt/Obj.pbc';
- function main(argv)
- {
- using namespace WinxedMysql;
- var getopts = new ['Getopt', 'Obj'];
- getopts.notOptStop(1);
- getopts.push_string('host|h=s');
- getopts.push_string('user|u=s');
- getopts.push_string('password|p:s');
- getopts.push_string('database|D=s');
- getopts.push_string('encoding|D=s');
- getopts.push_string('config|D=s');
- argv.shift();
- var opts = getopts.get_options(argv);
- string host;
- string user;
- string pass;
- string database;
- string encoding;
- string config = opts['config'];
- if (config != null && config != '') {
- // Get config from file in json format.
- try {
- var json = load_language('data_json');
- var code = json.compile(open(config).readall());
- var data = code();
- if (data['host'] != null)
- host = data['host'];
- if (data['user'] != null)
- user = data['user'];
- if (data['password'] != null)
- pass = data['password'];
- if (data['database'] != null)
- database = data['database'];
- if (data['encoding'] != null)
- encoding = data['encoding'];
- }
- catch (e) {
- cry('Error reading config file: ', e['message']);
- return;
- }
- }
- if (opts['host'] != null)
- host = opts['host'];
- if (opts['user'] != null)
- user = opts['user'];
- if (opts['password'] != null)
- pass= opts['password'];
- if (opts['database'] != null)
- database = opts['database'];
- if (opts['encoding'] != null)
- encoding = opts['encoding'];
- if (encoding != null) {
- // Get the encoding number and get back the name from it.
- // This simplifies the usage of encoding aliases and
- // catch not available or invalid encodings.
- int encnum;
- try {
- ${ find_encoding encnum, encoding };
- ${ encodingname encoding, encnum };
- }
- catch() {
- cry("Encoding '" + encoding + "' not available");
- exit(1);
- }
- }
- if (host == null || host == '')
- host = 'localhost';
- if (user == null || user == '')
- user = 'parrot';
- if (pass == null)
- pass = 'baDworD';
- else if (pass == '')
- die("Sorry, asking for password not implemented");
- if (database == null || database == '')
- database = 'parrot';
- var mysql = new WinxedMysql.Connection();
- say('Mysql version: ', mysql.get_client_info());
- var result;
- try [min_severity(ERROR),max_severity(ERROR),handle_types(TYPE)] {
- mysql.connect(host, user, pass, database, encoding);
- string q = 'select * from hello;';
- if (elements(argv) > 0) {
- q = string(argv.shift());
- }
- for(;;) {
- say("Query: '", q, "'");
- mysql.query(q);
- result = mysql.use_result();
- int fields = result.field_count();
- int nrows = 0;
- var row;
- while ((row = result.fetch_row()) != null) {
- ++nrows;
- print(nrows, ': ');
- for (int i = 0; i < fields; ++i) {
- if (i > 0)
- print(", ");
- string s = row.get(i);
- if (s == null)
- print('(null)');
- else
- print("'", s, "'");
- }
- say();
- }
- result.close();
- say('Total rows: ', nrows);
- if (elements(argv) == 0)
- break;
- q = string(argv.shift());
- }
- }
- catch (e) {
- say("\tERROR: ", e['message']);
- if (result != null)
- result.close();
- mysql.close();
- return;
- }
- mysql.close();
- }
- // End