diff --git a/.travis.yml b/.travis.yml index 15a23c3..f95d1ac 100644 --- a/.travis.yml +++ b/.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/Statement.php b/src/Statement.php index f25e804..3bc3466 100644 --- a/src/Statement.php +++ b/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/test/Cases/Mysql/SqlTest.php b/test/Cases/Mysql/SqlTest.php index 4950b22..d923741 100644 --- a/test/Cases/Mysql/SqlTest.php +++ b/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/test/Testing/Entity/Group.php b/test/Testing/Entity/Group.php new file mode 100644 index 0000000..454c2b7 --- /dev/null +++ b/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