sqlite.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. <?php
  2. /**
  3. * Copyright (C) 2008-2012 FluxBB
  4. * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
  5. * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  6. */
  7. namespace ForkBB\Core\DB;
  8. // Make sure we have built in support for SQLite
  9. if (!function_exists('sqlite_open'))
  10. exit('This PHP environment doesn\'t have SQLite support built in. SQLite support is required if you want to use a SQLite database to run this forum. Consult the PHP documentation for further assistance.');
  11. class DBLayer
  12. {
  13. var $prefix;
  14. var $link_id;
  15. var $query_result;
  16. var $in_transaction = 0;
  17. var $saved_queries = array();
  18. var $num_queries = 0;
  19. var $error_no = false;
  20. var $error_msg = 'Unknown';
  21. var $datatype_transformations = array(
  22. '%^SERIAL$%' => 'INTEGER',
  23. '%^(TINY|SMALL|MEDIUM|BIG)?INT( )?(\\([0-9]+\\))?( )?(UNSIGNED)?$%i' => 'INTEGER',
  24. '%^(TINY|MEDIUM|LONG)?TEXT$%i' => 'TEXT'
  25. );
  26. function __construct($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
  27. {
  28. // Prepend $db_name with the path to the forum root directory
  29. $db_name = PUN_ROOT.$db_name;
  30. $this->prefix = $db_prefix;
  31. if (!file_exists($db_name))
  32. {
  33. @touch($db_name);
  34. @chmod($db_name, 0666);
  35. if (!file_exists($db_name))
  36. error('Unable to create new database \''.$db_name.'\'. Permission denied', __FILE__, __LINE__);
  37. }
  38. if (!is_readable($db_name))
  39. error('Unable to open database \''.$db_name.'\' for reading. Permission denied', __FILE__, __LINE__);
  40. if (!forum_is_writable($db_name))
  41. error('Unable to open database \''.$db_name.'\' for writing. Permission denied', __FILE__, __LINE__);
  42. if ($p_connect)
  43. $this->link_id = @sqlite_popen($db_name, 0666, $sqlite_error);
  44. else
  45. $this->link_id = @sqlite_open($db_name, 0666, $sqlite_error);
  46. if (!$this->link_id)
  47. error('Unable to open database \''.$db_name.'\'. SQLite reported: '.$sqlite_error, __FILE__, __LINE__);
  48. else
  49. return $this->link_id;
  50. }
  51. function start_transaction()
  52. {
  53. ++$this->in_transaction;
  54. return (@sqlite_query($this->link_id, 'BEGIN')) ? true : false;
  55. }
  56. function end_transaction()
  57. {
  58. --$this->in_transaction;
  59. if (@sqlite_query($this->link_id, 'COMMIT'))
  60. return true;
  61. else
  62. {
  63. @sqlite_query($this->link_id, 'ROLLBACK');
  64. return false;
  65. }
  66. }
  67. function query($sql, $unbuffered = false)
  68. {
  69. if (defined('PUN_SHOW_QUERIES'))
  70. $q_start = microtime(true);
  71. if ($unbuffered)
  72. $this->query_result = @sqlite_unbuffered_query($this->link_id, $sql);
  73. else
  74. $this->query_result = @sqlite_query($this->link_id, $sql);
  75. if ($this->query_result)
  76. {
  77. if (defined('PUN_SHOW_QUERIES'))
  78. $this->saved_queries[] = array($sql, sprintf('%.5f', microtime(true) - $q_start));
  79. ++$this->num_queries;
  80. return $this->query_result;
  81. }
  82. else
  83. {
  84. if (defined('PUN_SHOW_QUERIES'))
  85. $this->saved_queries[] = array($sql, 0);
  86. $this->error_no = @sqlite_last_error($this->link_id);
  87. $this->error_msg = @sqlite_error_string($this->error_no);
  88. if ($this->in_transaction)
  89. @sqlite_query($this->link_id, 'ROLLBACK');
  90. --$this->in_transaction;
  91. return false;
  92. }
  93. }
  94. function result($query_id = 0, $row = 0, $col = 0)
  95. {
  96. if ($query_id)
  97. {
  98. if ($row !== 0 && @sqlite_seek($query_id, $row) === false)
  99. return false;
  100. $cur_row = @sqlite_current($query_id);
  101. if ($cur_row === false)
  102. return false;
  103. return $cur_row[$col];
  104. }
  105. else
  106. return false;
  107. }
  108. function fetch_assoc($query_id = 0)
  109. {
  110. if ($query_id)
  111. {
  112. $cur_row = @sqlite_fetch_array($query_id, SQLITE_ASSOC);
  113. if ($cur_row)
  114. {
  115. // Horrible hack to get rid of table names and table aliases from the array keys
  116. foreach ($cur_row as $key => $value)
  117. {
  118. $dot_spot = strpos($key, '.');
  119. if ($dot_spot !== false)
  120. {
  121. unset($cur_row[$key]);
  122. $key = substr($key, $dot_spot+1);
  123. $cur_row[$key] = $value;
  124. }
  125. }
  126. }
  127. return $cur_row;
  128. }
  129. else
  130. return false;
  131. }
  132. function fetch_row($query_id = 0)
  133. {
  134. return ($query_id) ? @sqlite_fetch_array($query_id, SQLITE_NUM) : false;
  135. }
  136. function num_rows($query_id = 0)
  137. {
  138. return ($query_id) ? @sqlite_num_rows($query_id) : false;
  139. }
  140. function affected_rows()
  141. {
  142. return ($this->link_id) ? @sqlite_changes($this->link_id) : false;
  143. }
  144. function insert_id()
  145. {
  146. return ($this->link_id) ? @sqlite_last_insert_rowid($this->link_id) : false;
  147. }
  148. function get_num_queries()
  149. {
  150. return $this->num_queries;
  151. }
  152. function get_saved_queries()
  153. {
  154. return $this->saved_queries;
  155. }
  156. function free_result($query_id = false)
  157. {
  158. return true;
  159. }
  160. function escape($str)
  161. {
  162. return is_array($str) ? '' : sqlite_escape_string($str);
  163. }
  164. function error()
  165. {
  166. $result['error_sql'] = @current(@end($this->saved_queries));
  167. $result['error_no'] = $this->error_no;
  168. $result['error_msg'] = $this->error_msg;
  169. return $result;
  170. }
  171. function close()
  172. {
  173. if ($this->link_id)
  174. {
  175. if ($this->in_transaction)
  176. {
  177. if (defined('PUN_SHOW_QUERIES'))
  178. $this->saved_queries[] = array('COMMIT', 0);
  179. @sqlite_query($this->link_id, 'COMMIT');
  180. }
  181. return @sqlite_close($this->link_id);
  182. }
  183. else
  184. return false;
  185. }
  186. function get_names()
  187. {
  188. return '';
  189. }
  190. function set_names($names)
  191. {
  192. return true;
  193. }
  194. function get_version()
  195. {
  196. return array(
  197. 'name' => 'SQLite',
  198. 'version' => sqlite_libversion()
  199. );
  200. }
  201. function table_exists($table_name, $no_prefix = false)
  202. {
  203. $result = $this->query('SELECT 1 FROM sqlite_master WHERE name = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\' AND type=\'table\'');
  204. return $this->num_rows($result) > 0;
  205. }
  206. function field_exists($table_name, $field_name, $no_prefix = false)
  207. {
  208. $result = $this->query('SELECT sql FROM sqlite_master WHERE name = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\' AND type=\'table\'');
  209. if (!$this->num_rows($result))
  210. return false;
  211. return preg_match('%[\r\n]'.preg_quote($field_name, '%').' %', $this->result($result));
  212. }
  213. function index_exists($table_name, $index_name, $no_prefix = false)
  214. {
  215. $result = $this->query('SELECT 1 FROM sqlite_master WHERE tbl_name = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\' AND name = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_'.$this->escape($index_name).'\' AND type=\'index\'');
  216. return $this->num_rows($result) > 0;
  217. }
  218. function create_table($table_name, $schema, $no_prefix = false)
  219. {
  220. if ($this->table_exists($table_name, $no_prefix))
  221. return true;
  222. $query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n";
  223. // Go through every schema element and add it to the query
  224. foreach ($schema['FIELDS'] as $field_name => $field_data)
  225. {
  226. $field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']);
  227. $query .= $field_name.' '.$field_data['datatype'];
  228. if (!$field_data['allow_null'])
  229. $query .= ' NOT NULL';
  230. if (isset($field_data['default']))
  231. $query .= ' DEFAULT '.$field_data['default'];
  232. $query .= ",\n";
  233. }
  234. // If we have a primary key, add it
  235. if (isset($schema['PRIMARY KEY']))
  236. $query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n";
  237. // Add unique keys
  238. if (isset($schema['UNIQUE KEYS']))
  239. {
  240. foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields)
  241. $query .= 'UNIQUE ('.implode(',', $key_fields).'),'."\n";
  242. }
  243. // We remove the last two characters (a newline and a comma) and add on the ending
  244. $query = substr($query, 0, strlen($query) - 2)."\n".')';
  245. $result = $this->query($query) ? true : false;
  246. // Add indexes
  247. if (isset($schema['INDEXES']))
  248. {
  249. foreach ($schema['INDEXES'] as $index_name => $index_fields)
  250. $result &= $this->add_index($table_name, $index_name, $index_fields, false, $no_prefix);
  251. }
  252. return $result;
  253. }
  254. function drop_table($table_name, $no_prefix = false)
  255. {
  256. if (!$this->table_exists($table_name, $no_prefix))
  257. return true;
  258. return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name)) ? true : false;
  259. }
  260. function rename_table($old_table, $new_table, $no_prefix = false)
  261. {
  262. // If the old table does not exist
  263. if (!$this->table_exists($old_table, $no_prefix))
  264. return false;
  265. // If the table names are the same
  266. else if ($old_table == $new_table)
  267. return true;
  268. // If the new table already exists
  269. else if ($this->table_exists($new_table, $no_prefix))
  270. return false;
  271. $table = $this->get_table_info($old_table, $no_prefix);
  272. // Create new table
  273. $query = str_replace('CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($old_table).' (', 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($new_table).' (', $table['sql']);
  274. $result = $this->query($query) ? true : false;
  275. // Recreate indexes
  276. if (!empty($table['indices']))
  277. {
  278. foreach ($table['indices'] as $cur_index)
  279. {
  280. $query = str_replace('CREATE INDEX '.($no_prefix ? '' : $this->prefix).$this->escape($old_table), 'CREATE INDEX '.($no_prefix ? '' : $this->prefix).$this->escape($new_table), $cur_index);
  281. $query = str_replace('ON '.($no_prefix ? '' : $this->prefix).$this->escape($old_table), 'ON '.($no_prefix ? '' : $this->prefix).$this->escape($new_table), $query);
  282. $result &= $this->query($query) ? true : false;
  283. }
  284. }
  285. // Copy content across
  286. $result &= $this->query('INSERT INTO '.($no_prefix ? '' : $this->prefix).$this->escape($new_table).' SELECT * FROM '.($no_prefix ? '' : $this->prefix).$this->escape($old_table)) ? true : false;
  287. // Drop the old table if the new one exists
  288. if ($this->table_exists($new_table, $no_prefix))
  289. $result &= $this->drop_table($old_table, $no_prefix);
  290. return $result;
  291. }
  292. function get_table_info($table_name, $no_prefix = false)
  293. {
  294. // Grab table info
  295. $result = $this->query('SELECT sql FROM sqlite_master WHERE tbl_name = \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\' ORDER BY type DESC') or error('Unable to fetch table information', __FILE__, __LINE__, $this->error());
  296. $num_rows = $this->num_rows($result);
  297. if ($num_rows == 0)
  298. return;
  299. $table = array();
  300. $table['indices'] = array();
  301. while ($cur_index = $this->fetch_assoc($result))
  302. {
  303. if (empty($cur_index['sql']))
  304. continue;
  305. if (!isset($table['sql']))
  306. $table['sql'] = $cur_index['sql'];
  307. else
  308. $table['indices'][] = $cur_index['sql'];
  309. }
  310. // Work out the columns in the table currently
  311. $table_lines = explode("\n", $table['sql']);
  312. $table['columns'] = array();
  313. foreach ($table_lines as $table_line)
  314. {
  315. $table_line = trim($table_line, " \t\n\r,"); // trim spaces, tabs, newlines, and commas
  316. if (substr($table_line, 0, 12) == 'CREATE TABLE')
  317. continue;
  318. else if (substr($table_line, 0, 11) == 'PRIMARY KEY')
  319. $table['primary_key'] = $table_line;
  320. else if (substr($table_line, 0, 6) == 'UNIQUE')
  321. $table['unique'] = $table_line;
  322. else if (substr($table_line, 0, strpos($table_line, ' ')) != '')
  323. $table['columns'][substr($table_line, 0, strpos($table_line, ' '))] = trim(substr($table_line, strpos($table_line, ' ')));
  324. }
  325. return $table;
  326. }
  327. function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
  328. {
  329. if ($this->field_exists($table_name, $field_name, $no_prefix))
  330. return true;
  331. $table = $this->get_table_info($table_name, $no_prefix);
  332. // Create temp table
  333. $now = time();
  334. $tmptable = str_replace('CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).' (', 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_t'.$now.' (', $table['sql']);
  335. $result = $this->query($tmptable) ? true : false;
  336. $result &= $this->query('INSERT INTO '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_t'.$now.' SELECT * FROM '.($no_prefix ? '' : $this->prefix).$this->escape($table_name)) ? true : false;
  337. // Create new table sql
  338. $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
  339. $query = $field_type;
  340. if (!$allow_null)
  341. $query .= ' NOT NULL';
  342. if (is_string($default_value))
  343. $default_value = '\''.$this->escape($default_value).'\'';
  344. if (!is_null($default_value))
  345. $query .= ' DEFAULT '.$default_value;
  346. $old_columns = array_keys($table['columns']);
  347. // Determine the proper offset
  348. if (!is_null($after_field))
  349. $offset = array_search($after_field, array_keys($table['columns']), true) + 1;
  350. else
  351. $offset = count($table['columns']);
  352. // Out of bounds checks
  353. if ($offset > count($table['columns']))
  354. $offset = count($table['columns']);
  355. else if ($offset < 0)
  356. $offset = 0;
  357. if (!is_null($field_name) && $field_name !== '')
  358. $table['columns'] = array_merge(array_slice($table['columns'], 0, $offset), array($field_name => $query), array_slice($table['columns'], $offset));
  359. $new_table = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).' (';
  360. foreach ($table['columns'] as $cur_column => $column_details)
  361. $new_table .= "\n".$cur_column.' '.$column_details.',';
  362. if (isset($table['unique']))
  363. $new_table .= "\n".$table['unique'].',';
  364. if (isset($table['primary_key']))
  365. $new_table .= "\n".$table['primary_key'].',';
  366. $new_table = trim($new_table, ',')."\n".');';
  367. // Drop old table
  368. $result &= $this->drop_table($table_name, $no_prefix);
  369. // Create new table
  370. $result &= $this->query($new_table) ? true : false;
  371. // Recreate indexes
  372. if (!empty($table['indices']))
  373. {
  374. foreach ($table['indices'] as $cur_index)
  375. $result &= $this->query($cur_index) ? true : false;
  376. }
  377. // Copy content back
  378. $result &= $this->query('INSERT INTO '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).' ('.implode(', ', $old_columns).') SELECT * FROM '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_t'.$now) ? true : false;
  379. // Drop temp table
  380. $result &= $this->drop_table($table_name.'_t'.$now, $no_prefix);
  381. return $result;
  382. }
  383. function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
  384. {
  385. // Unneeded for SQLite
  386. return true;
  387. }
  388. function drop_field($table_name, $field_name, $no_prefix = false)
  389. {
  390. if (!$this->field_exists($table_name, $field_name, $no_prefix))
  391. return true;
  392. $table = $this->get_table_info($table_name, $no_prefix);
  393. // Create temp table
  394. $now = time();
  395. $tmptable = str_replace('CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).' (', 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_t'.$now.' (', $table['sql']);
  396. $result = $this->query($tmptable) ? true : false;
  397. $result &= $this->query('INSERT INTO '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_t'.$now.' SELECT * FROM '.($no_prefix ? '' : $this->prefix).$this->escape($table_name)) ? true : false;
  398. // Work out the columns we need to keep and the sql for the new table
  399. unset($table['columns'][$field_name]);
  400. $new_columns = array_keys($table['columns']);
  401. $new_table = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).' (';
  402. foreach ($table['columns'] as $cur_column => $column_details)
  403. $new_table .= "\n".$cur_column.' '.$column_details.',';
  404. if (isset($table['unique']))
  405. $new_table .= "\n".$table['unique'].',';
  406. if (isset($table['primary_key']))
  407. $new_table .= "\n".$table['primary_key'].',';
  408. $new_table = trim($new_table, ',')."\n".');';
  409. // Drop old table
  410. $result &= $this->drop_table($table_name, $no_prefix);
  411. // Create new table
  412. $result &= $this->query($new_table) ? true : false;
  413. // Recreate indexes
  414. if (!empty($table['indices']))
  415. {
  416. foreach ($table['indices'] as $cur_index)
  417. if (!preg_match('%\('.preg_quote($field_name, '%').'\)%', $cur_index))
  418. $result &= $this->query($cur_index) ? true : false;
  419. }
  420. // Copy content back
  421. $result &= $this->query('INSERT INTO '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).' SELECT '.implode(', ', $new_columns).' FROM '.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'_t'.$now) ? true : false;
  422. // Drop temp table
  423. $result &= $this->drop_table($table_name.'_t'.$now, $no_prefix);
  424. return $result;
  425. }
  426. function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false)
  427. {
  428. if ($this->index_exists($table_name, $index_name, $no_prefix))
  429. return true;
  430. return $this->query('CREATE '.($unique ? 'UNIQUE ' : '').'INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.' ON '.($no_prefix ? '' : $this->prefix).$table_name.'('.implode(',', $index_fields).')') ? true : false;
  431. }
  432. function drop_index($table_name, $index_name, $no_prefix = false)
  433. {
  434. if (!$this->index_exists($table_name, $index_name, $no_prefix))
  435. return true;
  436. return $this->query('DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false;
  437. }
  438. function truncate_table($table_name, $no_prefix = false)
  439. {
  440. return $this->query('DELETE FROM '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
  441. }
  442. }