Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class mysqli
- {
- inst = null;
- constructor(host, username, password, database)
- {
- local i_host, port;
- if (host.find(":") != null)
- {
- local spl = split(host, ":");
- i_host = spl[0];
- port = spl[1].tointeger();
- }
- else
- {
- i_host = host;
- port = 3306;
- }
- inst = ::mysql_connect(i_host, username, password, database, port);
- }
- function transact(...)
- {
- exec("start transaction");
- foreach (arr in vargv) i_exec(arr[0], arr == null ? null : arr.slice(1));
- exec("commit");
- }
- function exec(qr, ...) return i_exec(qr, vargv);
- function i_exec(qr, inar)
- {
- if (inst == null) throw "Connection closed";
- local q = "", cidx = 0;
- for (local i = 0; i < qr.len(); ++i)
- {
- local chr = qr.slice(i, i + 1);
- if (chr == "?")
- {
- if (inar.len() <= cidx) throw "Mismatch between query and input array";
- q += ::mysql_escape_string(inst, inar[cidx]);
- ++cidx;
- }
- else
- {
- q += chr;
- }
- }
- local r = ::mysql_query(inst, q);
- if (::mysql_error(inst) != "") throw ::mysql_error(inst);
- if (r != null)
- {
- local rs = [];
- while (true)
- {
- local fa = ::mysql_fetch_assoc(r);
- if (fa == null) break;
- rs.push(fa);
- }
- ::mysql_free_result(r);
- return rs;
- }
- }
- function rowsaffected()
- {
- return ::mysql_affected_rows(inst);
- }
- function info()
- {
- return ::mysql_info(inst);
- }
- function close()
- {
- if (::mysql_close(inst) == false) throw "Failed to close MySQL connection";
- inst = null;
- }
- function connect(host, username, password, database)
- {
- if (inst != null) close();
- local i_host, port;
- if (host.find(":") != null)
- {
- local spl = split(host, ":");
- i_host = spl[0];
- port = spl[1].tointeger();
- }
- else
- {
- i_host = host;
- port = 3306;
- }
- inst = ::mysql_connect(i_host, username, password, database, port);
- }
- function use(database)
- {
- ::mysql_select_db(inst, database);
- }
- }
- class sqlitei
- {
- inst = null;
- constructor(path)
- {
- inst = ::SQLite_Open(path);
- }
- function transact(...)
- {
- exec("begin transaction");
- foreach (arr in vargv) i_exec(arr[0], arr == null ? null : arr.slice(1));
- exec("commit transaction");
- }
- function exec(qr, ...) return i_exec(qr, vargv);
- function i_exec(qr, inar)
- {
- if (inst == null) throw "Connection closed";
- local q = "", cidx = 0;
- for (local i = 0; i < qr.len(); ++i)
- {
- local chr = qr.slice(i, i + 1);
- if (chr == "?")
- {
- if (inar.len() <= cidx) throw "Mismatch between query and input array";
- q += ::SQLite_Escape(inar[cidx] + "");
- ++cidx;
- }
- else
- {
- q += chr;
- }
- }
- local r;
- try r = ::SQLite_Query(inst, q)
- catch (e) throw ::SQLite_ErrMsg(inst);
- if (r != null)
- {
- local rs = [];
- while (::SQLite_ColumnData(r, 0))
- {
- local tb = {};
- for (local i = 0; i < ::SQLite_ColumnCount(r); ++i) tb.rawset(::SQLite_ColumnName(r, i), ::SQLite_ColumnData(r, i));
- rs.push(tb);
- ::SQLite_NextRow(r);
- }
- ::SQLite_Release(r);
- return rs;
- }
- }
- function rowsaffected()
- {
- throw "Not supported on SQLite";
- }
- function info()
- {
- throw "Not supported on SQLite";
- }
- function close()
- {
- if (::SQLite_Close(inst) == false) throw "Failed to close SQLite connection";
- inst = null;
- }
- function connect(path)
- {
- if (inst != null) close();
- inst = ::SQLite_Open(path);
- }
- function use(database)
- {
- throw "Not supported on SQLite";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement