Skip to content

Commit

Permalink
Merge branch 'master' into v5-demo
Browse files Browse the repository at this point in the history
  • Loading branch information
playadmin committed Mar 31, 2024
2 parents 803e6b9 + 81745b3 commit 39ebcdd
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 45 deletions.
3 changes: 2 additions & 1 deletion lib/z/dbc/DBmysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ function PriKey (?string $table = null): array|string
return $this->PRIKEYS[$table];
}

protected function DB_lockRows (string $sql): string
protected function DB_lockRows (string $sql, int|bool $lockExpire = null): string
{
$lockExpire && $this->PDO->Query("set @@innodb_lock_wait_timeout = {$lockExpire}");
return "{$sql} FOR UPDATE";
}

Expand Down
2 changes: 1 addition & 1 deletion lib/z/dbc/DBsqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function PriKey (?string $table = null): array|string
return $this->PRIKEYS[$table];
}

protected function DB_lockRows (string $sql): string|null
protected function DB_lockRows (string $sql, int|bool $lockExpire = null): string|null
{
return null;
}
Expand Down
17 changes: 9 additions & 8 deletions lib/z/dbc/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait cache
{
function GetCache (string $mod = '', string $dbname = '', string $table, string $key)
function GetCache (string $table, string $key, string $dbname = '', string $mod = '')
{
$mod || $mod = $this->DB_CONFIG['cache_mod'];
$dbname || $dbname = $this->DB_CONFIG['dbname'];
Expand All @@ -17,7 +17,7 @@ function GetCache (string $mod = '', string $dbname = '', string $table, string
};
}

function SetCache (string $mod = '', string $dbname = '', string $table, string $key, $data, int $expire = 0)
function SetCache (string $table, string $key, $data, int $expire = 0, string $dbname = '', string $mod = '')
{
$mod || $mod = $this->DB_CONFIG['cache_mod'];
$dbname || $dbname = $this->DB_CONFIG['dbname'];
Expand All @@ -28,14 +28,15 @@ function SetCache (string $mod = '', string $dbname = '', string $table, string
};
}

protected function cacheDir (string $dbname = '', string $table = '')
protected function cacheDir (string $table, string $dbname = '')
{
$dbname || $dbname = $this->DB_CONFIG['dbname'];
return P_CACHE . "{$this->DB_CONFIG['cache_dir']}/{$dbname}/{$table}";
}
function CleanCache (string $mod = '', string $dbname, string $table = ''): int
function CleanCache (string $table, string $mod = '', string $dbname = ''): int
{
$mod || $mod = $this->DB_CONFIG['cache_mod'];
$dbname || $dbname = $this->DB_CONFIG['dbname'];
switch ($mod) {
case 'redis':
return $this->delRedis($dbname, $table);
Expand All @@ -48,13 +49,13 @@ function CleanCache (string $mod = '', string $dbname, string $table = ''): int
break;
}
}
private function delFile(string $dbname = '', string $table = ''): int | false
private function delFile(string $table, string $dbname = ''): int | false
{
$dbname || $dbname = $this->DB_CONFIG['dbname'];
$dir = $this->cacheDir($dbname, $table);
return DelDir($dir, true);
}
protected function delRedis (string $dbname = '', string $table = ''): int
protected function delRedis (string $table, string $dbname = ''): int
{
$dbname || $dbname = $this->DB_CONFIG['dbname'];
$path = "DB:{$dbname}:";
Expand All @@ -72,7 +73,7 @@ protected function delRedis (string $dbname = '', string $table = ''): int
}
return $result;
}
protected function delMemcached($dbname = '', $table): int
protected function delMemcached(string $table, string $dbname = ''): int
{
$dbname || $dbname = $this->DB_CONFIG['dbname'];
$path = "DB:{$dbname}:";
Expand All @@ -90,7 +91,7 @@ protected function delMemcached($dbname = '', $table): int
return $n;
}

protected function getFileCache (string $dir, $key)
protected function getFileCache (string $dir, string $key)
{
$file = "{$dir}/{$key}.php";
list($data, $expire) = c::GetExpireFileCache($file, c::CODE_PHP_ARR);
Expand Down
65 changes: 33 additions & 32 deletions lib/z/dbc/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function Wrap (string|array $key, string $alias = '')
}
return $this->WrapSql($key);
}
function WrapSql($sql)
function WrapSql(string $sql)
{
if (isset(self::SQL_KEYWORDS[strtoupper($sql)])) {
return "{$this->DB_WRAP_L}{$sql}{$this->DB_WRAP_R}";
Expand Down Expand Up @@ -330,18 +330,18 @@ public function QueryChain(&$data, array $chain)
}
public function Parse ($reset = true): array
{
$sql = $this->DB_sql(true);
$sql = $this->DB_sql();
$field = $this->DB_field();
$res[] = "SELECT {$field} FROM " . $sql;
$res[] = $this->DB_BIND;
$reset && $this->DB_done();
return $res;
}
public function SubQuery(string $field = '', bool $lock = false): string
public function SubQuery(string $field = '', int|bool $lock = false): string
{
$field && $this->DB_FIELD = $this->WrapSql($field);
list($sql) = $this->Parse(false);
$lock && $sql = $this->DB_lockRows($sql);
$lock && $sql = $this->DB_lockRows($sql, $lock);
$this->DB_WHERE = [];
$this->DB_WHERED = '';
$this->DB_TMP = '';
Expand All @@ -363,7 +363,7 @@ abstract function Columns (string $table): array | false;
abstract function Column (string $table, string $field): array | bool;
abstract function PriKey (?string $table = null): array|string;
abstract protected function DB_ApproximateRows (string $table): int; // 数据表总数据量的模糊值,非必要,无此功能的数据库可返回-1
abstract protected function DB_lockRows (string $sql): string|null; // 锁定数据行,无此功能的数据库可直接返回参数$sql(返回null则会抛出异常)
abstract protected function DB_lockRows (string $sql, int|bool $lockExpire = null): string|null; // 锁定数据行,无此功能的数据库可直接返回参数$sql(返回null则会抛出异常)
abstract protected function DB_duplicate(array $insert, array $update = null): array|string|bool|int|null; // 有则更新,无则插入(返回null则会抛出异常)
abstract protected function DB_dcount (string $from, string $sfield, string $field): int|false|null;// 去重统计(返回null则会抛出异常)
abstract protected function DB_iinsert(string $table, array $data): stmt|false|null; // 插入数据, 已存在数据则不插入(返回null则会抛出异常)
Expand Down Expand Up @@ -393,7 +393,7 @@ public function Count(string $field = '', bool $done = true): int
} else {
$result = $this->PDO->Stmt("SELECT COUNT({$field}) FROM {$from}", $this->DB_BIND)->Row(\PDO::FETCH_COLUMN);
}
$done && $this->DB_done();
$done ? $this->DB_done() : $this->DB_SQLD = '';
return (int)$result;
}
public function Merge(string $sql, string|array $type = ''): static
Expand All @@ -415,7 +415,7 @@ public function Merge(string $sql, string|array $type = ''): static
}
return $this;
}
public function Fetch(bool $lock = false): \PDOStatement
public function Fetch(int|bool $lock = false): \PDOStatement
{
if ($this->DB_PAGE) {
$this->DB_page();
Expand All @@ -426,15 +426,15 @@ public function Fetch(bool $lock = false): \PDOStatement
}
$field = $this->DB_field();
$sql = "SELECT {$field} FROM " . $sql;
$lock && $sql = $this->DB_lockRows($sql);
$lock && $sql = $this->DB_lockRows($sql, $lock);
if (null === $sql) {
throw new Exception('Row lock is not supported');
}
$stmt = $this->PDO->Stmt($sql, $this->DB_BIND)->Execute();
$this->DB_done();
return $stmt;
}
public function Find(string | bool $field = false, bool $lock = false)
public function Find(string | bool $field = false, int|bool $lock = false)
{
if ($field) {
$fetch = \PDO::FETCH_COLUMN;
Expand All @@ -446,7 +446,7 @@ public function Find(string | bool $field = false, bool $lock = false)
$field = $this->DB_field();
$sql = "SELECT {$field} FROM {$sql}";
if ($lock || !$this->Z_CACHE) {
$lock && $sql = $this->DB_lockRows($sql);
$lock && $sql = $this->DB_lockRows($sql, $lock);
if (null === $sql) {
throw new Exception('Row lock is not supported');
}
Expand All @@ -464,10 +464,10 @@ public function CacheFind(int $fetch, string $sql): array
$this->Z_CACHE = [];
$result = $this->GetCache($mod, $this->DB_CONFIG['dbname'], $table, $key);
if (null === $result) {
$res = $this->SetCache($mod, $this->DB_CONFIG['dbname'], $table, $key, function () use ($sql, $fetch) {
$res = $this->SetCache($table, $key, function () use ($sql, $fetch) {
$stmt = $this->PDO->Stmt($sql, $this->DB_BIND);
return $stmt->Row($fetch);
}, $expire);
}, $expire, $this->DB_CONFIG['dbname'], $mod);
$result = $res[1];
}
return $result;
Expand All @@ -487,34 +487,34 @@ public function CacheSelect(int $fetch): array
$result = $this->GetCache($mod, $this->DB_CONFIG['dbname'], $table, $key);
$paged = $this->GetCache($mod, $this->DB_CONFIG['dbname'], $table, $pkey);
if (null === $result || !$paged) {
$res = $this->SetCache($mod, $this->DB_CONFIG['dbname'], $table, $key, function () use ($sql, $fetch, $mod, $table, $pkey, $expire) {
$res = $this->SetCache($table, $key, function () use ($sql, $fetch, $mod, $table, $pkey, $expire) {
$this->DB_page();
$sql .= $this->DB_sql(true);
$rows = $this->PDO->Stmt($sql, $this->DB_BIND)->Rows($fetch);
$this->SetCache($mod, $this->DB_CONFIG['dbname'], $table, $pkey, $this->DB_PAGED, $expire);
$this->SetCache($table, $pkey, $this->DB_PAGED, $expire, $mod, $this->DB_CONFIG['dbname']);
$rows && $this->DB_CHAIN && $this->QueryChain($rows, $this->DB_CHAIN);
$this->DB_done();
return $rows;
}, $expire);
}, $expire, $this->DB_CONFIG['dbname'], $mod);
$result = $res[1];
} else {
$this->DB_PAGED = $paged;
}
} else {
$sql .= $this->DB_sql(true);
list($table, $key) = $this->CacheKey('2', $fetch, $sql);
$result = $this->GetCache($mod, $this->DB_CONFIG['dbname'], $table, $key);
$result = $this->GetCache($table, $key, $this->DB_CONFIG['dbname'], $mod);
if (null === $result) {
$res = $this->SetCache($mod, $this->DB_CONFIG['dbname'], $table, $key, function () use ($sql, $fetch) {
$res = $this->SetCache($table, $key, function () use ($sql, $fetch) {
$stmt = $this->PDO->Stmt($sql, $this->DB_BIND);
return $stmt->Rows($fetch);
}, $expire);
}, $expire, $this->DB_CONFIG['dbname'], $mod);
$result = $res[1];
}
}
return $result;
}
public function Select(string | bool $field = false, bool $lock = false): array
public function Select(string | bool $field = false, int|bool $lock = false): array
{
if ($field) {
$fetch = \PDO::FETCH_COLUMN;
Expand All @@ -525,8 +525,8 @@ public function Select(string | bool $field = false, bool $lock = false): array
if ($lock || !$this->Z_CACHE) {
$field = $this->DB_field();
$this->DB_PAGE && $this->DB_page();
$sql = "SELECT {$field} FROM " . $this->DB_sql(!!$this->DB_PAGE);
$lock && $sql = $this->DB_lockRows($sql);
$sql = "SELECT {$field} FROM " . $this->DB_sql();
$lock && $sql = $this->DB_lockRows($sql, $lock);
if (null === $sql) {
throw new Exception('Row lock is not supported');
}
Expand Down Expand Up @@ -966,14 +966,14 @@ protected function DB_whereArr(array $where): array
foreach ($where as $k => $value) {
if (is_int($k)) {
// 键名不是字段的情况
if (!$sql) {
$pre = strtoupper(substr($value, 0, 3));
if ('AND' === $pre || 'OR ' === $pre) {
$value = ltrim(substr($value, 3));
}
$pre = strtoupper(trim(substr($value, 0, 3)));
if ('AND' === $pre || 'OR' === $pre) {
$lc || $lc = "{$pre} ";
return [$this->WrapSql($value), ''];
} else {
$lc || $lc = 'AND ';
return [$this->WrapSql($value), $lc];
}
$sql .= $this->WrapSql($value);
continue;
}

list($isSqlValue, $logic, $key, $operator) = $this->DB_checkKey($k);
Expand All @@ -987,7 +987,7 @@ protected function DB_whereArr(array $where): array
'<>', '!=' => 'NOT IN',
default => $operator ?: 'IN',
};
} elseif ($isSqlValue && is_string($value) && preg_match('/^SELECT\s/i', $value)) {
} elseif (is_string($value) && 'SELECT' === strtoupper(substr($value, 0, 6))) {
// 包含子查询
$operator || $operator = 'IN';
$sql && $sql .= " {$logic}";
Expand Down Expand Up @@ -1047,20 +1047,21 @@ protected function DB_checkKey(string $key): array
$isSqlValue = false;
$operator = '';
$logic = '';
$preg = '/^(OR\s+|AND\s+)?([\|\:\w.]+)\s*([\<\>\=\!]{0,2}|\s+(NOT\s+)?(IN|LIKE|BETWEEN))$/i';
$preg = '/^(OR\s+|AND\s+)?([\|\:\w.]+)\s*([\<\>\=\!]{0,2}|\s+(NOT\s+)?(IN|LIKE\s+|BETWEEN\s+))$/i';
if (preg_match($preg, $key, $match)) {
$logic = empty($match[1]) ? '' : $match[1];
$keys = $match[2];
$operator = empty($match[3]) ? '=' : trim($match[3]);
$operator = $match[3] ?? '=';
if (str_contains($keys, '|') && $keys = explode('|', $keys)) {
':' === $keys[0][0] && ($isSqlValue = true) && $keys[0] = substr($keys[0], 1);
} else {
$keys = $match[2];
':' === $keys[0] && ($isSqlValue = true) && $keys = substr($keys, 1);
}
$keys = $this->Wrap($keys);
} else {
':' === $key[0] && ($isSqlValue = true) && $key = substr($key, 1);
$keys = $this->WrapSql($key);
':' === $keys[0] && ($isSqlValue = true) && $keys = substr($keys, 1);
}
return [$isSqlValue, $logic, $keys, $operator];
}
Expand Down
7 changes: 4 additions & 3 deletions lib/z/dbc/dbc.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,22 @@ public static function InitCfg (array &$cfg): string {
} elseif (empty($c['drive'])) {
$c['drive'] = strstr($c['dsn'], ':', true);
}
$key = md5($c['dsn'] . '|' . ($c['user'] ?? ''));
$key = empty($c['key']) ? md5($c['dsn'] . '|' . ($c['user'] ?? '')) : $c['key'];
$c['__key'] = $key;
$keys[] = $key;
empty($c['dbname']) && $c['dbname'] = 'mydb';
empty($c['cache_mod']) && $c['cache_mod'] = 'file';
empty($c['cache_dir']) && $c['cache_dir'] = 'db';
}
$s = md5(implode(',', $keys));
$s = implode(',', $keys);
strlen($s) > 32 && $s = md5($s);
} else {
if (empty($cfg['dsn'])) {
$cfg['dsn'] = self::dsn($cfg);
} elseif (empty($cfg['drive'])) {
$cfg['drive'] = strstr($cfg['dsn'], ':', true);
}
$key = md5($cfg['dsn'] . '|' . ($cfg['user'] ?? ''));
$key = empty($cfg['key']) ? md5($cfg['dsn'] . '|' . ($cfg['user'] ?? '')) : $cfg['key'];
$s = $cfg['__key'] = $key;
empty($cfg['dbname']) && $cfg['dbname'] = 'mydb';
empty($cfg['cache_mod']) && $cfg['cache_mod'] = 'file';
Expand Down

0 comments on commit 39ebcdd

Please sign in to comment.