Pelzini

This is the code documentation for the Pelzini project

source of /viewer/database_sqlite.php

The SQLite wrapper functions
  1. <?php
  2. /*
  3. Copyright 2008 Josh Heidenreich
  4.  
  5. This file is part of Pelzini.
  6.  
  7. Pelzini is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. Pelzini is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Pelzini. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21. /**
  22.  * The SQLite wrapper functions
  23.  * @package Viewer
  24.  * @author Josh Heidenreich
  25.  * @since 0.2
  26.  **/
  27.  
  28.  
  29. /**
  30.  * Connects to the SQLite database
  31.  **/
  32. function db_connect($settings)
  33. {
  34. global $db_connection;
  35.  
  36. $db_connection = sqlite_open(
  37. $settings['filename']
  38. );
  39. }
  40.  
  41.  
  42. /**
  43.  * Makes a query to the SQLite database
  44.  **/
  45. function db_query($q)
  46. {
  47. global $db_connection;
  48.  
  49. return sqlite_query($db_connection, $q);
  50. }
  51.  
  52.  
  53. /**
  54.  * Escapses a string for use by the SQLite database
  55.  **/
  56. function db_escape($str)
  57. {
  58. return sqlite_escape_string($str);
  59. }
  60.  
  61.  
  62. /**
  63.  * Quotes a string as nessasary for use by hte SQLite database
  64.  * The result will be different depending on the type of the input.
  65.  * - a number will be left as is
  66.  * - a string will be quoted
  67.  * - a null value will be returned as NULL
  68.  **/
  69. function db_quote($str)
  70. {
  71. if ($str === null) {
  72. return 'NULL';
  73.  
  74. } else if (is_int($str)) {
  75. return $str;
  76.  
  77. } else {
  78. return "'" . sqlite_escape_string($str) . "'";
  79. }
  80. }
  81.  
  82.  
  83. /**
  84.  * Fetches a SQLite result set as an associative array
  85.  **/
  86. function db_fetch_assoc($res)
  87. {
  88. $row = sqlite_fetch_array($res, SQLITE_ASSOC);
  89.  
  90. // MySQL and PostgreSQL will, for the query "SELECT classes.id, classes.name" return $row as [ 'id' => ... ; 'name' => ... ]
  91. // but SQLite returns it as [ 'classes.id' => ... ; 'classes.name' => ... ]
  92. // This code looks for a '.' in the name
  93. // and trims out everything after the dot, so that SQLite behaves like the others
  94. if ($row) {
  95. $new = array();
  96. foreach ($row as $key => $val) {
  97. $x = strpos($key, '.');
  98. if ($x !== false) {
  99. $key = substr($key, $x + 1);
  100. }
  101.  
  102. $new[$key] = $val;
  103. }
  104.  
  105. $row = $new;
  106. }
  107.  
  108. return $row;
  109. }
  110.  
  111.  
  112. /**
  113.  * Returns the number of rows in a SQLite result set
  114.  **/
  115. function db_num_rows($res)
  116. {
  117. return sqlite_num_rows($res);
  118. }
  119.  
  120.  
  121. /**
  122.  * Returns the number of rows affected by the last SQLite query that was executed.
  123.  * SQLite has no affected rows system. Assume if there is a result, that rows were affected.
  124.  **/
  125. function db_affected_rows($res)
  126. {
  127. if ($res) return 1;
  128. return 0;
  129. }
  130.  
  131.  
  132. /**
  133.  * Returns the last unique ID generated by a query
  134.  **/
  135. function db_insert_id()
  136. {
  137. global $db_connection;
  138.  
  139. return sqlite_last_insert_rowid($q, $db_connection);
  140. }
  141.  
  142.  
  143. ?>
  144.