* improved the loadFromArray method to support joined objects
[vsc.git] / _res / _libs / tdoabstract.class.php
blobdb15381c99c4b75beb141e2531d27771edbc9d19
1 <?php
2 /**
3 * @desc The Abstract Data Objects series
4 *
5 * @author Marius Orcsik <marius.orcsik@gmail.com>
6 * @version 0.0.1
7 */
9 class tdoAbstract {
10 public $db,
11 $wheres = array(),
12 $groups,
13 $orders,
14 $refers = array();
16 protected $id, // public to allow debugging outside the class
17 $name,
18 $alias,
19 $fields;
21 /**
22 * @desc function to implement get_ | set_ virtual methods
24 * @param string $method
25 * @param array $args
26 * @return bool|mixed
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]);
38 return true;
39 // check for obj members with $found[1] name
40 } elseif (array_key_exists( $found[1], $all)){
41 $this->$found[1] = $args[0];
42 return true;
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];
51 return false;
54 /**
55 * @desc A function to implement a virtual getter member of the class
57 * @param string $key
58 * @return mixed
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];
66 /**
67 * @desc A function to implement a virtual setter member for this class
69 * @param string $key
70 * @param mixed $value
71 * @return bool
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 (
77 array_key_exists ($key, $this->get_members()) /*&&
78 $this->isValidMember($this->fields[$key])*/
79 ) {
80 $this->fields[$key]->set_value ($value);
81 return true;
82 } else {
83 $this->fields[$key] = new tdoAbstractField ($key, $this->alias);
84 $this->fields[$key]->set_value ($value);
88 public function __construct(&$db) {
89 $this->db = &$db;
90 $this->alias = 'm1';
92 $this->instantiateMembers();
95 public function __destruct() {}
97 /**
98 * gets the members we consider table fields
100 * @return array ('fieldName' => tdoAbstractField)
102 public function &get_members () {
103 return $this->fields;
107 * checks if a field is a valid member of the current object
109 * @param tdoAbstractField $incMember
110 * @return bool
112 public function isValidMember ($incMember) {
113 if (
114 ! ($incMember instanceof tdoAbstractField ) ||
115 // this prevents an error in php > 5.2 object comparison
116 ! in_array ($incMember, $this->get_members() /*/, true/**/)
118 return false;
119 else
120 return true;
124 * instantiating the object's members
126 public function instantiateMembers () {
127 foreach ($this->get_members() as $key => $field) {
128 if ( is_int ($key) && is_string ($field) ) {
129 $this->fields[$field] = new tdoAbstractField ($field, $this->alias);
131 // FIXME: this is so bad :D - it implies that the primary key must be the first field containing _id :D
132 if (stristr ($field,'_id') && !isset ($this->id) && $this->isValidMember ($this->fields[$field])) {
133 $this->id = &$this->fields[$field];
135 unset ($this->fields[$key]);
139 $this->wheres = array();
140 $this->groups = '';
141 $this->orders = '';
142 $this->refers = array();
145 * setting the table's index field
147 * @param tdoAbstractField $obj
149 public function set_index (&$obj) {
150 if ($this->isValidMember($obj)) {
151 $this->id = &$obj;
152 $this->id->flags = PRIMARY;
157 * setting an alias for the table in case of joins.
158 * this method also sets the alias on all the fields of the current object
160 * @param string $alias
162 public function set_alias ($alias) {
163 foreach ($this->get_members() as $field){
164 if ($field->table == $this->alias) {
165 $field->table = 't'.$alias;
168 $this->alias = 't'.$alias;
172 * For adding custom sql functions for certain fields
174 * @param string $modif
175 * @param tdoAbstractField $field
177 public function set_fieldModifier ($modif, &$field) {
178 if ($this->isValidMember ($field) && stristr ($modif, '%'))
179 $field->set_modifier ($modif);
183 * method used in join cases to add the joined object's fields
184 * to the current one
186 * @param array ('fieldName' => tdoAbstractField) $incArr
188 public function addFields (&$incArr) {
189 if (is_array($incArr)) {
190 foreach ($incArr as $fieldName => $field) {
191 $this->fields[$fieldName] = $field;
197 * Based on field values, we get the _first_ row in the table that matches
199 * TODO: maybe we can have a function that returns _all_ the rows that match
201 * @return bool
203 public function buildObj () {
204 $sql = $this->buildSql(1);
205 $this->db->query( $sql );
207 $arr = $this->db->getAssoc();
208 if (is_array($arr)) {
209 foreach ($arr as $field => $var) {
210 $this->fields[$field]->set_value ($var);
212 return true;
214 return false;
218 * Gets the row in the table for $id
220 * @param mixed $id
223 public function get ($id) {
224 $id = $this->db->escape($id);
226 $this->instantiateMembers();
228 $this->id->set_value ($id);
229 $this->buildObj ();
232 * Returns the last id of the table
233 * OBS: this assumes that we didn't delete any rows from the table.
235 * @return int
237 public function getLastInsertedId() {
238 $sql = $this->db->_SELECT ($this->id->name);
239 $sql .= $this->db->_FROM ($this->name);
241 $this->addOrder($this->id, false);
242 // $orders[] = array($this->id->name, false);
244 $sql .= $this->db->_ORDER($this->outputOrders());
246 $sql .= ' LIMIT 1';
247 $this->db->query ($sql);
248 return $this->db->getScalar();
252 * encapsulating the $this->db->escape() method
254 * @param mixed $value
255 * @return mixed
257 public function escape ($value) {
258 if (is_numeric ($value)) {
259 return $value;
260 } else {
261 return '"'.$this->db->escape ($value).'"';
265 * inserting into the database
266 * TODO: multiple inserts to use with loadFromArray
268 * @return int
270 public function insert () {
271 $sql = 'INSERT INTO '.$this->name;
273 $fieldStr = '';
274 $valueStr = '';
275 $f = $this->get_members();
277 foreach ($f as $fieldName => $field) {
278 if ($this->isValidMember ($field) && ($field != $this->id) && !is_null ($field->value)) {
279 $value = $this->escape ($field->value);
281 $fieldStr.= (!empty ($fieldStr) ? ', ' : ' ') . $fieldName;
282 $valueStr.= (!empty ($valueStr) ? ', ' : ' ') . $value;
286 $sql.= ' ('.$fieldStr.') VALUES ('.$valueStr.')';
287 if ($fieldStr) {
288 $this->db->query($sql);
290 return $this->getLastInsertedId();
294 public function update ($id = null) {
295 if (empty ($id)) {
296 $id = $this->id->value;
299 if (!isset($id)) {
300 throw new Exception('Cannot update record in table '.$this->name.' because an id hasn\'t been provided');
301 return false;
304 $sql = $this->db->_UPDATE (array ($this->name,$this->alias) );
306 if (is_array ($this->refers) && !empty ($this->refers)){
307 $this->refers = array_reverse ($this->refers);
309 foreach ($this->refers as $ref)
310 $sql .= $ref;
313 $sql .= $this->db->_SET();
315 $fields = $this->get_members();
317 foreach ($fields as $fieldName => $field) {
318 if (($field instanceof tdoAbstractField) && $field != $this->id) { // TODO: make a more real check for field is an id
319 $value = $field->value;
322 if ((isset($value) && !is_null($value))) {
323 $sql.= $field->table.'.'.$fieldName.' = '.$this->escape ($value).', ';
327 $sql = substr ($sql, 0, -2);
329 $sql.= $this->db->_WHERE($this->alias.'.'.$this->id->name.' = '.$this->escape($id));
331 $this->db->query($sql);
333 return $id;
336 public function replace ( $id = null ) {
337 if ($id == null) {
338 $id = $this->id->value;
340 // var_dump ($this, 'repl');die;
342 if (!isset($id) || !$this->idExists( $id ) ) {
343 return $this->insert ();
344 } else {
345 return $this->update ($id);
349 // TODO: make this the same way the find first method works
350 public function delete ($id = null) {
351 if ($id == null && empty($this->wheres) && !empty($this->id->value)) {
352 $this->addWhere($this->id, '=', $this->id->value);
355 if (empty($this->wheres)) {
356 return false;
359 $temp = $this->alias;
360 $this->set_alias(null);
362 // var_dump($this->wheres);die;
363 $sql = 'DELETE FROM '.$this->name.' WHERE '; //$this->id->name.' = '.$this->escape($value).' LIMIT 1';
364 $sql.= $this->outputWheres();
365 // echo $this->outputWheres(false);die;
366 $affRows = $this->db->query($sql);
368 $this->set_alias($temp);
369 return $affRows;
372 public function reset () {
373 foreach ($this->fields as $key => $field) {
374 if ($field instanceof tdoAbstractField) {
375 $this->fields[$key] = new tdoAbstractField($key, $this->alias);
379 $this->wheres = array();
380 $this->groups = array();
381 $this->orders = array();
382 $this->refers = array();
383 return true;
386 public function idExists ($id = null) {
387 if (!$id) {
388 $id = $this->id->value;
390 if (!$id)
391 return false;
393 $this->id->set_modifier ('COUNT (%s)');
395 $sql = $this->db->_SELECT ($this->id).
396 $this->db->_FROM ($this->name).
397 $this->db->_WHERE ($this->id->name.' = '.$this->escape($id));
399 $this->db->query($sql);
401 if ($this->db->getScalar())
402 return true;
403 else
404 return false;
407 protected function outputFieldList () {
408 $fields = '';
409 $f = $this->get_members();
410 // var_dump($f);
411 foreach ($f as $fieldName => $field) {
412 if ($this->isValidMember ($field)) {
414 if (!is_null ($field->modifier)) {
415 // i replaced str_replace ('%s', $curField, '%s', $curField)
416 // as sometimes I might need it for something else than %s
417 $fields .= sprintf ($field->modifier, (!is_null ($field->table) ? $field->table . '.' : '') . $field->name) .
418 $this->db->_AS($field->name) . ', ';
419 } elseif ( !$field->inWhere ()) {
420 $fields .= (!is_null ($field->table) ? $field->table.'.' : '') . $field->name . ', ';
423 // $fields .= $curField;
424 } else {
425 throw new Exception ($fieldName . ' is not a valid member of ' . get_class($this));
426 return false;
429 return substr ($fields,0,-2);//$fields;
432 protected function outputWheres ($bIW = true) {
433 return implode ($this->db->_AND(), $this->wheres);
436 protected function outputGroups () {
437 // groups
438 $groups = '';
439 $f = $this->get_members();
440 foreach ($f as $field)
441 if (!is_null ($field->group) )
442 $groups .= (!empty($groups) ? ', ' : '') . $field->name;
444 return $groups;
447 protected function outputOrders () {
448 $orders = '';
449 $f = $this->get_members();
450 foreach ($f as $field)
451 if (!is_null($field->order)) {
452 $orders .= (!empty($orders) ? ', ': '').$field->name.($field->order == true ? ' ASC' : ' DESC');
454 return $orders;
457 protected function outputRefers () {
458 $refers= '';
460 if (is_array($this->refers) && !empty($this->refers)){
461 $this->refers = array_reverse($this->refers);
462 foreach ($this->refers as $ref)
463 $refers .= $ref;
466 return $refers;
469 protected function buildInherentWheres () {
470 $diff = $this->get_members();
471 // let's hope this doesn't break stuff.
472 // it's needed when we use more queries on the same instance of the object :D
474 if (is_array ($diff)) {
475 foreach ($diff as $fieldName => $field) {
476 if ( $this->isValidMember ($field) && $field->value != null) {
477 $this->addWhere ($field, '=', $this->escape($field->value));
478 } else {
479 // throw new Exception($fieldName . ' is not a valid member of ' . get_class($this));
484 if (empty($this->wheres)) {
485 $t = 1;
486 $this->wheres[] = new tdoAbstractClause($t);
489 // var_dump($this->wheres);
492 * function to add an abstract clause to the current object if it doesn't exist
494 * @param tdoAbstractField|tdoAbstractClause $field1
495 * @param string $condition
496 * @param string $field2
498 public function addWhere (&$field1, $condition = null, $field2 = null) {
499 if (($field1 instanceof tdoAbstractClause) && ($condition == null || $field2 == null)) {
500 $w = &$field1;
501 } else {
502 $w = new tdoAbstractClause ($field1, $condition, $field2);
504 // this might generate an infinite recursion error on some PHP > 5.2 due to object comparison
505 if (!in_array ($w, $this->wheres /*, true */))
506 $this->wheres[] = &$w;
509 public function addOrder (&$orderField, $asc = true) {
510 if (!($orderField instanceof tdoAbstractField) && is_string ($orderField)) {
511 $orderField = &$this->fields[$orderField];
513 if ($this->isValidMember ($orderField))
514 $orderField->set_order ($asc);
517 public function addGroup (&$groupField) {
518 if (!($groupField instanceof tdoAbstractField) && is_string ($groupField)) {
519 $groupField = &$this->fields[$groupField];
521 if (($groupField instanceof tdoAbstractField)) {
522 $groupField->set_group (true);
527 * building a normal SELECT query
529 * @param int $start
530 * @param int $end
531 * @return string
533 private function buildSql($start = 0, $end = 0) {
534 $sql = $this->db->_SELECT($this->outputFieldList()). ' FROM '.$this->name.' AS '.$this->alias.' ';
536 $this->buildInherentWheres(); // will it work
538 $sql .= $this->outputRefers();
540 $sql .= $this->db->_WHERE($this->outputWheres());// add LIMITS
542 $sql .= $this->db->_GROUP($this->outputGroups());
544 $sql .= $this->db->_ORDER($this->outputOrders());
546 if (!empty($start))
547 $sql .= $this->db->_LIMIT($start, $end);
549 return $sql;
552 public function find ($start = 0, $count = 0) {
553 $result = $this->db->query ($this->buildSql(), $start, $count);
554 return $result;
557 public function findFirst () {
558 $this->buildInherentWheres();
560 $this->db->query($this->buildSql(), 0, 1);
561 $row = $this->db->getAssoc();
562 if (is_array($row))
563 foreach ($row as $field => $value){
564 $this->fields[$field]->value = $value;
568 public function getArray ($start = 0, $count = 0, $orderBy = null) {
569 $this->buildInherentWheres ();
571 $sql = $this->buildSql ($start, $count, $orderBy);
573 $this->db->query ($sql);
574 return $this->db->getArray();
577 public function getCount() {
578 $this->set_fieldModifier('COUNT(%s)', $this->id);
579 $this->db->query($this->buildSql());
580 return $this->db->getScalar();
583 public function getVector() {
584 // I really don't see why we need this. ?
588 * Function to load the values of current object from an array
589 * of type field_name => field_value
590 * If strict is false, the current object can have fields that are not already
591 * present in $this->fields[]
593 * @param array $valArray
594 * @param bool $strict
596 public function loadFromArray ($valArray, $strict = true) {
597 foreach ($valArray as $fieldName => $value) {
598 if (array_key_exists ($fieldName, $this->fields)) {
599 $this->fields[$fieldName]->set_value($value);
600 } elseif (!strinct) {
601 // if the field name is not in the field list of the current object
602 // it means that the valArray object is got from an JOIN sql
603 $this->fields[$fieldName] = new tdoAbstractField($field,'j1');
604 $this->fields[$fieldName]->set_value ($value);
610 * FIXME: make it work when composing with the same object
612 * @param tdoAbstract $incOb
613 * @return void
615 private function composeObj ($incOb) {
616 if (!($incOb instanceof tdoAbstract))
617 return false;
618 $refs = count($this->refers);
620 foreach ($incOb->refers as $alias => $ref) {
621 $tAl = $refs++;
623 $this->refers[$tAl] = $ref;//str_replace(array($alias, $aliases[$alias][1]), array($tAl, $aliases[$alias][2]), $ref);
624 $this->refers[$tAl]->set_state($tAl);
629 * Function to execute a join between two tdoAbstract objects
631 * @param string $jType
632 * @param tdoAbstractField $thisJField
633 * @param tdoAbstract $incOb
634 * @param tdoAbstractField $incObJField
635 * @return unknown
637 public function joinWith ($jType = null, &$thisJField = null, &$incOb = null, &$incObJField = null) {
638 if (
639 !tdoAbstractJoin::isValidType($jType) ||
640 !$this->isValidMember ($thisJField)
642 return false;
645 $this->composeObj ($incOb);
647 $tAl = (count($this->refers));
648 if ($tAl > 59) {
649 trigger_error('Join aborted for table '.$this->name.': Too many tables; MySQL can only use 61 tables in a join', E_USER_NOTICE);
650 return;
651 } else {
652 $incOb->set_alias($tAl);
654 if($thisJField == null || !($thisJField instanceof tdoAbstractField))
655 $thisJField = $this->id;
657 if($incObJField == null || !($incObJField instanceof tdoAbstractField))
658 $incObJField = $incOb->id;
660 $this->refers[$tAl] = new tdoAbstractJoin ($jType, $this, $incOb, $thisJField, $incObJField, $tAl);
661 $this->refers[$tAl]->set_state ($tAl);
666 * function to dump a <type>Sql
667 * problem with the field types. :D
669 * @param tdoAbstract $obj
671 static public function dumpSchema ($obj) {
672 if ($obj instanceof tdoAbstract)
673 throw new Exception('Can\'t generate sql dump');
675 $sql = 'CREATE TABLE '. $obj->name . ' (';
677 foreach ($obj->getFields() as $fieldName => $data) {
678 $sql .= $fieldName.' '.$data[0].', ';
680 $sql .= ')';
681 return $sql;
687 * class to abstract a where clause in a SQL query
688 * TODO: add possibility of complex wheres: (t1 condition1 OR|AND|XOR t2.condition2)
689 * TODO: abstract the condition part of a where clause - currently string based :D
691 class tdoAbstractClause {
692 protected $subject, $predicate, $predicative;
695 * initializing a WHERE|ON clause
697 * @param tdoAbstractClause|tdoAbstractField $subject
698 * @param string $predicate
699 * @param tdoAbstractClause|tdoAbstractField|null $complement
701 public function __construct ($subject, $predicate = null, $predicative = null) {
702 if ($subject == 1 && $predicate == null && $predicative == null) {
703 $this->subject = '1';
704 $this->predicate = '';
705 $this->predicative = '';
706 return;
709 if (($subject instanceof tdoAbstractField ) || ($subject instanceof tdoAbstractClause )) {
710 $this->subject = &$subject;
712 $subject->set_where = true;
714 $this->predicative = &$predicative;
716 if ($this->validPredicative ($predicate))
717 $this->predicate = $predicate;
719 } else {
720 $this->subject = '';
721 $this->predicate = '';
722 $this->predicative = '';
724 return;
727 public function __destruct () {}
729 public function __toString () {
730 // var_dump($this->subject, $this->predicate, $this->predicative);
732 if ($this->subject instanceof tdoAbstractClause) {
733 $subStr = (string)$this->subject;
734 } elseif ($this->subject instanceof tdoAbstractField) {
735 // without $this->subject->table != 't' we have a bug in the delete op
736 $subStr = ($this->subject->table != 't' ? $this->subject->table.'.': '').$this->subject->name;
737 } elseif ($this->subject == '1') {
738 return $this->subject;
739 } else {
740 return '';
743 if (is_null($this->predicative)) {
744 if ($this->validPredicative($this->predicate)) {
745 $preStr = 'NULL';
746 } else
747 $preStr = '';
748 } elseif (is_numeric($this->predicative)) {
749 $preStr = $this->predicative;
750 } elseif (is_string($this->predicative)) {
751 $preStr = $this->predicative;
753 if ($this->predicate == 'LIKE') {
754 $preStr = '%'.$this->predicate.'%';
757 $preStr = (stripos($preStr, '"') !== 0 ? '"'.$preStr.'"' : $preStr);//'"'.$preStr.'"';
758 } elseif (is_array($this->predicative)) {
759 $preStr = '("'.implode('", "',$this->predicative).'")';
760 } elseif ($this->predicative instanceof tdoAbstractfield) {
761 $preStr = ($this->predicative->table != 't' ? $this->predicative->table.'.': '').$this->predicative->name;
762 } elseif ($this->predicative instanceof tdoAbstractClause) {
763 $subStr = $subStr;
764 $preStr = $this->predicative;
767 $retStr = $subStr.' '.$this->predicate.' '.$preStr;
768 if (($this->subject instanceof tdoAbstractClause) && ($this->predicative instanceof tdoAbstractClause))
769 return '('.$retStr.')';
771 return $retStr;
774 private function validPredicative ($predicate) {
775 // need to find a way to abstract these
776 // $validPredicates = array (
777 // 'AND',
778 // '&&',
779 // 'OR',
780 // '||',
781 // 'XOR',
782 // 'IS',
783 // 'IS NOT',
784 // '!',
785 // 'IN',
786 // 'LIKE',
787 // '=',
788 // '!=',
789 // '<>'
790 // );
791 if ($this->predicative instanceof tdoAbstractClause) {
792 // we'll have Subject AND|OR|XOR Predicative
793 $validPredicates = array (
794 'AND',
795 '&&',
796 'OR',
797 '||',
798 'XOR'
800 } elseif (($this->predicative instanceof tdoAbstractField) || is_numeric($this->predicative)) {
801 // we'll have Subject =|!= Predicative
802 $validPredicates = array (
803 '=',
804 '!=',
805 '>',
806 '<',
807 '>=',
808 '<='
810 } elseif (is_array($this->predicative)) {
811 $validPredicates = array (
812 'IN',
813 'NOT IN'
815 } elseif (is_string($this->predicative)) {
816 $validPredicates = array (
817 '=',
818 'LIKE',
819 // dates
820 '>',
821 '<',
822 '>=',
823 '<='
825 } elseif (is_null($this->predicative)) {
826 $validPredicates = array (
827 'IS',
828 'IS NOT'
832 return in_array($predicate, $validPredicates);
834 // if (in_array($predicate, $validPredicates) && (($predicative instanceof tdoAbstractClause) || ($predicative instanceof tdoAbstractField)))
835 // return true;
836 // return false;
840 class tdoAbstractJoin {
841 static public $validTypes = array (
842 'INNER',
843 'LEFT',
844 'RIGHT',
845 'OUTER'
847 protected $state,
848 $type,
850 $leftTable,
851 $rightTable,
853 $leftField,
854 $rightField;
856 public function __construct ($type, &$lt, &$rt, &$lf, &$rf, $state) {
857 if ($rt instanceof tdoAbstract ||
858 $lt instanceof tdoAbstract
860 $this->leftTable = &$lt;
861 $this->rightTable = &$rt;
863 if (tdoAbstractJoin::isValidType($type))
864 $this->type = $type;
866 if (
867 $lf instanceof tdoAbstractField &&
868 $rf instanceof tdoAbstractField
870 $this->rightField = &$rf;
871 $this->leftField = &$lf;
874 $this->state = $state;
877 $this->composeFields ();
878 $this->composeWheres ();
882 public function __destruct () {}
884 public function __toString() {
885 $lAlias = $this->leftTable->get_alias();
886 return (string)$this->type.' JOIN '.$this->rightTable->get_name().
887 ' AS t'.$this->state.' ON '.(isset($lAlias) ? $lAlias : $this->leftTable->get_name()).
888 '.'.$this->leftField->name.
889 ' = t'.$this->state.'.'.$this->rightField->name.' ';
893 * Will compose the $rightTable and $leftTable fields
894 * @return void
896 public function composeFields () {
897 $leftFields = $this->leftTable->get_members();
899 $this->rightTable->set_alias($this->state);
900 $rightFields = $this->rightTable->get_members();
903 $this->leftTable->addFields($rightFields);
907 * Will compose the $rightTable and $leftTable WHERE clauses
908 * @return void
910 public function composeWheres () {
911 if (!is_array($this->leftTable->wheres))
912 $this->leftTable->wheres = array();
913 if (!is_array($this->rightTable->wheres))
914 $this->rightTable->wheres = array();
916 // var_dump($this->rightTable->wheres, $this->leftTable );die;
917 foreach ($this->rightTable->wheres as $where) {
918 $this->leftTable->addWhere ($where);
920 $this->leftTable->wheres = &array_merge($this->rightTable->wheres, $this->leftTable->wheres);
923 public function set_state ($st) {
924 $this->state = $st;
927 static public function isValidType ($inc) {
928 if (in_array($inc, tdoAbstractJoin::$validTypes))
929 return true;
930 return false;
934 define ('INDEX', 1);
935 define ('PRIMARY', 2);
936 define ('UNIQUE', 4);
937 define ('FULLTEXT', 8);
939 class tdoAbstractField {
940 static public $validTypes = array (
941 'VARCHAR',
942 'INT',
943 'DATE',
944 'TEXT',
945 'FLOAT',
946 'TIMESTAMP',
947 'ENUM'
950 protected $name, $type, $flags, $value, $table, $modifier = null, $order = null, $group = null, $where = false;
952 public function __construct ($incName, $incTable, $incType='INT', $incFlags=0) {
953 $this->name = $incName;
954 $this->table = $incTable;
955 $this->type = $incType;
956 $this->flags = $incFlags;
959 public function __set ( $key, $value ) {
960 if ( array_key_exists ($key, get_object_vars($this)) ) {
961 $this->$key = $value;
963 // if ($key == 'where')
964 // var_dump($this);
966 if ( is_null ($this->type) )
967 $this->setType();
969 return true;
970 } else
971 return false;
974 public function __get ( $key ) {
975 return $this->$key;
978 public function __call ( $method, $args) {
979 $all = get_object_vars($this);
981 if ( preg_match( '/set_(.*)/', $method, $found ) ) {
982 if (array_key_exists( $found[1], $all)){
983 $this->$found[1] = $args[0];
984 return true;
986 } elseif ( preg_match( '/get_(.*)/', $method, $found ) ) {
987 if (array_key_exists( $found[1], $all)){
988 return $this->$found[1];
991 return false;
994 public function __destruct () {}
996 public function __toString () {
997 return (string)$this->value;
1000 // public function inWhere () {
1001 // return $this->where;
1002 // }
1004 static public function isValidType ($inc) {
1005 if (in_array($inc, tdoAbstractField::$validTypes)){
1006 return true;
1008 return false;
1011 public function set_modifier ($modif) {
1012 // if (stristr($modif, '%'))
1013 $this->modifier = $modif;
1016 public function set_value ($value) {
1017 $this->value = $value;
1020 public function set_group ($true = true) {
1021 $this->group = (bool)$true;
1024 public function set_order ($asc = true) {
1025 $this->order = (bool)$asc;
1028 public function isIndex() {
1029 return (($this->flags & INDEX) == INDEX);
1032 public function isPrimary() {
1033 return (($this->flags & PRIMARY) == PRIMARY);
1036 public function isFullText() {
1037 return (($this->flags & FULLTEXT) == FULLTEXT);
1039 public function isUnique () {
1040 return (($this->flags & UNIQUE) == UNIQUE);
1042 public function setType () {
1043 $incValue = $this->value;
1044 // TODO : enums
1045 if (is_int($incValue)) {
1046 $this->type = 'INT';
1047 } elseif (is_float($incValue)) {
1048 $this->type = 'FLOAT';
1049 } elseif (is_string($incValue)) {
1050 if (strtotime($this->value)){
1051 $this->type = 'DATE';
1052 }elseif (strlen($incValue) > 255)
1053 $this->type = 'TEXT';
1054 else
1055 $this->type = 'VARCHAR';