3 * @desc The Abstract Data Objects series
5 * @author Marius Orcsik <marius.orcsik@gmail.com>
16 protected $id, // public to allow debugging outside the class
22 * @desc function to implement get_ | set_ virtual methods
24 * @param string $method
28 * @see http://www.ibm.com/developerworks/xml/library/os-php-flexobj/ by Jack Herrington <jherr@pobox.com>
30 function __call( $method, $args) {
31 $diff = $this->get_members();
32 $all = get_object_vars($this);
34 if ( preg_match( '/set_(.*)/', $method, $found ) ) {
35 // check for fields with $found[1] name
36 if ( array_key_exists( $found[1], $diff) ) {
37 $this->fields
[$found[1]]->setValue($args[0]);
39 // check for obj members with $found[1] name
40 } elseif (array_key_exists( $found[1], $all)){
41 $this->$found[1] = $args[0];
44 } else if ( preg_match( '/get_(.*)/', $method, $found ) ) {
45 if ( array_key_exists( $found[1], $diff ) ) {
46 return $this->fields
[$found[1]]->getValue();
47 } elseif (array_key_exists( $found[1], $all)){
48 return $this->$found[1];
55 * @desc A function to implement a virtual getter member of the class
60 * @see http://www.ibm.com/developerworks/xml/library/os-php-flexobj/ by Jack Herrington <jherr@pobox.com>
62 function __get( $key ) {
63 return $this->fields
[$key];
67 * @desc A function to implement a virtual setter member for this class
73 * @see http://www.ibm.com/developerworks/xml/library/os-php-flexobj/ by Jack Herrington <jherr@pobox.com>
75 function __set( $key, $value ) {
76 if (array_key_exists($key,$this->get_members()))
77 return ($this->fields
[$key]->value
= $value);
80 public function __construct(&$db) {
84 $this->instantiateMembers();
87 public function __destruct() {}
90 * gets the members we consider table fields
92 * @return array ('fieldName' => tdoAbstractField)
94 public function &get_members () {
99 * checks if a field is a valid member of the current object
101 * @param tdoAbstractField $incMember
104 public function isValidMember ($incMember) {
106 ! ($incMember instanceof tdoAbstractField
) ||
107 ! in_array ($incMember, $this->get_members(), true) // this prevents an error in php5.2 object comparison
115 * instantiating the object's members
117 public function instantiateMembers () {
119 foreach ($this->get_members() as $key => $field) {
120 if ( is_int($key) && is_string($field)) {
121 $this->fields
[$field] = new tdoAbstractField($field, $this->alias
);
123 // FIXME: this is so bad :D - it implies that the primary key must be the first field containing _id :D
124 if (stristr($field,'_id') && !isset($this->id
))
125 $this->id
= &$this->fields
[$field];
127 unset($this->fields
[$key]);
130 $this->wheres
= array();
133 $this->refers
= array();
136 * setting the table's index field
138 * @param tdoAbstractField $obj
140 public function set_index (&$obj) {
141 if ($this->isValidMember($obj)) {
143 $this->id
->flags
= PRIMARY
;
148 * setting an alias for the table in case of joins.
149 * this method also sets the alias on all the fields of the current object
151 * @param string $alias
153 public function set_alias ($alias) {
154 foreach ($this->get_members() as $field){
155 if ($field->table
== $this->alias
) {
156 $field->table
= 't'.$alias;
159 $this->alias
= 't'.$alias;
163 * For adding custom sql functions for certain fields
165 * @param string $modif
166 * @param tdoAbstractField $field
168 public function set_fieldModifier ($modif, &$field) {
169 if ($this->isValidMember($field) && stristr($modif, '%s'))
170 $field->set_modifier($modif);
174 * method used in join cases to add the joined object's fields
177 * @param array ('fieldName' => tdoAbstractField) $incArr
179 public function addFields (&$incArr) {
180 if (is_array($incArr)) {
181 foreach ($incArr as $fieldName => $field) {
182 $this->fields
[$fieldName] = $field;
188 * Based on field values, we get the _first_ row in the table that matches
190 * TODO: maybe we can have a function that returns _all_ the rows that match
194 public function buildObj () {
195 $sql = $this->buildSql(1);
196 $this->db
->query( $sql );
198 $arr = $this->db
->getAssoc();
199 if (is_array($arr)) {
200 foreach ($arr as $field => $var) {
201 $this->fields
[$field] = $var;
209 * Gets the row in the table for $id
214 public function get ($id) {
215 $id = $this->db
->escape($id);
217 $this->instantiateMembers();
219 $this->id
->value
= $id;
223 * Returns the last id of the table
224 * OBS: this assumes that we didn't delete any rows from the table.
228 public function getLastInsertedId() {
229 $sql = $this->db
->_SELECT($this->id
->name
);
230 $sql .= $this->db
->_FROM($this->name
);
232 $this->addOrder($this->id
, false);
233 // $orders[] = array($this->id->name, false);
235 $sql .= $this->db
->_ORDER($this->outputOrders());
238 $this->db
->query($sql);
239 return $this->db
->getScalar();
243 * encapsulating the $this->db->escape() method
245 * @param mixed $value
248 public function escape ($value) {
249 if (is_numeric($value)) {
252 return '"'.$this->db
->escape($value).'"';
256 * inserting into the database
257 * TODO: multiple inserts to use with loadFromArray
261 public function insert () {
262 $sql = 'INSERT INTO '.$this->name
;
266 $f = $this->get_members();
268 foreach ($f as $fieldName => $field) {
269 if ($this->isValidMember($field) && ($field != $this->id
) && !is_null($field->value
)) {
270 $value = $this->escape($field->value
);
272 $fieldStr.= (!empty($fieldStr) ?
', ' : ' ').$fieldName;
273 $valueStr.= (!empty($valueStr) ?
', ' : ' ').$value;
277 $sql.= ' ('.$fieldStr.') VALUES ('.$valueStr.')';
279 $this->db
->query($sql);
281 return $this->getLastInsertedId();
285 public function update($id = null) {
287 $id = $this->id
->value
;
291 trigger_error('Cannot update record in table '.$this->name
.' because an id hasn\'t been provided', E_USER_WARNING
);
295 $sql = $this->db
->_UPDATE(array($this->name
,$this->alias
));
297 if (is_array($this->refers
) && !empty($this->refers
)){
298 $this->refers
= array_reverse($this->refers
);
300 foreach ($this->refers
as $ref)
306 $fields = $this->get_members();
308 foreach ($fields as $fieldName => $field) {
309 if (($field instanceof tdoAbstractField
) && $field != $this->id
) { // TODO: make a more real check for field is an id
310 $value = $field->value
;
313 if ((isset($value) && !is_null($value))) {
314 $sql.= $field->table
.'.'.$fieldName.' = '.$this->escape($value).', ';
318 $sql = substr($sql, 0, -2);
320 $sql.= ' WHERE '.$this->alias
.'.'.$this->id
->name
.' = '.$this->escape($id);
322 $this->db
->query($sql);
327 public function replace ( $id = null ) {
329 $id = $this->id
->value
;
332 if (!isset($id) ||
!$this->idExists( $id ) ) {
333 return $this->insert();
335 return $this->update($id);
339 // TODO: make this the same way the find first method works
340 public function delete ($id = null) {
341 if ($id == null && empty($this->wheres
) && !empty($this->id
->value
)) {
342 $this->addWhere($this->id
, '=', $this->id
->value
);
345 if (empty($this->wheres
)) {
349 $temp = $this->alias
;
350 $this->set_alias(null);
352 // var_dump($this->wheres);die;
353 $sql = 'DELETE FROM '.$this->name
.' WHERE '; //$this->id->name.' = '.$this->escape($value).' LIMIT 1';
354 $sql.= $this->outputWheres();
355 // echo $this->outputWheres(false);die;
356 $affRows = $this->db
->query($sql);
358 $this->set_alias($temp);
362 public function reset () {
363 foreach ($this->fields
as $key => $field) {
364 if ($field instanceof tdoAbstractField
) {
365 $this->fields
[$key] = new tdoAbstractField($key, $this->alias
);
369 $this->wheres
= array();
370 $this->groups
= array();
371 $this->orders
= array();
372 $this->refers
= array();
376 public function idExists ($id = null) {
378 $id = $this->id
->value
;
382 $sql = 'SELECT COUNT('.$this->id
->name
.') FROM '.$this->name
.' WHERE '.$this->id
->name
.' = '.$this->escape($id);
383 $this->db
->query($sql);
385 if ($this->db
->getScalar())
391 protected function outputFieldList () {
393 $f = $this->get_members();
395 foreach ($f as $fieldName => $field) {
396 $curField = (!is_null($field->table
) ?
$field->table
.'.' : '').$field->name
;
398 if (!is_null($field->modifier
))
399 $curField = str_replace ('%s', $curField, $field->modifier
).$this->db
->_AS($field->name
);
401 $fields .= $curField;
402 if ($field != end($f))
410 protected function outputWheres ($bIW = true) {
411 return implode ($this->db
->_AND(), $this->wheres
);
414 protected function outputGroups () {
417 $f = $this->get_members();
418 foreach ($f as $field)
419 if (!is_null($field->group
) )
420 $groups .= (!empty($groups) ?
', ' : '').$field->name
;
425 protected function outputOrders () {
427 $f = $this->get_members();
428 foreach ($f as $field)
429 if (!is_null($field->order
)) {
430 $orders .= (!empty($orders) ?
', ': '').$field->name
.($field->order
== true ?
' ASC' : ' DESC');
435 protected function outputRefers () {
438 if (is_array($this->refers
) && !empty($this->refers
)){
439 $this->refers
= array_reverse($this->refers
);
440 foreach ($this->refers
as $ref)
447 protected function buildInherentWheres () {
448 $diff = $this->get_members();
449 // let's hope this doesn't break stuff.
450 // it's needed when we use more queries on the same instance of the object :D
452 if (is_array($diff)) {
453 foreach ($diff as $fieldName => $field) {
454 if ($this->isValidMember($field) && $field->value
!= null)
455 $this->addWhere ($field, '=', $this->escape($field->value
));
459 if (empty($this->wheres
)) {
460 $this->wheres
[] = new tdoAbstractClause(1);
463 // var_dump($this->wheres);
466 public function addWhere($field1, $condition = null, $field2 = null) {
467 $this->wheres
[] = new tdoAbstractClause($field1, $condition, $field2);
470 public function addOrder (&$orderField, $asc = true) {
471 if (!($orderField instanceof tdoAbstractField
) && is_string($orderField)) {
472 $orderField = &$this->fields
[$orderField];
475 $orderField->set_order($asc);
478 public function addGroup (&$groupField) {
479 if (!($groupField instanceof tdoAbstractField
) && is_string($groupField)) {
480 $groupField = &$this->fields
[$groupField];
482 if (($groupField instanceof tdoAbstractField
)) {
483 $groupField->set_group (true);
488 * building a normal SELECT query
494 private function buildSql($start = 0, $end = 0) {
495 $sql = $this->db
->_SELECT($this->outputFieldList()). ' FROM '.$this->name
.' AS '.$this->alias
.' ';
497 $this->buildInherentWheres(); // will it work
499 $sql .= $this->outputRefers();
501 $sql .= $this->db
->_WHERE($this->outputWheres());// add LIMITS
503 $sql .= $this->db
->_GROUP($this->outputGroups());
505 $sql .= $this->db
->_ORDER($this->outputOrders());
508 $sql .= $this->db
->_LIMIT($start, $end);
513 public function find ($start = 0, $count = 0) {
514 $result = $this->db
->query($this->buildSql(), $start, $count);
518 public function findFirst () {
519 $this->buildInherentWheres();
521 $this->db
->query($this->buildSql(), 0, 1);
522 $row = $this->db
->getAssoc();
524 foreach ($row as $field => $value){
525 $this->fields
[$field]->value
= $value;
529 public function getArray ($start = 0, $count = 0, $orderBy = null) {
530 $this->buildInherentWheres ();
532 $sql = $this->buildSql ($start, $count, $orderBy);
534 $this->db
->query ($sql);
535 return $this->db
->getArray();
538 public function getCount() {
539 // $sql = str_replace(
540 // $this->outputFieldList(),
541 // 'COUNT('.$this->id.') ',
544 $this->set_fieldModifier('COUNT(%s)', $this->id
);
545 $this->db
->query($this->buildSql());
546 return $this->db
->getScalar();
549 public function getVector() {
550 // I really don't see why we need this. ?
553 public function loadFromArray($valArray) {
554 foreach ($this->fields
as $field => $value) {
555 if (isset($valArray[$field])) {
556 $this->$field = $valArray[$field];
562 * FIXME: make it work when composing with the same object
564 * @param tdoAbstract $incOb
567 private function composeObj ($incOb) {
568 if (!($incOb instanceof tdoAbstract
))
570 $refs = count($this->refers
);
573 // because of the possible conflicts between fields names we must put the table alias
574 //array_merge ($this->wheres, $incOb->get_wheres());
575 // $this->orders = array_merge ($this->orders, $incOb->get_orders());
577 foreach ($incOb->refers
as $alias => $ref) {
580 $this->refers
[$tAl] = $ref;//str_replace(array($alias, $aliases[$alias][1]), array($tAl, $aliases[$alias][2]), $ref);
581 $this->refers
[$tAl]->set_state($tAl);
586 * Enter description here...
588 * @param string $jType
589 * @param tdoAbstractField $thisJField
590 * @param tdoAbstract $incOb
591 * @param tdoAbstractField $incObJField
594 public function joinWith ($jType = null, &$thisJField = null, &$incOb = null, &$incObJField = null) {
596 !tdoAbstractJoin
::isValidType($jType) ||
597 !$this->isValidMember ($thisJField)
602 $this->composeObj ($incOb);
604 $tAl = (count($this->refers
));
606 trigger_error('Join aborted for table '.$this->name
.': Too many tables; MySQL can only use 61 tables in a join', E_USER_NOTICE
);
609 $incOb->set_alias($tAl);
611 if($thisJField == null ||
!($thisJField instanceof tdoAbstractField
))
612 $thisJField = $this->id
;
614 if($incObJField == null ||
!($incObJField instanceof tdoAbstractField
))
615 $incObJField = $incOb->id
;
617 $this->refers
[$tAl] = new tdoAbstractJoin($jType, $this, $incOb, $thisJField, $incObJField, $tAl);
618 $this->refers
[$tAl]->set_state ($tAl);
623 * function to dump a <type>Sql
624 * problem with the field types. :D
626 * @param tdoAbstract $obj
628 static public function dumpSchema ($obj) {
629 if ($obj instanceof tdoAbstract)
630 throw new Exception('Can\'t generate sql dump');
632 $sql = 'CREATE TABLE '. $obj->name . ' (';
634 foreach ($obj->getFields() as $fieldName => $data) {
635 $sql .= $fieldName.' '.$data[0].', ';
644 * class to abstract a where clause in a SQL query
645 * TODO: add possibility of complex wheres: (t1 condition1 OR|AND|XOR t2.condition2)
646 * TODO: abstract the condition part of a where clause - currently string based :D
648 class tdoAbstractClause
{
649 protected $subject, $predicate, $predicative;
652 * initializing a WHERE|ON clause
654 * @param tdoAbstractClause|tdoAbstractField $subject
655 * @param string $predicate
656 * @param tdoAbstractClause|tdoAbstractField|null $complement
658 public function __construct ($subject, $predicate = null, $predicative = null) {
659 if ($subject == 1 && $predicate == null && $predicative == null) {
660 $this->subject
= '1';
661 $this->predicate
= '';
662 $this->predicative
= '';
666 if (($subject instanceof tdoAbstractField
) ||
($subject instanceof tdoAbstractClause
)) {
667 $this->subject
= $subject;
668 $this->predicative
= $predicative;
670 if ($this->validPredicative($predicate))
671 $this->predicate
= $predicate;
674 $this->predicate
= '';
675 $this->predicative
= '';
680 public function __destruct () {}
682 public function __toString () {
683 // var_dump($this->subject, $this->predicate, $this->predicative);
685 if ($this->subject
instanceof tdoAbstractClause
) {
686 $subStr = (string)$this->subject
;
687 } elseif ($this->subject
instanceof tdoAbstractField
) {
688 // without $this->subject->table != 't' we have a bug in the delete op
689 $subStr = ($this->subject
->table
!= 't' ?
$this->subject
->table
.'.': '').$this->subject
->name
;
690 } elseif ($this->subject
== '1') {
691 return $this->subject
;
696 if (is_null($this->predicative
)) {
697 if ($this->validPredicative($this->predicate
)) {
701 } elseif (is_numeric($this->predicative
)) {
702 $preStr = $this->predicative
;
703 } elseif (is_string($this->predicative
)) {
704 $preStr = $this->predicative
;
706 if ($this->predicate
== 'LIKE') {
707 $preStr = '%'.$this->predicate
.'%';
710 $preStr = (stripos($preStr, '"') !== 0 ?
'"'.$preStr.'"' : $preStr);//'"'.$preStr.'"';
711 } elseif (is_array($this->predicative
)) {
712 $preStr = '("'.implode('", "',$this->predicative
).'")';
713 } elseif ($this->predicative
instanceof tdoAbstractfield
) {
714 $preStr = ($this->predicative
->table
!= 't' ?
$this->predicative
->table
.'.': '').$this->predicative
->name
;
715 } elseif ($this->predicative
instanceof tdoAbstractClause
) {
717 $preStr = $this->predicative
;
720 $retStr = $subStr.' '.$this->predicate
.' '.$preStr;
721 if (($this->subject
instanceof tdoAbstractClause
) && ($this->predicative
instanceof tdoAbstractClause
))
722 return '('.$retStr.')';
727 private function validPredicative ($predicate) {
728 // need to find a way to abstract these
729 // $validPredicates = array (
744 if ($this->predicative
instanceof tdoAbstractClause
) {
745 // we'll have Subject AND|OR|XOR Predicative
746 $validPredicates = array (
753 } elseif (($this->predicative
instanceof tdoAbstractField
) ||
is_numeric($this->predicative
)) {
754 // we'll have Subject =|!= Predicative
755 $validPredicates = array (
763 } elseif (is_array($this->predicative
)) {
764 $validPredicates = array (
768 } elseif (is_string($this->predicative
)) {
769 $validPredicates = array (
778 } elseif (is_null($this->predicative
)) {
779 $validPredicates = array (
785 return in_array($predicate, $validPredicates);
787 // if (in_array($predicate, $validPredicates) && (($predicative instanceof tdoAbstractClause) || ($predicative instanceof tdoAbstractField)))
793 class tdoAbstractJoin
{
794 static public $validTypes = array (
809 public function __construct ($type, &$lt, &$rt, &$lf, &$rf, $state) {
810 if ($rt instanceof tdoAbstract ||
811 $lt instanceof tdoAbstract
813 $this->leftTable
= &$lt;
814 $this->rightTable
= &$rt;
816 if (tdoAbstractJoin
::isValidType($type))
820 $lf instanceof tdoAbstractField
&&
821 $rf instanceof tdoAbstractField
823 $this->rightField
= &$rf;
824 $this->leftField
= &$lf;
827 $this->state
= $state;
830 $this->composeFields();
831 $this->composeWheres();
835 public function __destruct () {}
837 public function __toString() {
838 $lAlias = $this->leftTable
->get_alias();
839 return (string)$this->type
.' JOIN '.$this->rightTable
->get_name().
840 ' AS t'.$this->state
.' ON '.(isset($lAlias) ?
$lAlias : $this->leftTable
->get_name()).
841 '.'.$this->leftField
->name
.
842 ' = t'.$this->state
.'.'.$this->rightField
->name
.' ';
846 * Will compose the $rightTable and $leftTable fields
849 public function composeFields () {
850 $leftFields = $this->leftTable
->get_members();
852 $this->rightTable
->set_alias($this->state
);
853 $rightFields = $this->rightTable
->get_members();
856 $this->leftTable
->addFields($rightFields);
860 * Will compose the $rightTable and $leftTable WHERE clauses
863 public function composeWheres () {
864 if (!is_array($this->leftTable
->wheres
))
865 $this->leftTable
->wheres
= array();
866 if (!is_array($this->rightTable
->wheres
))
867 $this->rightTable
->wheres
= array();
869 // var_dump($this->rightTable->wheres, $this->leftTable );die;
870 $this->leftTable
->wheres
= &array_merge($this->rightTable
->wheres
, $this->leftTable
->wheres
);
873 public function set_state ($st) {
877 static public function isValidType ($inc) {
878 if (in_array($inc, tdoAbstractJoin
::$validTypes))
885 define ('PRIMARY', 2);
886 define ('UNIQUE', 4);
887 define ('FULLTEXT', 8);
889 class tdoAbstractField
{
890 static public $validTypes = array (
900 protected $name, $type, $flags, $value, $table, $modifier = null, $order = null, $group = null, $where;
902 public function __construct ($incName, $incTable, $incType='INT', $incFlags=0) {
903 $this->name
= $incName;
904 $this->table
= $incTable;
905 $this->type
= $incType;
906 $this->flags
= $incFlags;
908 public function __set( $key, $value ) {
909 if (array_key_exists($key,get_object_vars($this))) {
910 $this->$key = $value;
911 if (is_null($this->type
))
918 public function __get( $key ) {
922 public function __call( $method, $args) {
923 $all = get_object_vars($this);
924 if ( preg_match( '/set_(.*)/', $method, $found ) ) {
925 if (array_key_exists( $found[1], $all)){
926 $this->$found[1] = $args[0];
929 } elseif ( preg_match( '/get_(.*)/', $method, $found ) ) {
930 if (array_key_exists( $found[1], $all)){
931 return $this->$found[1];
937 public function __destruct () {}
939 public function __toString() {
940 return (string)$this->value
;
943 static public function isValidType ($inc) {
944 if (in_array($inc, tdoAbstractField
::$validTypes)){
950 public function set_modifier ($modif) {
951 if (stristr($modif, '%s'))
952 $this->modifier
= $modif;
955 public function set_group ($true = true) {
956 $this->group
= (bool)$true;
959 public function set_order ($asc = true) {
960 $this->order
= (bool)$asc;
963 public function isIndex() {
964 return (($this->flags
& INDEX
) == INDEX
);
967 public function isPrimary() {
968 return (($this->flags
& PRIMARY
) == PRIMARY
);
971 public function isFullText() {
972 return (($this->flags
& FULLTEXT
) == FULLTEXT
);
974 public function isUnique () {
975 return (($this->flags
& UNIQUE
) == UNIQUE
);
977 public function setType () {
978 $incValue = $this->value
;
980 if (is_int($incValue)) {
982 } elseif (is_float($incValue)) {
983 $this->type
= 'FLOAT';
984 } elseif (is_string($incValue)) {
985 if (strtotime($this->value
)){
986 $this->type
= 'DATE';
987 }elseif (strlen($incValue) > 255)
988 $this->type
= 'TEXT';
990 $this->type
= 'VARCHAR';