* some minor fixes + more debugging on queries
[vsc.git] / _res / _libs / tdoabstract.class.php
blobc672cdc2a1f386382a372014033ba327acffc013
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 /**
11 * @var mySqlIm $db
13 public $db,
14 $wheres = array(),
15 $groups,
16 // $orders,
17 $limit,
18 $refers = array();
20 protected $id, // public to allow debugging outside the class
21 $name,
22 $alias,
23 $fields;
25 /**
26 * @desc function to implement get_ | set_ virtual methods
28 * @param string $method
29 * @param array $args
30 * @return bool|mixed
32 * @see http://www.ibm.com/developerworks/xml/library/os-php-flexobj/ by Jack Herrington <jherr@pobox.com>
34 function __call ( $method, $args) {
35 $diff = $this->get_members();
36 $all = get_object_vars($this);
38 if ( preg_match( '/set_(.*)/', $method, $found ) ) {
39 // check for fields with $found[1] name
40 if ( array_key_exists( $found[1], $diff) ) {
41 $this->fields[$found[1]]->setValue($args[0]);
42 return true;
43 // check for obj members with $found[1] name
44 } elseif (array_key_exists( $found[1], $all)){
45 $this->$found[1] = $args[0];
46 return true;
48 } else if ( preg_match( '/get_(.*)/', $method, $found ) ) {
49 if ( array_key_exists( $found[1], $diff ) ) {
50 return $this->fields[$found[1]]->getValue();
51 } elseif (array_key_exists( $found[1], $all)){
52 return $this->$found[1];
55 return false;
58 /**
59 * @desc A function to implement a virtual getter member of the class
61 * @param string $key
62 * @return mixed
64 * @see http://www.ibm.com/developerworks/xml/library/os-php-flexobj/ by Jack Herrington <jherr@pobox.com>
66 function __get ( $key ) {
67 return $this->fields[$key];
70 /**
71 * @desc A function to implement a virtual setter member for this class
73 * @param string $key
74 * @param mixed $value
75 * @return bool
77 * @see http://www.ibm.com/developerworks/xml/library/os-php-flexobj/ by Jack Herrington <jherr@pobox.com>
79 function __set ( $key, $value ) {
80 if (
81 array_key_exists ($key, $this->get_members()) /*&&
82 $this->isValidMember($this->fields[$key])*/
83 ) {
84 $this->fields[$key]->set_value ($value);
85 return true;
86 } else {
87 $this->fields[$key] = new tdoAbstractField ($key, $this->alias);
88 $this->fields[$key]->set_value ($value);
92 public function __construct(&$db) {
93 $this->db = &$db;
94 $this->alias = 'm1';
96 $this->instantiateMembers();
99 public function __destruct() {}
102 * gets the members we consider table fields
104 * @return array ('fieldName' => tdoAbstractField)
106 public function &get_members () {
107 return $this->fields;
111 * checks if a field is a valid member of the current object
113 * @param tdoAbstractField $incMember
114 * @return bool
116 public function isValidMember ($incMember) {
117 if (
118 ! ($incMember instanceof tdoAbstractField ) ||
119 // this prevents an error in php > 5.2 object comparison
120 ! in_array ($incMember, $this->get_members() /**/, true/**/)
122 return false;
123 } else
124 return true;
128 * instantiating the object's members
130 public function instantiateMembers () {
131 foreach ($this->get_members() as $key => $field) {
132 if ( is_int ($key) && is_string ($field) ) {
133 $this->fields[$field] = new tdoAbstractField ($field, $this->alias);
135 // FIXME: this is so bad :D - it implies that the primary key must be the first field containing _id :D
136 if (stristr ($field,'_id') && !isset ($this->id) && $this->isValidMember ($this->fields[$field])) {
137 $this->id = &$this->fields[$field];
139 unset ($this->fields[$key]);
143 $this->wheres = array();
144 $this->groups = '';
145 // $this->orders = '';
146 $this->refers = array();
149 * setting the table's index field
151 * @param tdoAbstractField $obj
153 public function set_index (&$obj) {
154 if ($this->isValidMember($obj)) {
155 $this->id = &$obj;
156 $this->id->flags = PRIMARY;
161 * setting an alias for the table in case of joins.
162 * this method also sets the alias on all the fields of the current object
164 * @param string $alias
166 public function set_alias ($alias) {
167 foreach ($this->get_members() as $field){
168 if ($field->table == $this->alias) {
169 $field->table = 't'.$alias;
172 $this->alias = 't'.$alias;
176 * For adding custom sql functions for certain fields
178 * @param string $modif
179 * @param tdoAbstractField $field
181 public function set_fieldModifier ($modif, &$field) {
182 if ($this->isValidMember ($field) && stristr ($modif, '%'))
183 $field->set_modifier ($modif);
187 * method used in join cases to add the joined object's fields
188 * to the current one
190 * @param array ('fieldName' => tdoAbstractField) $incArr
192 public function addFields (&$incArr) {
193 if (is_array($incArr)) {
194 foreach ($incArr as $fieldName => $field) {
195 $this->fields[$fieldName] = $field;
201 * Based on field values, we get the _first_ row in the table that matches
203 * TODO: maybe we can have a function that returns _all_ the rows that match
205 * @return bool
207 public function buildObj () {
208 $sql = $this->buildSql(1);
209 $this->db->query( $sql );
211 $arr = $this->db->getAssoc();
212 if (is_array($arr)) {
213 foreach ($arr as $field => $var) {
214 $this->fields[$field]->set_value ($var);
216 return true;
218 return false;
222 * Gets the row in the table for $id
224 * @param mixed $id
227 public function get ($id) {
228 $id = $this->db->escape($id);
230 $this->instantiateMembers();
232 $this->id->set_value ($id);
233 $this->buildObj ();
236 * Returns the last id of the table
237 * OBS: this assumes that we didn't delete any rows from the table.
239 * @return int
241 public function getLastInsertedId () {
242 $sql = $this->db->_SELECT ($this->id->name)
243 .$this->db->_FROM ($this->name);
245 $this->addOrder ($this->id, false);
247 $sql .= $this->db->_ORDER ($this->outputOrders ());
249 $sql .= ' LIMIT 1';
251 $this->db->query ($sql);
252 return $this->db->getScalar ();
256 * encapsulating the $this->db->escape() method
258 * @param mixed $value
259 * @return mixed
261 public function escape ($value) {
262 if (is_numeric ($value)) {
263 return $value;
264 } else {
265 return '"'.$this->db->escape ($value).'"';
269 * inserting into the database
270 * TODO: multiple inserts to use with loadFromArray
272 * @return int
274 public function insert () {
275 $sql = $this->db->_INSERT ($this->name);//`'INSERT INTO '.$this->name;
277 $fieldStr = '';
278 $valueStr = '';
279 $f = $this->get_members();
281 foreach ($f as $fieldName => $field) {
282 if ($this->isValidMember ($field) && ($field != $this->id) && !is_null ($field->value)) {
283 $value = $this->escape ($field->value);
285 $fieldStr.= (!empty ($fieldStr) ? ', ' : ' ') . $fieldName;
286 $valueStr.= (!empty ($valueStr) ? ', ' : ' ') . $value;
290 $sql.= ' ('.$fieldStr.') VALUES ('.$valueStr.')';
291 if ($fieldStr) {
292 $this->db->query($sql);
294 return $this->getLastInsertedId();
298 public function update ($id = null) {
299 if (is_null ($id)) {
300 $id = $this->id->value;
303 if (is_null ($id)) {
304 throw new Exception('Cannot update record in table '.$this->name.' because an id hasn\'t been provided');
305 return false;
308 $sql = $this->db->_UPDATE (array ($this->name, $this->alias) );
310 if (is_array ($this->refers) && !empty ($this->refers)){
311 $this->refers = array_reverse ($this->refers);
313 foreach ($this->refers as $ref)
314 $sql .= $ref;
317 $sql .= $this->db->_SET();
319 $fields = $this->get_members();
321 foreach ($fields as $fieldName => $field) {
322 if (($field instanceof tdoAbstractField) && $field != $this->id) { // TODO: make a more real check for field is an id
323 $value = $field->value;
326 if ((isset($value) && !is_null($value))) {
327 $sql.= $field->table.'.'.$fieldName.' = '.$this->escape ($value).', ';
331 $sql = substr ($sql, 0, -2);
333 $sql.= $this->db->_WHERE($this->alias.'.'.$this->id->name.' = '.$this->escape($id));
335 // var_dump($sql);die ('<br/>update');
336 $this->db->query($sql);
338 return $id;
341 public function replace ( $id = null ) {
342 if (is_null($id)) {
343 $id = $this->id->value;
346 if (is_null($id) || !$this->idExists ($id) ) {
347 return $this->insert ();
348 } else {
349 return $this->update ($id);
353 // TODO: make this the same way the find first method works
354 public function delete ($id = null) {
355 $id = (!is_null ($id) ? $id :$this->id->value);
357 if (!is_null ($id)) {
358 // no need for other wheres
359 $this->wheres = array();
360 $this->addWhere ($this->id, '=', $id);
363 if (empty($this->wheres)) {
364 return false;
367 $temp = $this->alias;
368 $this->set_alias(null);
371 $sql = 'DELETE FROM '.$this->name.' WHERE '; //$this->id->name.' = '.$this->escape($value).' LIMIT 1';
372 $sql.= $this->outputWheres();
373 // echo $sql;die;
374 $affRows = $this->db->query($sql);
376 $this->set_alias($temp);
377 return $affRows;
380 public function reset () {
381 foreach ($this->fields as $key => $field) {
382 if ($field instanceof tdoAbstractField) {
383 $this->fields[$key] = new tdoAbstractField($key, $this->alias);
387 $this->wheres = array();
388 $this->groups = array();
389 // $this->orders = array();
390 $this->refers = array();
391 return true;
394 public function idExists ($id = null) {
395 if (is_null($id)) {
396 $id = $this->id->value;
398 if (is_null($id))
399 return false;
401 $this->id->set_modifier ('COUNT(%s)');
403 $t = sprintf ($this->id->modifier, $this->id->name);
405 $sql = $this->db->_SELECT ($t).
406 $this->db->_FROM ($this->name).
407 $this->db->_WHERE ($this->id->name.' = '.$this->escape($id));
409 $this->db->query($sql);
411 if ($this->db->getScalar()) {
412 return true;
413 } else {
414 return false;
418 protected function outputFieldList () {
419 $fields = '';
420 $f = $this->get_members();
421 // var_dump($f);
422 foreach ($f as $fieldName => $field) {
423 if ($this->isValidMember ($field)) {
425 if (!is_null ($field->modifier)) {
426 // i replaced str_replace ('%s', $curField, '%s', $curField)
427 // as sometimes I might need it for something else than %s
428 $fields .= sprintf ($field->modifier, (!is_null ($field->table) ? $field->table . '.' : '') . $field->name) .
429 $this->db->_AS($field->name) . ', ';
430 } elseif ( !$field->inWhere ()) {
431 $fields .= (!is_null ($field->table) ? $field->table.'.' : '') . $field->name . ', ';
434 } else {
435 trigger_error ($fieldName . ' is not a valid member of ' . get_class($this));
436 // throw new Exception ($fieldName . ' is not a valid member of ' . get_class($this));
437 return false;
440 return substr ($fields,0,-2);//$fields;
443 protected function outputWheres ($bIW = true) {
444 // var_dump($this->wheres);
445 return implode ($this->db->_AND(), $this->wheres);
448 protected function outputGroups () {
449 // groups
450 $groups = '';
451 $f = $this->get_members ();
452 foreach ($f as $field) {
453 if (!is_null ($field->group) ) {
454 $groups .= (!empty($groups) ? ', ' : '') .
455 $field->table . '.' . $field->name;
459 return $groups;
462 protected function outputOrders () {
463 $orders = '';
464 $f = $this->get_members();
465 foreach ($f as $field)
466 if (!is_null($field->order)) {
467 $orders .= (!empty($orders) ? ', ': '') .
468 $field->table . '.' . $field->name .
469 ($field->order == true ? ' ASC' : ' DESC');
471 return $orders;
474 protected function outputRefers () {
475 $refers= '';
476 if (!empty($this->refers) && is_array($this->refers)){
477 $rs = array_reverse ($this->refers);
478 foreach ($rs as $ref) // using the __toString magic function
479 $refers .= $ref;
481 return $refers;
484 protected function buildInherentWheres () {
485 $diff = $this->get_members();
486 // let's hope this doesn't break stuff.
487 // it's needed when we use more queries on the same instance of the object :D
489 if (is_array ($diff)) {
490 foreach ($diff as $fieldName => $field) {
491 if ( $this->isValidMember ($field) ) {
492 if (!is_null($field->value))
493 $this->addWhere ($field, '=', $this->escape($field->value));
494 } else {
495 trigger_error ($fieldName . ' is not a valid member of ' . get_class($this));
496 // throw new Exception ($fieldName . ' is not a valid member of ' . get_class($this));
501 if (empty ($this->wheres)) {
502 $t = '1';
503 $this->wheres[] = new tdoAbstractClause ($t);
508 * function to add an abstract clause to the current object if it doesn't exist
510 * @param tdoAbstractField|tdoAbstractClause $field1
511 * @param string $condition
512 * @param string $field2
514 public function addWhere (&$field1, $condition = null, $field2 = null) {
515 if (($field1 instanceof tdoAbstractClause) && ($condition == null || $field2 == null)) {
516 $w = &$field1;
517 } else {
518 $w = new tdoAbstractClause ($field1, $condition, $field2);
520 // this might generate an infinite recursion error on some PHP > 5.2 due to object comparison
521 if (!in_array ($w, $this->wheres /*, true */))
522 $this->wheres[] = &$w;
525 public function addOrder (&$orderField, $asc = true) {
526 if (!($orderField instanceof tdoAbstractField) && is_string ($orderField)) {
527 $orderField = &$this->fields[$orderField];
529 if ($this->isValidMember ($orderField))
530 $orderField->set_order ($asc);
533 public function addGroup (&$groupField) {
534 if (!($groupField instanceof tdoAbstractField) && is_string ($groupField)) {
535 $groupField = &$this->fields[$groupField];
537 if (($groupField instanceof tdoAbstractField)) {
538 $groupField->set_group (true);
543 * @param int $start
544 * @param int $count
547 public function addLimit ($start = 0, $count=null) {
548 if (empty($this->limit))
549 $this->limit = $this->db->_LIMIT ($start, $count);
553 * building a normal SELECT query
555 * @param int $start
556 * @param int $end
557 * @return string
559 protected function buildSql ($start = 0, $count = 0) {
560 $sql = $this->db->_SELECT($this->outputFieldList()). ' FROM '.$this->name.' AS '.$this->alias.' ';
562 $this->buildInherentWheres(); // will it work
564 $sql .= $this->outputRefers();
566 $sql .= $this->db->_WHERE ($this->outputWheres());// add LIMITS
568 $sql .= $this->db->_GROUP ($this->outputGroups());
570 $sql .= $this->db->_ORDER ($this->outputOrders());
572 if (empty ($this->limit)) {
573 $this->addLimit ($start, $count);
575 $sql .= $this->limit;
576 return $sql;
579 public function find ($start = 0, $count = 0) {
580 $result = $this->db->query ($this->buildSql(), $start, $count);
581 return $result;
584 public function findFirst () {
585 $this->buildInherentWheres();
587 $this->db->query($this->buildSql(), 0, 1);
588 $row = $this->db->getAssoc();
589 if (is_array($row))
590 foreach ($row as $field => $value){
591 $this->fields[$field]->value = $value;
595 public function getArray ($start = 0, $count = 0, $orderBy = null) {
596 $this->buildInherentWheres ();
598 $sql = $this->buildSql ($start, $count, $orderBy);
600 $this->db->query ($sql);
601 return $this->db->getArray ();
605 * execute a select count () on the current object
607 * @return null
609 public function getCount1() {
610 $this->set_fieldModifier('COUNT(%s)', $this->id);
611 $this->db->query($this->buildSql());
612 return $this->db->getScalar();
615 public function getCount() {
616 // made it a bit more clean and less sql portable
617 $sql = $this->db->_SELECT (' COUNT(*) ');
619 $sql .= $this->db->_FROM( $this->name. $this->db->_AS($this->alias) );
621 $this->buildInherentWheres();
623 $sql .= $this->outputRefers();
625 $sql .= $this->db->_WHERE ($this->outputWheres());
627 $this->db->query($sql);
628 return $this->db->getScalar();
631 public function getVector() {
632 // I really don't see why we need this. ?
636 * Function to load the values of current object from an array
637 * of type field_name => field_value
638 * If strict is false, the current object can have fields that are not already
639 * present in $this->fields[]
641 * @param array $valArray
642 * @param bool $strict
644 public function loadFromArray ($valArray, $strict = true) {
645 foreach ($valArray as $fieldName => $value) {
646 if (array_key_exists ($fieldName, $this->fields)) {
647 $this->fields[$fieldName]->set_value($value);
648 } elseif (!$strict) {
649 // if the field name is not in the field list of the current object
650 // it means that the valArray object is got from an JOIN sql
651 $this->fields[$fieldName] = new tdoAbstractField ($value,'j1');
652 $this->fields[$fieldName]->set_value ($value);
658 * FIXME: make it work when composing with the same object
660 * @param tdoAbstract $incOb
661 * @return void
663 private function composeObj ($incOb) {
664 if (!($incOb instanceof tdoAbstract))
665 return false;
666 $refs = count($this->refers);
668 foreach ($incOb->refers as $alias => $ref) {
669 $tAl = $refs++;
671 $this->refers[$tAl] = $ref;//str_replace(array($alias, $aliases[$alias][1]), array($tAl, $aliases[$alias][2]), $ref);
672 $this->refers[$tAl]->set_state ($tAl);
677 * Function to execute a join between two tdoAbstract objects
679 * @param string $jType
680 * @param tdoAbstractField $thisJField
681 * @param tdoAbstract $incOb
682 * @param tdoAbstractField $incObJField
683 * @return unknown
685 public function joinWith ($jType = null, &$thisJField = null, &$incOb = null, &$incObJField = null) {
686 if (
687 !tdoAbstractJoin::isValidType ($jType) ||
688 !$this->isValidMember ($thisJField)
689 ) return false;
691 $this->composeObj ($incOb);
693 $tAl = (count($this->refers));
694 if ($tAl > 59) {
695 trigger_error('Join aborted for table '.$this->name.': Too many tables; MySQL can only use 61 tables in a join', E_USER_NOTICE);
696 return;
697 } else {
698 $incOb->set_alias($tAl);
700 if($thisJField == null || !($thisJField instanceof tdoAbstractField))
701 $thisJField = $this->id;
703 if($incObJField == null || !($incObJField instanceof tdoAbstractField))
704 $incObJField = $incOb->id;
706 $this->refers[$tAl] = new tdoAbstractJoin ($jType, $this, $incOb, $thisJField, $incObJField, $tAl);
707 $this->refers[$tAl]->set_state ($tAl);
709 // var_dump ($this->refers);
713 * function to dump a <type>Sql
714 * problem with the field types. :D
716 * @param tdoAbstract $obj
718 static public function dumpSchema ($obj) {
719 if ($obj instanceof tdoAbstract)
720 throw new Exception('Can\'t generate sql dump');
722 $sql = 'CREATE TABLE '. $obj->name . ' (';
724 foreach ($obj->getFields() as $fieldName => $data) {
725 $sql .= $fieldName.' '.$data[0].', ';
727 $sql .= ')';
728 return $sql;
734 * class to abstract a where clause in a SQL query
735 * TODO: add possibility of complex wheres: (t1 condition1 OR|AND|XOR t2.condition2)
736 * TODO: abstract the condition part of a where clause - currently string based :D
738 class tdoAbstractClause {
739 protected $subject, $predicate, $predicative;
742 * initializing a WHERE|ON clause
744 * @param tdoAbstractClause|tdoAbstractField $subject
745 * @param string $predicate
746 * @param tdoAbstractClause|tdoAbstractField|null $complement
748 public function __construct ($subject, $predicate = null, $predicative = null) {
749 // I must be careful with the $subject == 1 because (int)object == 1
750 if (($subject === 1 || is_string($subject)) && $predicate == null && $predicative == null) {
751 $this->subject = $subject;
752 $this->predicate = '';
753 $this->predicative = '';
754 return;
757 if (($subject instanceof tdoAbstractField ) || ($subject instanceof tdoAbstractClause )) {
758 $this->subject = &$subject;
760 $subject->set_where = true;
762 $this->predicative = &$predicative;
764 if ($this->validPredicative ($predicate))
765 $this->predicate = $predicate;
767 } else {
768 $this->subject = '';
769 $this->predicate = '';
770 $this->predicative = '';
772 return;
775 public function __destruct () {}
777 public function __toString () {
778 // var_dump($this->subject, $this->predicate, $this->predicative);
779 // echo '<br/>';
780 if ($this->subject === '1' || is_string($this->subject)) {
781 // var_dump($this->subject);
782 return (string)$this->subject;
783 } elseif ($this->subject instanceof tdoAbstractClause) {
784 $subStr = (string)$this->subject;
785 } elseif ($this->subject instanceof tdoAbstractField) {
786 // without $this->subject->table != 't' we have a bug in the delete op
787 $subStr = ($this->subject->table != 't' ? $this->subject->table.'.': '').$this->subject->name;
788 } else {
789 return '';
792 if (is_null($this->predicative)) {
793 if ($this->validPredicative ($this->predicate)) {
794 $preStr = 'NULL';
795 } else
796 $preStr = '';
797 } elseif (is_numeric($this->predicative)) {
798 $preStr = $this->predicative;
799 } elseif (is_string($this->predicative)) {
800 $preStr = $this->predicative;
802 if ($this->predicate == 'LIKE') {
803 $preStr = '%'.$this->predicate.'%';
806 $preStr = (stripos($preStr, '"') !== 0 ? '"'.$preStr.'"' : $preStr);//'"'.$preStr.'"';
807 } elseif (is_array($this->predicative)) {
808 $preStr = '("'.implode('", "',$this->predicative).'")';
809 } elseif ($this->predicative instanceof tdoAbstractfield) {
810 $preStr = ($this->predicative->table != 't' ? $this->predicative->table.'.': '').$this->predicative->name;
811 } elseif ($this->predicative instanceof tdoAbstractClause) {
812 $subStr = $subStr;
813 $preStr = $this->predicative;
816 $retStr = $subStr.' '.$this->predicate.' '.$preStr;
817 if (($this->subject instanceof tdoAbstractClause) && ($this->predicative instanceof tdoAbstractClause))
818 return '('.$retStr.')';
820 return $retStr;
823 private function validPredicative ($predicate) {
824 // need to find a way to abstract these
825 // $validPredicates = array (
826 // 'AND',
827 // '&&',
828 // 'OR',
829 // '||',
830 // 'XOR',
831 // 'IS',
832 // 'IS NOT',
833 // '!',
834 // 'IN',
835 // 'LIKE',
836 // '=',
837 // '!=',
838 // '<>'
839 // );
840 if ($this->predicative instanceof tdoAbstractClause) {
841 // we'll have Subject AND|OR|XOR Predicative
842 $validPredicates = array (
843 'AND',
844 '&&',
845 'OR',
846 '||',
847 'XOR'
849 } elseif (($this->predicative instanceof tdoAbstractField) || is_numeric($this->predicative)) {
850 // we'll have Subject =|!= Predicative
851 $validPredicates = array (
852 '=',
853 '!=',
854 '>',
855 '<',
856 '>=',
857 '<='
859 } elseif (is_array($this->predicative)) {
860 $validPredicates = array (
861 'IN',
862 'NOT IN'
864 } elseif (is_string($this->predicative)) {
865 $validPredicates = array (
866 '=',
867 'LIKE',
868 // dates
869 '>',
870 '<',
871 '>=',
872 '<='
874 } elseif (is_null($this->predicative)) {
875 $validPredicates = array (
876 'IS',
877 'IS NOT'
881 return in_array($predicate, $validPredicates);
883 // if (in_array($predicate, $validPredicates) && (($predicative instanceof tdoAbstractClause) || ($predicative instanceof tdoAbstractField)))
884 // return true;
885 // return false;
889 class tdoAbstractJoin {
890 static public $validTypes = array (
891 'INNER',
892 'LEFT',
893 'RIGHT',
894 'OUTER'
896 protected $state,
897 $type,
899 $leftTable,
900 $rightTable,
902 $leftField,
903 $rightField;
905 public function __construct ($type, &$lt, &$rt, &$lf, &$rf, $state) {
906 if ($rt instanceof tdoAbstract ||
907 $lt instanceof tdoAbstract
909 $this->leftTable = &$lt;
910 $this->rightTable = &$rt;
912 if (tdoAbstractJoin::isValidType($type))
913 $this->type = $type;
915 if (
916 $lf instanceof tdoAbstractField &&
917 $rf instanceof tdoAbstractField
919 $this->rightField = &$rf;
920 $this->leftField = &$lf;
923 $this->state = $state;
926 $this->composeFields ();
927 $this->composeWheres ();
931 public function __destruct () {}
933 public function __toString() {
934 $lAlias = $this->leftTable->get_alias();
936 return (string)$this->type.' JOIN '.$this->rightTable->get_name().
937 ' AS t'.$this->state.' ON '.(isset($lAlias) ? $lAlias : $this->leftTable->get_name()).
938 '.'.$this->leftField->name.
939 ' = t'.$this->state.'.'.$this->rightField->name.' ';
943 * Will compose the $rightTable and $leftTable fields
944 * @return void
946 public function composeFields () {
947 $leftFields = $this->leftTable->get_members();
949 $this->rightTable->set_alias($this->state);
950 $rightFields = $this->rightTable->get_members();
953 $this->leftTable->addFields($rightFields);
957 * Will compose the $rightTable and $leftTable WHERE clauses
958 * @return void
960 public function composeWheres () {
961 if (!is_array($this->leftTable->wheres))
962 $this->leftTable->wheres = array();
963 if (!is_array($this->rightTable->wheres))
964 $this->rightTable->wheres = array();
966 // var_dump($this->rightTable->wheres, $this->leftTable );die;
967 foreach ($this->rightTable->wheres as $where) {
968 $this->leftTable->addWhere ($where);
970 $this->leftTable->wheres = array_merge($this->rightTable->wheres, $this->leftTable->wheres);
973 public function set_state ($st) {
974 $this->state = $st;
977 static public function isValidType ($inc) {
978 if (in_array($inc, tdoAbstractJoin::$validTypes))
979 return true;
980 return false;
984 define ('INDEX', 1);
985 define ('PRIMARY', 2);
986 define ('UNIQUE', 4);
987 define ('FULLTEXT', 8);
989 class tdoAbstractField {
990 static public $validTypes = array (
991 'VARCHAR',
992 'INT',
993 'DATE',
994 'TEXT',
995 'FLOAT',
996 'TIMESTAMP',
997 'ENUM'
1000 protected $name, $type, $flags, $value, $table, $modifier = null, $order = null, $group = null, $where = false;
1002 public function __construct ($incName, $incTable, $incType='INT', $incFlags=0) {
1003 $this->name = $incName;
1004 $this->table = $incTable;
1005 $this->type = $incType;
1006 $this->flags = $incFlags;
1009 public function __set ( $key, $value ) {
1010 if ( array_key_exists ($key, get_object_vars($this)) ) {
1011 $this->$key = $value;
1013 // if ($key == 'where')
1014 // var_dump($this);
1016 if ( is_null ($this->type) )
1017 $this->setType();
1019 return true;
1020 } else
1021 return false;
1024 public function __get ( $key ) {
1025 return $this->$key;
1028 public function __call ( $method, $args) {
1029 $all = get_object_vars($this);
1031 if ( preg_match( '/set_(.*)/', $method, $found ) ) {
1032 if (array_key_exists( $found[1], $all)){
1033 $this->$found[1] = $args[0];
1034 return true;
1036 } elseif ( preg_match( '/get_(.*)/', $method, $found ) ) {
1037 if (array_key_exists( $found[1], $all)){
1038 return $this->$found[1];
1041 return false;
1044 public function __destruct () {}
1046 public function __toString () {
1047 return (string)$this->value;
1050 // public function inWhere () {
1051 // return $this->where;
1052 // }
1054 static public function isValidType ($inc) {
1055 if (in_array($inc, tdoAbstractField::$validTypes)){
1056 return true;
1058 return false;
1061 public function set_modifier ($modif) {
1062 // if (stristr($modif, '%'))
1063 $this->modifier = $modif;
1066 public function set_value ($value) {
1067 $this->value = $value;
1070 public function set_group ($true = true) {
1071 $this->group = (bool)$true;
1074 public function set_order ($asc = true) {
1075 $this->order = (bool)$asc;
1078 public function isIndex() {
1079 return (($this->flags & INDEX) == INDEX);
1082 public function isPrimary() {
1083 return (($this->flags & PRIMARY) == PRIMARY);
1086 public function isFullText() {
1087 return (($this->flags & FULLTEXT) == FULLTEXT);
1089 public function isUnique () {
1090 return (($this->flags & UNIQUE) == UNIQUE);
1092 public function setType () {
1093 $incValue = $this->value;
1094 // TODO : enums
1095 if (is_int($incValue)) {
1096 $this->type = 'INT';
1097 } elseif (is_float($incValue)) {
1098 $this->type = 'FLOAT';
1099 } elseif (is_string($incValue)) {
1100 if (strtotime($this->value)){
1101 $this->type = 'DATE';
1102 }elseif (strlen($incValue) > 255)
1103 $this->type = 'TEXT';
1104 else
1105 $this->type = 'VARCHAR';