C_CustomTable_DataMapper_Driver_Mixin

Provides instance methods for C_CustomTable_DataMapper_Driver.

Defined (1)

The class is defined in the following location(s).

/products/photocrati_nextgen/modules/datamapper/package.module.datamapper.php  
  1. class C_CustomTable_DataMapper_Driver_Mixin extends Mixin 
  2. /** 
  3. * Gets the name of the primary key column 
  4. * @return string 
  5. */ 
  6. function get_primary_key_column() 
  7. return $this->object->_primary_key_column; 
  8. /** 
  9. * Selects which fields to collect from the table. 
  10. * NOTE: Not protected from SQL injection - DO NOT let your users 
  11. * specify DB columns 
  12. * @param string $fields 
  13. */ 
  14. function select($fields = NULL) 
  15. // Create a fresh slate 
  16. $this->object->_init(); 
  17. if (!$fields or $fields == '*') { 
  18. $fields = $this->get_table_name() . '.*'; 
  19. $this->object->_select_clause = "SELECT {$fields}"; 
  20. return $this->object; 
  21. /** 
  22. * Determines whether we're going to execute a SELECT statement 
  23. * @return boolean 
  24. */ 
  25. function is_select_statement() 
  26. return $this->object->_select_clause ? TRUE : FALSE; 
  27. /** 
  28. * Determines if we're going to be executing a DELETE statement 
  29. * @return type 
  30. */ 
  31. function is_delete_statement() 
  32. return $this->object->_delete_clause ? TRUE : FALSE; 
  33. /** 
  34. * Start a delete statement 
  35. */ 
  36. function delete() 
  37. // Create a fresh slate 
  38. $this->object->_init(); 
  39. $this->object->_delete_clause = "DELETE"; 
  40. return $this->object; 
  41. /** 
  42. * Orders the results of the query 
  43. * This method may be used multiple of times to order by more than column 
  44. * @param $order_by 
  45. * @param $direction 
  46. */ 
  47. function order_by($order_by, $direction = 'ASC') 
  48. // We treat the rand() function as an exception 
  49. if (preg_match("/rand\\(\\s*\\)/", $order_by)) { 
  50. $order = 'rand()'; 
  51. } else { 
  52. $order_by = $this->object->_clean_column($order_by); 
  53. // If the order by clause is a column, then it should be backticked 
  54. if ($this->object->has_column($order_by)) { 
  55. $order_by = "`{$order_by}`"; 
  56. $direction = $this->object->_clean_column($direction); 
  57. $order = "{$order_by} {$direction}"; 
  58. $this->object->_order_clauses[] = $order; 
  59. return $this->object; 
  60. /** 
  61. * Specifies a limit and optional offset 
  62. * @param integer $max 
  63. * @param integer $offset 
  64. */ 
  65. function limit($max, $offset = 0) 
  66. if ($offset) { 
  67. $limit = $this->_wpdb()->prepare("LIMIT %d, %d", max(0, $offset), $max); 
  68. } else { 
  69. $limit = $this->_wpdb()->prepare("LIMIT %d", max(0, $max)); 
  70. if ($limit) { 
  71. $this->object->_limit_clause = $limit; 
  72. return $this->object; 
  73. /** 
  74. * Specifics a group by clause for one or more columns 
  75. * @param array|string $columns 
  76. */ 
  77. function group_by($columns = array()) 
  78. if (!is_array($columns)) { 
  79. $columns = array($columns); 
  80. $this->object->_group_by_columns = array_merge($this->object->_group_by_columns, $columns); 
  81. return $this->object; 
  82. /** 
  83. * Adds a where clause to the driver 
  84. * @param array $where_clauses 
  85. * @param string $join 
  86. */ 
  87. function _add_where_clause($where_clauses, $join) 
  88. $clauses = array(); 
  89. foreach ($where_clauses as $clause) { 
  90. extract($clause); 
  91. if ($this->object->has_column($column)) { 
  92. $column = "`{$column}`"; 
  93. if (!is_array($value)) { 
  94. $value = array($value); 
  95. foreach ($value as $index => $v) { 
  96. $v = $clause['type'] == 'numeric' ? $v : "'{$v}'"; 
  97. $value[$index] = $v; 
  98. if ($compare == 'BETWEEN') { 
  99. $value = "{$value[0]} AND {$value[1]}"; 
  100. } else { 
  101. $value = implode(', ', $value); 
  102. if (strpos($compare, 'IN') !== FALSE) { 
  103. $value = "({$value})"; 
  104. $clauses[] = "{$column} {$compare} {$value}"; 
  105. $this->object->_where_clauses[] = implode(" {$join} ", $clauses); 
  106. /** 
  107. * Returns the total number of entities known 
  108. * @return type 
  109. */ 
  110. function count() 
  111. $retval = 0; 
  112. $key = $this->object->get_primary_key_column(); 
  113. $results = $this->object->run_query("SELECT COUNT(`{$key}`) AS `{$key}` FROM `{$this->object->get_table_name()}`"); 
  114. if ($results && isset($results[0]->{$key})) { 
  115. $retval = (int) $results[0]->{$key}; 
  116. return $retval; 
  117. /** 
  118. * Returns the generated SQL query to be executed 
  119. * @return string 
  120. */ 
  121. function get_generated_query($no_entities = FALSE) 
  122. $sql = array(); 
  123. if ($this->object->is_select_statement()) { 
  124. $sql[] = $this->object->_select_clause; 
  125. } elseif ($this->object->is_delete_statement()) { 
  126. $sql[] = $this->object->_delete_clause; 
  127. $sql[] = 'FROM `' . $this->object->get_table_name() . '`'; 
  128. $where_clauses = array(); 
  129. foreach ($this->object->_where_clauses as $where) { 
  130. $where_clauses[] = '(' . $where . ')'; 
  131. if ($where_clauses) { 
  132. $sql[] = 'WHERE ' . implode(' AND ', $where_clauses); 
  133. if ($this->object->is_select_statement()) { 
  134. if ($this->object->_group_by_columns) { 
  135. $sql[] = 'GROUP BY ' . implode(', ', $this->object->_group_by_columns); 
  136. if ($this->object->_order_clauses) { 
  137. $sql[] = 'ORDER BY ' . implode(', ', $this->object->_order_clauses); 
  138. if ($this->object->_limit_clause) { 
  139. $sql[] = $this->object->_limit_clause; 
  140. return implode(' ', $sql); 
  141. /** 
  142. * Run the query 
  143. * @param $sql optionally run the specified SQL insteads 
  144. * return 
  145. */ 
  146. function run_query($sql = FALSE, $model = FALSE, $no_entities = FALSE) 
  147. $results = FALSE; 
  148. $retval = array(); 
  149. // Or generate SQL query 
  150. if (!$sql) { 
  151. $sql = $this->object->get_generated_query($no_entities); 
  152. // If we have a SQL statement to execute, then heck, execute it! 
  153. if ($sql) { 
  154. if ($this->object->debug) { 
  155. var_dump($sql); 
  156. // Try getting the result from cache first 
  157. if ($this->is_select_statement() && $this->object->_use_cache) { 
  158. $results = $this->object->get_from_cache($sql); 
  159. if (!$results) { 
  160. $this->_wpdb()->query($sql); 
  161. $results = $this->_wpdb()->last_result; 
  162. if ($this->is_select_statement()) { 
  163. $this->object->cache($sql, $results); 
  164. if ($results) { 
  165. $retval = array(); 
  166. // For each row, create an entity, update it's properties, and add it to the result set 
  167. if ($no_entities) { 
  168. $retval = $results; 
  169. } else { 
  170. $id_field = $this->get_primary_key_column(); 
  171. foreach ($results as $row) { 
  172. if ($row) { 
  173. if (isset($row->{$id_field})) { 
  174. if ($model) { 
  175. $retval[] = $this->object->convert_to_model($row); 
  176. } else { 
  177. $retval[] = $this->object->_convert_to_entity($row); 
  178. } elseif ($this->object->debug) { 
  179. var_dump("No entities returned from query"); 
  180. // Just a safety check 
  181. if (!$retval) { 
  182. $retval = array(); 
  183. return $retval; 
  184. /** 
  185. * Stores the entity 
  186. * @param stdClass $entity 
  187. */ 
  188. function _save_entity($entity) 
  189. $retval = FALSE; 
  190. unset($entity->id_field); 
  191. $primary_key = $this->object->get_primary_key_column(); 
  192. if (isset($entity->{$primary_key}) && $entity->{$primary_key} > 0) { 
  193. if ($this->object->_update($entity)) { 
  194. $retval = intval($entity->{$primary_key}); 
  195. } else { 
  196. $retval = $this->object->_create($entity); 
  197. if ($retval) { 
  198. $new_entity = $this->object->find($retval); 
  199. foreach ($new_entity as $key => $value) { 
  200. $entity->{$key} = $value; 
  201. $entity->id_field = $primary_key; 
  202. // Clean cache 
  203. if ($retval) { 
  204. $this->object->_cache = array(); 
  205. return $retval; 
  206. /** 
  207. * Converts an entity to something suitable for inserting into 
  208. * a database column 
  209. * @param stdObject $entity 
  210. * @return array 
  211. */ 
  212. function _convert_to_table_data($entity) 
  213. $data = (array) $entity; 
  214. foreach ($data as $key => $value) { 
  215. if (is_array($value)) { 
  216. $data[$key] = $this->object->serialize($value); 
  217. return $data; 
  218. /** 
  219. * Destroys/deletes an entity 
  220. * @param stdObject|C_DataMapper_Model|int $entity 
  221. * @return boolean 
  222. */ 
  223. function destroy($entity) 
  224. $retval = FALSE; 
  225. $key = $this->object->get_primary_key_column(); 
  226. // Find the id of the entity 
  227. if (is_object($entity) && isset($entity->{$key})) { 
  228. $id = (int) $entity->{$key}; 
  229. } else { 
  230. $id = (int) $entity; 
  231. // If we have an ID, then delete the post 
  232. if (is_numeric($id)) { 
  233. $sql = $this->object->_wpdb()->prepare("DELETE FROM `{$this->object->get_table_name()}` WHERE {$key} = %s", $id); 
  234. $retval = $this->object->_wpdb()->query($sql); 
  235. return $retval; 
  236. /** 
  237. * Creates a new record in the database 
  238. * @param stdObject $entity 
  239. * @return boolean 
  240. */ 
  241. function _create($entity) 
  242. $retval = FALSE; 
  243. $id = $this->object->_wpdb()->insert($this->object->get_table_name(), $this->object->_convert_to_table_data($entity)); 
  244. if ($id) { 
  245. $key = $this->object->get_primary_key_column(); 
  246. $retval = $entity->{$key} = intval($this->object->_wpdb()->insert_id); 
  247. return $retval; 
  248. /** 
  249. * Updates a record in the database 
  250. * @param stdObject $entity 
  251. */ 
  252. function _update($entity) 
  253. $key = $this->object->get_primary_key_column(); 
  254. return $this->object->_wpdb()->update($this->object->get_table_name(), $this->object->_convert_to_table_data($entity), array($key => $entity->{$key})); 
  255. /** 
  256. * Fetches the last row 
  257. * @param array $conditions 
  258. * @return C_DataMapper_Entity 
  259. */ 
  260. function find_last($conditions = array(), $model = FALSE) 
  261. $retval = NULL; 
  262. // Get row number for the last row 
  263. $this->select()->limit(1)->order_by('date', 'DESC'); 
  264. if ($conditions) { 
  265. $this->where_and($conditions); 
  266. $results = $this->run_query(); 
  267. if ($results) { 
  268. $retval = $model ? $this->object->convert_to_model($results[0]) : $results[0]; 
  269. return $retval; 
  270. function _add_column($column_name, $datatype, $default_value = NULL) 
  271. $sql = "ALTER TABLE `{$this->get_table_name()}` ADD COLUMN `{$column_name}` {$datatype}"; 
  272. if ($default_value) { 
  273. if (is_string($default_value)) { 
  274. $default_value = str_replace("'", "\\'", $default_value); 
  275. $sql .= " NOT NULL DEFAULT " . (is_string($default_value) ? "'{$default_value}" : "{$default_value}"); 
  276. $return = $this->object->_wpdb()->query($sql) ? TRUE : FALSE; 
  277. $this->object->update_columns_cache(); 
  278. return $return; 
  279. function _remove_column($column_name) 
  280. $sql = "ALTER TABLE `{$this->get_table_name()}` DROP COLUMN `{$column_name}`"; 
  281. $return = $this->object->_wpdb()->query($sql) ? TRUE : FALSE; 
  282. $this->object->update_columns_cache(); 
  283. return $return; 
  284. /** 
  285. * Migrates the schema of the database 
  286. */ 
  287. function migrate() 
  288. if (!$this->object->_columns) { 
  289. throw new E_ColumnsNotDefinedException("Columns not defined for {$this->get_table_name()}"); 
  290. $added = FALSE; 
  291. $removed = FALSE; 
  292. // Add any missing columns 
  293. foreach ($this->object->_columns as $key => $properties) { 
  294. if (!in_array($key, $this->object->_table_columns)) { 
  295. if ($this->object->_add_column($key, $properties['type'], $properties['default_value'])) { 
  296. $added = TRUE; 
  297. // Remove any columns not defined 
  298. // foreach ($this->object->_table_columns as $key) { 
  299. // if (!isset($this->object->_columns[$key])) { 
  300. // $this->object->_remove_column($key); 
  301. // $removed = TRUE; 
  302. // } 
  303. // } 
  304. if ($added or $removed) { 
  305. // var_dump($this->object->_table_columns); 
  306. $this->object->lookup_columns(); 
  307. // var_dump($added, $removed); 
  308. function _init() 
  309. $this->object->_where_clauses = array(); 
  310. $this->object->_order_clauses = array(); 
  311. $this->object->_group_by_columns = array(); 
  312. $this->object->_limit_clause = ''; 
  313. $this->object->_select_clause = '';