Addshore has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/89826


Change subject: Improve sqlite documentation with links
......................................................................

Improve sqlite documentation with links

Various links to the relevant parts of
http://www.sqlite.org/syntaxdiagrams.html/ClaimGuidValidatorTest.php

Change-Id: If9f955f69f98b303272b38093f5850c31b8f78e1
---
M src/SQLite/SQLiteFieldSqlBuilder.php
M src/SQLite/SQLiteIndexSqlBuilder.php
M src/SQLite/SQLiteSchemaSqlBuilder.php
M src/SQLite/SQLiteTableSqlBuilder.php
4 files changed, 62 insertions(+), 8 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseDatabase 
refs/changes/26/89826/1

diff --git a/src/SQLite/SQLiteFieldSqlBuilder.php 
b/src/SQLite/SQLiteFieldSqlBuilder.php
index a19d27c..36b560d 100644
--- a/src/SQLite/SQLiteFieldSqlBuilder.php
+++ b/src/SQLite/SQLiteFieldSqlBuilder.php
@@ -23,6 +23,9 @@
                $this->escaper = $escaper;
        }
 
+       /**
+        * @see http://www.sqlite.org/syntaxdiagrams.html#column-def
+        */
        public function getFieldSQL( FieldDefinition $field ){
                $sql =  $this->escaper->getEscapedIdentifier( $field->getName() 
) . ' ';
 
@@ -37,6 +40,9 @@
                return $sql;
        }
 
+       /**
+        * @see http://www.sqlite.org/syntaxdiagrams.html#column-constraint
+        */
        protected function getDefault( $default, $type ) {
                if ( $default !== null ) {
                        //TODO ints shouldn't have quotes added to them so we 
can not use the escaper used for strings below???
@@ -49,12 +55,17 @@
                return '';
        }
 
+       /**
+        * @see http://www.sqlite.org/syntaxdiagrams.html#column-constraint
+        */
        protected function getNull( $allowsNull ) {
                return $allowsNull ? ' NULL' : ' NOT NULL';
        }
 
        /**
         * Returns the MySQL field type for a given FieldDefinition type 
constant.
+        *
+        * @see http://www.sqlite.org/syntaxdiagrams.html#type-name
         *
         * @param string $fieldType
         *
@@ -76,6 +87,9 @@
                }
        }
 
+       /**
+        * @see http://www.sqlite.org/syntaxdiagrams.html#column-constraint
+        */
        protected function getAutoInc( $shouldAutoInc ){
                if ( $shouldAutoInc ){
                        return ' PRIMARY KEY AUTOINCREMENT';
diff --git a/src/SQLite/SQLiteIndexSqlBuilder.php 
b/src/SQLite/SQLiteIndexSqlBuilder.php
index cf0539d..fa69a8d 100644
--- a/src/SQLite/SQLiteIndexSqlBuilder.php
+++ b/src/SQLite/SQLiteIndexSqlBuilder.php
@@ -28,6 +28,9 @@
                $this->tableNameFormatter = $tableNameFormatter;
        }
 
+       /**
+        * @see http://www.sqlite.org/syntaxdiagrams.html#create-index-stmt
+        */
        public function getIndexSQL( IndexDefinition $index, $tableName ){
                $sql = 'CREATE ';
                $sql .= $this->getIndexType( $index->getType() ) . ' ';
diff --git a/src/SQLite/SQLiteSchemaSqlBuilder.php 
b/src/SQLite/SQLiteSchemaSqlBuilder.php
index 0e37fa3..58caa2e 100644
--- a/src/SQLite/SQLiteSchemaSqlBuilder.php
+++ b/src/SQLite/SQLiteSchemaSqlBuilder.php
@@ -6,6 +6,7 @@
 use Wikibase\Database\Escaper;
 use Wikibase\Database\Schema\Definitions\FieldDefinition;
 use Wikibase\Database\Schema\Definitions\IndexDefinition;
+use Wikibase\Database\Schema\Definitions\TableDefinition;
 use Wikibase\Database\Schema\SchemaModificationSqlBuilder;
 use Wikibase\Database\TableNameFormatter;
 
@@ -40,6 +41,10 @@
        }
 
        /**
+        * This performs a series of sql queries to rename a table, create a 
new table with
+        * the new definition (without the field we are removing), copy all of 
the data
+        * across and drop the old table.
+        *
         * @param string $tableName
         * @param string $fieldName
         *
@@ -48,18 +53,45 @@
         */
        public function getRemoveFieldSql( $tableName, $fieldName ) {
                $definition = $this->tableDefinitionReader->readDefinition( 
$tableName );
-               $tableName = $this->tableNameFormatter->formatTableName( 
$tableName );
-               $tmpTableName = $this->tableNameFormatter->formatTableName( 
$tableName . '_tmp' );
-               $sql = "ALTER TABLE {$tableName} RENAME TO {$tmpTableName};" . 
PHP_EOL;
 
+               $tmpTableName = $tableName . '_tmp';
+               $sql = $this->getRenameTableSql( $tableName, $tmpTableName ) . 
PHP_EOL;
+
+               /** @var TableDefinition $definition */
                $definition = $definition->mutateFieldAway( $fieldName );
-               $sql .= $this->tableSqlBuilder->getCreateTableSql( $definition 
) . PHP_EOL;
 
-               $fieldsSql = $this->getFieldsSql( $definition->getFields() );
-               $sql .= "INSERT INTO {$tableName}({$fieldsSql}) SELECT 
{$fieldsSql} FROM {$tmpTableName};" . PHP_EOL;
-               $sql .= "DROP TABLE {$tmpTableName};";
+               $sql .= $this->tableSqlBuilder->getCreateTableSql( $definition 
) . PHP_EOL;
+               $sql .= $this->getContentsCopySql( $tmpTableName, $tableName, 
$definition->getFields() ) . PHP_EOL;
+               $sql .= $this->getDropTableSql( $tmpTableName );
 
                return $sql;
+       }
+
+       /**
+        * @see http://www.sqlite.org/syntaxdiagrams.html#alter-table-stmt
+        */
+       protected function getRenameTableSql( $fromTable, $toTable ){
+               $fromTable = $this->tableNameFormatter->formatTableName( 
$fromTable );
+               $toTable = $this->tableNameFormatter->formatTableName( $toTable 
);
+               return "ALTER TABLE {$fromTable} RENAME TO {$toTable};";
+       }
+
+       /**
+        * @see http://www.sqlite.org/syntaxdiagrams.html#insert-stmt
+        */
+       protected function getContentsCopySql( $fromTable, $toTable, $fields ){
+               $fromTable = $this->tableNameFormatter->formatTableName( 
$fromTable );
+               $toTable = $this->tableNameFormatter->formatTableName( $toTable 
);
+               $fieldsSql = $this->getFieldsSql( $fields );
+               return "INSERT INTO {$toTable}({$fieldsSql}) SELECT 
{$fieldsSql} FROM {$fromTable};";
+       }
+
+       /**
+        * @see http://www.sqlite.org/syntaxdiagrams.html#drop-table-stmt
+        */
+       protected function getDropTableSql( $tableName ){
+               $tableName = $this->tableNameFormatter->formatTableName( 
$tableName );
+               return "DROP TABLE {$tableName};";
        }
 
        /**
@@ -75,6 +107,8 @@
        }
 
        /**
+        * @see http://www.sqlite.org/syntaxdiagrams.html#alter-table-stmt
+        *
         * @param string $tableName
         * @param FieldDefinition $field
         *
@@ -86,10 +120,11 @@
        }
 
        /**
+        * @see http://www.sqlite.org/syntaxdiagrams.html#drop-index-stmt
+        *
         * @param string $tableName Ignored by this method
         * @param string $indexName
         *
-        * @see http://www.sqlite.org/lang_dropindex.html
         * @return string
         */
        public function getRemoveIndexSql( $tableName, $indexName ){
diff --git a/src/SQLite/SQLiteTableSqlBuilder.php 
b/src/SQLite/SQLiteTableSqlBuilder.php
index d7f114c..dda1cb6 100644
--- a/src/SQLite/SQLiteTableSqlBuilder.php
+++ b/src/SQLite/SQLiteTableSqlBuilder.php
@@ -39,6 +39,7 @@
 
        /**
         * @see ExtendedAbstraction::createTable
+        * @see http://www.sqlite.org/lang_createtable.html
         *
         * @since 0.1
         *
@@ -90,6 +91,7 @@
 
        /**
         * @param IndexDefinition $index
+        * @see http://www.sqlite.org/syntaxdiagrams.html#table-constraint
         * @return string
         */
        protected function getPrimaryKey( $index ){

-- 
To view, visit https://gerrit.wikimedia.org/r/89826
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If9f955f69f98b303272b38093f5850c31b8f78e1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseDatabase
Gerrit-Branch: master
Gerrit-Owner: Addshore <addshorew...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to