protected function setUp() { parent::setUp(); DbClient::connect('backup'); $this->connection = DbClient::getConnection(); $this->statement = DbClient::prepare($this->sql); Config::set('hyperframework.db.operation_profiler.enable', true); Config::set('hyperframework.db.operation_profiler.enable_logger', false); $this->profiler = new DbOperationProfiler(); }
public function testNestedTransactionUsingDifferentConnections() { DbTransaction::run(function () { DbClient::connect('backup'); DbTransaction::run(function () { $this->assertTrue(DbClient::inTransaction()); }); $this->assertFalse(DbClient::inTransaction()); DbClient::connect(); $this->assertTrue(DbClient::inTransaction()); }); $this->assertFalse(DbClient::inTransaction()); }
/** * @param Closure $callback * @return mixed */ public static function run($callback) { $connection = DbClient::getConnection(); $index = array_search($connection, self::$connections, true); if ($index === false) { self::$connections[] = $connection; self::$counts[] = 0; end(self::$connections); $index = key(self::$connections); } $result = null; $count = self::$counts[$index]; ++self::$counts[$index]; try { if ($count === 0) { $connection->beginTransaction(); } $e = null; try { $result = $callback(); if ($count === 0) { $connection->commit(); } } catch (Exception $e) { } catch (Throwable $e) { } if ($e !== null) { if ($count === 0) { $connection->rollback(); } throw $e; } } finally { if ($count === 0) { unset(self::$counts[$index]); unset(self::$connections[$index]); } else { --self::$counts[$index]; } } return $result; }
public function testColumnNameOption() { DbImportCommand::execute('Document', [[1, 'doc 1', 12.34]], ['column_names' => ['id', 'name', 'decimal']]); $this->assertSame(1, DbClient::count('Document')); }
public function testGetConnection() { $statement = DbClient::prepare('SELECT * FROM Document'); $this->assertTrue($statement->getConnection() instanceof DbConnection); }
public function testDelete() { $doc = new Document(); $doc->setName('doc 1'); $doc->insert(); $doc->delete(); $this->assertSame(2, DbClient::count('Document')); }
protected function tearDown() { DbClient::delete('Document', null); parent::tearDown(); }
/** * @param string $table * @param array[] $rows * @param array $options * @return void */ public static function execute($table, $rows, $options = []) { $count = count($rows); if ($count === 0) { return; } $columnNames = null; if (isset($options['column_names'])) { $columnNames = $options['column_names']; if (is_array($columnNames) === false) { throw new InvalidArgumentException("The value of option 'column_names' must be an array, " . gettype($columnNames) . ' given.'); } } else { if (is_array($rows[0]) === false) { throw new InvalidArgumentException("Row must be an array, " . gettype($rows[0]) . " given at row 0."); } $columnNames = array_keys($rows[0]); } $columnCount = count($columnNames); if ($columnCount === 0) { return; } if (isset($options['batch_size'])) { $batchSize = (int) $options['batch_size']; if ($batchSize <= 0) { throw new InvalidArgumentException("The value of option 'batch_size' must be greater than 0, " . $batchSize . ' given.'); } } else { $batchSize = 1000; } foreach ($columnNames as &$columnName) { $columnName = DbClient::quoteIdentifier($columnName); } $prefix = 'INSERT INTO ' . DbClient::quoteIdentifier($table) . '(' . implode($columnNames, ', ') . ') VALUES'; $placeHolders = '(' . str_repeat('?, ', $columnCount - 1) . '?)'; $statement = null; $index = 0; while ($index < $count) { $values = []; $size = $batchSize; if ($index + $batchSize >= $count) { $size = $count - $index; } if ($statement === null || $size !== $batchSize) { $sql = $prefix . str_repeat($placeHolders . ',', $size - 1) . $placeHolders; $statement = DbClient::prepare($sql, [PDO::ATTR_EMULATE_PREPARES => false]); } while ($size > 0) { if (is_array($rows[$index]) === false) { throw new InvalidArgumentException("Row must be an array, " . gettype($rows[0]) . " given at row {$index}."); } if (count($rows[$index]) !== $columnCount) { throw new InvalidArgumentException("Number of columns is invalid at row {$index}," . " expected {$columnCount}, actual " . count($rows[$index]) . "."); } $values = array_merge($values, array_values($rows[$index])); ++$index; --$size; } $statement->execute($values); } }
private function mockEngineMethod($method) { $engine = $this->getMock('Hyperframework\\Db\\DbClientEngine'); DbClient::setEngine($engine); return $engine->expects($this->once())->method($method); }
/** * @param string $where * @return string */ private static function completeSelectSql($where) { $result = 'SELECT * FROM ' . DbClient::quoteIdentifier(static::getTableName()); $where = (string) $where; if ($where !== '') { $result .= ' WHERE ' . $where; } return $result; }
protected function setUp() { Config::set('hyperframework.app_root_path', dirname(__DIR__)); DbClient::execute(ConfigFileLoader::loadData('init.sql')); DbClient::setEngine(null); }