Advertisement
Guest User

SQLi.nut

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