/private/registry/aspects/database/mysql.database.class.php

https://github.com/mkpeacock/Talk-Rating--twilio- · PHP · 273 lines · 154 code · 33 blank · 86 comment · 14 complexity · c95c679a8ee32655e7d2d704362e2b9c MD5 · raw file

  1. <?php
  2. /**
  3. * Ickle MVC MySQL Database Library
  4. *
  5. * @author Michael Peacock
  6. * @copyright Ickle MVC Project
  7. */
  8. class MySQLDatabase extends Database {
  9. /**
  10. * Construct our database object
  11. */
  12. public function __construct( IckleRegistry $registry )
  13. {
  14. $this->registry = $registry;
  15. }
  16. /**
  17. * Create a new database connection
  18. * @param String database hostname
  19. * @param String database username
  20. * @param String database password
  21. * @param String database we are using
  22. * @return int the id of the new connection
  23. */
  24. public function newConnection( $host, $user, $password, $database=null )
  25. {
  26. if( is_null( $database ) )
  27. {
  28. $this->connections[] = new mysqli( $host, $user, $password );
  29. }
  30. else
  31. {
  32. $this->connections[] = new mysqli( $host, $user, $password, $database );
  33. }
  34. $connection_id = count( $this->connections )-1;
  35. if( mysqli_connect_errno() )
  36. {
  37. trigger_error('Error connecting to host. '.$this->connections[$connection_id]->error, E_USER_ERROR);
  38. }
  39. return $connection_id;
  40. }
  41. public function selectDatabase( $db )
  42. {
  43. $this->connections[ $this->activeConnection ]->select_db( $db );
  44. }
  45. /**
  46. * Execute an SQL statement
  47. * @param String $sql
  48. * @return void
  49. */
  50. public function executeQuery( $sql )
  51. {
  52. if( ! $result = $this->connections[$this->activeConnection]->query( $sql ) )
  53. {
  54. trigger_error('Error executing query: ' . $sql .' - '.$this->connections[$this->activeConnection]->error, E_USER_ERROR);
  55. }
  56. else
  57. {
  58. $this->previouslyExecuted = $result;
  59. }
  60. }
  61. public function executeMultipleQueries( $sql )
  62. {
  63. if( $this->connections[ $this->activeConnection ]->multi_query( $sql ) )
  64. {
  65. do{
  66. // nothing...
  67. } while( $this->connections[ $this->activeConnection ]->next_result() );
  68. }
  69. }
  70. /**
  71. * Get the number of rows returned from the previously executed query
  72. * @return int
  73. */
  74. public function getNumRows()
  75. {
  76. return $this->previouslyExecuted->num_rows;
  77. }
  78. /**
  79. * Get the rows from the most recently executed query, excluding cached queries
  80. * @return array
  81. */
  82. public function getRows()
  83. {
  84. return $this->previouslyExecuted->fetch_array(MYSQLI_ASSOC);
  85. }
  86. /**
  87. * Gets the number of affected rows from the previous query
  88. * @return int the number of affected rows
  89. */
  90. public function getNumAffectedRows()
  91. {
  92. return $this->connections[$this->activeConnection]->affected_rows;
  93. }
  94. /**
  95. * Get the ID of the last inserted record
  96. * @return int
  97. */
  98. public function getLastInsertID()
  99. {
  100. return $this->connections[ $this->activeConnection]->insert_id;
  101. }
  102. public function insertRecord( $table, $record )
  103. {
  104. }
  105. /**
  106. * Insert records into the database
  107. * @param String the database table
  108. * @param array data to insert field => value
  109. * @return bool
  110. */
  111. public function insertRecords( $table, $data )
  112. {
  113. // setup some variables for fields and values
  114. $fields = "";
  115. $values = "";
  116. // populate them
  117. foreach ($data as $f => $v)
  118. {
  119. $fields .= "`$f`,";
  120. $values .= ( is_numeric( $v ) && ( intval( $v ) === $v ) ) ? $v."," : "'$v',";
  121. }
  122. // remove our trailing ,
  123. $fields = substr($fields, 0, -1);
  124. // remove our trailing ,
  125. $values = substr($values, 0, -1);
  126. $insert = "INSERT INTO $table ({$fields}) VALUES({$values})";
  127. //echo $insert;
  128. $this->executeQuery( $insert );
  129. return true;
  130. }
  131. /**
  132. * Update records in the database
  133. * @param String the table
  134. * @param array of changes field => value
  135. * @param String the condition
  136. * @return bool
  137. */
  138. public function updateRecords( $table, $changes, $condition )
  139. {
  140. $update = "UPDATE " . $table . " SET ";
  141. foreach( $changes as $field => $value )
  142. {
  143. if( $value == NULL )
  144. {
  145. $update .= "`" . $field . "`=null,";
  146. }
  147. else
  148. {
  149. $update .= "`" . $field . "`='{$value}',";
  150. }
  151. }
  152. // remove our trailing ,
  153. $update = substr($update, 0, -1);
  154. if( $condition != '' )
  155. {
  156. $update .= " WHERE " . $condition;
  157. }
  158. $this->executeQuery( $update );
  159. //echo $update;
  160. return true;
  161. }
  162. public function updateRecordsMulti( $table, $records, $conditions )
  163. {
  164. }
  165. public function deleteRecords( $table, $condition, $limit )
  166. {
  167. }
  168. /**
  169. * Store a query in the query cache for processing later
  170. * @param String the query string
  171. * @return the pointed to the query in the cache
  172. */
  173. public function cacheQuery( $queryStr )
  174. {
  175. if( !$result = $this->connections[$this->activeConnection]->query( $queryStr ) )
  176. {
  177. trigger_error('Error executing and caching query: '.$this->connections[$this->activeConnection]->error, E_USER_ERROR);
  178. return -1;
  179. }
  180. else
  181. {
  182. $this->queryCache[] = $result;
  183. return count($this->queryCache)-1;
  184. }
  185. }
  186. /**
  187. * Get the number of rows from the cache
  188. * @param int the query cache pointer
  189. * @return int the number of rows
  190. */
  191. public function getNumRowsFromCache( $cache_id )
  192. {
  193. return $this->queryCache[$cache_id]->num_rows;
  194. }
  195. /**
  196. * Get the rows from a cached query
  197. * @param int the query cache pointer
  198. * @return array the row
  199. */
  200. public function getResultsFromCache( $cache_id )
  201. {
  202. return $this->queryCache[$cache_id]->fetch_array(MYSQLI_ASSOC);
  203. }
  204. /**
  205. * Sanitize data
  206. * @param String the data to be sanitized
  207. * @return String the sanitized data
  208. */
  209. public function sanitizeData( $value )
  210. {
  211. // Stripslashes
  212. if ( get_magic_quotes_gpc() )
  213. {
  214. $value = stripslashes ( $value );
  215. }
  216. // Quote value
  217. if ( version_compare( phpversion(), "4.3.0" ) == "-1" )
  218. {
  219. $value = $this->connections[$this->activeConnection]->escape_string( $value );
  220. }
  221. else
  222. {
  223. $value = $this->connections[$this->activeConnection]->real_escape_string( $value );
  224. }
  225. return $value;
  226. }
  227. /**
  228. * Destructor
  229. * Closes all MySQL connections
  230. * @return void
  231. */
  232. public function __destruct()
  233. {
  234. foreach( $this->connections as $connection )
  235. {
  236. $connection->close();
  237. }
  238. }
  239. }
  240. ?>