/environment/library/database/adapters/MysqlDBAdapter.class.php

https://gitlab.com/x33n/ProjectPier-Core · PHP · 335 lines · 145 code · 40 blank · 150 comment · 17 complexity · 226ed20d9d9bc7f78b964804b6c1a9d5 MD5 · raw file

  1. <?php
  2. /**
  3. * DB access for MySQL
  4. *
  5. * @version 1.0
  6. * @http://www.projectpier.org/
  7. */
  8. class MysqlDBAdapter extends AbstractDBAdapter {
  9. /**
  10. * Connect to the database based on the params array
  11. *
  12. * @access protected
  13. * @param array $params
  14. * @return null
  15. * @throws DBConnectError
  16. */
  17. protected function connect($params) {
  18. $host = array_var($params, 'host', '');
  19. $user = array_var($params, 'user', '');
  20. $pass = array_var($params, 'pass', '');
  21. $database = array_var($params, 'name', '');
  22. $persist = array_var($params, 'persist', false);
  23. $link = $persist ?
  24. @mysql_pconnect($host, $user, $pass) :
  25. @mysql_connect($host, $user, $pass);
  26. if (!is_resource($link)) {
  27. throw new DBConnectError($host, $user, $pass, $database);
  28. } // if
  29. if (!@mysql_select_db($database, $link)) {
  30. throw new DBConnectError($host, $user, $pass, $database);
  31. } // if
  32. $this->setLink($link);
  33. $this->setParams($params);
  34. $this->setDatabaseName($database);
  35. return true;
  36. } // connect
  37. /**
  38. * Basic query execution
  39. *
  40. * @access protected
  41. * @param string $sql
  42. * @return mixed
  43. */
  44. protected function executeQuery($sql) {
  45. return @mysql_query($sql, $this->link);
  46. } // executeQuery
  47. /**
  48. * Get begin work SQL (start transaction)
  49. *
  50. * @access public
  51. * @param void
  52. * @return string
  53. */
  54. function getBeginWorkCommand() {
  55. return 'BEGIN WORK';
  56. } // getBeginWorkCommand
  57. /**
  58. * Get comming SQL
  59. *
  60. * @access public
  61. * @param void
  62. * @return string
  63. */
  64. function getCommitCommand() {
  65. return 'COMMIT';
  66. } // getCommitCommand
  67. /**
  68. * Get rollback SQL
  69. *
  70. * @access public
  71. * @param void
  72. * @return string
  73. */
  74. function getRollbackCommand() {
  75. return 'ROLLBACK';
  76. } // getRollbackCommand
  77. /**
  78. * Return number of affected rows
  79. *
  80. * @access public
  81. * @param void
  82. * @return integer
  83. */
  84. function affectedRows() {
  85. return mysql_affected_rows($this->link);
  86. } // affectedRows
  87. /**
  88. * Return last insert ID
  89. *
  90. * @access public
  91. * @param void
  92. * @return integer
  93. */
  94. function lastInsertId() {
  95. return mysql_insert_id($this->link);
  96. } // lastInsertId
  97. /**
  98. * Returns last error message that server thrown
  99. *
  100. * @access public
  101. * @param void
  102. * @return string
  103. */
  104. function lastError() {
  105. return mysql_error($this->link);
  106. } // lastError
  107. /**
  108. * Returns code of the last error
  109. *
  110. * @access public
  111. * @param void
  112. * @return integer
  113. */
  114. function lastErrorCode() {
  115. return mysql_errno($this->link);
  116. } // lastErrorCode
  117. /**
  118. * Return array of tables that exists in database. This function will return NULL if there are
  119. * no tables in database
  120. *
  121. * @access public
  122. * @param void
  123. * @return array
  124. */
  125. function listTables() {
  126. $extracted_table_names = $this->executeAll('SHOW TABLES');
  127. $table_names = array();
  128. if (count($extracted_table_names)) {
  129. foreach ($extracted_table_names as $extracted_table_name) {
  130. $table_names[] = array_var($extracted_table_name, 'Tables_in_' . $this->getDatabaseName());
  131. } // foreach
  132. } // if
  133. return count($table_names) ? $table_names : null;
  134. } // listTables
  135. /**
  136. * Drop one or more tables. If $table_names is string only that table will be droped, else script will drop
  137. *
  138. * @access public
  139. * @param mixed $table_names Array of table names or single table name
  140. * @return boolean
  141. */
  142. function dropTables($table_names) {
  143. if (empty($table_names)) {
  144. return true;
  145. } // if
  146. if (!is_array($table_names)) {
  147. $table_names = array($table_names);
  148. } // if
  149. $escaped_table_names = array();
  150. foreach ($table_names as $table_name) {
  151. $escaped_table_names[] = $this->escapeField($table_name);
  152. }
  153. return count($escaped_table_names) ?
  154. $this->execute('DROP TABLE ' . implode(', ', $escaped_table_names)) :
  155. true;
  156. } // dropTables
  157. /**
  158. * Remove all data from specific tables
  159. *
  160. * @access public
  161. * @param mixed $table_names Single table name or array of table names
  162. * @return boolean
  163. */
  164. function emptyTables($table_names) {
  165. if (empty($table_names)) {
  166. return true;
  167. } // if
  168. if (!is_array($table_names)) {
  169. $table_names = array($table_names);
  170. } // if
  171. foreach ($table_names as $table_name) {
  172. $this->execute('TRUNCATE ' . $this->escapeField($table_name));
  173. } // foreach
  174. return true;
  175. } // emptyTables
  176. /**
  177. * This function will return array of table names and their CREATE TABLE commands
  178. *
  179. * @access public
  180. * @param void
  181. * @return array or NULL if there are no tables in database
  182. */
  183. function exportDatabaseStructure() {
  184. $tables = $this->listTables();
  185. if (!is_array($tables) || !count($tables)) {
  186. return null;
  187. } // if
  188. $create_commands = array();
  189. foreach ($tables as $table) {
  190. $create_command = $this->exportTableStructure($table);
  191. if (trim($create_command) <> '') {
  192. $create_commands[$table] = $create_command;
  193. } // if
  194. } // foreach
  195. return count($create_commands) ? $create_commands : null;
  196. } // exportDatabaseStructure
  197. /**
  198. * This function is able to import database construction from any connected adapter
  199. *
  200. * @access public
  201. * @param AbstractDBAdapter $adapter
  202. * @param boolean $clear Clean up the database before execution
  203. * @return boolean
  204. */
  205. function importDatabaseStructure(AbstractDBAdapter $adapter, $clear = false) {
  206. if ($clear) {
  207. $this->clearDatabase();
  208. } // if
  209. $structure = $adapter->exportDatabaseStructure();
  210. if (is_array($structure)) {
  211. foreach ($structure as $table_name => $table_construction) {
  212. $this->execute($table_construction);
  213. } // foreach
  214. } // if
  215. } // importDatabaseStructure
  216. /**
  217. * Return CREATE TABLE sql for specific table
  218. *
  219. * @access public
  220. * @param string $table_name
  221. * @return string or NULL if table does not exists
  222. */
  223. function exportTableStructure($table_name) {
  224. $result = $this->executeOne('SHOW CREATE TABLE ' . $this->escapeField($table_name));
  225. return array_var($result, 'Create Table');
  226. } // exportTableStructure
  227. /**
  228. * Escape name of table field or name of the table
  229. *
  230. * @access public
  231. * @param string $field
  232. * @return string
  233. */
  234. function escapeField($field) {
  235. return '`' . str_replace('`', '``', trim($field)) . '`';
  236. } // escapeField
  237. /**
  238. * Escape value before use it in query. This function makes difference between NULL, scalar
  239. * and DateTime values
  240. *
  241. * @access public
  242. * @param mixed $unescaped Value that need to be escaped
  243. * @return string
  244. */
  245. function escapeValue($unescaped) {
  246. if (is_null($unescaped)) {
  247. return 'NULL';
  248. } // if
  249. if (is_bool($unescaped)) {
  250. return $unescaped ? "'1'" : "'0'";
  251. } // if
  252. if (is_array($unescaped)) {
  253. $escaped_array = array();
  254. foreach ($unescaped as $unescaped_value) {
  255. $escaped_array[] = self::escapeValue($unescaped_value);
  256. }
  257. return implode(', ', $escaped_array);
  258. } // if
  259. if (is_object($unescaped) && ($unescaped instanceof DateTimeValue)) {
  260. return "TIMESTAMP '" . mysql_real_escape_string($unescaped->toMySQL()) . "'";
  261. } // if
  262. return "'" . mysql_real_escape_string($unescaped, $this->link) . "'";
  263. } // escapeValue
  264. /**
  265. * Fetch row from query result
  266. *
  267. * @access public
  268. * @param resource $resource
  269. * @return array
  270. */
  271. function fetchRow($resource) {
  272. return mysql_fetch_assoc($resource);
  273. } // fetchRow
  274. /**
  275. * Return number of rows in specific query result
  276. *
  277. * @access public
  278. * @param resource $resource
  279. * @return integer
  280. */
  281. function numRows($resource) {
  282. return mysql_num_rows($resource);
  283. } // numRows
  284. /**
  285. * Free database result
  286. *
  287. * @access public
  288. * @param resource $resource
  289. * @return boolean
  290. */
  291. function freeResult($resource) {
  292. return mysql_free_result($resource);
  293. } // freeResult
  294. } // MysqlDBAdapter
  295. ?>