Advertisement
Guest User

sqli.nut

a guest
Mar 18th, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1. class sqli
  2. {
  3.     function Init()
  4.     {
  5.     }
  6. }
  7.  
  8. class mysqli
  9. {
  10.     inst = null;
  11.  
  12.     constructor(host, username, password, database)
  13.     {
  14.         local i_host, port;
  15.         if (host.find(":") != null)
  16.         {
  17.             local spl = split(host, ":");
  18.             i_host = spl[0];
  19.             port = spl[1].tointeger();
  20.         }
  21.         else
  22.         {
  23.             i_host = host;
  24.             port = 3306;
  25.         }
  26.         inst = ::mysql_connect(i_host, username, password, database, port);
  27.     }
  28.  
  29.     function transact(...)
  30.     {
  31.         exec("start transaction");
  32.         foreach (arr in vargv) i_exec(arr[0], arr == null ? null : arr.slice(1));
  33.         exec("commit");
  34.     }
  35.  
  36.     function exec(qr, ...) return i_exec(qr, vargv);
  37.  
  38.     function i_exec(qr, inar)
  39.     {
  40.         if (inst == null) throw "Connection closed";
  41.         local q = "", cidx = 0;
  42.         for (local i = 0; i < qr.len(); ++i)
  43.         {
  44.             local chr = qr.slice(i, i + 1);
  45.             if (chr == "?")
  46.             {
  47.                 if (inar.len() <= cidx) throw "Mismatch between query and input array";
  48.                 q += ::mysql_escape_string(inst, inar[cidx]);
  49.                 ++cidx;
  50.             }
  51.             else
  52.             {
  53.                 q += chr;
  54.             }
  55.         }
  56.         local r = ::mysql_query(inst, q);
  57.         if (::mysql_error(inst) != "") throw ::mysql_error(inst);
  58.         if (r != null)
  59.         {
  60.             local rs = [];
  61.             while (true)
  62.             {
  63.                 local fa = ::mysql_fetch_assoc(r);
  64.                 if (fa == null) break;
  65.                 rs.push(fa);
  66.             }
  67.             ::mysql_free_result(r);
  68.             return rs;
  69.         }
  70.     }
  71.  
  72.     function rowsaffected()
  73.     {
  74.         return ::mysql_affected_rows(inst);
  75.     }
  76.  
  77.     function info()
  78.     {
  79.         return ::mysql_info(inst);
  80.     }
  81.  
  82.     function close()
  83.     {
  84.         if (::mysql_close(inst) == false) throw "Failed to close MySQL connection";
  85.         inst = null;
  86.     }
  87.  
  88.     function connect(host, username, password, database)
  89.     {
  90.         if (inst != null) close();
  91.         local i_host, port;
  92.         if (host.find(":") != null)
  93.         {
  94.             local spl = split(host, ":");
  95.             i_host = spl[0];
  96.             port = spl[1].tointeger();
  97.         }
  98.         else
  99.         {
  100.             i_host = host;
  101.             port = 3306;
  102.         }
  103.         inst = ::mysql_connect(i_host, username, password, database, port);
  104.     }
  105.  
  106.     function use(database)
  107.     {
  108.         ::mysql_select_db(inst, database);
  109.     }
  110. }
  111.  
  112. class sqlitei
  113. {
  114.     inst = null;
  115.  
  116.     constructor(path)
  117.     {
  118.         inst = ::SQLite_Open(path);
  119.     }
  120.  
  121.     function transact(...)
  122.     {
  123.         exec("begin transaction");
  124.         foreach (arr in vargv) i_exec(arr[0], arr == null ? null : arr.slice(1));
  125.         exec("commit transaction");
  126.     }
  127.  
  128.     function exec(qr, ...) return i_exec(qr, vargv);
  129.  
  130.     function i_exec(qr, inar)
  131.     {
  132.         if (inst == null) throw "Connection closed";
  133.         local q = "", cidx = 0;
  134.         for (local i = 0; i < qr.len(); ++i)
  135.         {
  136.             local chr = qr.slice(i, i + 1);
  137.             if (chr == "?")
  138.             {
  139.                 if (inar.len() <= cidx) throw "Mismatch between query and input array";
  140.                 q += ::SQLite_Escape(inar[cidx] + "");
  141.                 ++cidx;
  142.             }
  143.             else
  144.             {
  145.                 q += chr;
  146.             }
  147.         }
  148.         local r;
  149.         try r = ::SQLite_Query(inst, q)
  150.         catch (e) throw ::SQLite_ErrMsg(inst);
  151.         if (r != null)
  152.         {
  153.             local rs = [];
  154.             while (::SQLite_ColumnData(r, 0))
  155.             {
  156.                 local tb = {};
  157.                 for (local i = 0; i < ::SQLite_ColumnCount(r); ++i) tb.rawset(::SQLite_ColumnName(r, i), ::SQLite_ColumnData(r, i));
  158.                 rs.push(tb);
  159.                 ::SQLite_NextRow(r);
  160.             }
  161.             ::SQLite_Release(r);
  162.             return rs;
  163.         }
  164.     }
  165.  
  166.     function rowsaffected()
  167.     {
  168.         throw "Not supported on SQLite";
  169.     }
  170.  
  171.     function info()
  172.     {
  173.         throw "Not supported on SQLite";
  174.     }
  175.  
  176.     function close()
  177.     {
  178.         if (::SQLite_Close(inst) == false) throw "Failed to close SQLite connection";
  179.         inst = null;
  180.     }
  181.  
  182.     function connect(path)
  183.     {
  184.         if (inst != null) close();
  185.         inst = ::SQLite_Open(path);
  186.     }
  187.  
  188.     function use(database)
  189.     {
  190.         throw "Not supported on SQLite";
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement