PageRenderTime 50ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/sql.php

https://bitbucket.org/lordgnu/greyhole
PHP | 256 lines | 193 code | 19 blank | 44 comment | 26 complexity | 523895c1039fcc76b6ced7fd8e9a382e MD5 | raw file
  1. <?php
  2. /*
  3. Copyright 2010 Guillaume Boudreau, Carlos Puchol (Amahi)
  4. This file is part of Greyhole.
  5. Greyhole is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Greyhole is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Greyhole. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. Small abstraction layer for supporting MySQL and SQLite based
  18. on a user choice. Specify
  19. db_engine = sqlite
  20. db_path = /var/cache/greyhole.sqlite
  21. in /etc/greyhole.conf to enable sqlite support, otherwise the
  22. standard Greyhole MySQL support will be used.
  23. Carlos Puchol, Amahi
  24. cpg+git@amahi.org
  25. */
  26. if ($db_options->engine == 'sqlite') {
  27. function db_connect() {
  28. global $db_options;
  29. if (!file_exists($db_options->db_path)) {
  30. // create the db automatically if it does not exist
  31. system("sqlite3 $db_options->db_path < $db_options->schema");
  32. }
  33. $db_options->dbh = new PDO("sqlite:" . $db_options->db_path);
  34. return $db_options->dbh;
  35. }
  36. function db_query($query) {
  37. global $db_options;
  38. return $db_options->dbh->query($query);
  39. }
  40. function db_escape_string($string) {
  41. global $db_options;
  42. $escaped_string = $db_options->dbh->quote($string);
  43. return substr($escaped_string, 1, strlen($escaped_string)-2);
  44. }
  45. function db_fetch_object($result) {
  46. return $result->fetchObject();
  47. }
  48. function db_free_result($result) {
  49. return TRUE;
  50. }
  51. function db_insert_id() {
  52. global $db_options;
  53. return $db_options->dbh->lastInsertId();
  54. }
  55. function db_error() {
  56. global $db_options;
  57. $error = $db_options->dbh->errorInfo();
  58. return $error[2];
  59. }
  60. } else {
  61. // MySQL
  62. function db_connect() {
  63. global $db_options;
  64. $connected = mysql_connect($db_options->host, $db_options->user, $db_options->pass);
  65. if ($connected) {
  66. $connected = mysql_select_db($db_options->name);
  67. if ($connected) {
  68. db_query("SET SESSION group_concat_max_len = 1048576");
  69. db_query("SET SESSION wait_timeout = 86400"); # Allow 24h fsck!
  70. }
  71. }
  72. return $connected;
  73. }
  74. function db_query($query) {
  75. return mysql_query($query);
  76. }
  77. function db_escape_string($string) {
  78. return mysql_real_escape_string($string);
  79. }
  80. function db_fetch_object($result) {
  81. return mysql_fetch_object($result);
  82. }
  83. function db_free_result($result) {
  84. return mysql_free_result($result);
  85. }
  86. function db_insert_id() {
  87. return mysql_insert_id();
  88. }
  89. function db_error() {
  90. return mysql_error();
  91. }
  92. }
  93. function db_migrate() {
  94. global $db_options, $db_use_mysql, $db_use_sqlite;
  95. // Migration #1 (complete = frozen|thawed)
  96. if (@$db_use_mysql) {
  97. $query = "DESCRIBE tasks";
  98. $result = db_query($query) or die("Can't describe tasks with query: $query - Error: " . db_error());
  99. while ($row = db_fetch_object($result)) {
  100. if ($row->Field == 'complete') {
  101. if ($row->Type == "enum('yes','no')") {
  102. // migrate
  103. db_query("ALTER TABLE tasks CHANGE complete complete ENUM('yes','no','frozen','thawed') NOT NULL");
  104. db_query("ALTER TABLE tasks_completed CHANGE complete complete ENUM('yes','no','frozen','thawed') NOT NULL");
  105. }
  106. break;
  107. }
  108. }
  109. } else if (@$db_use_sqlite) {
  110. $query = "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'tasks'";
  111. $result = db_query($query) or die("Can't describe tasks with query: $query - Error: " . db_error());
  112. while ($row = db_fetch_object($result)) {
  113. if (strpos($row->sql, 'complete BOOL NOT NULL') !== FALSE) {
  114. // migrate; not supported! @see http://sqlite.org/omitted.html
  115. gh_log(CRITICAL, "Your SQLite database is not up to date. Column tasks.complete needs to be a TINYTEXT. Please fix, then retry.");
  116. }
  117. }
  118. }
  119. // Migration #2 (complete = idle)
  120. if (@$db_use_mysql) {
  121. $query = "DESCRIBE tasks";
  122. $result = db_query($query) or die("Can't describe tasks with query: $query - Error: " . db_error());
  123. while ($row = db_fetch_object($result)) {
  124. if ($row->Field == 'complete') {
  125. if ($row->Type == "enum('yes','no','frozen','thawed')") {
  126. // migrate
  127. db_query("ALTER TABLE tasks CHANGE complete complete ENUM('yes','no','frozen','thawed','idle') NOT NULL");
  128. db_query("ALTER TABLE tasks_completed CHANGE complete complete ENUM('yes','no','frozen','thawed','idle') NOT NULL");
  129. }
  130. break;
  131. }
  132. }
  133. }
  134. // Migration #3 (larger settings.value: tinytext > text)
  135. if (@$db_use_mysql) {
  136. $query = "DESCRIBE settings";
  137. $result = db_query($query) or die("Can't describe settings with query: $query - Error: " . db_error());
  138. while ($row = db_fetch_object($result)) {
  139. if ($row->Field == 'value') {
  140. if ($row->Type == "tinytext") {
  141. // migrate
  142. db_query("ALTER TABLE settings CHANGE value value TEXT CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL");
  143. }
  144. break;
  145. }
  146. }
  147. }
  148. // Migration #4 (new index for find_next_task function, used by simplify_task, and also for execute_next_task function; also remove deprecated indexes)
  149. if (@$db_use_mysql) {
  150. $query = "SHOW INDEX FROM tasks WHERE Key_name = 'find_next_task'";
  151. $result = db_query($query) or die("Can't show index with query: $query - Error: " . db_error());
  152. if (db_fetch_object($result) === FALSE) {
  153. // migrate
  154. db_query("ALTER TABLE tasks ADD INDEX find_next_task (complete, share(64), id)");
  155. }
  156. $query = "SHOW INDEX FROM tasks WHERE Key_name = 'incomplete_open'";
  157. $result = db_query($query) or die("Can't show index with query: $query - Error: " . db_error());
  158. if (db_fetch_object($result)) {
  159. // migrate
  160. db_query("ALTER TABLE tasks DROP INDEX incomplete_open");
  161. }
  162. $query = "SHOW INDEX FROM tasks WHERE Key_name = 'subsequent_writes'";
  163. $result = db_query($query) or die("Can't show index with query: $query - Error: " . db_error());
  164. if (db_fetch_object($result)) {
  165. // migrate
  166. db_query("ALTER TABLE tasks DROP INDEX subsequent_writes");
  167. }
  168. $query = "SHOW INDEX FROM tasks WHERE Key_name = 'unneeded_unlinks'";
  169. $result = db_query($query) or die("Can't show index with query: $query - Error: " . db_error());
  170. if (db_fetch_object($result)) {
  171. // migrate
  172. db_query("ALTER TABLE tasks DROP INDEX unneeded_unlinks");
  173. }
  174. }
  175. // Migration #5 (fix find_next_task index)
  176. if (@$db_use_mysql) {
  177. $query = "SHOW INDEX FROM tasks WHERE Key_name = 'find_next_task' and Column_name = 'share'";
  178. $result = db_query($query) or die("Can't show index with query: $query - Error: " . db_error());
  179. if (db_fetch_object($result) !== FALSE) {
  180. // migrate
  181. db_query("ALTER TABLE tasks DROP INDEX find_next_task ADD INDEX find_next_task (complete, id)");
  182. }
  183. }
  184. // Migration #6 (new indexes for md5_worker_thread/gh_check_md5 functions)
  185. if (@$db_use_mysql) {
  186. $query = "SHOW INDEX FROM tasks WHERE Key_name = 'md5_worker'";
  187. $result = db_query($query) or die("Can't show index with query: $query - Error: " . db_error());
  188. if (db_fetch_object($result) === FALSE) {
  189. // migrate
  190. db_query("ALTER TABLE tasks ADD INDEX md5_worker (action, complete, additional_info(100), id)");
  191. }
  192. $query = "SHOW INDEX FROM tasks WHERE Key_name = 'md5_checker'";
  193. $result = db_query($query) or die("Can't show index with query: $query - Error: " . db_error());
  194. if (db_fetch_object($result) === FALSE) {
  195. // migrate
  196. db_query("ALTER TABLE tasks ADD INDEX md5_checker (action, share(64), full_path(255), complete)");
  197. }
  198. $query = "DESCRIBE tasks";
  199. $result = db_query($query) or die("Can't describe tasks with query: $query - Error: " . db_error());
  200. while ($row = db_fetch_object($result)) {
  201. if ($row->Field == 'additional_info') {
  202. if ($row->Type == "tinytext") {
  203. // migrate
  204. db_query("ALTER TABLE tasks CHANGE additional_info additional_info TEXT CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL");
  205. }
  206. break;
  207. }
  208. }
  209. }
  210. // Migration #7 (full_path new size: 4096)
  211. if (@$db_use_mysql) {
  212. $query = "DESCRIBE tasks";
  213. $result = db_query($query) or die("Can't describe tasks with query: $query - Error: " . db_error());
  214. while ($row = db_fetch_object($result)) {
  215. if ($row->Field == 'full_path') {
  216. if ($row->Type == "tinytext") {
  217. // migrate
  218. db_query("ALTER TABLE tasks CHANGE full_path full_path TEXT CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL");
  219. db_query("ALTER TABLE tasks_completed CHANGE full_path full_path TEXT CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL");
  220. }
  221. break;
  222. }
  223. }
  224. }
  225. }
  226. ?>