Example #1
0
 /**
  * Constructor.
  *
  * @param string $table
  */
 public function __construct($table)
 {
     //set a table_desc object with the fields for a table
     $rs = SqlTools::sqlQuery("desc {$table}");
     if ($rs) {
         $this->table['name'] = $table;
         while ($field = mysql_fetch_object($rs, 'FieldDesc')) {
             $field->Decorate();
             $this->fields[$field->Field] = $field;
         }
         $this->field_keys = array_keys($this->fields);
         $this->nb_fields = count($this->field_keys);
         self::$tables[$table] = $this;
     } else {
         throw new Exception('Table ' . $table . ' may not exist ' . mysql_error());
     }
 }
Example #2
0
File: Morm.php Project: anicet/morm
 /**
  * createIdentifyingWhereSql 
  *
  * build the sql where statement used to get the model's corresponding row or 
  * the given key's one and return it
  * 
  * @param mixed $not_yet_loaded_key 
  * @return string
  */
 private function createIdentifyingWhereSql($not_yet_loaded_key = null)
 {
     $pkey = is_null($not_yet_loaded_key) ? $this->getPkeyVal() : $not_yet_loaded_key;
     if (is_array($this->_pkey)) {
         $req = array();
         foreach ($this->_pkey as $key) {
             $req[] = '`' . $this->_table . '`.`' . $key . '` = ' . SqlTools::formatSqlValue($pkey[$key]);
         }
         $where = " where " . implode(' AND ', $req);
     } else {
         $where = ' where `' . $this->_table . '`.`' . $this->_pkey . "`=" . SqlTools::formatSqlValue($pkey);
     }
     return $where;
 }
Example #3
0
 /**
  * @todo cache the result
  * @param boolean $with_limit
  * @return integer
  */
 public function get_count($with_limit = false)
 {
     $limit_stmt = $with_limit ? SqlBuilder::order_by($this->order, $this->order_dir) . SqlBuilder::limit($this->offset, $this->limit) : '';
     $rs = SqlTools::sqlQuery("SELECT count(1)\n                        \nFROM " . SqlBuilder::from($this->get_from_tables()) . SqlBuilder::joins($this->joins, $this->getJoinType()) . SqlBuilder::where($this->where, $this->sql_where) . $limit_stmt);
     if ($rs) {
         return (int) mysql_result($rs, 0);
     } else {
         throw new Exception("Fatal error:" . mysql_error());
     }
 }
Example #4
0
 public function delete()
 {
     $position = $this->position;
     if (parent::delete()) {
         $sql = "update `" . $this->_table . "` set position = position - 1 where position > " . $position;
         if (SqlTools::sqlQuery($sql)) {
             return true;
         }
         return false;
     }
 }
Example #5
0
 public function testFormatSqlToolsWithArrayNoFake()
 {
     $result = mysql_fetch_array(SqlTools::sqlQuery('SELECT ? + ?', array(40, 1)));
     $this->assertEqual(41, $result[0]);
 }
Example #6
0
File: field.php Project: AF83/morm
 public function testformatSqlValue()
 {
     $field = new MormFieldSqlFunction('MYSQLFUCNTION()');
     $this->assertEqual('MYSQLFUCNTION()', SqlTools::formatSqlValue($field));
 }
Example #7
0
 static function set($values)
 {
     $set = array();
     foreach ($values as $field => $value) {
         $set[] = '`' . $field . '`=' . SqlTools::formatSqlValue($value);
     }
     return 'set ' . implode(' , ', $set);
 }