From aa8f96016dbc603cdb4b9ff15ace8eb22ab88bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Sat, 15 Sep 2018 01:30:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=9B=B4=E6=96=B0=E5=92=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=B6=EF=BC=8C=E8=A1=A8=E5=90=8D=E6=98=AF?= =?UTF-8?q?Mysql=E5=85=B3=E9=94=AE=E5=AD=97=E4=BC=9A=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E7=9A=84BUG=20(https://github.com/swoft-cloud/swoft-component/?= =?UTF-8?q?pull/204)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/.travis.yml | 5 +++ src/db/src/Statement.php | 4 +- src/db/test/Cases/Mysql/SqlTest.php | 33 ++++++++++++--- src/db/test/Testing/Entity/Group.php | 61 ++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 src/db/test/Testing/Entity/Group.php diff --git a/src/db/.travis.yml b/src/db/.travis.yml index 15a23c35e..f95d1ac4e 100644 --- a/src/db/.travis.yml +++ b/src/db/.travis.yml @@ -66,6 +66,11 @@ before_install: `id` bigint(20) unsigned NOT NULL, `name` varchar(32) NOT NULL DEFAULT "", PRIMARY KEY (`id`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + CREATE TABLE `group` ( + `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(32) NOT NULL DEFAULT "", + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;' - mysql -e 'CREATE DATABASE IF NOT EXISTS test2; USE test2; diff --git a/src/db/src/Statement.php b/src/db/src/Statement.php index f25e8042d..3bc3466ed 100644 --- a/src/db/src/Statement.php +++ b/src/db/src/Statement.php @@ -684,7 +684,7 @@ protected function getInsertString(): string $statement .= $this->getInsert(); if (!empty($statement)) { - $statement = 'INSERT INTO ' . $statement; + $statement = sprintf('INSERT INTO `%s`', $statement); } return $statement; @@ -708,7 +708,7 @@ protected function getUpdateString(): string $statement .= ' ' . $this->getJoinString(); $statement = rtrim($statement); if (!empty($statement)) { - $statement = 'UPDATE ' . $statement; + $statement = sprintf('UPDATE `%s`', $statement); } return $statement; diff --git a/src/db/test/Cases/Mysql/SqlTest.php b/src/db/test/Cases/Mysql/SqlTest.php index 4950b22d4..d92374193 100644 --- a/src/db/test/Cases/Mysql/SqlTest.php +++ b/src/db/test/Cases/Mysql/SqlTest.php @@ -10,6 +10,7 @@ namespace SwoftTest\Db\Cases\Mysql; use Swoft\Db\Db; +use SwoftTest\Db\Testing\Entity\Group; use SwoftTest\Db\Testing\Entity\User; use SwoftTest\Db\Cases\AbstractMysqlCase; @@ -20,14 +21,14 @@ class SqlTest extends AbstractMysqlCase { public function testInsert() { - $name = 'swoft insert'; + $name = 'swoft insert'; $result = Db::query('insert into user(name, sex,description, age) values("' . $name . '", 1, "xxxx", 99)')->getResult(); - $user = User::findById($result)->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)')->getResult(); - $user = User::findById($result)->getResult(); + $user = User::findById($result)->getResult(); $this->assertEquals($user['name'], $name); } @@ -71,7 +72,7 @@ public function testSelectByCo($id) */ public function testSelect2($id) { - $result = Db::query('select * from user where id=:id and name=:name', ['id' => $id, ':name'=>'name'])->getResult(); + $result = Db::query('select * from user where id=:id and name=:name', ['id' => $id, ':name' => 'name'])->getResult(); $result2 = Db::query('select * from user where id=? and name=?', [$id, 'name'])->getResult(); $this->assertEquals($id, $result[0]['id']); $this->assertEquals($id, $result2[0]['id']); @@ -119,11 +120,11 @@ public function testDeleteByCo($id) */ public function testUpdate($id) { - $name = 'update name1'; + $name = 'update name1'; $result = Db::query('update user set name="' . $name . '" where id=' . $id)->getResult(); $this->assertEquals(1, $result); - $name = 'update name 协程框架'; + $name = 'update name 协程框架'; $result = Db::query('UPDATE user set name="' . $name . '" where id=' . $id)->getResult(); $this->assertEquals(1, $result); @@ -173,4 +174,24 @@ public function testErrorSqlByCo() $this->testErrorSql(); }); } + + public function testTableNameIsDbKeyword() + { + $model = new Group(); + $model->setName(uniqid()); + $id = $model->save()->getResult(); + $this->assertTrue($id > 0); + + $model = Group::findById($id)->getResult(); + $model->setName(uniqid()); + $rows = $model->update()->getResult(); + $this->assertEquals(1, $rows); + } + + public function testTableNameIsDbKeywordByCo() + { + go(function () { + $this->testTableNameIsDbKeyword(); + }); + } } diff --git a/src/db/test/Testing/Entity/Group.php b/src/db/test/Testing/Entity/Group.php new file mode 100644 index 000000000..454c2b7ed --- /dev/null +++ b/src/db/test/Testing/Entity/Group.php @@ -0,0 +1,61 @@ +id; + } + + /** + * @param int $id + */ + public function setId(int $id) + { + $this->id = $id; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName(string $name) + { + $this->name = $name; + } +} \ No newline at end of file