2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class PlDBBadValueException
extends PlException
24 public function __construct($value, PlDBTableField
$field, $reason)
26 parent
::__construct('Erreur lors de l\'accès à la base de données',
27 'Illegal value '. (is_null($value) ?
'(null)' : '(\'' . $value . '\')')
28 . ' for field (\'' . $field->table
->table
. '.' . $field->name
. '\'): '
33 class PlDBNoSuchFieldException
extends PlException
35 public function __construct($field, PlDBTable
$table)
37 parent
::__construct('Erreur lors de l\'accès à la base de données',
38 'No such field ' . $field . ' in table ' . $table->table
);
42 class PlDBNoSuchKeyException
extends PlException
44 public function __construct($key, PlDBTable
$table)
46 parent
::__construct('Erreur lors de l\'accès à la base de données',
47 'No such key ' . $key . ' in table ' . $table->table
);
52 class PlDBIncompleteEntryDescription
extends PlException
54 public function __construct($field, PlDBTable
$table)
56 parent
::__construct('Erreur lors de l\'accès à la base de données',
57 'The field ' . $field . ' is required to describe an entry in table '
71 public $typeParameters;
75 public $autoIncrement;
79 public function __construct(array $column)
81 $this->name
= $column['Field'];
82 $this->typeParameters
= explode(' ', str_replace(array('(', ')', ',', '\''), ' ',
84 $this->type
= array_shift($this->typeParameters
);
85 if ($this->type
== 'enum' ||
$this->type
== 'set') {
86 $this->typeParameters
= new PlFlagSet(implode(',', $this->typeParameters
));
87 } else if (ctype_digit($this->typeParameters
[0])) {
88 $this->typeLength
= intval($this->typeParameters
[0]);
89 array_shift($this->typeParameters
);
91 $this->allowNull
= ($column['Null'] === 'YES');
92 $this->autoIncrement
= (strpos($column['Extra'], 'auto_increment') !== false
);
93 $this->inPrimaryKey
= ($column['Key'] == 'PRI');
96 $this->defaultValue
= $this->format($column['Default']);
97 } catch (PlDBBadValueException
$e) {
98 $this->defaultValue
= null
;
102 public function registerFormatter($class)
104 $this->formatter
= $class;
107 public function format($value, $badNullFallbackToDefault = false
)
109 if (is_null($value)) {
110 if ($this->allowNull ||
$this->autoIncrement
) {
113 if ($badNullFallbackToDefault) {
114 return $this->defaultValue
;
116 throw new PlDBBadValueException($value, $this, 'null not allowed');
117 } else if (!is_null($this->formatter
)) {
118 $class = $this->formatter
;
119 $value = new $class($this, $value);
120 } else if ($this->type
== 'enum') {
121 if (!$this->typeParameters
->hasFlag($value)) {
122 throw new PlDBBadValueException($value, $this, 'invalid value for enum ' . $this->typeParameters
->flags());
125 } else if ($this->type
== 'set') {
126 $value = new PlFlagSet($value);
127 foreach ($value as $flag) {
128 if (!$this->typeParameters
->hasFlag($flag)) {
129 throw new PlDBBadValueException($value, $this, 'invalid flag for set ' . $this->typeParameters
->flags());
133 } else if (ends_with($this->type
, 'int')) {
134 if (!is_int($value) && !ctype_digit($value)) {
135 throw new PlDBBadValueException($value, $this, 'value is not an integer');
137 $value = intval($value);
138 if (count($this->typeParameters
) > 0 && $this->typeParameters
[0] == 'unsigned') {
140 throw new PlDBBadValueException($value, $this, 'value is negative in an unsigned field');
143 /* TODO: Check bounds */
145 } else if (ends_with($this->type
, 'char')) {
146 if (strlen($value) > $this->typeLength
) {
147 throw new PlDBBadValueException($value, $this, 'value is expected to be at most ' . $this->typeLength
. ' characters long, ' . strlen($value) . ' given');
150 } else if (starts_with($this->type
, 'date') ||
$this->type
== 'timestamp') {
151 return new DateFieldFormatter($this, $value);
157 interface PlDBTableFieldFormatter
extends XDBFormat
159 public function __construct(PlDBTableField
$field, $value);
162 class DateFieldFormatter
implements PlDBTableFieldFormatter
165 private $storageFormat;
167 public function __construct(PlDBTableField
$field, $date)
169 $this->datetime
= make_datetime($date);
170 if (is_null($this->datetime
)) {
171 throw new PlDBBadValueException($date, $field, 'value is expected to be a date/time, ' . $date . ' given');
173 if ($field->type
== 'date') {
174 $this->storageFormat
= 'Y-m-d';
175 } else if ($field->type
== 'datetime') {
176 $this->storageFormat
= 'Y-m-d H:i:s';
178 $this->storageFormat
= 'U';
182 public function format()
184 return XDB
::escape($this->datetime
->format($this->storageFormat
));
187 public function date($format)
189 return $this->datetime
->format($format);
193 class JSonFieldFormatter
implements PlDBTableFieldFormatter
, ArrayAccess
197 public function __construct(PlDBTableField
$field, $data)
199 if (strpos($field->type
, 'text') === false
) {
200 throw new PlDBBadValueException($data, $field, 'json formatting requires a text field');
203 if (is_string($data)) {
204 $this->data
= json_decode($data, true
);
205 } else if (is_object($data)) {
206 $this->data
= json_decode(json_encode($data), true
);
207 } else if (is_array($data)) {
211 if (is_null($this->data
)) {
212 throw new PlDBBadValueException($data, $field, 'cannot interpret data as json: ' . $data);
216 public function format()
218 return XDB
::escape(json_encode($this->data
));
221 public function offsetExists($offset)
223 return isset($this->data
[$offset]);
226 public function offsetGet($offset)
228 return $this->data
[$offset];
231 public function offsetSet($offset, $value)
233 $this->data
[$offset] = $value;
236 public function offsetUnset($offset)
238 unset($this->data
[$offset]);
243 /** This class aims at providing a simple interface to interact with a single
244 * table of a database. It is implemented as a wrapper around XDB.
248 const PRIMARY_KEY
= 'PRIMARY';
255 private $multipleKeys;
256 private $mutableFields;
258 public function __construct($table)
260 $this->table
= $table;
264 private function parseSchema(PlIterator
$schema, PlIterator
$keys)
266 $this->schema
= array();
267 $this->primaryKey
= array();
268 $this->uniqueKeys
= array();
269 $this->multipleKeys
= array();
270 $this->mutableFields
= array();
271 while ($column = $schema->next()) {
272 $field = new PlDBTableField($column);
273 $this->schema
[$field->name
] = $field;
274 if (!$field->inPrimaryKey
) {
275 $this->mutableFields
[] = $field->name
;
278 while ($column = $keys->next()) {
279 $name = $column['Key_name'];
280 $multiple = intval($column['Non_unique']) != 0;
281 $field = $column['Column_name'];
283 if (!isset($this->multipleKeys
[$name])) {
284 $this->multipleKeys
[$name] = array();
286 $this->multipleKeys
[$name][] = $field;
287 } else if ($name == self
::PRIMARY_KEY
) {
288 $this->primaryKey
[] = $field;
290 if (!isset($this->uniqueKeys
[$name])) {
291 $this->uniqueKeys
[$name] = array();
293 $this->uniqueKeys
[$name][] = $field;
299 private function schema()
301 if (!$this->schema
) {
302 $schema = XDB
::iterator('DESCRIBE ' . $this->table
);
303 $keys = XDB
::iterator('SHOW INDEX FROM ' . $this->table
);
304 $this->parseSchema($schema, $keys);
306 return $this->schema
;
309 private function field($field)
311 $schema = $this->schema();
312 if (!isset($schema[$field])) {
313 throw new PlDBNoSuchFieldException($field, $this);
315 return $schema[$field];
318 public function formatField($field, $value)
320 return $this->field($field)->format($value);
323 public function registerFieldFormatter($field, $class)
325 return $this->field($field)->registerFormatter($class);
328 public function defaultValue($field)
330 return $this->field($field)->defaultValue
;
333 private function hasKeyField(PlDBTableEntry
$entry, array $fields)
335 foreach ($fields as $field) {
336 if (isset($entry->$field)) {
343 private function keyFields($keyName)
345 if ($keyName == self
::PRIMARY_KEY
) {
346 return $this->primaryKey
;
347 } else if (isset($this->uniqueKeys
[$keyName])) {
348 return $this->uniqueKeys
[$keyName];
349 } else if (isset($this->multipleKeys
[$keyName])) {
350 return $this->multipleKeys
[$keyName];
352 throw new PlDBNoSuchKeyException($keyName, $this);
355 private function bestKeyFields(PlDBTableEntry
$entry, $allowMultiple)
357 if ($this->hasKeyField($entry, $this->primaryKey
)) {
358 return $this->primaryKey
;
360 foreach ($this->uniqueKeys
as $fields) {
361 if ($this->hasKeyField($entry, $fields)) {
365 if ($allowMultiple) {
366 foreach ($this->multipleKeys
as $fields) {
367 if ($this->hasKeyField($entry, $fields)) {
372 return $this->primaryKey
;
375 public function key(PlDBTableEntry
$entry, array $keyFields)
378 foreach ($keyFields as $field) {
379 if (!isset($entry->$field)) {
380 throw new PlDBIncompleteEntryDescription($field, $this);
382 $key[] = XDB
::escape($this->$field);
385 return implode('-', $key);
388 public function primaryKey(PlDBTableEntry
$entry)
390 return $this->key($this->keyFields(self
::PRIMARY_KEY
));
393 private function buildKeyCondition(PlDBTableEntry
$entry, array $keyFields, $allowIncomplete)
395 $condition = array();
396 foreach ($keyFields as $field) {
397 if (!isset($entry->$field)) {
398 if (!$allowIncomplete) {
399 throw new PlDBIncompleteEntryDescription($field, $this);
402 $condition[] = XDB
::format($field . ' = {?}', $entry->$field);
405 return implode(' AND ', $condition);
408 public function fetchEntry(PlDBTableEntry
$entry)
410 $result = XDB
::rawFetchOneAssoc('SELECT *
411 FROM ' . $this->table
. '
412 WHERE ' . $this->buildKeyCondition($entry,
413 $this->bestKeyFields($entry, false
),
418 return $entry->fillFromDBData($result);
421 public function iterateOnCondition(PlDBTableEntry
$entry, $condition, $sortField)
423 if (empty($sortField)) {
424 $sortField = $this->primaryKey
;
426 if (!is_array($sortField)) {
427 $sortField = array($sortField);
429 $sort = ' ORDER BY ' . implode(', ', $sortField);
430 $it = XDB
::rawIterator('SELECT *
431 FROM ' . $this->table
. '
432 WHERE ' . $condition . '
434 return PlIteratorUtils
::map($it, array($entry, 'cloneAndFillFromDBData'));
437 public function iterateOnEntry(PlDBTableEntry
$entry, $sortField)
439 return $this->iterateOnCondition($entry,
440 $this->buildKeyCondition($entry,
441 $this->bestKeyFields($entry, true
),
446 const SAVE_INSERT_MISSING
= 0x01;
447 const SAVE_UPDATE_EXISTING
= 0x02;
448 const SAVE_IGNORE_DUPLICATE
= 0x04;
449 public function saveEntry(PlDBTableEntry
$entry, $flags)
451 $flags &= (self
::SAVE_INSERT_MISSING | self
::SAVE_UPDATE_EXISTING | self
::SAVE_IGNORE_DUPLICATE
);
452 Platal
::assert($flags != 0, "Hey, the flags ($flags) here are so stupid, don't know what to do");
453 if ($flags == self
::SAVE_UPDATE_EXISTING
) {
455 foreach ($this->mutableFields
as $field) {
456 if ($entry->hasChanged($field)) {
457 $values[] = XDB
::format($field . ' = {?}', $entry->$field);
460 if (count($values) > 0) {
461 XDB
::rawExecute('UPDATE ' . $this->table
. '
462 SET ' . implode(', ', $values) . '
463 WHERE ' . $this->buildKeyCondition($entry,
464 $this->keyFields(self
::PRIMARY_KEY
),
469 foreach ($this->schema
as $field=>$type) {
470 if ($entry->hasChanged($field)) {
471 $values[$field] = XDB
::escape($entry->$field);
474 if (count($values) > 0) {
475 $query = $this->table
. ' (' . implode(', ', array_keys($values)) . ')
476 VALUES (' . implode(', ', $values) . ')';
477 if (($flags & self
::SAVE_UPDATE_EXISTING
)) {
479 foreach ($this->mutableFields
as $field) {
480 if (isset($values[$field])) {
481 $update[] = "$field = VALUES($field)";
484 if (count($update) > 0) {
485 $query = 'INSERT ' . $query;
486 $query .= "\n ON DUPLICATE KEY UPDATE " . implode(', ', $update);
488 $query = 'INSERT IGNORE ' . $query;
490 } else if (($flags & self
::SAVE_IGNORE_DUPLICATE
)) {
491 $query = 'INSERT IGNORE ' . $query;
493 $query = 'INSERT ' . $query;
495 XDB
::rawExecute($query);
496 $id = XDB
::insertId();
498 foreach ($this->primaryKey
as $field) {
499 if ($this->schema
[$field]->autoIncrement
) {
500 $entry->$field = $id;
509 public function deleteEntry(PlDBTableEntry
$entry, $allowIncomplete)
511 XDB
::rawExecute('DELETE FROM ' . $this->table
. '
512 WHERE ' . $this->buildKeyCondition($entry,
513 $this->bestKeyFields($entry, $allowIncomplete),
517 public static function get($name)
519 return new PlDBTable($name);
523 class PlDBTableEntry
extends PlAbstractIterable
527 private $fetched = false
;
530 private $data = array();
532 public function __construct($table, $autoFetch = false
)
534 if ($table instanceof PlDBTable
) {
535 $this->table
= $table;
537 $this->table
= PlCache
::getGlobal('pldbtable_' . $table, array('PlDBTable', 'get'), array($table));
539 $this->autoFetch
= $autoFetch;
540 $this->changed
= new PlFlagSet();
543 /** Register a custom formatter for a field.
545 * A formatter can be used to perform on-the-fly conversion from db storage to a user-friendly format.
546 * For example, if you have a textual field that contain json, you can use a JSonFieldFormatter on this
547 * field to perform automatic decoding when reading from the database (or when assigning the field)
548 * and automatic json_encoding when storing the object back to the db.
550 protected function registerFieldFormatter($field, $formatterClass)
552 $this->table
->registerFieldFormatter($field, $formatterClass);
555 /** This hook is called when the entry is going to be updated in the db.
557 * A typical usecase is a class that stores low-level representation of
558 * an object in db and perform a conversion between this low-level representation
559 * and a higher-level representation.
561 * @return true in case of success
563 protected function preSave()
568 /** This hook is called when the entry has been save in the database.
570 * It can be used to perform post-actions on save like storing extra data
571 * in database or sending a notification.
573 protected function postSave()
577 /** This hook is called when the entry is going to be deleted from the db.
579 * Default behavior is to call preSave().
581 * @return true in case of success.
583 protected function preDelete()
585 return $this->preSave();
588 /** This hook is called when the entry has just been fetched from the db.
590 * This is the counterpart of @ref preSave and a typical use-case is the conversion
591 * from a high-level representation of the objet to a representation suitable for
592 * storage in the database.
594 * @return true in case of success.
596 protected function postFetch()
601 public function __get($field)
603 if (isset($this->data
[$field])) {
604 return $this->data
[$field];
605 } else if (!$this->fetched
&& $this->autoFetch
) {
607 if (isset($this->data
[$field])) {
608 return $this->data
[$field];
611 return $this->table
->defaultValue($field);
614 public function __set($field, $value)
616 $this->data
[$field] = $this->table
->formatField($field, $value);
617 $this->changed
->addFlag($field);
620 public function __isset($field)
622 return isset($this->data
[$field]);
625 public function primaryKey()
627 $this->table
->primaryKey($this);
630 public function hasChanged($field)
632 return $this->changed
->hasFlag($field);
635 public function fillFromArray(array $data)
637 foreach ($data as $field => $value) {
638 $this->$field = $value;
642 public function fillFromDBData(array $data)
644 $this->fillFromArray($data);
645 $this->changed
->clear();
646 return $this->postFetch();
649 public function copy(PlDBTableEntry
$other)
651 Platal
::assert($this->table
== $other->table
,
652 "Trying to fill an entry of table {$this->table->table} with content of {$other->table->table}.");
653 $this->changed
= $other->changed
;
654 $this->fetched
= $other->fetched
;
655 $this->data
= $other->data
;
658 public function cloneAndFillFromDBData(array $data)
660 $clone = clone $this;
661 $clone->fillFromDBData($data);
665 public function fetch()
667 return $this->table
->fetchEntry($this);
670 public function iterate($sortField = null
)
672 return $this->table
->iterateOnEntry($this, $sortField);
675 public function iterateOnCondition($condition, $sortField = null
)
677 return $this->table
->iterateOnCondition($this, $condition, $sortField);
680 public function save($flags)
682 if (!$this->preSave()) {
685 $this->table
->saveEntry($this, $flags);
686 $this->changed
->clear();
691 public function update($insertMissing = false
)
693 $flags = PlDBTable
::SAVE_UPDATE_EXISTING
;
694 if ($insertMissing) {
695 $flags = PlDBTable
::SAVE_INSERT_MISSING
;
697 return $this->save($flags);
700 public function insert($allowUpdate = false
)
702 $flags = PlDBTable
::SAVE_INSERT_MISSING
;
704 $flags |
= PlDBTable
::SAVE_UPDATE_EXISTING
;
706 return $this->save($flags);
709 public function delete()
711 if (!$this->preDelete()) {
714 return $this->table
->deleteEntry($this, true
);
718 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: