Skip to content

Commit

Permalink
Merge pull request #728 from swoftcloud/master
Browse files Browse the repository at this point in the history
Add dmeo
  • Loading branch information
stelin committed Jun 21, 2019
2 parents 8c7161d + 7ff0f5d commit 70574c7
Show file tree
Hide file tree
Showing 11 changed files with 434 additions and 21 deletions.
4 changes: 4 additions & 0 deletions app/Common/DbSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function select(Connection $connection): void
$selectIndex = '';
}

if($createDbName == 'test2'){
$createDbName = 'test';
}

$dbName = sprintf('%s%s', $createDbName, (string)$selectIndex);
$connection->db($dbName);
}
Expand Down
4 changes: 3 additions & 1 deletion app/Console/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public function ab()
}

foreach ($exeUris as $uri) {
$abShell = sprintf('ab -k -n 10000 -k -c 2000 127.0.0.1:18306%s', $uri);
$curlResult = null;
$abShell = sprintf('ab -k -n 10000 -c 2000 127.0.0.1:18306%s', $uri);
$curlShell = sprintf('curl 127.0.0.1:18306%s', $uri);

exec($curlShell, $curlResult);
Expand Down Expand Up @@ -71,6 +72,7 @@ private function uris(): array
'/dbTransaction/ts2',
'/dbTransaction/cm2',
'/dbTransaction/rl2',
'/dbTransaction/multiPool',
'/dbModel/find',
'/dbModel/update',
'/dbModel/delete',
Expand Down
9 changes: 6 additions & 3 deletions app/Http/Controller/DbModelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use App\Model\Entity\User;
use Exception;
use Swoft\Http\Message\Response;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Throwable;
Expand All @@ -21,16 +22,18 @@ class DbModelController
/**
* @RequestMapping(route="find")
*
* @return array
* @param Response $response
*
* @return Response
*
* @throws Throwable
*/
public function find(): array
public function find(Response $response): Response
{
$id = $this->getId();
$user = User::find($id);

return $user->toArray();
return $response->withData($user);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions app/Http/Controller/DbTransactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace App\Http\Controller;

use App\Model\Entity\Count;
use App\Model\Entity\User;
use App\Model\Entity\User3;
use Swoft\Db\DB;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
Expand Down Expand Up @@ -153,6 +155,42 @@ public function rl2()
return json_encode($user->toArray());
}

/**
* @RequestMapping()
*/
public function multiPool()
{
DB::beginTransaction();

// db3.pool
$user = new User3();
$user->setAge(mt_rand(1, 100));
$user->setUserDesc('desc');

$user->save();
$uid3 = $user->getId();


//db.pool
$uid = $this->getId();

$count = new Count();
$count->setUserId(mt_rand(1, 100));
$count->setAttributes('attr');
$count->setCreateTime(time());

$count->save();
$cid = $count->getId();

DB::rollBack();

$u3 = User3::find($uid3)->toArray();
$u = User::find($uid);
$c = Count::find($cid);

return [$u3, $u, $c];
}

/**
* @return int
* @throws Throwable
Expand Down
44 changes: 39 additions & 5 deletions app/Http/Controller/SelectDbController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

namespace App\Http\Controller;

use App\Model\Entity\Count;
use App\Model\Entity\Count2;
use App\Model\Entity\Desc;
use App\Model\Entity\User;
use Exception;
use ReflectionException;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Db\DB;
use Swoft\Db\Exception\DbException;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Throwable;
Expand Down Expand Up @@ -61,15 +65,18 @@ public function modelDb(): array
$this->insertId2();
$result = User::db('test2')->count('id');

$desc = $this->desc();
sgo(function () {
$id = $this->getId();
User::find($id)->toArray();

$this->insertId2();
User::db('test2')->count('id');

$this->desc();
});

return [$user, $result];
return [$user, $result, $desc];
}

/**
Expand Down Expand Up @@ -115,6 +122,7 @@ public function queryDb(): array

$count = DB::table('user')->db('test2')->count();

$desc = $this->desc();
sgo(function () {
$id = $this->getId();

Expand All @@ -123,9 +131,11 @@ public function queryDb(): array
$this->insertId2();

DB::table('user')->db('test2')->count();

$this->desc();
});

return [$user, $count];
return [$user, $count, $desc];
}

/**
Expand Down Expand Up @@ -166,14 +176,18 @@ public function dbDb(): array

$result = DB::db('test2')->selectOne('select * from user limit 1');

$desc = $this->desc();

sgo(function () {
$id = $this->getId();
User::find($id)->toArray();

DB::db('test2')->selectOne('select * from user limit 1');

$this->desc();
});

return [$user, $result];
return [$user, $result, $desc];
}

/**
Expand All @@ -184,7 +198,7 @@ public function dbDb(): array
*/
public function select(): array
{
$count = new Count();
$count = new Count2();
$count->setUserId(mt_rand(1, 100));
$count->setAttributes('attr');
$count->setCreateTime(time());
Expand All @@ -194,6 +208,12 @@ public function select(): array
return [$result, $count->getId()];
}

/**
* @return bool
* @throws ContainerException
* @throws DbException
* @throws ReflectionException
*/
public function insertId2(): bool
{
$result = User::db('test2')->insert([
Expand All @@ -209,6 +229,20 @@ public function insertId2(): bool
return $result;
}

/**
* @throws ReflectionException
* @throws ContainerException
* @throws DbException
*/
public function desc(): array
{
$desc = new Desc();
$desc->setDesc("desc");
$desc->save();

return Desc::find($desc->getId())->toArray();
}

/**
* @return int
* @throws Throwable
Expand Down
2 changes: 1 addition & 1 deletion app/Model/Entity/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @since 2.0
*
* @Entity(table="count", pool="db2.pool")
* @Entity(table="count")
*/
class Count extends Model
{
Expand Down
114 changes: 114 additions & 0 deletions app/Model/Entity/Count2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php declare(strict_types=1);


namespace App\Model\Entity;


use Swoft\Db\Annotation\Mapping\Column;
use Swoft\Db\Annotation\Mapping\Entity;
use Swoft\Db\Annotation\Mapping\Id;
use Swoft\Db\Eloquent\Model;

/**
* Class Count
*
* @since 2.0
*
* @Entity(table="count", pool="db2.pool")
*/
class Count2 extends Model
{
/**
* @Id(incrementing=true)
*
* @Column(name="id", prop="id")
* @var int|null
*/
private $id;

/**
* @Column(name="user_id", prop="userId")
* @var int|null
*/
private $userId;

/**
* @Column(name="create_time", prop="createTime")
*
* @var int|null
*/
private $createTime;

/**
* attributes
*
* @Column()
*
* @var string|null
*/
private $attributes;

/**
* @return null|int
*/
public function getId(): ?int
{
return $this->id;
}

/**
* @param null|int $id
*/
public function setId(?int $id): void
{
$this->id = $id;
}

/**
* @return null|int
*/
public function getUserId(): ?int
{
return $this->userId;
}

/**
* @param null|int $userId
*/
public function setUserId(?int $userId): void
{
$this->userId = $userId;
}

/**
* @return null|int
*/
public function getCreateTime(): ?int
{
return $this->createTime;
}

/**
* @param null|int $createTime
*/
public function setCreateTime(?int $createTime): void
{
$this->createTime = $createTime;
}

/**
* @return null|string
*/
public function getAttributes(): ?string
{
return $this->attributes;
}

/**
* @param null|string $attributes
*/
public function setAttributes(?string $attributes): void
{
$this->attributes = $attributes;
}
}

0 comments on commit 70574c7

Please sign in to comment.