Skip to content

Commit

Permalink
修改更新和新增时,表名是Mysql关键字会报错的BUG (swoft-cloud/swoft-component#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo authored and huangzhhui committed Sep 14, 2018
1 parent 3bd24b2 commit a61928c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
33 changes: 27 additions & 6 deletions test/Cases/Mysql/SqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
});
}
}
61 changes: 61 additions & 0 deletions test/Testing/Entity/Group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace SwoftTest\Db\Testing\Entity;

use Swoft\Db\Bean\Annotation\Column;
use Swoft\Db\Bean\Annotation\Entity;
use Swoft\Db\Bean\Annotation\Id;
use Swoft\Db\Bean\Annotation\Table;
use Swoft\Db\Model;

/**
* @Entity()
* @Table("group")
*/
class Group extends Model
{
/**
* @Id()
* @Column(name="id", type="int")
* @var int
*/
private $id;

/**
* @Column(name="name", type="string")
* @var string
*/
private $name;

/**
* @return int
*/
public function getId()
{
return $this->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;
}
}

0 comments on commit a61928c

Please sign in to comment.