SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/ExportDBMS_SQLite.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. class ExportDBMS_SQLite
  17. {
  18.  
  19. public function hdr()
  20. {
  21. return "--\n"
  22. . "-- Sprout3 Database Dump\n"
  23. . "-- Export date: " . date('Y-m-d H:i:s') . "\n"
  24. . "-- Export format: SQLite\n"
  25. . "--\n";
  26. }
  27.  
  28.  
  29. /**
  30.   * Return a query to drop this table
  31.   **/
  32. public function drop($table_def)
  33. {
  34. return "DROP TABLE IF EXISTS `{$table_def->name}`;\n";
  35. }
  36.  
  37.  
  38. /**
  39.   * Return a query to create the table
  40.   **/
  41. public function structure($table_def)
  42. {
  43. $q = "SHOW COLUMNS FROM `{$table_def->name}`";
  44. $res = Pdb::query($q, [], 'arr-num');
  45.  
  46. // Build the column bits
  47. $autoinc = false;
  48. $cols = array();
  49. foreach ($res as $row) {
  50. $c = $row[0] . ' ';
  51.  
  52. // SQLite is quite stict about autoinc types
  53. if ($row[5] == 'auto_increment') {
  54. $c .= 'INTEGER PRIMARY KEY AUTOINCREMENT';
  55. $cols[] = $c;
  56. $autoinc = true;
  57. continue;
  58. }
  59.  
  60. $c .= $this->mapDatatype($row[1]);
  61.  
  62. if ($row[2] == 'NO') {
  63. $c .= ' NOT NULL';
  64. }
  65.  
  66. if ($row[4]) {
  67. $c .= ' DEFAULT ' . "'" . str_replace("'", "''", $row[4]) . "'";
  68. }
  69.  
  70. $cols[] = $c;
  71. }
  72.  
  73. // If it's not an autoinc, we need to manually specify the PK
  74. if (! $autoinc) {
  75. $pk_cols = array();
  76. foreach ($res as $row) {
  77. if ($row[3] == 'PRI') $pk_cols[] = $row[0];
  78. }
  79.  
  80. $cols[] = 'PRIMARY KEY(' . implode(',', $pk_cols) . ')';
  81. }
  82.  
  83. // Join up the create table statement
  84. $sql = "CREATE TABLE {$table_def->name} (\n\t" . implode(",\n\t", $cols) . "\n);\n";
  85.  
  86.  
  87. // Grab the indexes for the 'create index' clauses
  88. $q = "SHOW INDEX FROM `{$table_def->name}`";
  89. $res = Pdb::query($q, [], 'arr-num');
  90.  
  91. // Iterate indexes and build a temp array
  92. $indexes = array();
  93. foreach ($res as $row) {
  94. if ($row[2] == 'PRIMARY') continue;
  95.  
  96. $indexes[$row[2]]['type'] = ($row[1] == 1 ? 'INDEX' : 'UNIQUE INDEX');
  97. $indexes[$row[2]]['cols'][] = $row[4];
  98. }
  99.  
  100. // Create index SQL statements
  101. foreach ($indexes as $name => $def) {
  102. $sql .= 'CREATE ' . $def['type'] . ' ' . $table_def->name . '_' . $name . ' ON ' . $table_def->name;
  103. $sql .= '(' . implode(',', $def['cols']) . ");\n";
  104. }
  105.  
  106. return $sql;
  107.  
  108. }
  109.  
  110.  
  111. /**
  112.   * Map a MySQL data type to a SQLite data type
  113.   **/
  114. private function mapDatatype($mysql_type)
  115. {
  116. $mysql_type = strtolower($mysql_type);
  117.  
  118. if (preg_match('/(int|bit|bool)/', $mysql_type)) return 'INTEGER';
  119. if (preg_match('/(varchar|char|text|enum|set)/', $mysql_type)) return 'TEXT';
  120. if (preg_match('/(float|double|decimal|dec)/', $mysql_type)) return 'REAL';
  121. if (preg_match('/(date|time|year)/', $mysql_type)) return 'TEXT';
  122. if (preg_match('/(blob|binary)/', $mysql_type)) return 'BLOB';
  123.  
  124. return 'TEXT';
  125. }
  126.  
  127.  
  128. /**
  129.   * Create an INSERT query
  130.   **/
  131. public function insert($table_def, $row)
  132. {
  133. $str = "INSERT INTO `{$table_def->name}` (";
  134. $j = 0;
  135. foreach ($row as $key => $val) {
  136. if ($j++ > 0) $str .= ',';
  137. $str .= $key;
  138. }
  139. $str .= ") VALUES (";
  140. $j = 0;
  141. foreach ($row as $key => $val) {
  142. if ($j++ > 0) $str .= ',';
  143. if ($val === null) {
  144. $str .= 'NULL';
  145. } else {
  146. $str .= "'" . str_replace("'", "''", $val) . "'";
  147. }
  148. }
  149. $str .= ");\n";
  150.  
  151. return $str;
  152. }
  153.  
  154.  
  155. /**
  156.   * Create an UPDATE query
  157.   **/
  158. public function update($table_def, $pk_names, $row)
  159. {
  160. $pk = array();
  161. foreach ($pk_names as $col) {
  162. $pk[$col] = $row[$col];
  163. }
  164.  
  165. $row = array_diff_key($row, $pk);
  166.  
  167. $str = "UPDATE `{$table_def->name}` SET ";
  168. $str .= $this->createKvpString($row);
  169. $str .= " WHERE ";
  170. $str .= $this->createKvpString($pk, ' AND ');
  171. $str .= ";\n";
  172.  
  173. return $str;
  174. }
  175.  
  176.  
  177. /**
  178.   * Create an INSERT...UPDATE query
  179.   **/
  180. public function insertUpdate($table_def, $pk_names, $row)
  181. {
  182. $row_no_pk = $row;
  183. foreach ($pk_names as $name) {
  184. unset ($row_no_pk[$name]);
  185. }
  186.  
  187. if (count($row_no_pk) == 0) {
  188. $str = "INSERT IGNORE INTO `{$table_def->name}` SET ";
  189. $str .= $this->createKvpString($row);
  190. $str .= ";\n";
  191. } else {
  192. $str = "INSERT INTO `{$table_def->name}` SET ";
  193. $str .= $this->createKvpString($row);
  194. $str .= " ON DUPLICATE KEY UPDATE ";
  195. $str .= $this->createKvpString($row_no_pk);
  196. $str .= ";\n";
  197. }
  198.  
  199. return $str;
  200. }
  201.  
  202.  
  203. /**
  204.   * Creates a key-value-pair string for use in a sql query
  205.   **/
  206. private function createKvpString($row, $sep = ', ')
  207. {
  208. $str = '';
  209.  
  210. $j = 0;
  211. foreach ($row as $key => $val) {
  212. if ($j++ > 0) $str .= $sep;
  213. if ($val === null) {
  214. $val = 'NULL';
  215. } else {
  216. $val = "'" . str_replace("'", "''", $val) . "'";
  217. }
  218. $str .= "`{$key}` = " . $val;
  219. }
  220.  
  221. return $str;
  222. }
  223.  
  224. }
  225.  
  226.