Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PDOLayer.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /* Poweradmin, a friendly web-based admin tool for PowerDNS.
  3. * See <http://www.poweradmin.org> for more details.
  4. *
  5. * Copyright 2007-2009 Rejo Zenger <rejo@zenger.nl>
  6. * Copyright 2010-2017 Poweradmin Development Team
  7. * <http://www.poweradmin.org/credits.html>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * PDO DB access layer
  24. *
  25. * @package Poweradmin
  26. * @copyright 2007-2010 Rejo Zenger <rejo@zenger.nl>
  27. * @copyright 2010-2017 Poweradmin Development Team
  28. * @license http://opensource.org/licenses/GPL-3.0 GPL
  29. */
  30. include_once "PDOCommon.class.php";
  31. /**
  32. * Overrided PEAR class
  33. */
  34. class PEAR {
  35. /**
  36. * Overrided isError method
  37. */
  38. public static function isError() {
  39. }
  40. }
  41. /**
  42. * Fake PDO Extended module
  43. */
  44. class PDOExtended {
  45. /**
  46. * Does several execute() calls on the same statement handle
  47. *
  48. * @link http://pear.php.net/package/MDB2/docs/2.5.0b3/MDB2/MDB2_Extended.html#methodexecuteMultiple
  49. * @param resource $stmt Statement handle
  50. * @param array $params numeric array containing the data to insert into the query
  51. */
  52. public function executeMultiple($stmt, $params) {
  53. foreach ($params as $values) {
  54. $stmt->execute($values);
  55. }
  56. }
  57. }
  58. /**
  59. * PDO access layer
  60. */
  61. class PDOLayer extends PDOCommon {
  62. /**
  63. * Enables/disables debugging
  64. * @var boolean
  65. */
  66. private $debug = false;
  67. /**
  68. * Internal storage for queries
  69. * @var array
  70. */
  71. private $queries = array();
  72. /**
  73. * Quotes a string
  74. *
  75. * @param string $string
  76. * @param string $paramtype
  77. * @return string Returns quoted string
  78. */
  79. public function quote($string, $paramtype = NULL) {
  80. if ($paramtype == 'integer') {
  81. $paramtype = PDO::PARAM_INT;
  82. } elseif ($paramtype == 'text') {
  83. $paramtype = PDO::PARAM_STR;
  84. }
  85. return parent::quote($string, $paramtype);
  86. }
  87. /**
  88. * Set execution options
  89. *
  90. * @param string $option Option name
  91. * @param int $value Option value
  92. */
  93. public function setOption($option, $value) {
  94. if ($option == 'debug' && $value == 1) {
  95. $this->debug = true;
  96. }
  97. }
  98. /**
  99. * Return debug output
  100. *
  101. * @param string Debug output
  102. */
  103. public function getDebugOutput() {
  104. echo join("<br>", $this->queries);
  105. }
  106. /**
  107. * Executes SQL query
  108. *
  109. * @param string $str SQL query
  110. * @return PDOStatement
  111. */
  112. public function query($str) {
  113. if ($this->debug) {
  114. $this->queries[] = $str;
  115. }
  116. return parent::query($str);
  117. }
  118. /**
  119. * Dummy method
  120. */
  121. public function disconnect() {
  122. }
  123. /**
  124. * Load PDO module
  125. *
  126. * @param string $name Module name to load
  127. */
  128. public function loadModule($name) {
  129. if ($name == 'Extended') {
  130. $this->extended = new PDOExtended();
  131. }
  132. }
  133. /**
  134. * List all tables in the current database
  135. *
  136. * @link http://pear.php.net/package/MDB2/docs/2.5.0b3/MDB2/MDB2_Driver_Manager_Common.html#methodlistTables
  137. */
  138. public function listTables() {
  139. // TODO: addapt this function also to pgsql & sqlite
  140. $tables = array();
  141. $db_type = $this->getAttribute(PDO::ATTR_DRIVER_NAME);
  142. $query = '';
  143. if ($db_type == 'mysql') {
  144. $query = 'SHOW TABLES';
  145. } elseif ($db_type == 'pgsql') {
  146. $query = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
  147. } elseif ($db_type == 'sqlite') {
  148. $query = "SELECT name FROM sqlite_master WHERE type='table'";
  149. } else {
  150. die(ERR_DB_UNK_TYPE);
  151. }
  152. $result = $this->query($query);
  153. while ($row = $result->fetch(PDO::FETCH_NUM)) {
  154. $tables[] = $row[0];
  155. }
  156. return $tables;
  157. }
  158. /**
  159. * Create a new table
  160. *
  161. * @link http://pear.php.net/package/MDB2/docs/2.5.0b3/MDB2/MDB2_Driver_Manager_Common.html#methodcreateTable
  162. * @param string $name Name of the table that should be created
  163. * @param mixed[] $fields Associative array that contains the definition of each field of the new table
  164. * @param mixed[] $options An associative array of table options
  165. */
  166. public function createTable($name, $fields, $options = array()) {
  167. $db_type = $this->getAttribute(PDO::ATTR_DRIVER_NAME);
  168. $query_fields = array();
  169. foreach ($fields as $key => $arr) {
  170. if ($arr['type'] == 'text' and isset($arr['length'])) {
  171. $arr['type'] = 'VARCHAR';
  172. }
  173. if ($db_type == 'pgsql' && isset($arr['autoincrement'])) {
  174. $line = $key . ' SERIAL';
  175. } elseif ($db_type == 'pgsql' && $arr['type'] == 'integer') {
  176. $line = $key . ' ' . $arr['type'];
  177. } else {
  178. $line = $key . ' ' . $arr['type'] . (isset($arr['length']) ? '(' . $arr['length'] . ')' : '');
  179. }
  180. if ($arr['notnull'] && $db_type != 'pgsql' && !isset($arr['autoincrement'])) {
  181. $line .= ' NOT NULL';
  182. }
  183. if ($db_type == 'mysql' && isset($arr['autoincrement'])) {
  184. $line .= ' AUTO_INCREMENT';
  185. }
  186. if ($arr['flags'] == 'primary_keynot_null') {
  187. $line .= ' PRIMARY KEY';
  188. }
  189. $query_fields[] = $line;
  190. }
  191. $query = "CREATE TABLE $name (" . implode(', ', $query_fields) . ')';
  192. if ($db_type == 'mysql') {
  193. if (isset($options['type'])) {
  194. $query .= ' ENGINE=' . $options['type'];
  195. }
  196. if (isset($options['charset'])) {
  197. $query .= ' DEFAULT CHARSET=' . $options['charset'];
  198. }
  199. if (isset($options['collation'])) {
  200. $query .= ' COLLATE=' . $options['collation'];
  201. }
  202. }
  203. $this->exec($query);
  204. }
  205. /**
  206. * Drop an existing table
  207. *
  208. * @link http://pear.php.net/package/MDB2/docs/2.5.0b3/MDB2/MDB2_Driver_Manager_Common.html#methoddropTable
  209. * @param string $name name of the table that should be dropped
  210. */
  211. public function dropTable($name) {
  212. $query = "DROP TABLE $name";
  213. $this->exec($query);
  214. }
  215. }