Jeroen De Dauw has uploaded a new change for review. https://gerrit.wikimedia.org/r/81254
Change subject: Add code to read in table definitions from the db [DNM, DRAFT] ...................................................................... Add code to read in table definitions from the db [DNM, DRAFT] Change-Id: I11dd081d1835abbcc978b7f576f90cad4903db6c --- M src/DBConnectionProvider.php M src/LazyDBConnectionProvider.php A src/TableDefinitionReader.php 3 files changed, 224 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseDatabase refs/changes/54/81254/1 diff --git a/src/DBConnectionProvider.php b/src/DBConnectionProvider.php index 9bac3ab..a65f955 100644 --- a/src/DBConnectionProvider.php +++ b/src/DBConnectionProvider.php @@ -7,6 +7,8 @@ /** * Interface for database connection providers. * + * TODO: specify connection handling requirements + * * @since 0.1 * * @file diff --git a/src/LazyDBConnectionProvider.php b/src/LazyDBConnectionProvider.php index 541a649..0f88755 100644 --- a/src/LazyDBConnectionProvider.php +++ b/src/LazyDBConnectionProvider.php @@ -8,6 +8,8 @@ * Lazy database connection provider. * The connection is fetched when needed using the id provided in the constructor. * + * TODO: implement connection handling requirements + * * @since 0.1 * * @file diff --git a/src/TableDefinitionReader.php b/src/TableDefinitionReader.php new file mode 100644 index 0000000..8b8c5ad --- /dev/null +++ b/src/TableDefinitionReader.php @@ -0,0 +1,220 @@ +<?php + +namespace Wikibase\Database; + +use Exception; + +/** + * Returns the TableDefinition for the specified table, + * by reading the information from somewhere. + * + * @since 0.1 + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < jeroended...@gmail.com > + */ +interface TableDefinitionReader { + + /** + * @param string $tableName + * + * @return TableDefinition mixed + */ + public function readDefinition( $tableName ); + +} + +/** + * Constructs and returns a TableDefinition for the specified table + * based on how it current exists in the database. + * + * @since 0.1 + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < jeroended...@gmail.com > + */ +class MediaWikiTableDefinitionReader implements TableDefinitionReader { + + protected $connectionProvider; + protected $explainSqlBuilder; + protected $explainSqlInterpreter; + + public function __construct( DBConnectionProvider $connectionProvider, + TableExplainSqlBuilder $explainSqlBuilder, TableExplainSqlInterpreter $explainSqlInterpreter ) { + $this->connectionProvider = $connectionProvider; + $this->explainSqlBuilder = $explainSqlBuilder; + $this->explainSqlInterpreter = $explainSqlInterpreter; + } + + /** + * @param string $tableName + * + * @return TableDefinition mixed + */ + public function readDefinition( $tableName ) { + $db = $this->connectionProvider->getConnection(); + + $queryResult = $db->query( $this->explainSqlBuilder->getExplainSql( $tableName ) ); + + // TODO: handle connection properly + // TODO: handle result before passing it along + + return $this->explainSqlInterpreter->buildTableDefinition( $tableName, $queryResult ); + } + +} + +/** + * Builds an SQL string to explain/describe the structure of the specified table. + * + * @since 0.1 + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < jeroended...@gmail.com > + */ +interface TableExplainSqlBuilder { + + /** + * @param string $tableName + * + * @return string + */ + public function getExplainSql( $tableName ); + +} + +class SQLiteTableExplainSqlBuilder implements TableExplainSqlBuilder { + + protected $tableNameFormatter; + + public function __construct( TableNameFormatter $tableNameFormatter ) { + $this->tableNameFormatter = $tableNameFormatter; + } + + public function getExplainSql( $tableName ) { + return 'PRAGMA table_info(' . $this->tableNameFormatter->formatTableName( $tableName ) . ')'; + } + +} + +/** + * Does any formatting and escaping of a table name (as for instance obtained from a TableDefinition) + * which needs to be done before using it in an SQL statement. + * + * @since 0.1 + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < jeroended...@gmail.com > + */ +interface TableNameFormatter { + + /** + * @param string $tableName + * + * @return string + */ + public function formatTableName( $tableName ); + +} + +class MediaWikiTableNameFormatter implements TableNameFormatter { + + protected $connectionProvider; + + public function __construct( DBConnectionProvider $connectionProvider ) { + $this->connectionProvider = $connectionProvider; + } + + public function formatTableName( $tableName ) { + $db = $this->connectionProvider->getConnection(); + + try { + $tableName = $db->tableName( $tableName ); + } + catch ( Exception $ex ) { + $this->connectionProvider->releaseConnection(); + throw $ex; + } + + $this->connectionProvider->releaseConnection(); + return $tableName; + } + +} + +/** + * Builds a TableDefinition from the result of a explain/describe query. + * TODO: determine exact format of this result + * + * @since 0.1 + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < jeroended...@gmail.com > + */ +interface TableExplainSqlInterpreter { + + /** + * @param string $tableName + * @param TODO $explainResult + * + * @return TableDefinition + */ + public function buildTableDefinition( $tableName, $explainResult ); + +} + +class SQLiteTableExplainSqlInterpreter implements TableExplainSqlInterpreter { + + public function buildTableDefinition( $tableName, $explainResult ) { + // TODO + return new TableDefinition( $tableName, array() ); + } + +} + +/** + * Updates a tables schema from the old definition to the new one. + * + * @since 0.1 + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < jeroended...@gmail.com > + */ +interface TableSchemaUpdater { + + public function updateTable( TableDefinition $originalTable, TableDefinition $newTable ); + +} + +// Something like this would be in the SQLStore. +// TODO: move +class StoreSchemaUpdater { + + protected $tableDefinitionReader; + protected $tableUpdater; + + public function __construct( TableDefinitionReader $tableDefinitionReader, TableSchemaUpdater $tableUpdater ) { + $this->tableDefinitionReader = $tableDefinitionReader; + $this->tableUpdater = $tableUpdater; + } + + public function updateSchema() { + foreach ( $this->getCurrentTables() as $currentTable ) { + $this->updateTable( $currentTable ); + } + } + + /** + * @return TableDefinition[] + */ + protected function getCurrentTables() { + return array(); // TODO + } + + protected function updateTable( TableDefinition $currentTable ) { + $oldDefinition = $this->tableDefinitionReader->readDefinition( $currentTable->getName() ); + + $this->tableUpdater->updateTable( $oldDefinition, $currentTable ); + } + +} \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/81254 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I11dd081d1835abbcc978b7f576f90cad4903db6c Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikibaseDatabase Gerrit-Branch: master Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits