Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/swoft-cloud/swoft-db
Browse files Browse the repository at this point in the history
  • Loading branch information
stelin committed Mar 23, 2018
2 parents a163549 + cf3444b commit edae181
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 153 deletions.
4 changes: 2 additions & 2 deletions src/Entity/Mysql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Schema extends \Swoft\Db\Entity\Schema
'number' => 'Types::NUMBER',
'decimal' => 'Types::NUMBER',
'bool' => 'Types::BOOLEAN',
'tinyint' => 'Types::BOOLEAN',
'tinyint' => 'Types::INT',
];

/**
Expand All @@ -43,6 +43,6 @@ class Schema extends \Swoft\Db\Entity\Schema
'number' => self::TYPE_INT,
'decimal' => self::TYPE_INT,
'bool' => self::TYPE_BOOL,
'tinyint' => self::TYPE_BOOL
'tinyint' => self::TYPE_INT
];
}
9 changes: 6 additions & 3 deletions src/Entity/SetGetGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ private function parseProperty(string $propertyStub, array $fieldInfo)
$this->checkAliasProperty($aliasProperty);

$formatComment = " * @var {$phpType} \${$aliasProperty} {$comment}\n";
if (!empty($comment)) {
$formatComment = " * @var {$phpType} \${$aliasProperty}\n";
}

$this->propertyStub .= PHP_EOL . str_replace([
"{{comment}}\n",
Expand Down Expand Up @@ -208,6 +205,7 @@ private function parseProperty(string $propertyStub, array $fieldInfo)
private function parseSetter(string $setterStub, array $fieldInfo)
{
$property = $fieldInfo['name'];
$comment = $fieldInfo['column_comment'];
$aliasProperty = $property;
$this->checkAliasProperty($aliasProperty);
$function = explode('_', $aliasProperty);
Expand All @@ -219,11 +217,13 @@ private function parseSetter(string $setterStub, array $fieldInfo)
$primaryKey = $fieldInfo['key'] === 'PRI';
$type = $this->schema->phpSchema[$fieldInfo['type']] ?? 'mixed';
$this->setterStub .= PHP_EOL . str_replace([
'{{comment}}',
'{{function}}',
'{{attribute}}',
'{{type}}',
'{{hasReturnType}}'
], [
$comment,
$function,
$aliasProperty,
$type !== 'mixed' ? "{$type} " : '',
Expand All @@ -241,6 +241,7 @@ private function parseSetter(string $setterStub, array $fieldInfo)
private function parseGetter(string $getterStub, array $fieldInfo)
{
$property = $fieldInfo['name'];
$comment = $fieldInfo['column_comment'];
$aliasProperty = $property;
$this->checkAliasProperty($aliasProperty);
$function = explode('_', $aliasProperty);
Expand All @@ -253,11 +254,13 @@ private function parseGetter(string $getterStub, array $fieldInfo)
$primaryKey = $fieldInfo['key'] === 'PRI';
$returnType = $this->schema->phpSchema[$fieldInfo['type']] ?? 'mixed';
$this->getterStub .= PHP_EOL . str_replace([
'{{comment}}',
'{{function}}',
'{{attribute}}',
'{{coReturnType}}',
'{{returnType}}',
], [
$comment,
$function,
$aliasProperty,
$returnType,
Expand Down
3 changes: 1 addition & 2 deletions src/Entity/Stub/Getter.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/**
* {{function}}
*
* {{comment}}
* @return {{coReturnType}}
*/
public function {{function}}(){{returnType}}
Expand Down
4 changes: 1 addition & 3 deletions src/Entity/Stub/Setter.stub
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* {{function}}
*
* {{comment}}
* @param {{type}}$value
*
* @return $this
*/
public function {{function}}({{type}}$value){{hasReturnType}}
Expand Down
4 changes: 2 additions & 2 deletions test/.env
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TEST_PROVIDER=c1

# the pool of master nodes pool
DB_NAME=master2
DB_URI=127.0.0.1:3306/test?user=root&password=123456&charset=utf8,127.0.0.1:3306/test?user=root&password=123456&charset=utf8
DB_URI=127.0.0.1:3306/test?user=root&password=&charset=utf8,127.0.0.1:3306/test?user=root&password=&charset=utf8
DB_MIN_ACTIVE=1
DB_MAX_ACTIVE=30
DB_MAX_WAIT=10
Expand All @@ -24,7 +24,7 @@ DB_PROVIDER=consul2

# the pool of slave nodes pool
DB_SLAVE_NAME=slave2
DB_SLAVE_URI=127.0.0.1:3306/test?user=root&password=123456&charset=utf8,127.0.0.1:3306/test?user=root&password=123456&charset=utf8
DB_SLAVE_URI=127.0.0.1:3306/test?user=root&password=&charset=utf8,127.0.0.1:3306/test?user=root&password=&charset=utf8
DB_SLAVE_MIN_ACTIVE=1
DB_SLAVE_MAX_ACTIVE=30
DB_SLAVE_MAX_WAIT=10
Expand Down
111 changes: 64 additions & 47 deletions test/Cases/DbTestCase.php → test/Cases/AbstractDbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Swoft\Db\Test\Cases;

use PHPUnit\Framework\TestCase;
use Swoft\Db\EntityManager;
use Swoft\Db\Pool;
use Swoft\Db\QueryBuilder;
Expand All @@ -12,7 +11,7 @@
/**
* DbTestCache
*/
class DbTestCase extends TestCase
abstract class AbstractDbTestCase extends AbstractTestCase
{
public function arSave(string $group = Pool::GROUP)
{
Expand All @@ -22,25 +21,25 @@ public function arSave(string $group = Pool::GROUP)
$user->setDesc('this my desc');
$user->setAge(mt_rand(1, 100));

$id = $user->save($group)->getResult();
$id = $user->save($group)->getResult();
$reuslt = $id > 0;
$this->assertTrue($reuslt);
}

/**
* @param int $id
* @param int $id
* @param string $group
*/
public function arDelete(int $id, string $group = Pool::GROUP)
{
/* @var User $user */
$user = User::findById($id, $group)->getResult(User::class);
$user = User::findById($id, $group)->getResult(User::class);
$result = $user->delete($group)->getResult();
$this->assertEquals(1, $result);
}

/**
* @param int $id
* @param int $id
* @param string $group
*/
public function arDeleteById(int $id, string $group = Pool::GROUP)
Expand All @@ -50,7 +49,7 @@ public function arDeleteById(int $id, string $group = Pool::GROUP)
}

/**
* @param array $ids
* @param array $ids
* @param string $group
*/
public function arDeleteByIds(array $ids, string $group = Pool::GROUP)
Expand All @@ -60,7 +59,7 @@ public function arDeleteByIds(array $ids, string $group = Pool::GROUP)
}

/**
* @param int $id
* @param int $id
* @param string $group
*/
public function arUpdate(int $id, string $group = Pool::GROUP)
Expand All @@ -78,7 +77,7 @@ public function arUpdate(int $id, string $group = Pool::GROUP)
}

/**
* @param int $id
* @param int $id
* @param string $group
*/
public function arFindById(int $id, string $group = Pool::GROUP)
Expand All @@ -88,7 +87,7 @@ public function arFindById(int $id, string $group = Pool::GROUP)
}

/**
* @param int $id
* @param int $id
* @param string $group
*/
public function arFindByIdClass(int $id, string $group = Pool::GROUP)
Expand All @@ -99,7 +98,7 @@ public function arFindByIdClass(int $id, string $group = Pool::GROUP)
}

/**
* @param array $ids
* @param array $ids
* @param string $group
*/
public function arFindByIds(array $ids, string $group = Pool::GROUP)
Expand Down Expand Up @@ -131,7 +130,12 @@ public function arFindByIdsByClass(array $ids, string $group = Pool::GROUP)

public function arQuery(array $ids, string $group = Pool::GROUP)
{
$result = User::query($group)->select('*')->orderBy('id', QueryBuilder::ORDER_BY_DESC)->limit(2)->execute()->getResult();
$result = User::query($group)
->select('*')
->orderBy('id', QueryBuilder::ORDER_BY_DESC)
->limit(2)
->execute()
->getResult();
$this->assertCount(2, $result);
}

Expand All @@ -152,14 +156,14 @@ public function emSave(string $group = Pool::GROUP)
}

/**
* @param int $id
* @param int $id
* @param string $group
*/
public function emDelete(int $id, string $group = Pool::GROUP)
{

/* @var User $user */
$user = User::findById($id, $group)->getResult(User::class);
$user = User::findById($id, $group)->getResult(User::class);
$em = EntityManager::create($group);
$result = $em->delete($user)->getResult();
$em->close();
Expand All @@ -168,7 +172,7 @@ public function emDelete(int $id, string $group = Pool::GROUP)
}

/**
* @param int $id
* @param int $id
* @param string $group
*/
public function emDeleteById(int $id, string $group = Pool::GROUP)
Expand All @@ -181,7 +185,7 @@ public function emDeleteById(int $id, string $group = Pool::GROUP)
}

/**
* @param array $ids
* @param array $ids
* @param string $group
*/
public function emDeleteByIds(array $ids, string $group = Pool::GROUP)
Expand All @@ -194,7 +198,7 @@ public function emDeleteByIds(array $ids, string $group = Pool::GROUP)
}

/**
* @param int $id
* @param int $id
* @param string $group
*/
public function emUpdate(int $id, string $group = Pool::GROUP)
Expand Down Expand Up @@ -229,7 +233,7 @@ public function emFindById(int $id, string $group = Pool::GROUP)
}

/**
* @param array $ids
* @param array $ids
* @param string $group
*/
public function emFindByIds(array $ids, string $group = Pool::GROUP)
Expand All @@ -249,62 +253,75 @@ public function emFindByIds(array $ids, string $group = Pool::GROUP)
public function emQuery(array $ids, string $group = Pool::GROUP)
{
$em = EntityManager::create($group);
$result = $em->createQuery()->select('*')->from(User::class)->orderBy('id', QueryBuilder::ORDER_BY_DESC)->limit(2)->execute()->getResult();
$result = $em->createQuery()
->select('*')
->from(User::class)
->orderBy('id', QueryBuilder::ORDER_BY_DESC)
->limit(2)
->execute()
->getResult();
$em->close();

$this->assertCount(2, $result);
}

public function emSql(array $ids, string $group = Pool::GROUP){
public function emSql(array $ids, string $group = Pool::GROUP)
{
$em = EntityManager::create($group);
$result = $em->createQuery('select * from user where id in(?, ?) and name = ? order by id desc limit 2')
->setParameter(0, $ids[0])
->setParameter(1, $ids[1])
->setParameter(2, 'stelin')
->execute()->getResult();
->setParameter(0, $ids[0])
->setParameter(1, $ids[1])
->setParameter(2, 'stelin')
->execute()
->getResult();
$em->close();

$em = EntityManager::create($group);
$result2 = $em->createQuery('select * from user where id in(?, ?) and name = ? order by id desc limit 2')
->setParameter(0, $ids[0])
->setParameter(1, $ids[1])
->setParameter(2, 'stelin', Types::STRING)
->execute()->getResult();
->setParameter(0, $ids[0])
->setParameter(1, $ids[1])
->setParameter(2, 'stelin', Types::STRING)
->execute()
->getResult();
$em->close();

$em = EntityManager::create($group);
$result3 = $em->createQuery('select * from user where id in(?, ?) and name = ? order by id desc limit 2')
->setParameters([$ids[0], $ids[1], 'stelin'])
->execute()->getResult();
->setParameters([$ids[0], $ids[1], 'stelin'])
->execute()
->getResult();
$em->close();

$em = EntityManager::create($group);
$result4 = $em->createQuery('select * from user where id in(:id1, :id2) and name = :name order by id desc limit 2')
->setParameter(':id1', $ids[0])
->setParameter('id2', $ids[1])
->setParameter('name', 'stelin')
->execute()->getResult();
->setParameter(':id1', $ids[0])
->setParameter('id2', $ids[1])
->setParameter('name', 'stelin')
->execute()
->getResult();
$em->close();

$em = EntityManager::create($group);
$result5 = $em->createQuery('select * from user where id in(:id1, :id2) and name = :name order by id desc limit 2')
->setParameters([
'id1' => $ids[0],
':id2' => $ids[1],
'name' => 'stelin'
])
->execute()->getResult();
->setParameters([
'id1' => $ids[0],
':id2' => $ids[1],
'name' => 'stelin'
])
->execute()
->getResult();
$em->close();


$em = EntityManager::create($group);
$result6 = $em->createQuery('select * from user where id in(:id1, :id2) and name = :name order by id desc limit 2')
->setParameters([
['id1', $ids[0]],
[':id2', $ids[1], Types::INT],
['name', 'stelin', Types::STRING],
])
->execute()->getResult();
->setParameters([
['id1', $ids[0]],
[':id2', $ids[1], Types::INT],
['name', 'stelin', Types::STRING],
])
->execute()
->getResult();
$em->close();

$this->assertCount(2, $result);
Expand All @@ -322,7 +339,7 @@ public function addUsers(string $group = Pool::GROUP)
$user->setSex(1);
$user->setDesc('this my desc');
$user->setAge(mt_rand(1, 100));
$id = $user->save($group)->getResult();
$id = $user->save($group)->getResult();
$id2 = $user->save($group)->getResult();

return [
Expand Down
Loading

0 comments on commit edae181

Please sign in to comment.