Ejemplo n.º 1
0
 /**
  * Reads a record from the database (from this mapper's table) by the
  * WHERE clause provided. Hidden records will be retrieved as well.
  *
  * @throws tx_oelib_Exception_NotFound if there is no record in the DB
  *                                     which matches the WHERE clause
  * @throws tx_oelib_Exception_NotFound if database access is disabled
  *
  * @param string[] $whereClauseParts
  *        WHERE clause parts for the record to retrieve, each element must consist of a column name as key and a value to
  *        search for as value (will automatically get quoted), must not be empty
  *
  * @return string[] the record from the database, will not be empty
  */
 protected function retrieveRecord(array $whereClauseParts)
 {
     if (!$this->hasDatabaseAccess()) {
         throw new tx_oelib_Exception_NotFound('No record can be retrieved from the database because database' . ' access is disabled for this mapper instance.');
     }
     $databaseConnection = Tx_Oelib_Db::getDatabaseConnection();
     $whereClauses = array($this->getUniversalWhereClause(TRUE));
     foreach ($whereClauseParts as $key => $value) {
         $columnDefinition = Tx_Oelib_Db::getColumnDefinition($this->getTableName(), $key);
         $whereClauses[] = $key . ' = ' . (strpos($columnDefinition['Type'], 'int') !== FALSE ? $value : $databaseConnection->fullQuoteStr($value, $this->getTableName()));
     }
     $whereClause = implode(' AND ', $whereClauses);
     try {
         $data = Tx_Oelib_Db::selectSingle($this->columns, $this->getTableName(), $whereClause);
     } catch (tx_oelib_Exception_EmptyQueryResult $exception) {
         throw new tx_oelib_Exception_NotFound('The record where "' . $whereClause . '" could not be retrieved from the table ' . $this->getTableName() . '.');
     }
     return $data;
 }
Ejemplo n.º 2
0
 /**
  * @test
  */
 public function messageAfterQueryWithLastQueryEnabledContainsLastQuery()
 {
     Tx_Oelib_Db::getDatabaseConnection()->exec_SELECTquery('title', 'tx_oelib_test', '');
     $subject = new tx_oelib_Exception_EmptyQueryResult();
     self::assertContains('SELECT', $subject->getMessage());
 }
Ejemplo n.º 3
0
 /**
  * Frees as much memory that has been used by this object as possible.
  */
 public function __destruct()
 {
     $databaseConnection = Tx_Oelib_Db::getDatabaseConnection();
     if ($this->dbResult !== FALSE && $databaseConnection !== NULL) {
         $databaseConnection->sql_free_result($this->dbResult);
         $this->dbResult = FALSE;
     }
     $this->currentItem = NULL;
 }
Ejemplo n.º 4
0
 /**
  * @test
  */
 public function getDatabaseConnectionReturnsGlobalsDatabaseConnection()
 {
     self::assertSame($GLOBALS['TYPO3_DB'], Tx_Oelib_Db::getDatabaseConnection());
 }
Ejemplo n.º 5
0
 /**
  * @test
  */
 public function getAutoIncrementReturnsOneForTruncatedTable()
 {
     Tx_Oelib_Db::enableQueryLogging();
     $dbResult = Tx_Oelib_Db::getDatabaseConnection()->sql_query('TRUNCATE TABLE tx_oelib_test;');
     if ($dbResult === FALSE) {
         throw new tx_oelib_Exception_Database(1418586819);
     }
     self::assertSame(1, $this->subject->getAutoIncrement('tx_oelib_test'));
 }
Ejemplo n.º 6
0
 /**
  * Updates an integer field of a database table by one. This is mainly needed
  * for counting up the relation counter when creating a database relation.
  *
  * The field to update must be of type integer.
  *
  * @param string $tableName name of the table, must not be empty
  * @param int $uid the UID of the record to modify, must be > 0
  * @param string $fieldName the field name of the field to modify, must not be empty
  *
  * @return void
  *
  * @throws tx_oelib_Exception_Database
  * @throws InvalidArgumentException
  * @throws BadMethodCallException
  */
 public function increaseRelationCounter($tableName, $uid, $fieldName)
 {
     if (!$this->isTableNameAllowed($tableName)) {
         throw new InvalidArgumentException('The table name "' . $tableName . '" is invalid. This means it is either empty or not in the list of allowed tables.', 1331490960);
     }
     if (!Tx_Oelib_Db::tableHasColumn($tableName, $fieldName)) {
         throw new InvalidArgumentException('The table ' . $tableName . ' has no column ' . $fieldName . '.', 1331490986);
     }
     Tx_Oelib_Db::enableQueryLogging();
     $databaseConnection = Tx_Oelib_Db::getDatabaseConnection();
     $dbResult = $databaseConnection->sql_query('UPDATE ' . $tableName . ' SET ' . $fieldName . '=' . $fieldName . '+1 WHERE uid=' . $uid);
     if ($dbResult === FALSE) {
         throw new tx_oelib_Exception_Database(1418586263);
     }
     if ($databaseConnection->sql_affected_rows() === 0) {
         throw new BadMethodCallException('The table ' . $tableName . ' does not contain a record with UID ' . $uid . '.', 1331491003);
     }
     $this->markTableAsDirty($tableName);
 }