PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/include/dblayer/sqlite.php

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