From 57e94d6a4f7e28f88a07860976e59d6a488dfc1d Mon Sep 17 00:00:00 2001 From: jeroendedauw Date: Fri, 18 Jan 2013 10:29:18 +0100 Subject: [PATCH] Made ORMTable and ORMRow non-abstract ORMTable now optionally takes info such as table name and field definitions in its constructor Change-Id: I9cee11fdb58d4ef57d442f69650c6640d767cfa1 --- includes/dao/DBAccessBase.php | 2 +- includes/db/ORMRow.php | 2 +- includes/db/ORMTable.php | 128 +++++++++++++++++++++++++++++++--- 3 files changed, 120 insertions(+), 12 deletions(-) diff --git a/includes/dao/DBAccessBase.php b/includes/dao/DBAccessBase.php index a27bef50d5..20cf5b4edb 100644 --- a/includes/dao/DBAccessBase.php +++ b/includes/dao/DBAccessBase.php @@ -33,7 +33,7 @@ abstract class DBAccessBase implements IDBAccessObject { * @var String|bool $wiki The target wiki's name. This must be an ID * that LBFactory can understand. */ - protected $wiki; + protected $wiki = false; /** * @param String|bool $wiki The target wiki's name. This must be an ID diff --git a/includes/db/ORMRow.php b/includes/db/ORMRow.php index 5c730fb314..6acc124aed 100644 --- a/includes/db/ORMRow.php +++ b/includes/db/ORMRow.php @@ -31,7 +31,7 @@ * @author Jeroen De Dauw < jeroendedauw@gmail.com > */ -abstract class ORMRow implements IORMRow { +class ORMRow implements IORMRow { /** * The fields of the object. diff --git a/includes/db/ORMTable.php b/includes/db/ORMTable.php index 40793158af..8abfdb6484 100644 --- a/includes/db/ORMTable.php +++ b/includes/db/ORMTable.php @@ -19,6 +19,7 @@ * http://www.gnu.org/copyleft/gpl.html * * @since 1.20 + * Non-abstract since 1.21 * * @file ORMTable.php * @ingroup ORM @@ -27,34 +28,140 @@ * @author Jeroen De Dauw < jeroendedauw@gmail.com > */ -abstract class ORMTable extends DBAccessBase implements IORMTable { +class ORMTable extends DBAccessBase implements IORMTable { /** - * Gets the db field prefix. + * Cache for instances, used by the singleton method. * * @since 1.20 + * @deprecated since 1.21 * - * @return string + * @var ORMTable[] */ - protected abstract function getFieldPrefix(); + protected static $instanceCache = array(); /** - * Cache for instances, used by the singleton method. + * @since 1.21 * - * @since 1.20 - * @var array of DBTable + * @var string */ - protected static $instanceCache = array(); + protected $tableName; + + /** + * @since 1.21 + * + * @var string[] + */ + protected $fields = array(); + + /** + * @since 1.21 + * + * @var string + */ + protected $fieldPrefix = ''; + + /** + * @since 1.21 + * + * @var string + */ + protected $rowClass = 'ORMRow'; + + /** + * @since 1.21 + * + * @var array + */ + protected $defaults = array(); /** * ID of the database connection to use for read operations. * Can be changed via @see setReadDb. * * @since 1.20 + * * @var integer DB_ enum */ protected $readDb = DB_SLAVE; + /** + * Constructor. + * + * @since 1.21 + * + * @param string $tableName + * @param string[] $fields + * @param array $defaults + * @param string|null $rowClass + * @param string $fieldPrefix + */ + public function __construct( $tableName = '', array $fields = array(), array $defaults = array(), $rowClass = null, $fieldPrefix = '' ) { + $this->tableName = $tableName; + $this->fields = $fields; + $this->defaults = $defaults; + + if ( is_string( $rowClass ) ) { + $this->rowClass = $rowClass; + } + + $this->fieldPrefix = $fieldPrefix; + } + + /** + * @see IORMTable::getName + * + * @since 1.21 + * + * @return string + * @throws MWException + */ + public function getName() { + if ( $this->tableName === '' ) { + throw new MWException( 'The table name needs to be set' ); + } + + return $this->tableName; + } + + /** + * Gets the db field prefix. + * + * @since 1.20 + * + * @return string + */ + protected function getFieldPrefix() { + return $this->fieldPrefix; + } + + /** + * @see IORMTable::getRowClass + * + * @since 1.21 + * + * @return string + */ + public function getRowClass() { + return $this->rowClass; + } + + /** + * @see ORMTable::getFields + * + * @since 1.21 + * + * @return array + * @throws MWException + */ + public function getFields() { + if ( $this->fields === array() ) { + throw new MWException( 'The table needs to have one or more fields' ); + } + + return $this->fields; + } + /** * Returns a list of default field values. * field name => field value @@ -64,7 +171,7 @@ abstract class ORMTable extends DBAccessBase implements IORMTable { * @return array */ public function getDefaults() { - return array(); + return $this->defaults; } /** @@ -680,6 +787,7 @@ abstract class ORMTable extends DBAccessBase implements IORMTable { * Get an instance of this class. * * @since 1.20 + * @deprecated since 1.21 * * @return IORMTable */ @@ -793,4 +901,4 @@ abstract class ORMTable extends DBAccessBase implements IORMTable { return array_key_exists( $name, $this->getFields() ); } -} +} \ No newline at end of file -- 2.20.1