| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @file 4 * Definition of DataHandler class. 5 */ 6 7 /** 8 * Simple access methods to table data. Can be used on any table, not just Data 9 * managed tables. 10 */ 11 class DataHandler { 12 13 // Holds the name of the table that this handler is responsible for. 14 protected $table; 15 16 /** 17 * Constructor, call indirectly through DataHandler::instance(); 18 */ 19 protected function __construct($table) { 20 $this->table = $table; 21 } 22 23 /** 24 * Instantiate a DataHandler object. 25 * 26 * @param $table 27 * The name of the table to access with this DataHandler object. 28 */ 29 public static function instance($table) { 30 static $handlers; 31 if (!isset($handlers[$table])) { 32 $handlers[$table] = new DataHandler($table); 33 } 34 return $handlers[$table]; 35 } 36 37 /** 38 * Getter. 39 */ 40 public function __get($name) { 41 return $this->$name; 42 } 43 44 /** 45 * Load a record. 46 */ 47 public function load($keys) { 48 $where = $values = array(); 49 $schema = drupal_get_schema($this->table); 50 $fields = $schema['fields']; 51 foreach ($keys as $key => $value) { 52 // Return if a key does not exist. 53 if (!isset($fields[$key]['type'])) { 54 return FALSE; 55 } 56 $where[] = db_escape_string($key) ." = ". db_type_placeholder($fields[$key]['type']); 57 $values[] = $value; 58 } 59 60 if (!empty($where)) { 61 $result = db_query('SELECT * FROM {'. db_escape_table($this->table) .'} WHERE '. implode(' AND ', $where), $values); 62 $results = array(); 63 while ($row = db_fetch_array($result)) { 64 $results[] = $row; 65 } 66 return count($results) ? $results : FALSE; 67 } 68 return FALSE; 69 } 70 71 /** 72 * Insert a record. 73 * 74 * @see drupal_write_record(). 75 * 76 * @param $record 77 * An array that is the record to save to this handler's table. 78 */ 79 public function insert(&$record) { 80 $result = drupal_write_record($this->table, $record); 81 module_invoke_all('data_insert', $record, $this->table); 82 return $result; 83 } 84 85 /** 86 * Update a record. 87 * 88 * @see drupal_write_record(). 89 * 90 * @param $record 91 * An array that is the record to save to this handler's table. 92 * @param $update 93 * A string or an array of strings that defines the keys to use for 94 * this update. 95 */ 96 public function update(&$record, $update) { 97 $result = drupal_write_record($this->table, $record, $update); 98 module_invoke_all('data_update', $record, $this->table); 99 return $result; 100 } 101 102 /** 103 * Save one or more records to the table. 104 * 105 * If $update is given, method will try to update before. 106 * 107 * This method is more comfortable, but slower than using insert() or 108 * update(). 109 * 110 * @param $record 111 * An array that is the record to save to this handler's table. 112 * @param 113 * A string or an array of strings that defines the keys to use for 114 * this update. 115 */ 116 public function save(&$record, $update = array()) { 117 if (is_string($update)) { 118 $update = array($update); 119 } 120 if (is_array($update)) { 121 $keys = array(); 122 foreach ($update as $key) { 123 $keys[$key] = $record[$key]; 124 } 125 if (count($keys)) { 126 if ($this->load($keys)) { 127 return $this->update($record, $update); 128 } 129 } 130 } 131 return $this->insert($record); 132 } 133 134 /** 135 * Delete one or more records from the table. 136 * 137 * @param $clause 138 * An array where the keys are columns in the data table and the values are 139 * the values to filter on. This array will be turned into the where clause 140 * of the delete query. 141 * 142 * Example: 143 * 144 * array( 145 * 'feed_nid' => 10, 146 * 'timestamp' => array( 147 * '<', 148 * 1255623780, 149 * ), 150 * ); 151 */ 152 public function delete($clause) { 153 $query = new DataQuery($this->table); 154 $schema = drupal_get_schema($this->table); 155 $fields = $schema['fields']; 156 foreach ($clause as $key => $value) { 157 $operator = '='; 158 if (is_array($value)) { 159 $operator = array_shift($value); 160 $value = array_shift($value); 161 } 162 $query->addWhere(db_escape_table($this->table) .'.'. db_escape_string($key) ." ". $operator ." ". db_type_placeholder($fields[$key]['type']), $value); 163 } 164 drupal_alter('data_delete_query', $query, $this->table); 165 if (!empty($query->where)) { 166 $query->execute(); 167 } 168 return db_affected_rows(); 169 } 170 171 /** 172 * Empty data table. 173 */ 174 public function truncate() { 175 db_query('TRUNCATE TABLE {'. db_escape_table($this->table) .'}'); 176 } 177 } 178 179 /** 180 * Builds and executes a query, only delete queries are supported at the moment. 181 */ 182 class DataQuery { 183 184 public $subject, $table, $from, $join, $where; 185 186 /** 187 * Creates an empty query. 188 * 189 * @param $table 190 * Base table to operate on, also the subject table to delete 191 * from. 192 */ 193 public function __construct($table) { 194 $this->table = db_escape_table($table); 195 $this->subject = array($this->table => $this->table); 196 $this->from = $this->table; 197 $this->join = array(); 198 $this->where = array(); 199 } 200 201 /** 202 * Add a subject table. 203 */ 204 public function addSubject($table) { 205 $this->subject[$table] = $table; 206 } 207 208 /** 209 * Add a join. 210 * 211 * @param $table 212 * The table to join to. 213 * @param $join 214 * The columns to join on. 215 */ 216 public function addJoin($table, $join, $type = 'INNER JOIN') { 217 $this->join[$table] = array( 218 $join, 219 $type, 220 ); 221 } 222 223 /** 224 * Add a where clause. 225 * 226 * @param $clause 227 * The actual clause. Caller is responsible for referring to existing fields 228 * and supplying correct placeholder for value. 229 * @param $value 230 * The value the clause compares against. 231 */ 232 public function addWhere($clause, $value = NULL) { 233 $this->where[$clause] = $value; 234 } 235 236 /** 237 * Build and execute a query. 238 */ 239 public function execute() { 240 $table = db_escape_table($this->table); 241 $query = "DELETE ". implode(', ', $this->subject); 242 $query .= " FROM {{$table}} {$table}"; 243 if (!empty($this->join)) { 244 foreach ($this->join as $table => $join) { 245 $table = db_escape_table($table); 246 $clause = array_shift($join); 247 $join = array_shift($join); 248 $query .= " $join {{$table}} $table ON $clause"; 249 } 250 } 251 $where = $values = array(); 252 foreach ($this->where as $k => $v) { 253 $where[] = $k; 254 if ($v !== NULL) { 255 $values[] = $v; 256 } 257 } 258 if (!empty($where)) { 259 $query .= " WHERE ". implode(' AND ', $where); 260 } 261 return db_query($query, $values); 262 } 263 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |