* Selects the the specified fields of the records matching the provided
* conditions and returns them as DBDataObject. Field names get prefixed.
*
+ * @see DatabaseBase::select()
+ *
* @since 1.20
*
* @param array|string|null $fields
* @param array $options
* @param string|null $functionName
*
- * @return ORMResult
+ * @return ORMResult The result set
+ * @throw DBQueryError if the query failed (even if the database was in ignoreErrors mode)
*/
public function select( $fields = null, array $conditions = array(),
array $options = array(), $functionName = null );
* @param null|string $functionName
*
* @return ResultWrapper
+ * @throw DBQueryError if the query failed (even if the database was in ignoreErrors mode)
*/
public function rawSelect( $fields = null, array $conditions = array(),
array $options = array(), $functionName = null );
*/
public function select( $fields = null, array $conditions = array(),
array $options = array(), $functionName = null ) {
- return new ORMResult( $this, $this->rawSelect( $fields, $conditions, $options, $functionName ) );
+ $res = $this->rawSelect( $fields, $conditions, $options, $functionName );
+ return new ORMResult( $this, $res );
}
/**
* @param array $options
* @param string|null $functionName
*
- * @return array of self
+ * @return array of row objects
+ * @throws DBQueryError if the query failed (even if the database was in ignoreErrors mode).
*/
public function selectObjects( $fields = null, array $conditions = array(),
array $options = array(), $functionName = null ) {
* @since 1.20
*
* @param null|string|array $fields
- * @param array $conditions
- * @param array $options
- * @param null|string $functionName
+ * @param array $conditions
+ * @param array $options
+ * @param null|string $functionName
*
* @return ResultWrapper
+ * @throws DBQueryError if the quey failed (even if the database was in ignoreErrors mode).
*/
public function rawSelect( $fields = null, array $conditions = array(),
array $options = array(), $functionName = null ) {
$options
);
+ /* @var Exception $error */
+ $error = null;
+
+ if ( $result === false ) {
+ // Database connection was in "ignoreErrors" mode. We don't like that.
+ // So, we emulate the DBQueryError that should have been thrown.
+ $error = new \DBQueryError(
+ $dbr,
+ $dbr->lastError(),
+ $dbr->lastErrno(),
+ $dbr->lastQuery(),
+ is_null( $functionName ) ? __METHOD__ : $functionName
+ );
+ }
+
$this->releaseConnection( $dbr );
+
+ if ( $error ) {
+ // Note: construct the error before releasing the connection,
+ // but throw it after.
+ throw $error;
+ }
+
return $result;
}
$objects = $this->select( $fields, $conditions, $options, $functionName );
- return $objects->isEmpty() ? false : $objects->current();
+ return ( !$objects || $objects->isEmpty() ) ? false : $objects->current();
}
/**
* @ingroup Test
*
* @group ORM
+ * @group Database
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
+ * @author Daniel Kinzler
*/
-abstract class ORMTableTest extends MediaWikiTestCase {
+class ORMTableTest extends MediaWikiTestCase {
/**
* @since 1.21
* @return string
*/
- protected abstract function getTableClass();
+ protected function getTableClass() {
+ return 'PageORMTableForTesting';
+ }
/**
* @since 1.21
$this->assertTrue( $class::singleton() === $class::singleton() );
}
+ /**
+ * @since 1.21
+ */
+ public function testIgnoreErrorsOverride() {
+ $table = $this->getTable();
+
+ $db = $table->getReadDbConnection();
+ $db->ignoreErrors( true );
+
+ try {
+ $table->rawSelect( "this is invalid" );
+ $this->fail( "An invalid query should trigger a DBQueryError even if ignoreErrors is enabled." );
+ } catch ( DBQueryError $ex ) {
+ $this->assertTrue( true, "just making phpunit happy" );
+ }
+
+ $db->ignoreErrors( false );
+ }
+
+}
+
+/**
+ * Dummy ORM table for testing, reading Title objects from the page table.
+ *
+ * @since 1.21
+ */
+
+class PageORMTableForTesting extends ORMTable {
+
+ /**
+ * @see ORMTable::getName
+ *
+ * @return string
+ */
+ public function getName() {
+ return 'page';
+ }
+
+ /**
+ * @see ORMTable::getRowClass
+ *
+ * @return string
+ */
+ public function getRowClass() {
+ return 'Title';
+ }
+
+ /**
+ * @see ORMTable::newRow
+ *
+ * @return IORMRow
+ */
+ public function newRow( array $data, $loadDefaults = false ) {
+ return Title::makeTitle( $data['namespace'], $data['title'] );
+ }
+
+ /**
+ * @see ORMTable::getFields
+ *
+ * @return array
+ */
+ public function getFields() {
+ return array(
+ 'id' => 'int',
+ 'namespace' => 'int',
+ 'title' => 'str',
+ );
+ }
+
+ /**
+ * @see ORMTable::getFieldPrefix
+ *
+ * @return string
+ */
+ protected function getFieldPrefix() {
+ return 'page_';
+ }
}