/** * 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()); } }
/** * getForeignValues * * only used by the scaffolding, should probably be removed or extended to * make it more generic * * @throws Exception if field is not a foreign key * @param mixed $field * @param array $foreign_fields (can be null)) * @param mixed $conditions * @return array */ public function getForeignValues($field, $foreign_fields = NULL, $conditions = NULL) { if ($this->isForeignKey($field)) { if (!isset($this->_foreign_values[$field])) { $select = is_null($foreign_fields) ? '*' : '`' . implode('`,`', $foreign_fields) . '`'; $conditions = is_null($conditions) ? '' : $conditions; $sql = "select " . $select . " from `" . $this->getForeignTable($field) . "` " . $conditions; $rs = SqlTools::sqlQuery($sql); $foreign_values = array(); while ($line = mysql_fetch_assoc($rs)) { $foreign_values[] = $line; } $this->_foreign_values[$field] = $foreign_values; } return $this->_foreign_values[$field]; } else { throw new Exception($field . ' is not a foreign key in table ' . $this->_table); } }
/** * @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()); } }
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; } }
public function testFormatSqlToolsWithArrayNoFake() { $result = mysql_fetch_array(SqlTools::sqlQuery('SELECT ? + ?', array(40, 1))); $this->assertEqual(41, $result[0]); }