Giter VIP home page Giter VIP logo

mysqli's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mysqli's Issues

where null查询会查不到数据

  1. where('time', null) // time = null ×
  2. where('time', null, '=') // time = null ×
  3. where(['time' => null]) // time = null ×
  4. where('time', null, 'is') // time is null √
  5. where('time is null') // time is null √

以上1-3常用的方式会被解析成错误的where条件, 导致查询null值失败

model 创建链接池使用的引用变量 自己测试没问题 想听一下权威的建议

public function __construct(&$transmit, $params)
{
$this->transmit = &$transmit;
$this->params = $params;
if (isset($this->transmit['dbs'][$this->database])
&& $this->transmit['dbs'][$this->database] instanceof MysqlObject) {
$this->db = $this->transmit['dbs'][$this->database];
} else {
$db = PoolManager::getInstance()->getPool($this->getMysqlPool())->getObj(Config::getInstance()->getConf('mysql.' . $this->database . '.POOL_TIME_OUT'));
$this->db = $this->transmit['dbs'][$this->database] = $db;
}
if (!($this->db instanceof MysqlObject)) {
throw new \Exception($this->database . 'mysql pool is empty');
}
}

这种方式调用的mysql池 自己测试没有问题

是在controller 和 model 中使用getModel调用 model

/**

  • @var $serviceModel ServiceModel
    */
    $serviceModel = $this->getModel(ServiceModel::class);
    protected function getModel($model)
    {
    if (!class_exists($model)) {
    $this->error('model 类 ' . $model . ' 不存在!');
    }
    $obj = new $model($this->transmit, $this->params);
    return $obj;
    }
    释放连接池也是在 如 controller gc()中完成
    但是之前有人在群里说这种方式 怎么死的也不会知道 就是心理有点怪怪的
    不知道这种方式会不会真有问题

只是想用这种方式实现 没有mysql代理情况下的分库 和 跨model的事务嵌套

auto add back quote

image
在数据库字段为关键词时,直接使用field方法声明字段会报错,如下:
image
需要手动在字段上增加“``”反引号才不会报错,querybuilder这里是不是应该给字段自动增加一下反引号

使用计数器开启嵌套事务出现问题在初始化的时候没有 导致回滚出错+计数器

改造后:

public function startTransactionWithCount($resetLevel = false)
{
// 强制结束上次事务并重开
if ($resetLevel) {
$this->transactionLevel = 0;
$this->rollback();
$this->commit();
}
++$this->transactionLevel;
if ($this->transactionLevel == 1) {
$this->startTransaction();
}
}

public function commitWithCount()
{
if ($this->transactionLevel == 1) {
$this->commit();
}
$this->transactionLevel--;
}

public function rollbackWithCount($commit = true)
{
if ($this->transactionLevel == 1) {
$this->transactionLevel = 0;
$this->rollback($commit);
} else {
$this->transactionLevel--;
}
}

不支持where括号嵌套语句,只能手写,但是特殊情况相当麻烦,比如in多值条件特别麻烦

贡上一个简单的函数,功能非常羞涩

public function buildWhere($fields)
{
if (count($fields) > 1 && isset($fields[0]) && is_string($fields[0])) {
$tmpFill = [null, null, '='];
list($field, $params, $operator) = $fields + $tmpFill;
switch (strtolower($operator)) {
case 'not in':
case 'in':
$inSql = '';
foreach ($params as $param) {
if (is_string($param)) {
$inSql .= ", '{$param}' ";
} else {
$inSql .= ", {$param} ";
}
}
$newSql = " " . $field . " " . strtoupper($operator) . "(" . ltrim($inSql, ',') . ") ";
break;
case 'not between':
case 'between':
$beginStr = is_string($params[0]) ? "'{$params[0]}'" : $params[0];
$endStr = is_string($params[1]) ? "'{$params[1]}'" : $params[1];
$newSql = " {$field} " . strtoupper($operator) . " {$beginStr} AND {$endStr} ";
break;
default:
if (is_string($params)) {
$newSql = " " . $field . " " . strtoupper($operator) . " '" . $params . "' ";
} elseif ($params === null) {
$newSql = " " . $field . " " . strtoupper($operator) . " NULL";
} else {
$newSql = " " . $field . " " . strtoupper($operator) . " " . $params . " ";
}
}
return $newSql;
} else {
$whereArray = [];
$cond = $fields[count($fields) - 1];
if (is_string($cond) && in_array(strtoupper($cond), ['OR', 'AND'])) {
$cond = strtoupper($cond);
unset($fields[count($fields) - 1]);
} else {
$cond = ' AND ';
}
foreach ($fields as $value) {
$whereArray[] = $this->buildWhere($value);
}
return " ( " . implode($cond, $whereArray) . " ) ";
}

}

使用方法:

$db = Mysql::defer('mysql');
$where = $db->buildWhere([
["ip", "%127.0.0.1%", 'like'],
[
['id', '1', '>'],
[
['id', ['5', 10], 'not in'],
['id', '1'],
'or'
],
["username", "abc"],
'or'
],
'and'
]);
$db->where($where)->get('user');
// "SELECT * FROM user WHERE (
// ip LIKE '%127.0.0.1%' AND (
// id > '1' OR (
// id NOT IN( '5' , 10 ) OR
// id = '1' )
// OR username = 'abc'
// )
// ) "

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.