public function __invoke()
 {
     $function = $this->_object;
     $arguments = func_get_args();
     return Db::getInstance()->runInTransaction(function () use($function, $arguments) {
         return call_user_func_array($function, $arguments);
     });
 }
 public function tearDown()
 {
     Cache::clear();
     if (Db::getInstance()->isConnected()) {
         Db::getInstance()->enableTransactions();
         Db::getInstance()->rollbackTransaction();
     }
 }
Exemple #3
0
 public function __construct(Model $model, Db $db = null, $alias = null)
 {
     $this->_db = $db ? $db : Db::getInstance();
     $this->_model = $model;
     $this->_query = new Query();
     $this->_query->table = $model->getTableName();
     $this->_query->aliasTable = $alias;
     $this->_query->selectType = PDO::FETCH_NUM;
     $this->_query->selectColumns = array();
     $this->selectModelColumns($model, $this->getModelAliasOrTable());
 }
Exemple #4
0
 private function _getTableColumns($tableName)
 {
     $schema = Db::getInstance()->query("SELECT column_name, data_type, column_default FROM information_schema.columns WHERE table_name = '{$tableName}' ORDER BY ordinal_position")->fetchAll();
     $tableColumns = array();
     foreach ($schema as $columnInfo) {
         $columnName = $columnInfo['column_name'];
         $columnDefault = $columnInfo['column_default'];
         $columnType = $this->_postgresDataTypeToPhpType($columnInfo['data_type']);
         $tableColumns[$columnName] = new DatabaseColumn($columnName, $columnType, $columnDefault);
     }
     return $tableColumns;
 }
Exemple #5
0
 private function _getTableColumns($tableName)
 {
     $schema = Db::getInstance()->query("SHOW COLUMNS FROM {$tableName}")->fetchAll();
     $tableColumns = array();
     foreach ($schema as $columnInfo) {
         $columnName = $columnInfo['Field'];
         $columnDefault = $columnInfo['Default'];
         $columnType = $this->dataTypeToPhpType($columnInfo['Type']);
         $tableColumns[$columnName] = new DatabaseColumn($columnName, $columnType, $columnDefault);
     }
     return $tableColumns;
 }
 private function substituteParam()
 {
     $value = $this->_boundValues[$this->_param_index];
     $this->_param_index++;
     if ($value === null) {
         return "null";
     }
     if (is_bool($value)) {
         return Objects::booleanToString($value);
     }
     $value = Db::getInstance()->_dbHandle->quote($value);
     return $value;
 }
Exemple #7
0
 /**
  * @test
  */
 public function runInTransactionShouldInvokeRollbackOnFailure()
 {
     // given
     Db::getInstance()->enableTransactions();
     $dbHandle = Mock::mock();
     $db = new Db(false);
     $db->_dbHandle = $dbHandle;
     //when
     CatchException::when($db)->runInTransaction(array(new Sample(), 'exceptionMethod'));
     //then
     CatchException::assertThat()->isInstanceOf('InvalidArgumentException');
     Mock::verify($dbHandle)->beginTransaction();
     Mock::verify($dbHandle)->neverReceived()->commitTransaction();
     Mock::verify($dbHandle)->rollBack();
 }
Exemple #8
0
 public function execute()
 {
     if (empty($this->_models)) {
         return;
     }
     $this->_callBeforeSaveCallbacks();
     $metaInstance = Arrays::first($this->_models);
     $columns = $metaInstance->getFieldsWithoutPrimaryKey();
     $primaryKey = $metaInstance->getIdName();
     $table = $metaInstance->getTableName();
     $sql = DialectFactory::create()->batchInsert($table, $primaryKey, $columns, count($this->_models));
     $params = $this->_prepareParams($primaryKey);
     $ids = Arrays::flatten(Db::getInstance()->query($sql, $params)->fetchAll(PDO::FETCH_NUM));
     $this->_assignPrimaryKeys($primaryKey, $ids);
     $this->_callAfterSaveCallbacks();
 }
 /**
  * @test
  */
 public function shouldFetchIteratorWrappedWithBatching()
 {
     // given
     $numberOfItems = 100;
     $chunkSize = 20;
     for ($i = 0; $i < $numberOfItems; $i++) {
         Product::create(array('name' => sprintf('p%03d', $i)));
     }
     // when
     $iterator = new BatchingIterator(Db::getInstance()->query('SELECT * FROM products ORDER BY name ASC')->fetchIterator(), $chunkSize);
     // then
     $batches = 0;
     $items = 0;
     foreach ($iterator as $products) {
         foreach ($products as $product) {
             $this->assertEquals(sprintf('p%03d', $items), $product['name'], "Product name {$i} does not match");
             $items++;
         }
         $batches++;
     }
     $this->assertEquals($numberOfItems, $items);
     $this->assertEquals($numberOfItems, $chunkSize * $batches);
 }
Exemple #10
0
 /**
  * @test
  */
 public function shouldHandleSubQueries()
 {
     //given
     Product::create(array('name' => 'prod1', 'description' => 'd'));
     Product::create(array('name' => 'prod1', 'description' => 'd'));
     Product::create(array('name' => 'prod2', 'description' => 'd'));
     $query = Query::select(array('count(*) AS c'))->from(Query::select(array('name', 'count(*) c'))->from('products')->groupBy('name')->where(array('description' => 'd')), 'sub')->where(array('c' => 2));
     $executor = QueryExecutor::prepare(Db::getInstance(), $query);
     //when
     $result = $executor->fetch();
     //then
     $this->assertEquals(array('c' => 1), $result);
 }
Exemple #11
0
 public function getName($name)
 {
     return Db::callFunction('get_name', array($name));
 }
Exemple #12
0
 public function lastErrorMessage()
 {
     return $this->_db->lastErrorMessage();
 }
 public function method()
 {
     $startedTransaction = Db::getInstance()->_startedTransaction;
     $this->transactionStatus = $startedTransaction;
 }
Exemple #14
0
 /**
  * @param $class
  * @param $params
  * @return ModelDefinition
  */
 private static function _createDefinition($class, $params)
 {
     $table = Arrays::getValue($params, 'table') ?: self::defaultTable($class);
     $primaryKey = Arrays::getValue($params, 'primaryKey', 'id');
     $sequence = Arrays::getValue($params, 'sequence', "{$table}_{$primaryKey}_seq");
     list($fields, $defaults) = self::_extractFieldsAndDefaults($params['fields']);
     $relations = new Relations($class, $params, $primaryKey);
     $db = empty($params['db']) ? Db::getInstance() : $params['db'];
     if ($primaryKey && !in_array($primaryKey, $fields)) {
         $fields[] = $primaryKey;
     }
     $afterSaveCallbacks = Arrays::toArray(Arrays::getValue($params, 'afterSave'));
     $beforeSaveCallbacks = Arrays::toArray(Arrays::getValue($params, 'beforeSave'));
     return new ModelDefinition($db, $table, $sequence, $primaryKey, $fields, $relations, $afterSaveCallbacks, $beforeSaveCallbacks, $defaults);
 }
 /**
  * @test
  */
 public function shouldQuoteParams()
 {
     //given
     $sql = "select * from users where surname = ?";
     $param = "' or '1' = '1";
     //when
     $result = PreparedStatementEmulator::substitute($sql, array($param));
     //then
     Assert::thatString($result)->isEqualTo("select * from users where surname = " . Db::getInstance()->_dbHandle->quote($param));
 }