| @@ -236,7 +236,7 @@ | ||
| 236 | 236 | * Create configuration file. | 
| 237 | 237 | * | 
| 238 | 238 | * @param mixed[] $config | 
| 239 | - * @return mixed[] | |
| 239 | + * @return string | |
| 240 | 240 | */ | 
| 241 | 241 | protected function createConfig($config = []) | 
| 242 | 242 |      { | 
| @@ -8,7 +8,7 @@ discard block | ||
| 8 | 8 | use Dazzle\PgSQL\Transaction\Transaction; | 
| 9 | 9 | use Dazzle\Promise\Promise; | 
| 10 | 10 | use Dazzle\Throwable\Exception; | 
| 11 | -use Dazzle\PgSQL\Transaction\TransactionInterface ; | |
| 11 | +use Dazzle\PgSQL\Transaction\TransactionInterface; | |
| 12 | 12 | use Dazzle\Throwable\Exception\Runtime\ExecutionException; | 
| 13 | 13 | |
| 14 | 14 | class Database extends BaseEventEmitter implements DatabaseInterface | 
| @@ -18,42 +18,42 @@ discard block | ||
| 18 | 18 | /** | 
| 19 | 19 | * @var int | 
| 20 | 20 | */ | 
| 21 | - const STATE_INIT = 0; | |
| 21 | + const STATE_INIT = 0; | |
| 22 | 22 | |
| 23 | 23 | /** | 
| 24 | 24 | * @var int | 
| 25 | 25 | */ | 
| 26 | - const STATE_CONNECT_PENDING = 4; | |
| 26 | + const STATE_CONNECT_PENDING = 4; | |
| 27 | 27 | |
| 28 | 28 | /** | 
| 29 | 29 | * @var int | 
| 30 | 30 | */ | 
| 31 | - const STATE_CONNECT_FAILED = 2; | |
| 31 | + const STATE_CONNECT_FAILED = 2; | |
| 32 | 32 | |
| 33 | 33 | /** | 
| 34 | 34 | * @var int | 
| 35 | 35 | */ | 
| 36 | - const STATE_CONNECT_SUCCEEDED = 6; | |
| 36 | + const STATE_CONNECT_SUCCEEDED = 6; | |
| 37 | 37 | |
| 38 | 38 | /** | 
| 39 | 39 | * @var int | 
| 40 | 40 | */ | 
| 41 | - const STATE_AUTH_PENDING = 5; | |
| 41 | + const STATE_AUTH_PENDING = 5; | |
| 42 | 42 | |
| 43 | 43 | /** | 
| 44 | 44 | * @var int | 
| 45 | 45 | */ | 
| 46 | - const STATE_AUTH_FAILED = 3; | |
| 46 | + const STATE_AUTH_FAILED = 3; | |
| 47 | 47 | |
| 48 | 48 | /** | 
| 49 | 49 | * @var int | 
| 50 | 50 | */ | 
| 51 | - const STATE_AUTH_SUCCEEDED = 7; | |
| 51 | + const STATE_AUTH_SUCCEEDED = 7; | |
| 52 | 52 | |
| 53 | 53 | /** | 
| 54 | 54 | * @var int | 
| 55 | 55 | */ | 
| 56 | - const STATE_DISCONNECT_PENDING = 8; | |
| 56 | + const STATE_DISCONNECT_PENDING = 8; | |
| 57 | 57 | |
| 58 | 58 | /** | 
| 59 | 59 | * @var int | 
| @@ -203,7 +203,7 @@ discard block | ||
| 203 | 203 | return Promise::doResolve($trans); | 
| 204 | 204 | } | 
| 205 | 205 | //bad method: append 1 callback | 
| 206 | -        return $this->start()->success(function ($_) { | |
| 206 | +        return $this->start()->success(function($_) { | |
| 207 | 207 | $trans = new Transaction($this->connector, $this); | 
| 208 | 208 | |
| 209 | 209 | return $trans->begin(); | 
| @@ -136,7 +136,8 @@ discard block | ||
| 136 | 136 | $this->connector = new Connector($this->config); | 
| 137 | 137 | $this->state = self::STATE_CONNECT_PENDING; | 
| 138 | 138 | $promise = $this->connector->getConnection(); | 
| 139 | -        if (!($sock = $this->connector->getSock())) { | |
| 139 | + if (!($sock = $this->connector->getSock())) | |
| 140 | +        { | |
| 140 | 141 |              return $promise->reject(new Exception('No server connection is currently open')); | 
| 141 | 142 | } | 
| 142 | 143 | $this->loop->addReadStream($sock, [$this->connector, 'connect']); | 
| @@ -197,7 +198,8 @@ discard block | ||
| 197 | 198 | */ | 
| 198 | 199 | public function beginTransaction() | 
| 199 | 200 |      { | 
| 200 | -        if ($this->isStarted()) { | |
| 201 | + if ($this->isStarted()) | |
| 202 | +        { | |
| 201 | 203 | $trans = new Transaction($this->connector, $this); | 
| 202 | 204 | |
| 203 | 205 | return Promise::doResolve($trans); | 
| @@ -247,8 +249,10 @@ discard block | ||
| 247 | 249 | 'dbname' => '', | 
| 248 | 250 | ]; | 
| 249 | 251 | $config = array_merge($default, $config); | 
| 250 | -        foreach ($config as $key => &$value) { | |
| 251 | -            if (!$value) { | |
| 252 | + foreach ($config as $key => &$value) | |
| 253 | +        { | |
| 254 | + if (!$value) | |
| 255 | +            { | |
| 252 | 256 | unset($config[$key]); | 
| 253 | 257 | continue; | 
| 254 | 258 | } | 
| @@ -1,5 +1,5 @@ discard block | ||
| 1 | 1 | <?php | 
| 2 | -require_once __DIR__.'/../vendor/autoload.php'; | |
| 2 | +require_once __DIR__ . '/../vendor/autoload.php'; | |
| 3 | 3 | |
| 4 | 4 | use Dazzle\Loop\Loop; | 
| 5 | 5 | use Dazzle\Loop\Model\SelectLoop; | 
| @@ -18,12 +18,12 @@ discard block | ||
| 18 | 18 | 'dbname' => 'postgres' | 
| 19 | 19 | ]); | 
| 20 | 20 | |
| 21 | -$db->start()->then(function (Connection $conn) use ($loop) { | |
| 22 | -    $conn->query('select \'ok\'')->then(function (Tuple $ret) use ($loop) { | |
| 21 | +$db->start()->then(function(Connection $conn) use ($loop) { | |
| 22 | +    $conn->query('select \'ok\'')->then(function(Tuple $ret) use ($loop) { | |
| 23 | 23 | print_r($ret->fetchRow()); | 
| 24 | 24 | }); | 
| 25 | -    $conn->prepare('select \'ok\'')->then(function (Prepared $prepared) use ($loop) { | |
| 26 | -        $prepared->execPreparedStmt()->then(function (TupleResultStatement $ret) use ($loop) { | |
| 25 | +    $conn->prepare('select \'ok\'')->then(function(Prepared $prepared) use ($loop) { | |
| 26 | +        $prepared->execPreparedStmt()->then(function(TupleResultStatement $ret) use ($loop) { | |
| 27 | 27 | print_r($ret->fetchRow()); | 
| 28 | 28 | $loop->stop(); | 
| 29 | 29 | }); | 
| @@ -34,7 +34,7 @@ | ||
| 34 | 34 | }); | 
| 35 | 35 | |
| 36 | 36 | $db->beginTransaction() | 
| 37 | - ->then(function (TransactionInterface $trans) use ($loop) { | |
| 37 | +    ->then(function (TransactionInterface $trans) use ($loop) { | |
| 38 | 38 |      $trans->query('select * from demo order by id desc limit 1') | 
| 39 | 39 |      ->then(function (TupleResultStatement $tuple) { | 
| 40 | 40 | print_r($tuple->fetchAll()); | 
| @@ -1,5 +1,5 @@ discard block | ||
| 1 | 1 | <?php | 
| 2 | -require_once __DIR__.'/../vendor/autoload.php'; | |
| 2 | +require_once __DIR__ . '/../vendor/autoload.php'; | |
| 3 | 3 | |
| 4 | 4 | use Dazzle\Loop\Loop; | 
| 5 | 5 | use Dazzle\PgSQL\Database; | 
| @@ -18,29 +18,29 @@ discard block | ||
| 18 | 18 | 'dbname' => 'postgres' | 
| 19 | 19 | ]); | 
| 20 | 20 | |
| 21 | -$db->on('transaction:begin', function () use ($loop, $db) { | |
| 22 | - echo 'Transaction Stated'.PHP_EOL; | |
| 21 | +$db->on('transaction:begin', function() use ($loop, $db) { | |
| 22 | + echo 'Transaction Stated' . PHP_EOL; | |
| 23 | 23 | }); | 
| 24 | 24 | |
| 25 | -$db->on('transaction:end', function () use ($loop, $db) { | |
| 26 | -    $db->getConnection()->then(function (ConnectionInterface $conn) use ($loop) { | |
| 25 | +$db->on('transaction:end', function() use ($loop, $db) { | |
| 26 | +    $db->getConnection()->then(function(ConnectionInterface $conn) use ($loop) { | |
| 27 | 27 |          $conn->query('select * from demo order by id desc limit 1') | 
| 28 | -        ->then(function (TupleResultStatement $tuple) use ($loop) { | |
| 28 | +        ->then(function(TupleResultStatement $tuple) use ($loop) { | |
| 29 | 29 | print_r($tuple->fetchAll()); | 
| 30 | 30 | $loop->stop(); | 
| 31 | - echo 'Transaction End'.PHP_EOL; | |
| 31 | + echo 'Transaction End' . PHP_EOL; | |
| 32 | 32 | }); | 
| 33 | 33 | }); | 
| 34 | 34 | }); | 
| 35 | 35 | |
| 36 | 36 | $db->beginTransaction() | 
| 37 | - ->then(function (TransactionInterface $trans) use ($loop) { | |
| 37 | + ->then(function(TransactionInterface $trans) use ($loop) { | |
| 38 | 38 |      $trans->query('select * from demo order by id desc limit 1') | 
| 39 | -    ->then(function (TupleResultStatement $tuple) { | |
| 39 | +    ->then(function(TupleResultStatement $tuple) { | |
| 40 | 40 | print_r($tuple->fetchAll()); | 
| 41 | 41 | }); | 
| 42 | 42 |      $trans->execute('insert into demo default values') | 
| 43 | -    ->then(function (CommandResult $result) use ($trans) { | |
| 43 | +    ->then(function(CommandResult $result) use ($trans) { | |
| 44 | 44 |          if ($result->getAffectedRows() > 0) { | 
| 45 | 45 | $trans->rollback(); | 
| 46 | 46 | } | 
| @@ -41,7 +41,8 @@ | ||
| 41 | 41 | }); | 
| 42 | 42 |      $trans->execute('insert into demo default values') | 
| 43 | 43 |      ->then(function (CommandResult $result) use ($trans) { | 
| 44 | -        if ($result->getAffectedRows() > 0) { | |
| 44 | + if ($result->getAffectedRows() > 0) | |
| 45 | +        { | |
| 45 | 46 | $trans->rollback(); | 
| 46 | 47 | } | 
| 47 | 48 | }); | 
| @@ -45,11 +45,11 @@ | ||
| 45 | 45 | $promise = $this->conn->getPromise(); | 
| 46 | 46 | |
| 47 | 47 | return $promise | 
| 48 | -               ->success(function (ConnectorInterface $connector) use ($connection) { | |
| 49 | - $connection->setConnector($connector); | |
| 48 | +                ->success(function (ConnectorInterface $connector) use ($connection) { | |
| 49 | + $connection->setConnector($connector); | |
| 50 | 50 | |
| 51 | - return $connection; | |
| 52 | - }); | |
| 51 | + return $connection; | |
| 52 | + }); | |
| 53 | 53 | } | 
| 54 | 54 | |
| 55 | 55 | public function connect() | 
| @@ -24,7 +24,7 @@ discard block | ||
| 24 | 24 | |
| 25 | 25 | public function __construct($config) | 
| 26 | 26 |      { | 
| 27 | - $this->stream = pg_connect($config, \PGSQL_CONNECT_ASYNC|\PGSQL_CONNECT_FORCE_NEW); | |
| 27 | + $this->stream = pg_connect($config, \PGSQL_CONNECT_ASYNC | \PGSQL_CONNECT_FORCE_NEW); | |
| 28 | 28 | $this->conn = new Connection(); | 
| 29 | 29 | } | 
| 30 | 30 | |
| @@ -45,7 +45,7 @@ discard block | ||
| 45 | 45 | $promise = $this->conn->getPromise(); | 
| 46 | 46 | |
| 47 | 47 | return $promise | 
| 48 | -               ->success(function (ConnectorInterface $connector) use ($connection) { | |
| 48 | +               ->success(function(ConnectorInterface $connector) use ($connection) { | |
| 49 | 49 | $connection->setConnector($connector); | 
| 50 | 50 | |
| 51 | 51 | return $connection; | 
| @@ -54,32 +54,40 @@ discard block | ||
| 54 | 54 | |
| 55 | 55 | public function connect() | 
| 56 | 56 |      { | 
| 57 | -        switch ($polled = $this->poll()) { | |
| 57 | + switch ($polled = $this->poll()) | |
| 58 | +        { | |
| 58 | 59 | case \PGSQL_POLLING_FAILED: | 
| 59 | 60 | return; | 
| 60 | 61 | case \PGSQL_POLLING_OK: | 
| 61 | -                if ($this->isConnected() != true) { | |
| 62 | + if ($this->isConnected() != true) | |
| 63 | +                { | |
| 62 | 64 | $this->connected = true; | 
| 63 | 65 | $this->conn->resolve($this); | 
| 64 | 66 | |
| 65 | 67 | return; | 
| 66 | 68 | } | 
| 67 | 69 | $retRsrc = \pg_get_result($this->stream); | 
| 68 | -                if ($retRsrc) { | |
| 69 | -                    if (empty($this->retQueue)) { | |
| 70 | + if ($retRsrc) | |
| 71 | +                { | |
| 72 | + if (empty($this->retQueue)) | |
| 73 | +                    { | |
| 70 | 74 | |
| 71 | 75 | return; | 
| 72 | 76 | } | 
| 73 | 77 | $ret = array_shift($this->retQueue); | 
| 74 | 78 | $ret->resolve($ret->handle($retRsrc)); | 
| 75 | -                } else { | |
| 76 | -                    if (empty($this->queryQueue)) { | |
| 79 | + } | |
| 80 | + else | |
| 81 | +                { | |
| 82 | + if (empty($this->queryQueue)) | |
| 83 | +                    { | |
| 77 | 84 | |
| 78 | 85 | return; | 
| 79 | 86 | } | 
| 80 | 87 | $query = array_shift($this->queryQueue); | 
| 81 | 88 | $ok = $query->execute($this); | 
| 82 | -                    if (!$ok) { | |
| 89 | + if (!$ok) | |
| 90 | +                    { | |
| 83 | 91 |                          $query->reject(new \Exception('failed')); | 
| 84 | 92 | |
| 85 | 93 | return; | 
| @@ -92,7 +100,7 @@ discard block | ||
| 92 | 100 | } | 
| 93 | 101 | |
| 94 | 102 | public function prepare($sql) | 
| 95 | -    {         | |
| 103 | +    { | |
| 96 | 104 | $prepare = new PrepareQuery($sql); | 
| 97 | 105 | $this->appendQuery($prepare); | 
| 98 | 106 | $promise = $prepare->getPromise(); | 
| @@ -78,13 +78,13 @@ discard block | ||
| 78 | 78 | */ | 
| 79 | 79 | public function commit() | 
| 80 | 80 |      { | 
| 81 | -       return $this->connector->execute('COMMIT')->success(function (CommandResultStatement $result) { | |
| 82 | -           if ($result) { | |
| 83 | -               $this->emitter->emit('transaction:end'); | |
| 84 | - } | |
| 81 | +        return $this->connector->execute('COMMIT')->success(function (CommandResultStatement $result) { | |
| 82 | +            if ($result) { | |
| 83 | +                $this->emitter->emit('transaction:end'); | |
| 84 | + } | |
| 85 | 85 | |
| 86 | - return $result; | |
| 87 | - }); | |
| 86 | + return $result; | |
| 87 | + }); | |
| 88 | 88 | } | 
| 89 | 89 | |
| 90 | 90 | /** | 
| @@ -93,12 +93,12 @@ discard block | ||
| 93 | 93 | */ | 
| 94 | 94 | public function rollback() | 
| 95 | 95 |      { | 
| 96 | -       return $this->connector->execute('ROLLBACK')->success(function (CommandResultStatement $result) { | |
| 97 | -           if ($result) { | |
| 98 | -               $this->emitter->emit('transaction:end');     | |
| 99 | - } | |
| 96 | +        return $this->connector->execute('ROLLBACK')->success(function (CommandResultStatement $result) { | |
| 97 | +            if ($result) { | |
| 98 | +                $this->emitter->emit('transaction:end');     | |
| 99 | + } | |
| 100 | 100 | |
| 101 | - return $result; | |
| 102 | - }); | |
| 101 | + return $result; | |
| 102 | + }); | |
| 103 | 103 | } | 
| 104 | 104 | } | 
| @@ -63,7 +63,7 @@ discard block | ||
| 63 | 63 | return Promise::doResolve($this); | 
| 64 | 64 | } | 
| 65 | 65 | |
| 66 | -        return $this->connector->execute('BEGIN')->success(function (CommandResultStatement $result) { | |
| 66 | +        return $this->connector->execute('BEGIN')->success(function(CommandResultStatement $result) { | |
| 67 | 67 |              if ($result) { | 
| 68 | 68 |                  $this->emitter->emit('transaction:begin'); | 
| 69 | 69 | } | 
| @@ -78,7 +78,7 @@ discard block | ||
| 78 | 78 | */ | 
| 79 | 79 | public function commit() | 
| 80 | 80 |      { | 
| 81 | -       return $this->connector->execute('COMMIT')->success(function (CommandResultStatement $result) { | |
| 81 | +       return $this->connector->execute('COMMIT')->success(function(CommandResultStatement $result) { | |
| 82 | 82 |             if ($result) { | 
| 83 | 83 |                 $this->emitter->emit('transaction:end'); | 
| 84 | 84 | } | 
| @@ -93,7 +93,7 @@ discard block | ||
| 93 | 93 | */ | 
| 94 | 94 | public function rollback() | 
| 95 | 95 |      { | 
| 96 | -       return $this->connector->execute('ROLLBACK')->success(function (CommandResultStatement $result) { | |
| 96 | +       return $this->connector->execute('ROLLBACK')->success(function(CommandResultStatement $result) { | |
| 97 | 97 |             if ($result) { | 
| 98 | 98 |                 $this->emitter->emit('transaction:end');     | 
| 99 | 99 | } | 
| @@ -59,12 +59,14 @@ discard block | ||
| 59 | 59 | |
| 60 | 60 | public function begin() | 
| 61 | 61 |      { | 
| 62 | -        if ($this->isOpen()) { | |
| 62 | + if ($this->isOpen()) | |
| 63 | +        { | |
| 63 | 64 | return Promise::doResolve($this); | 
| 64 | 65 | } | 
| 65 | 66 | |
| 66 | 67 |          return $this->connector->execute('BEGIN')->success(function (CommandResultStatement $result) { | 
| 67 | -            if ($result) { | |
| 68 | + if ($result) | |
| 69 | +            { | |
| 68 | 70 |                  $this->emitter->emit('transaction:begin'); | 
| 69 | 71 | } | 
| 70 | 72 | |
| @@ -79,7 +81,8 @@ discard block | ||
| 79 | 81 | public function commit() | 
| 80 | 82 |      { | 
| 81 | 83 |         return $this->connector->execute('COMMIT')->success(function (CommandResultStatement $result) { | 
| 82 | -           if ($result) { | |
| 84 | + if ($result) | |
| 85 | +           { | |
| 83 | 86 |                 $this->emitter->emit('transaction:end'); | 
| 84 | 87 | } | 
| 85 | 88 | |
| @@ -94,7 +97,8 @@ discard block | ||
| 94 | 97 | public function rollback() | 
| 95 | 98 |      { | 
| 96 | 99 |         return $this->connector->execute('ROLLBACK')->success(function (CommandResultStatement $result) { | 
| 97 | -           if ($result) { | |
| 100 | + if ($result) | |
| 101 | +           { | |
| 98 | 102 |                 $this->emitter->emit('transaction:end');     | 
| 99 | 103 | } | 
| 100 | 104 | |
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php | 
| 2 | 2 | namespace Dazzle\PgSQL\Result; | 
| 3 | 3 | |
| 4 | -interface Result extends CommandResultStatement,TupleResultStatement | |
| 4 | +interface Result extends CommandResultStatement, TupleResultStatement | |
| 5 | 5 |  { | 
| 6 | 6 | |
| 7 | 7 | } | 
| 8 | 8 | \ No newline at end of file | 
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | |
| 4 | 4 | use Dazzle\PgSQL\Connection\ConnectorInterface; | 
| 5 | 5 | |
| 6 | -interface StatementHandler | |
| 6 | +interface StatementHandler | |
| 7 | 7 |  { | 
| 8 | 8 | public function execute(ConnectorInterface $connector); | 
| 9 | 9 | public function handle($result); | 
| @@ -9,7 +9,8 @@ | ||
| 9 | 9 | public function handle($result) | 
| 10 | 10 |      { | 
| 11 | 11 | $stat = pg_result_status($result, \PGSQL_STATUS_LONG); | 
| 12 | -        switch ($stat) { | |
| 12 | + switch ($stat) | |
| 13 | +        { | |
| 13 | 14 | case PGSQL_EMPTY_QUERY: | 
| 14 | 15 | break; | 
| 15 | 16 | case PGSQL_COMMAND_OK: | 
| @@ -16,7 +16,7 @@ | ||
| 16 | 16 | |
| 17 | 17 | public function getParams() | 
| 18 | 18 |      { | 
| 19 | - return $this->params?:[]; | |
| 19 | + return $this->params ?: []; | |
| 20 | 20 | } | 
| 21 | 21 | |
| 22 | 22 | public function setSQL($sql) |