Skip to content

Commit

Permalink
fix sql bug
Browse files Browse the repository at this point in the history
  • Loading branch information
stelin committed Mar 21, 2018
1 parent 9f2b121 commit 2ff10e3
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Driver/Mysql/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

Expand Down
16 changes: 16 additions & 0 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
134 changes: 134 additions & 0 deletions test/Cases/SqlMysqlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace Swoft\Db\Test\Cases;

use Swoft\Db\Db;
use Swoft\Db\Test\Testing\Entity\User;

/**
* SqlMysqlTest
*/
class SqlMysqlTest extends DbTestCase
{
/**
* @dataProvider mysqlProvider
*
* @param int $id
*/
public function testUpdate(int $id)
{
$this->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']);
}
}

0 comments on commit 2ff10e3

Please sign in to comment.