Skip to content

Commit

Permalink
Merge branch '2.0' of github.com:top-think/think-orm into 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Jan 21, 2022
2 parents c171243 + 2eb8d02 commit 149d266
Show file tree
Hide file tree
Showing 23 changed files with 383 additions and 305 deletions.
15 changes: 8 additions & 7 deletions src/Model.php
Expand Up @@ -260,11 +260,12 @@ public function getName(): string
/**
* 创建新的模型实例
* @access public
* @param array $data 数据
* @param mixed $where 更新条件
* @param array $data 数据
* @param mixed $where 更新条件
* @param array $options 参数
* @return Model
*/
public function newInstance(array $data = [], $where = null): Model
public function newInstance(array $data = [], $where = null, array $options = []): Model
{
$model = new static($data);

Expand Down Expand Up @@ -970,21 +971,25 @@ public function __unset(string $name): void
}

// ArrayAccess
#[\ReturnTypeWillChange]
public function offsetSet($name, $value)
{
$this->setAttr($name, $value);
}

#[\ReturnTypeWillChange]
public function offsetExists($name): bool
{
return $this->__isset($name);
}

#[\ReturnTypeWillChange]
public function offsetUnset($name)
{
$this->__unset($name);
}

#[\ReturnTypeWillChange]
public function offsetGet($name)
{
return $this->getAttr($name);
Expand Down Expand Up @@ -1037,10 +1042,6 @@ public function __call($method, $args)
return call_user_func_array(static::$macro[static::class][$method]->bindTo($this, static::class), $args);
}

if ('withattr' == strtolower($method)) {
return call_user_func_array([$this, 'withAttribute'], $args);
}

return call_user_func_array([$this->db(), $method], $args);
}

Expand Down
10 changes: 8 additions & 2 deletions src/Paginator.php
Expand Up @@ -410,7 +410,8 @@ public function each(callable $callback)
* @return Traversable An instance of an object implementing <b>Iterator</b> or
* <b>Traversable</b>
*/
public function getIterator()
#[\ReturnTypeWillChange]
public function getIterator(): Traversable
{
return new ArrayIterator($this->items->all());
}
Expand All @@ -421,7 +422,8 @@ public function getIterator()
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
#[\ReturnTypeWillChange]
public function offsetExists($offset): bool
{
return $this->items->offsetExists($offset);
}
Expand All @@ -432,6 +434,7 @@ public function offsetExists($offset)
* @param mixed $offset
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->items->offsetGet($offset);
Expand All @@ -443,6 +446,7 @@ public function offsetGet($offset)
* @param mixed $offset
* @param mixed $value
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
$this->items->offsetSet($offset, $value);
Expand All @@ -455,6 +459,7 @@ public function offsetSet($offset, $value)
* @return void
* @since 5.0.0
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
$this->items->offsetUnset($offset);
Expand Down Expand Up @@ -498,6 +503,7 @@ public function toArray(): array
/**
* Specify data which should be serialized to JSON
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->toArray();
Expand Down
17 changes: 11 additions & 6 deletions src/db/BaseQuery.php
Expand Up @@ -137,7 +137,7 @@ public function newQuery(): BaseQuery
$query->name($this->name);
}

if (isset($this->options['json'])) {
if (!empty($this->options['json'])) {
$query->json($this->options['json'], $this->options['json_assoc']);
}

Expand Down Expand Up @@ -278,7 +278,11 @@ public function value(string $field, $default = null)
public function column($field, string $key = ''): array
{
$result = $this->connection->column($this, $field, $key);
$this->resultSet($result, false);

if (count($result) != count($result, 1)) {
$this->resultSet($result, false);
}

return $result;
}

Expand Down Expand Up @@ -867,6 +871,7 @@ public function json(array $json = [], bool $assoc = false)
{
$this->options['json'] = $json;
$this->options['json_assoc'] = $assoc;

return $this;
}

Expand Down Expand Up @@ -1124,7 +1129,7 @@ public function select($data = null): Collection
* 查找单条记录
* @access public
* @param mixed $data 查询数据
* @return array|Model|null|static
* @return array|Model|null|static|mixed
* @throws Exception
* @throws ModelNotFoundException
* @throws DataNotFoundException
Expand All @@ -1149,7 +1154,7 @@ public function find($data = null)

if (!empty($this->model)) {
// 返回模型对象
$this->resultToModel($result, $this->options);
$this->resultToModel($result);
} else {
$this->result($result);
}
Expand Down Expand Up @@ -1178,7 +1183,7 @@ public function parseOptions(): array
$this->parseView($options);
}

foreach (['data', 'order', 'join', 'union'] as $name) {
foreach (['data', 'order', 'join', 'union', 'filter', 'json', 'with_attr', 'with_relation_attr'] as $name) {
if (!isset($options[$name])) {
$options[$name] = [];
}
Expand All @@ -1188,7 +1193,7 @@ public function parseOptions(): array
$options['strict'] = $this->connection->getConfig('fields_strict');
}

foreach (['master', 'lock', 'fetch_sql', 'array', 'distinct', 'procedure'] as $name) {
foreach (['master', 'lock', 'fetch_sql', 'array', 'distinct', 'procedure', 'with_cache'] as $name) {
if (!isset($options[$name])) {
$options[$name] = false;
}
Expand Down
6 changes: 2 additions & 4 deletions src/db/Fetch.php
Expand Up @@ -421,10 +421,8 @@ public function count(string $field = '*'): string

if (!empty($options['group'])) {
// 支持GROUP
$bind = $this->query->getBind();
$subSql = $this->query->options($options)->field('count(' . $field . ') AS think_count')->bind($bind)->buildSql();

$query = $this->query->newQuery()->table([$subSql => '_group_count_']);
$subSql = $this->query->field('count(' . $field . ') AS think_count')->buildSql();
$query = $this->query->newQuery()->table([$subSql => '_group_count_']);

return $query->fetchsql()->aggregate('COUNT', '*');
} else {
Expand Down
6 changes: 1 addition & 5 deletions src/db/Mongo.php
Expand Up @@ -630,7 +630,7 @@ public function parseOptions(): array
$options['table'] = $this->getTable();
}

foreach (['where', 'data'] as $name) {
foreach (['where', 'data', 'projection', 'filter', 'json', 'with_attr', 'with_relation_attr'] as $name) {
if (!isset($options[$name])) {
$options[$name] = [];
}
Expand All @@ -649,10 +649,6 @@ public function parseOptions(): array
$options['modifiers'] = $modifiers;
}

if (!isset($options['projection'])) {
$options['projection'] = [];
}

if (!isset($options['typeMap'])) {
$options['typeMap'] = $this->getConfig('type_map');
}
Expand Down
10 changes: 5 additions & 5 deletions src/db/PDOConnection.php
Expand Up @@ -279,19 +279,19 @@ public function fieldCase(array $info): array
*/
protected function getFieldType(string $type): string
{
if (0 === strpos($type, 'set') || 0 === strpos($type, 'enum')) {
if (0 === stripos($type, 'set') || 0 === stripos($type, 'enum')) {
$result = 'string';
} elseif (preg_match('/(double|float|decimal|real|numeric)/is', $type)) {
$result = 'float';
} elseif (preg_match('/(int|serial|bit)/is', $type)) {
$result = 'int';
} elseif (preg_match('/bool/is', $type)) {
$result = 'bool';
} elseif (0 === strpos($type, 'timestamp')) {
} elseif (0 === stripos($type, 'timestamp')) {
$result = 'timestamp';
} elseif (0 === strpos($type, 'datetime')) {
} elseif (0 === stripos($type, 'datetime')) {
$result = 'datetime';
} elseif (0 === strpos($type, 'date')) {
} elseif (0 === stripos($type, 'date')) {
$result = 'date';
} else {
$result = 'string';
Expand Down Expand Up @@ -1273,7 +1273,7 @@ public function getRealSql(string $sql, array $bind = []): string
$type = is_array($val) ? $val[1] : PDO::PARAM_STR;

if (self::PARAM_FLOAT == $type || PDO::PARAM_STR == $type) {
$value = '\'' . addslashes($value) . '\'';
$value = '\'' . addcslashes($value, "'") . '\'';
} elseif (PDO::PARAM_INT == $type && '' === $value) {
$value = '0';
}
Expand Down
32 changes: 24 additions & 8 deletions src/db/builder/Sqlite.php
Expand Up @@ -24,8 +24,8 @@ class Sqlite extends Builder
/**
* limit
* @access public
* @param Query $query 查询对象
* @param mixed $limit
* @param Query $query 查询对象
* @param mixed $limit
* @return string
*/
public function parseLimit(Query $query, string $limit): string
Expand All @@ -47,7 +47,7 @@ public function parseLimit(Query $query, string $limit): string
/**
* 随机排序
* @access protected
* @param Query $query 查询对象
* @param Query $query 查询对象
* @return string
*/
protected function parseRand(Query $query): string
Expand All @@ -58,9 +58,9 @@ protected function parseRand(Query $query): string
/**
* 字段和表名处理
* @access public
* @param Query $query 查询对象
* @param mixed $key 字段名
* @param bool $strict 严格检测
* @param Query $query 查询对象
* @param mixed $key 字段名
* @param bool $strict 严格检测
* @return string
*/
public function parseKey(Query $query, $key, bool $strict = false): string
Expand All @@ -73,7 +73,7 @@ public function parseKey(Query $query, $key, bool $strict = false): string

$key = trim($key);

if (strpos($key, '.')) {
if (strpos($key, '.') && !preg_match('/[,\'\"\(\)`\s]/', $key)) {
[$table, $key] = explode('.', $key, 2);

$alias = $query->getOptions('alias');
Expand All @@ -88,10 +88,26 @@ public function parseKey(Query $query, $key, bool $strict = false): string
}
}

if ('*' != $key && !preg_match('/[,\'\"\*\(\)`.\s]/', $key)) {
$key = '`' . $key . '`';
}

if (isset($table)) {
$key = $table . '.' . $key;
$key = '`' . $table . '`.' . $key;
}

return $key;
}

/**
* 设置锁机制
* @access protected
* @param Query $query 查询对象
* @param bool|string $lock
* @return string
*/
protected function parseLock(Query $query, $lock = false): string
{
return '';
}
}

0 comments on commit 149d266

Please sign in to comment.