public static function setUpBeforeClass() { $config = static::getTestingDatabaseConfig(); if (!is_null($config)) { $config['database'] = str_replace(array('\\', ' '), '_', trim(strtolower($config['database']))); $log = new Logger($config['database']); $log->pushHandler(new StreamHandler(TEMP_DIR . '/' . $config['database'] . '.log', Logger::DEBUG)); $db = new Database(static::getDefaultDatabaseConfig(), $log); $db->executeQuery("drop database if exists {$config['database']}"); $db->executeQuery("create database {$config['database']}"); static::$currentDatabase = new Database($config, $log); static::setUpSchema(); } }
/** * Loads columns for a given relation * @param Relation $relation */ protected function loadColumnsForRelation(Relation $relation) { $sql = new SelectStatement(); $sql->from('information_schema.columns')->where(sqlstr('table_schema')->equalsTo(':table_schema'))->andWhere(sqlstr('table_name')->equalsTo(':table_name')); $params = [':table_schema' => $relation->getSchemaName(), ':table_name' => $relation->getName()]; foreach ($this->database->executeQuery($sql, $params) as $record) { $column = new Column($record); $relation->addColumn($column); } }
/** * Gets many records from a relation * @param array $selectColumns columnt o select * @param array $byColumns condition * @param type $orderDirective * @param type $offsetLimitDirective * @return array */ protected function getManyBy($selectColumns, array $byColumns, $orderDirective = null, $offsetLimitDirective = null) { if (is_null($selectColumns)) { $selectColumns = [self::ALL_COLUMNS]; } else { if (!is_array($selectColumns)) { $selectColumns = [$selectColumns]; } } list($condition, $conditionParams) = $this->createAndCondition($byColumns); $sql = 'SELECT ' . implode(', ', $selectColumns) . ' FROM ' . $this->relation . ' WHERE ' . $condition . $this->createOrderDirective($orderDirective) . $this->createOffsetLimitDirective($offsetLimitDirective); $result = $this->database->executeQuery($sql, $conditionParams); return $this->recordsToModels($result); }
public function testExecuteQueryScalar() { $db = new Database(static::getDefaultDatabaseConfig()); $result = $db->executeScalar("select count(*) from pg_database where datname='postgres'"); $this->assertEquals(1, $result); }