diff --git a/src/Driver/Mysql/QueryBuilder.php b/src/Driver/Mysql/QueryBuilder.php index cde6a7a..f4a498e 100644 --- a/src/Driver/Mysql/QueryBuilder.php +++ b/src/Driver/Mysql/QueryBuilder.php @@ -54,9 +54,9 @@ private function getSyncResult() App::debug(sprintf('sql execute sqlId=%s, result=%s, sql=%s', $sqlId, JsonHelper::encode($result, JSON_UNESCAPED_UNICODE), $sql)); $isFindOne = isset($this->limit['limit']) && $this->limit['limit'] === 1; - if ($this->isInsert()) { + if ($this->isSqlOperation($sql, 'insert')) { $result = $connection->getInsertId(); - } elseif ($this->isUpdate() || $this->isDelete()) { + } elseif ($this->isSqlOperation($sql, 'update') || $this->isSqlOperation($sql, 'delete')) { $result = $connection->getAffectedRows(); } else { $result = $connection->fetch(); @@ -80,19 +80,20 @@ private function getCorResult() $sql = $this->getStatement(); list($sqlId, $profileKey) = $this->getSqlIdAndProfileKey($sql); - /* @var AbstractDbConnection $connection*/ + /* @var AbstractDbConnection $connection */ $connection = $this->selectConnection(); $connection->setDefer(); $connection->prepare($sql); $result = $connection->execute($this->parameters); App::debug(sprintf('sql execute sqlId=%s, sql=%s', $sqlId, $sql)); - $isUpdateOrDelete = $this->isDelete() || $this->isUpdate(); + $isInsert = $this->isSqlOperation($sql, 'insert'); + $isUpdateOrDelete = $this->isSqlOperation($sql, 'delete') || $this->isSqlOperation($sql, 'update'); $isFindOne = $this->isSelect() && isset($this->limit['limit']) && $this->limit['limit'] === 1; $corResult = new DbCoResult($connection, $profileKey); // 结果转换参数 - $corResult->setInsert($this->isInsert()); + $corResult->setInsert($isInsert); $corResult->setUpdateOrDelete($isUpdateOrDelete); $corResult->setFindOne($isFindOne); diff --git a/src/Statement.php b/src/Statement.php index ded81e6..3ac28ea 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -740,4 +740,20 @@ protected function getFromAlias(): string return $alias; } + + /** + * @param string $sql + * @param string $operation + * + * @return bool + */ + protected function isSqlOperation(string $sql, $operation = 'update'): bool + { + $sql = trim($sql); + $sql = strtolower($sql); + if(strpos($sql, $operation) === 0){ + return true; + } + return false; + } } diff --git a/test/Cases/SqlMysqlTest.php b/test/Cases/SqlMysqlTest.php new file mode 100644 index 0000000..52c3a71 --- /dev/null +++ b/test/Cases/SqlMysqlTest.php @@ -0,0 +1,134 @@ +update($id); + } + + /** + * @dataProvider mysqlProvider + * + * @param int $id + */ + public function testDelete(int $id) + { + $this->delete($id); + } + + /** + * @dataProvider mysqlProvider + * + * @param int $id + */ + public function testCoDelete(int $id) + { + go(function () use ($id) { + $result = Db::query('DELETE from user where id=' . $id)->execute()->getResult(); + $this->assertEquals(1, $result); + }); + } + + + /** + * @dataProvider mysqlProvider + * + * @param int $id + */ + public function testCoUpdate(int $id) + { + go(function () use ($id) { + $this->update($id); + }); + } + + /** + * @dataProvider mysqlProvider + * + * @param int $id + */ + public function testSelect(int $id) + { + $this->select($id); + } + + /** + * @dataProvider mysqlProvider + * + * @param int $id + */ + public function testCoSelect(int $id) + { + go(function () use ($id) { + $this->select($id); + }); + } + + public function testInsert() + { + $this->insert(); + } + + public function testCoInsert() + { + go(function () { + $this->insert(); + }); + } + + private function insert() + { + $name = "swoft insert"; + $result = Db::query('insert into user(name, sex,description, age) values("' . $name . '", 1, "xxxx", 99)')->execute()->getResult(); + $user = User::findById($result)->getResult(); + + $this->assertEquals($user['name'], $name); + + $result = Db::query('INSERT into user(name, sex,description, age) values("' . $name . '", 1, "xxxx", 99)')->execute()->getResult(); + $user = User::findById($result)->getResult(); + $this->assertEquals($user['name'], $name); + } + + private function select($id) + { + $result = Db::query('select * from user where id=' . $id)->execute()->getResult(); + $this->assertCount(1, $result); + + $result = Db::query('SELECT * from user where id=' . $id)->execute()->getResult(); + $this->assertCount(1, $result); + } + + private function delete($id) + { + $result = Db::query('delete from user where id=' . $id)->execute()->getResult(); + $this->assertEquals(1, $result); + } + + private function update($id) + { + $name = 'update name1'; + $result = Db::query('update user set name="' . $name . '" where id=' . $id)->execute()->getResult(); + $this->assertEquals(1, $result); + + $name = 'update name 协程框架'; + $result = Db::query('UPDATE user set name="' . $name . '" where id=' . $id)->execute()->getResult(); + $this->assertEquals(1, $result); + + $user = User::findById($id)->getResult(); + $this->assertEquals($name, $user['name']); + } +} \ No newline at end of file