Revert "* changed the end of lines to unix"
[vsc.git] / _res / _libs / tdoabstract.class.php
bloba8a1ede95ad8db95f66c928c564ed77af1228e9b
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; // joins
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 (array_key_exists($key,$this->get_members()))
77 return ($this->fields[$key]->value = $value);
80 public function __construct(&$db) {
81 $this->db = &$db;
82 $this->alias = 'm1';
84 $this->instantiateMembers();
87 public function __destruct() {}
89 /**
90 * gets the members we consider table fields
92 * @return array ('fieldName' => tdoAbstractField)
94 public function &get_members () {
95 return $this->fields;
98 /**
99 * checks if a field is a valid member of the current object
101 * @param tdoAbstractField $incMember
102 * @return bool
104 public function isValidMember ($incMember) {
105 if (
106 ! ($incMember instanceof tdoAbstractField ) ||
107 ! in_array ($incMember, $this->get_members(), true) // this prevents an error in php5.2 object comparison
109 return false;
110 else
111 return true;
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();
131 $this->groups = '';
132 $this->orders = '';
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)) {
142 $this->id = $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
175 * to the current one
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
192 * @return bool
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;
203 return true;
205 return false;
209 * Gets the row in the table for $id
211 * @param mixed $id
214 public function get ($id) {
215 $id = $this->db->escape($id);
217 $this->instantiateMembers();
219 $this->id->value = $id;
220 $this->buildObj();
223 * Returns the last id of the table
224 * OBS: this assumes that we didn't delete any rows from the table.
226 * @return int
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());
237 $sql .= ' LIMIT 1';
238 $this->db->query($sql);
239 return $this->db->getScalar();
243 * encapsulating the $this->db->escape() method
245 * @param mixed $value
246 * @return mixed
248 public function escape ($value) {
249 if (is_numeric($value)) {
250 return $value;
251 } else {
252 return '"'.$this->db->escape($value).'"';
256 * inserting into the database
257 * TODO: multiple inserts to use with loadFromArray
259 * @return int
261 public function insert () {
262 $sql = 'INSERT INTO '.$this->name;
264 $fieldStr = '';
265 $valueStr = '';
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.')';
278 if ($fieldStr) {
279 $this->db->query($sql);
281 return $this->getLastInsertedId();
285 public function update($id = null) {
286 if (empty($id)) {
287 $id = $this->id->value;
290 if (!isset($id)) {
291 trigger_error('Cannot update record in table '.$this->name.' because an id hasn\'t been provided', E_USER_WARNING);
292 return false;
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)
301 $sql .= $ref;
304 $sql .= ' SET ';
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);
324 return $id;
327 public function replace ( $id = null ) {
328 if ($id == null) {
329 $id = $this->id->value;
332 if (!isset($id) || !$this->idExists( $id ) ) {
333 return $this->insert();
334 } else {
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)) {
346 return false;
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);
359 return $affRows;
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();
373 return true;
376 public function idExists ($id = null) {
377 if (!$id) {
378 $id = $this->id->value;
380 if (!$id)
381 return false;
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())
386 return true;
387 else
388 return false;
391 protected function outputFieldList () {
392 $fields = '';
393 $f = $this->get_members();
394 // var_dump($f);
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))
403 $fields .= ', ';
404 else
405 $fields .= ' ';
407 return $fields;
410 protected function outputWheres ($bIW = true) {
411 return implode ($this->db->_AND(), $this->wheres);
414 protected function outputGroups () {
415 // groups
416 $groups = '';
417 $f = $this->get_members();
418 foreach ($f as $field)
419 if (!is_null($field->group) )
420 $groups .= (!empty($groups) ? ', ' : '').$field->name;
422 return $groups;
425 protected function outputOrders () {
426 $orders = '';
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');
432 return $orders;
435 protected function outputRefers () {
436 $refers= '';
438 if (is_array($this->refers) && !empty($this->refers)){
439 $this->refers = array_reverse($this->refers);
440 foreach ($this->refers as $ref)
441 $refers .= $ref;
444 return $refers;
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
490 * @param int $start
491 * @param int $end
492 * @return string
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());
507 if (!empty($start))
508 $sql .= $this->db->_LIMIT($start, $end);
510 return $sql;
513 public function find ($start = 0, $count = 0) {
514 $result = $this->db->query($this->buildSql(), $start, $count);
515 return $result;
518 public function findFirst () {
519 $this->buildInherentWheres();
521 $this->db->query($this->buildSql(), 0, 1);
522 $row = $this->db->getAssoc();
523 if (is_array($row))
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.') ',
542 // $this->buildSql()
543 // );
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
565 * @return void
567 private function composeObj ($incOb) {
568 if (!($incOb instanceof tdoAbstract))
569 return false;
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) {
578 $tAl = $refs++;
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
592 * @return unknown
594 public function joinWith ($jType = null, &$thisJField = null, &$incOb = null, &$incObJField = null) {
595 if (
596 !tdoAbstractJoin::isValidType($jType) ||
597 !$this->isValidMember ($thisJField)
599 return false;
602 $this->composeObj ($incOb);
604 $tAl = (count($this->refers));
605 if ($tAl > 59) {
606 trigger_error('Join aborted for table '.$this->name.': Too many tables; MySQL can only use 61 tables in a join', E_USER_NOTICE);
607 return;
608 } else {
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].', ';
637 $sql .= ')';
638 return $sql;
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 = '';
663 return;
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;
672 } else {
673 $this->subject = '';
674 $this->predicate = '';
675 $this->predicative = '';
677 return;
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;
692 } else {
693 return '';
696 if (is_null($this->predicative)) {
697 if ($this->validPredicative($this->predicate)) {
698 $preStr = 'NULL';
699 } else
700 $preStr = '';
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) {
716 $subStr = $subStr;
717 $preStr = $this->predicative;
720 $retStr = $subStr.' '.$this->predicate.' '.$preStr;
721 if (($this->subject instanceof tdoAbstractClause) && ($this->predicative instanceof tdoAbstractClause))
722 return '('.$retStr.')';
724 return $retStr;
727 private function validPredicative ($predicate) {
728 // need to find a way to abstract these
729 // $validPredicates = array (
730 // 'AND',
731 // '&&',
732 // 'OR',
733 // '||',
734 // 'XOR',
735 // 'IS',
736 // 'IS NOT',
737 // '!',
738 // 'IN',
739 // 'LIKE',
740 // '=',
741 // '!=',
742 // '<>'
743 // );
744 if ($this->predicative instanceof tdoAbstractClause) {
745 // we'll have Subject AND|OR|XOR Predicative
746 $validPredicates = array (
747 'AND',
748 '&&',
749 'OR',
750 '||',
751 'XOR'
753 } elseif (($this->predicative instanceof tdoAbstractField) || is_numeric($this->predicative)) {
754 // we'll have Subject =|!= Predicative
755 $validPredicates = array (
756 '=',
757 '!=',
758 '>',
759 '<',
760 '>=',
761 '<='
763 } elseif (is_array($this->predicative)) {
764 $validPredicates = array (
765 'IN',
766 'NOT IN'
768 } elseif (is_string($this->predicative)) {
769 $validPredicates = array (
770 '=',
771 'LIKE',
772 // dates
773 '>',
774 '<',
775 '>=',
776 '<='
778 } elseif (is_null($this->predicative)) {
779 $validPredicates = array (
780 'IS',
781 'IS NOT'
785 return in_array($predicate, $validPredicates);
787 // if (in_array($predicate, $validPredicates) && (($predicative instanceof tdoAbstractClause) || ($predicative instanceof tdoAbstractField)))
788 // return true;
789 // return false;
793 class tdoAbstractJoin {
794 static public $validTypes = array (
795 'INNER',
796 'LEFT',
797 'RIGHT',
798 'OUTER'
800 protected $state,
801 $type,
803 $leftTable,
804 $rightTable,
806 $leftField,
807 $rightField;
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))
817 $this->type = $type;
819 if (
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
847 * @return void
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
861 * @return void
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) {
874 $this->state = $st;
877 static public function isValidType ($inc) {
878 if (in_array($inc, tdoAbstractJoin::$validTypes))
879 return true;
880 return false;
884 define ('INDEX', 1);
885 define ('PRIMARY', 2);
886 define ('UNIQUE', 4);
887 define ('FULLTEXT', 8);
889 class tdoAbstractField {
890 static public $validTypes = array (
891 'VARCHAR',
892 'INT',
893 'DATE',
894 'TEXT',
895 'FLOAT',
896 'TIMESTAMP',
897 'ENUM'
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))
912 $this->setType();
913 return true;
914 } else
915 return false;
918 public function __get( $key ) {
919 return $this->$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];
927 return true;
929 } elseif ( preg_match( '/get_(.*)/', $method, $found ) ) {
930 if (array_key_exists( $found[1], $all)){
931 return $this->$found[1];
934 return false;
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)){
945 return true;
947 return false;
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;
979 // TODO : enums
980 if (is_int($incValue)) {
981 $this->type = 'INT';
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';
989 else
990 $this->type = 'VARCHAR';