Revert "* initial work towards factorizing the controllers (yeah, now it's tsControll...
[vsc.git] / _res / _libs / tdoabstract.class.php
blob6787dc309fc3002a8edbfe044452525d75e83796
1 <?php
2 /**
3 * @desc The Abstract Data Objects series
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 .$this->db->_FROM ($this->name);
241 $this->addOrder($this->id, false);
243 $sql .= $this->db->_ORDER ($this->outputOrders ());
245 $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 = $this->db->_INSERT ($this->name);//`'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 (is_null ($id)) {
296 $id = $this->id->value;
299 if (is_null ($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 // var_dump($sql);die ('<br/>update');
332 $this->db->query($sql);
334 return $id;
337 public function replace ( $id = null ) {
338 if (is_null($id)) {
339 $id = $this->id->value;
342 if (is_null($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 $id = (!is_null ($id) ? $id :$this->id->value);
353 if (!is_null ($id)) {
354 // no need for other wheres
355 $this->wheres = array();
356 $this->addWhere ($this->id, '=', $id);
359 if (empty($this->wheres)) {
360 return false;
363 $temp = $this->alias;
364 $this->set_alias(null);
367 $sql = 'DELETE FROM '.$this->name.' WHERE '; //$this->id->name.' = '.$this->escape($value).' LIMIT 1';
368 $sql.= $this->outputWheres();
369 // echo $sql;die;
370 $affRows = $this->db->query($sql);
372 $this->set_alias($temp);
373 return $affRows;
376 public function reset () {
377 foreach ($this->fields as $key => $field) {
378 if ($field instanceof tdoAbstractField) {
379 $this->fields[$key] = new tdoAbstractField($key, $this->alias);
383 $this->wheres = array();
384 $this->groups = array();
385 $this->orders = array();
386 $this->refers = array();
387 return true;
390 public function idExists ($id = null) {
391 if (is_null($id)) {
392 $id = $this->id->value;
394 if (is_null($id))
395 return false;
397 $this->id->set_modifier ('COUNT(%s)');
399 $t = sprintf ($this->id->modifier, $this->id->name);
401 $sql = $this->db->_SELECT ($t).
402 $this->db->_FROM ($this->name).
403 $this->db->_WHERE ($this->id->name.' = '.$this->escape($id));
405 $this->db->query($sql);
407 if ($this->db->getScalar()) {
408 return true;
409 } else {
410 return false;
414 protected function outputFieldList () {
415 $fields = '';
416 $f = $this->get_members();
417 // var_dump($f);
418 foreach ($f as $fieldName => $field) {
419 if ($this->isValidMember ($field)) {
421 if (!is_null ($field->modifier)) {
422 // i replaced str_replace ('%s', $curField, '%s', $curField)
423 // as sometimes I might need it for something else than %s
424 $fields .= sprintf ($field->modifier, (!is_null ($field->table) ? $field->table . '.' : '') . $field->name) .
425 $this->db->_AS($field->name) . ', ';
426 } elseif ( !$field->inWhere ()) {
427 $fields .= (!is_null ($field->table) ? $field->table.'.' : '') . $field->name . ', ';
430 } else {
431 throw new Exception ($fieldName . ' is not a valid member of ' . get_class($this));
432 return false;
435 return substr ($fields,0,-2);//$fields;
438 protected function outputWheres ($bIW = true) {
439 // var_dump($this->wheres);
440 return implode ($this->db->_AND(), $this->wheres);
443 protected function outputGroups () {
444 // groups
445 $groups = '';
446 $f = $this->get_members ();
447 foreach ($f as $field)
448 if (!is_null ($field->group) )
449 $groups .= (!empty($groups) ? ', ' : '') . $field->name;
451 return $groups;
454 protected function outputOrders () {
455 $orders = '';
456 $f = $this->get_members();
457 foreach ($f as $field)
458 if (!is_null($field->order)) {
459 $orders .= (!empty($orders) ? ', ': '').$field->name.($field->order == true ? ' ASC' : ' DESC');
461 return $orders;
464 protected function outputRefers () {
465 $refers= '';
467 if (is_array($this->refers) && !empty($this->refers)){
468 $this->refers = array_reverse($this->refers);
469 foreach ($this->refers as $ref)
470 $refers .= $ref;
473 return $refers;
476 protected function buildInherentWheres () {
477 $diff = $this->get_members();
478 // let's hope this doesn't break stuff.
479 // it's needed when we use more queries on the same instance of the object :D
481 if (is_array ($diff)) {
482 foreach ($diff as $fieldName => $field) {
483 if ( $this->isValidMember ($field) ) {
484 if (!is_null($field->value))
485 $this->addWhere ($field, '=', $this->escape($field->value));
486 } else {
487 throw new Exception ($fieldName . ' is not a valid member of ' . get_class($this));
492 if (empty ($this->wheres)) {
493 $t = '1';
494 $this->wheres[] = new tdoAbstractClause ($t);
496 // var_dump($this->wheres);
499 * function to add an abstract clause to the current object if it doesn't exist
501 * @param tdoAbstractField|tdoAbstractClause $field1
502 * @param string $condition
503 * @param string $field2
505 public function addWhere (&$field1, $condition = null, $field2 = null) {
506 if (($field1 instanceof tdoAbstractClause) && ($condition == null || $field2 == null)) {
507 $w = &$field1;
508 } else {
509 $w = new tdoAbstractClause ($field1, $condition, $field2);
511 // this might generate an infinite recursion error on some PHP > 5.2 due to object comparison
512 if (!in_array ($w, $this->wheres /*, true */))
513 $this->wheres[] = &$w;
516 public function addOrder (&$orderField, $asc = true) {
517 if (!($orderField instanceof tdoAbstractField) && is_string ($orderField)) {
518 $orderField = &$this->fields[$orderField];
520 if ($this->isValidMember ($orderField))
521 $orderField->set_order ($asc);
524 public function addGroup (&$groupField) {
525 if (!($groupField instanceof tdoAbstractField) && is_string ($groupField)) {
526 $groupField = &$this->fields[$groupField];
528 if (($groupField instanceof tdoAbstractField)) {
529 $groupField->set_group (true);
534 * building a normal SELECT query
536 * @param int $start
537 * @param int $end
538 * @return string
540 protected function buildSql ($start = 0, $end = 0) {
541 $sql = $this->db->_SELECT($this->outputFieldList()). ' FROM '.$this->name.' AS '.$this->alias.' ';
543 $this->buildInherentWheres(); // will it work
545 $sql .= $this->outputRefers();
547 $sql .= $this->db->_WHERE($this->outputWheres());// add LIMITS
549 $sql .= $this->db->_GROUP($this->outputGroups());
551 $sql .= $this->db->_ORDER($this->outputOrders());
553 if (!empty($start))
554 $sql .= $this->db->_LIMIT($start, $end);
556 return $sql;
559 public function find ($start = 0, $count = 0) {
560 $result = $this->db->query ($this->buildSql(), $start, $count);
561 return $result;
564 public function findFirst () {
565 $this->buildInherentWheres();
567 $this->db->query($this->buildSql(), 0, 1);
568 $row = $this->db->getAssoc();
569 if (is_array($row))
570 foreach ($row as $field => $value){
571 $this->fields[$field]->value = $value;
575 public function getArray ($start = 0, $count = 0, $orderBy = null) {
576 $this->buildInherentWheres ();
578 $sql = $this->buildSql ($start, $count, $orderBy);
580 $this->db->query ($sql);
581 return $this->db->getArray ();
584 public function getCount() {
585 $this->set_fieldModifier('COUNT(%s)', $this->id);
586 $this->db->query($this->buildSql());
587 return $this->db->getScalar();
590 public function getVector() {
591 // I really don't see why we need this. ?
595 * Function to load the values of current object from an array
596 * of type field_name => field_value
597 * If strict is false, the current object can have fields that are not already
598 * present in $this->fields[]
600 * @param array $valArray
601 * @param bool $strict
603 public function loadFromArray ($valArray, $strict = true) {
604 foreach ($valArray as $fieldName => $value) {
605 if (array_key_exists ($fieldName, $this->fields)) {
606 $this->fields[$fieldName]->set_value($value);
607 } elseif (!strinct) {
608 // if the field name is not in the field list of the current object
609 // it means that the valArray object is got from an JOIN sql
610 $this->fields[$fieldName] = new tdoAbstractField($field,'j1');
611 $this->fields[$fieldName]->set_value ($value);
617 * FIXME: make it work when composing with the same object
619 * @param tdoAbstract $incOb
620 * @return void
622 private function composeObj ($incOb) {
623 if (!($incOb instanceof tdoAbstract))
624 return false;
625 $refs = count($this->refers);
627 foreach ($incOb->refers as $alias => $ref) {
628 $tAl = $refs++;
630 $this->refers[$tAl] = $ref;//str_replace(array($alias, $aliases[$alias][1]), array($tAl, $aliases[$alias][2]), $ref);
631 $this->refers[$tAl]->set_state($tAl);
636 * Function to execute a join between two tdoAbstract objects
638 * @param string $jType
639 * @param tdoAbstractField $thisJField
640 * @param tdoAbstract $incOb
641 * @param tdoAbstractField $incObJField
642 * @return unknown
644 public function joinWith ($jType = null, &$thisJField = null, &$incOb = null, &$incObJField = null) {
645 if (
646 !tdoAbstractJoin::isValidType($jType) ||
647 !$this->isValidMember ($thisJField)
649 return false;
652 $this->composeObj ($incOb);
654 $tAl = (count($this->refers));
655 if ($tAl > 59) {
656 trigger_error('Join aborted for table '.$this->name.': Too many tables; MySQL can only use 61 tables in a join', E_USER_NOTICE);
657 return;
658 } else {
659 $incOb->set_alias($tAl);
661 if($thisJField == null || !($thisJField instanceof tdoAbstractField))
662 $thisJField = $this->id;
664 if($incObJField == null || !($incObJField instanceof tdoAbstractField))
665 $incObJField = $incOb->id;
667 $this->refers[$tAl] = new tdoAbstractJoin ($jType, $this, $incOb, $thisJField, $incObJField, $tAl);
668 $this->refers[$tAl]->set_state ($tAl);
673 * function to dump a <type>Sql
674 * problem with the field types. :D
676 * @param tdoAbstract $obj
678 static public function dumpSchema ($obj) {
679 if ($obj instanceof tdoAbstract)
680 throw new Exception('Can\'t generate sql dump');
682 $sql = 'CREATE TABLE '. $obj->name . ' (';
684 foreach ($obj->getFields() as $fieldName => $data) {
685 $sql .= $fieldName.' '.$data[0].', ';
687 $sql .= ')';
688 return $sql;
694 * class to abstract a where clause in a SQL query
695 * TODO: add possibility of complex wheres: (t1 condition1 OR|AND|XOR t2.condition2)
696 * TODO: abstract the condition part of a where clause - currently string based :D
698 class tdoAbstractClause {
699 protected $subject, $predicate, $predicative;
702 * initializing a WHERE|ON clause
704 * @param tdoAbstractClause|tdoAbstractField $subject
705 * @param string $predicate
706 * @param tdoAbstractClause|tdoAbstractField|null $complement
708 public function __construct ($subject, $predicate = null, $predicative = null) {
709 // I must be careful with the $subject == 1 because (int)object == 1
710 if (($subject === 1 || is_string($subject)) && $predicate == null && $predicative == null) {
711 $this->subject = $subject;
712 $this->predicate = '';
713 $this->predicative = '';
714 return;
717 if (($subject instanceof tdoAbstractField ) || ($subject instanceof tdoAbstractClause )) {
718 $this->subject = &$subject;
720 $subject->set_where = true;
722 $this->predicative = &$predicative;
724 if ($this->validPredicative ($predicate))
725 $this->predicate = $predicate;
727 } else {
728 $this->subject = '';
729 $this->predicate = '';
730 $this->predicative = '';
732 return;
735 public function __destruct () {}
737 public function __toString () {
738 // var_dump($this->subject, $this->predicate, $this->predicative);
739 // echo '<br/>';
740 if ($this->subject === '1' || is_string($this->subject)) {
741 // var_dump($this->subject);
742 return (string)$this->subject;
743 } elseif ($this->subject instanceof tdoAbstractClause) {
744 $subStr = (string)$this->subject;
745 } elseif ($this->subject instanceof tdoAbstractField) {
746 // without $this->subject->table != 't' we have a bug in the delete op
747 $subStr = ($this->subject->table != 't' ? $this->subject->table.'.': '').$this->subject->name;
748 } else {
749 return '';
752 if (is_null($this->predicative)) {
753 if ($this->validPredicative ($this->predicate)) {
754 $preStr = 'NULL';
755 } else
756 $preStr = '';
757 } elseif (is_numeric($this->predicative)) {
758 $preStr = $this->predicative;
759 } elseif (is_string($this->predicative)) {
760 $preStr = $this->predicative;
762 if ($this->predicate == 'LIKE') {
763 $preStr = '%'.$this->predicate.'%';
766 $preStr = (stripos($preStr, '"') !== 0 ? '"'.$preStr.'"' : $preStr);//'"'.$preStr.'"';
767 } elseif (is_array($this->predicative)) {
768 $preStr = '("'.implode('", "',$this->predicative).'")';
769 } elseif ($this->predicative instanceof tdoAbstractfield) {
770 $preStr = ($this->predicative->table != 't' ? $this->predicative->table.'.': '').$this->predicative->name;
771 } elseif ($this->predicative instanceof tdoAbstractClause) {
772 $subStr = $subStr;
773 $preStr = $this->predicative;
776 $retStr = $subStr.' '.$this->predicate.' '.$preStr;
777 if (($this->subject instanceof tdoAbstractClause) && ($this->predicative instanceof tdoAbstractClause))
778 return '('.$retStr.')';
780 return $retStr;
783 private function validPredicative ($predicate) {
784 // need to find a way to abstract these
785 // $validPredicates = array (
786 // 'AND',
787 // '&&',
788 // 'OR',
789 // '||',
790 // 'XOR',
791 // 'IS',
792 // 'IS NOT',
793 // '!',
794 // 'IN',
795 // 'LIKE',
796 // '=',
797 // '!=',
798 // '<>'
799 // );
800 if ($this->predicative instanceof tdoAbstractClause) {
801 // we'll have Subject AND|OR|XOR Predicative
802 $validPredicates = array (
803 'AND',
804 '&&',
805 'OR',
806 '||',
807 'XOR'
809 } elseif (($this->predicative instanceof tdoAbstractField) || is_numeric($this->predicative)) {
810 // we'll have Subject =|!= Predicative
811 $validPredicates = array (
812 '=',
813 '!=',
814 '>',
815 '<',
816 '>=',
817 '<='
819 } elseif (is_array($this->predicative)) {
820 $validPredicates = array (
821 'IN',
822 'NOT IN'
824 } elseif (is_string($this->predicative)) {
825 $validPredicates = array (
826 '=',
827 'LIKE',
828 // dates
829 '>',
830 '<',
831 '>=',
832 '<='
834 } elseif (is_null($this->predicative)) {
835 $validPredicates = array (
836 'IS',
837 'IS NOT'
841 return in_array($predicate, $validPredicates);
843 // if (in_array($predicate, $validPredicates) && (($predicative instanceof tdoAbstractClause) || ($predicative instanceof tdoAbstractField)))
844 // return true;
845 // return false;
849 class tdoAbstractJoin {
850 static public $validTypes = array (
851 'INNER',
852 'LEFT',
853 'RIGHT',
854 'OUTER'
856 protected $state,
857 $type,
859 $leftTable,
860 $rightTable,
862 $leftField,
863 $rightField;
865 public function __construct ($type, &$lt, &$rt, &$lf, &$rf, $state) {
866 if ($rt instanceof tdoAbstract ||
867 $lt instanceof tdoAbstract
869 $this->leftTable = &$lt;
870 $this->rightTable = &$rt;
872 if (tdoAbstractJoin::isValidType($type))
873 $this->type = $type;
875 if (
876 $lf instanceof tdoAbstractField &&
877 $rf instanceof tdoAbstractField
879 $this->rightField = &$rf;
880 $this->leftField = &$lf;
883 $this->state = $state;
886 $this->composeFields ();
887 $this->composeWheres ();
891 public function __destruct () {}
893 public function __toString() {
894 $lAlias = $this->leftTable->get_alias();
895 return (string)$this->type.' JOIN '.$this->rightTable->get_name().
896 ' AS t'.$this->state.' ON '.(isset($lAlias) ? $lAlias : $this->leftTable->get_name()).
897 '.'.$this->leftField->name.
898 ' = t'.$this->state.'.'.$this->rightField->name.' ';
902 * Will compose the $rightTable and $leftTable fields
903 * @return void
905 public function composeFields () {
906 $leftFields = $this->leftTable->get_members();
908 $this->rightTable->set_alias($this->state);
909 $rightFields = $this->rightTable->get_members();
912 $this->leftTable->addFields($rightFields);
916 * Will compose the $rightTable and $leftTable WHERE clauses
917 * @return void
919 public function composeWheres () {
920 if (!is_array($this->leftTable->wheres))
921 $this->leftTable->wheres = array();
922 if (!is_array($this->rightTable->wheres))
923 $this->rightTable->wheres = array();
925 // var_dump($this->rightTable->wheres, $this->leftTable );die;
926 foreach ($this->rightTable->wheres as $where) {
927 $this->leftTable->addWhere ($where);
929 $this->leftTable->wheres = &array_merge($this->rightTable->wheres, $this->leftTable->wheres);
932 public function set_state ($st) {
933 $this->state = $st;
936 static public function isValidType ($inc) {
937 if (in_array($inc, tdoAbstractJoin::$validTypes))
938 return true;
939 return false;
943 define ('INDEX', 1);
944 define ('PRIMARY', 2);
945 define ('UNIQUE', 4);
946 define ('FULLTEXT', 8);
948 class tdoAbstractField {
949 static public $validTypes = array (
950 'VARCHAR',
951 'INT',
952 'DATE',
953 'TEXT',
954 'FLOAT',
955 'TIMESTAMP',
956 'ENUM'
959 protected $name, $type, $flags, $value, $table, $modifier = null, $order = null, $group = null, $where = false;
961 public function __construct ($incName, $incTable, $incType='INT', $incFlags=0) {
962 $this->name = $incName;
963 $this->table = $incTable;
964 $this->type = $incType;
965 $this->flags = $incFlags;
968 public function __set ( $key, $value ) {
969 if ( array_key_exists ($key, get_object_vars($this)) ) {
970 $this->$key = $value;
972 // if ($key == 'where')
973 // var_dump($this);
975 if ( is_null ($this->type) )
976 $this->setType();
978 return true;
979 } else
980 return false;
983 public function __get ( $key ) {
984 return $this->$key;
987 public function __call ( $method, $args) {
988 $all = get_object_vars($this);
990 if ( preg_match( '/set_(.*)/', $method, $found ) ) {
991 if (array_key_exists( $found[1], $all)){
992 $this->$found[1] = $args[0];
993 return true;
995 } elseif ( preg_match( '/get_(.*)/', $method, $found ) ) {
996 if (array_key_exists( $found[1], $all)){
997 return $this->$found[1];
1000 return false;
1003 public function __destruct () {}
1005 public function __toString () {
1006 return (string)$this->value;
1009 // public function inWhere () {
1010 // return $this->where;
1011 // }
1013 static public function isValidType ($inc) {
1014 if (in_array($inc, tdoAbstractField::$validTypes)){
1015 return true;
1017 return false;
1020 public function set_modifier ($modif) {
1021 // if (stristr($modif, '%'))
1022 $this->modifier = $modif;
1025 public function set_value ($value) {
1026 $this->value = $value;
1029 public function set_group ($true = true) {
1030 $this->group = (bool)$true;
1033 public function set_order ($asc = true) {
1034 $this->order = (bool)$asc;
1037 public function isIndex() {
1038 return (($this->flags & INDEX) == INDEX);
1041 public function isPrimary() {
1042 return (($this->flags & PRIMARY) == PRIMARY);
1045 public function isFullText() {
1046 return (($this->flags & FULLTEXT) == FULLTEXT);
1048 public function isUnique () {
1049 return (($this->flags & UNIQUE) == UNIQUE);
1051 public function setType () {
1052 $incValue = $this->value;
1053 // TODO : enums
1054 if (is_int($incValue)) {
1055 $this->type = 'INT';
1056 } elseif (is_float($incValue)) {
1057 $this->type = 'FLOAT';
1058 } elseif (is_string($incValue)) {
1059 if (strtotime($this->value)){
1060 $this->type = 'DATE';
1061 }elseif (strlen($incValue) > 255)
1062 $this->type = 'TEXT';
1063 else
1064 $this->type = 'VARCHAR';