/** * Use this to quickly convert your function to sql without having to do complicated code * * @param $builder Columns * @param $property_path string * @param $sql_function string * @param $args mixed[] * @return string */ protected function quickSql(Columns $builder, $property_path, $sql_function, $args = []) { $sql = $sql_function . '(' . $builder->buildColumn($property_path); foreach ($args as $arg) { $sql .= ', ' . Value::escape($arg); } return $sql . ')' . ($builder->resolve_aliases ? ' AS ' . BQ . $property_path . BQ : ''); }
/** * @param $object object * @param $foreign_object object * @return string */ public function buildQuery($object, $foreign_object) { list($table, $field1, $field2, $id1, $id2) = Map::sqlElementsOf($object, $this->property, $foreign_object); if ($this->property->getType()->getElementTypeAsString() == 'object') { $class_field = substr($field2, 3) . '_class'; return 'INSERT INTO' . SP . BQ . $table . BQ . LF . 'SET ' . $field1 . ' = ' . $id1 . ', ' . $field2 . ' = ' . $id2 . ', ' . $class_field . ' = ' . Value::escape(get_class($foreign_object)); } else { return 'INSERT INTO' . SP . BQ . $table . BQ . LF . 'SET ' . $field1 . ' = ' . $id1 . ', ' . $field2 . ' = ' . $id2; } }
/** * Returns the Dao function as SQL * * @param $builder Builder\Where the sql query builder * @param $property_path string the property path * @param $prefix string column name prefix * @return string */ public function toSql(Builder\Where $builder, $property_path, $prefix = '') { $column = $builder->buildColumn($property_path, $prefix); if (is_null($this->than_value)) { switch ($this->sign) { case self::EQUAL: return $column . ' IS NULL'; case self::NOT_EQUAL: return $column . ' IS NOT NULL'; } } return $column . SP . $this->sign . SP . Value::escape($this->than_value, strpos($this->sign, 'LIKE') !== false); }
/** * Returns the Dao function as SQL * * @param $builder Builder\Where the sql query builder * @param $property_path string the property path * @param $prefix string column name prefix * @return string */ public function toSql(Builder\Where $builder, $property_path, $prefix = '') { $sql = ''; if ($this->values) { $sql = $builder->buildColumn($property_path, $prefix) . ' IN ('; $first = true; foreach ($this->values as $value) { if ($first) { $first = false; } else { $sql .= ', '; } $sql .= Value::escape($value); } $sql .= ')'; } return $sql; }
/** * Returns the Dao function as SQL * * @param $builder Builder\Where the sql query builder * @param $property_path string the property path * @param $prefix string column name prefix * @return string */ public function toSql(Builder\Where $builder, $property_path, $prefix = '') { $column = $builder->buildColumn($property_path, $prefix); $value = Value::escape($this->value); return $column . ' = LEFT(' . $value . ', LENGTH(' . $column . '))'; }
/** * Build a SQL UPDATE query * * @param $class Reflection_Class | string * @param $write array the data to write for each column : key is the column name * @param $id integer|integer[] * @return string */ public static function buildUpdate($class, $write, $id) { $sql_update = 'UPDATE ' . BQ . Dao::current()->storeNameOf($class) . BQ . LF . 'SET '; $i = 0; foreach ($write as $key => $value) { if ($i++) { $sql_update .= ', '; } if ($key != 'id' && substr($key, 0, 3) != 'id_') { $key = BQ . $key . BQ; } $sql_update .= $key . ' = ' . Value::escape($value); } $sql_update .= LF . 'WHERE'; if (is_numeric($id)) { $sql_update .= ' id = ' . $id; } elseif (is_array($id)) { $first = true; foreach ($id as $key => $value) { $sql_update .= $first ? $first = false : ' AND'; $sql_update .= ' ' . $key . ' = ' . $value; } } else { trigger_error("id must be an integer of an array of integer values", E_USER_ERROR); } return $sql_update; }
/** * Returns the Dao function as SQL * * @param $builder Builder\Where the sql query builder * @param $property_path string the property path * @param $prefix string column name prefix * @return string */ public function toSql(Builder\Where $builder, $property_path, $prefix = '') { return '(' . $builder->buildColumn($property_path, $prefix) . ' BETWEEN ' . Value::escape($this->from) . ' AND ' . Value::escape($this->to) . ')'; }
/** * Build SQL WHERE section for a unique value * * @param $path string search property path * @param $value mixed search property value * @param $prefix string Prefix for column name * @return string */ private function buildValue($path, $value, $prefix = '') { $column = $this->buildColumn($path, $prefix); $is_like = Value::isLike($value); return $column . SP . ($is_like ? 'LIKE' : '=') . SP . Value::escape($value, $is_like); }
/** * @return string */ public function toSql() { $column_name = $this->getName(); $type = $this->getSqlType(); $postfix = $this->getSqlPostfix(); $sql = BQ . $column_name . BQ . SP . $type; if (!$this->canBeNull()) { $sql .= ' NOT NULL'; } if ($postfix != ' auto_increment' && !in_array($type, ['tinyblob', 'tinytext', 'blob', 'text', 'mediumblob', 'mediumtext', 'longblob', 'longtext'])) { $sql .= ' DEFAULT ' . Value::escape($this->getDefaultValue()); } $sql .= $postfix; if ($postfix === ' auto_increment') { $sql .= ' PRIMARY KEY'; } return $sql; }