Addshore has uploaded a new change for review.

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


Change subject: Add AutoIncrement support
......................................................................

Add AutoIncrement support

Change-Id: I0668aee1ba61f39f117d4fe7056a652af0d4f4c0
---
M src/MySQL/MySQLFieldSqlBuilder.php
M src/MySQL/MySQLTableDefinitionReader.php
M src/SQLite/SQLiteFieldSqlBuilder.php
M src/SQLite/SQLiteIndexSqlBuilder.php
M src/SQLite/SQLiteTableDefinitionReader.php
M src/SQLite/SQLiteTableSqlBuilder.php
M src/Schema/Definitions/TableDefinition.php
M tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php
M tests/phpunit/SQLite/SQLiteFieldSqlBuilderTest.php
M tests/phpunit/SQLite/SQLiteSchemaSqlBuilderTest.php
M tests/phpunit/SQLite/SQLiteTableSqlBuilderTest.php
M tests/phpunit/Schema/Definition/TableDefinitionTest.php
12 files changed, 145 insertions(+), 33 deletions(-)


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

diff --git a/src/MySQL/MySQLFieldSqlBuilder.php 
b/src/MySQL/MySQLFieldSqlBuilder.php
index ccf67fd..4034902 100644
--- a/src/MySQL/MySQLFieldSqlBuilder.php
+++ b/src/MySQL/MySQLFieldSqlBuilder.php
@@ -35,7 +35,7 @@
 
                $sql .= $this->getAutoInc( $field->hasAutoIncrement() );
 
-               // TODO: add all field stuff relevant here
+               // TODO: Field Attributes
 
                return $sql;
        }
diff --git a/src/MySQL/MySQLTableDefinitionReader.php 
b/src/MySQL/MySQLTableDefinitionReader.php
index cf77899..4b38698 100644
--- a/src/MySQL/MySQLTableDefinitionReader.php
+++ b/src/MySQL/MySQLTableDefinitionReader.php
@@ -2,6 +2,7 @@
 
 namespace Wikibase\Database\MySQL;
 
+use RuntimeException;
 use Wikibase\Database\QueryInterface\QueryInterface;
 use Wikibase\Database\QueryInterface\QueryInterfaceException;
 use Wikibase\Database\QueryInterface\ResultIterator;
@@ -89,7 +90,10 @@
 
        /**
         * Simplifies the datatype and returns something a FieldDefinition can 
expect
+        *
         * @param $dataType string
+        *
+        * @throws RuntimeException
         * @return string
         */
        private function getDataType( $dataType ) {
@@ -102,8 +106,7 @@
                } else if ( stristr( $dataType, 'float' ) ){
                        return FieldDefinition::TYPE_FLOAT;
                } else {
-                       //TODO FIXME decide if this is the correct behaviour or 
throw a RuntimeException
-                       return $dataType;
+                       throw new RuntimeException( __CLASS__ . ' does not 
support db fields of type ' . $dataType );
                }
        }
 
@@ -135,7 +138,6 @@
         * @throws QueryInterfaceException
         * @return IndexDefinition[]
         * @TODO support currently don't notice FULLTEXT or SPATIAL indexes
-        * @TODO integration test that uses primarykey in MYSQL
         */
        private function getIndexes( $tableName ) {
                $indexes = array();
diff --git a/src/SQLite/SQLiteFieldSqlBuilder.php 
b/src/SQLite/SQLiteFieldSqlBuilder.php
index aedb1b3..88b5eef 100644
--- a/src/SQLite/SQLiteFieldSqlBuilder.php
+++ b/src/SQLite/SQLiteFieldSqlBuilder.php
@@ -2,7 +2,6 @@
 
 namespace Wikibase\Database\SQLite;
 
-use LogicException;
 use RuntimeException;
 use Wikibase\Database\Escaper;
 use Wikibase\Database\Schema\Definitions\FieldDefinition;
@@ -34,10 +33,7 @@
 
                $sql .= $this->getNull( $field->allowsNull() );
 
-               //TODO implement AutoIncrement Stuff for SQLite
-               if( $field->hasAutoIncrement() ){
-                       throw new LogicException( 'AutoIncrement support not 
yet implemented' );
-               }
+               $sql .= $this->getAutoInc( $field->hasAutoIncrement() );
 
                return $sql;
        }
@@ -69,7 +65,7 @@
        protected function getFieldType( $fieldType ) {
                switch ( $fieldType ) {
                        case FieldDefinition::TYPE_INTEGER:
-                               return 'INT';
+                               return 'INTEGER';
                        case FieldDefinition::TYPE_FLOAT:
                                return 'FLOAT';
                        case FieldDefinition::TYPE_TEXT:
@@ -81,4 +77,11 @@
                }
        }
 
+       protected function getAutoInc( $shouldAutoInc ){
+               if ( $shouldAutoInc ){
+                       return ' PRIMARY KEY AUTOINCREMENT';
+               }
+               return '';
+       }
+
 }
\ No newline at end of file
diff --git a/src/SQLite/SQLiteIndexSqlBuilder.php 
b/src/SQLite/SQLiteIndexSqlBuilder.php
index 676f534..7b8c0b5 100644
--- a/src/SQLite/SQLiteIndexSqlBuilder.php
+++ b/src/SQLite/SQLiteIndexSqlBuilder.php
@@ -43,6 +43,7 @@
 
        /**
         * Returns the SQL field type for a given IndexDefinition type constant.
+        * Primary keys are not supported through the IndexSqlBuilder
         *
         * @param string $indexType
         *
@@ -56,7 +57,6 @@
                        case IndexDefinition::TYPE_UNIQUE:
                                return 'UNIQUE INDEX';
                        default:
-                               //TODO FIXME SQLite doesnt actually support 
primary keys, but we probably want to support them some how
                                throw new RuntimeException( __CLASS__ . ' does 
not support db indexes of type ' . $indexType );
                }
        }
diff --git a/src/SQLite/SQLiteTableDefinitionReader.php 
b/src/SQLite/SQLiteTableDefinitionReader.php
index 3856c67..a37801c 100644
--- a/src/SQLite/SQLiteTableDefinitionReader.php
+++ b/src/SQLite/SQLiteTableDefinitionReader.php
@@ -60,14 +60,12 @@
                }
                $fields = array();
 
-               //todo read auto increment
-
                foreach( $results as $result ){
                        /** $createParts,  1 => tableName, 2 => fieldParts 
(fields, keys, etc.) */
                        preg_match( '/CREATE TABLE ([^ ]+) \(([^\)]+)\)/', 
$result->sql, $createParts );
 
-                       foreach( explode( ',', $createParts[2] ) as $fieldSql ){
-                               if( preg_match( '/([^ ]+) ([^ ]+)( DEFAULT ([^ 
]+))?( ((NOT )?NULL))?/', $fieldSql, $fieldParts )
+                       foreach( explode( ',', $createParts[2] ) as $fieldSql ) 
{
+                               if( preg_match( '/([^ ]+) ([^ ]+)( DEFAULT ([^ 
]+))?( ((NOT )?NULL))?( (PRIMARY KEY AUTOINCREMENT))?/', $fieldSql, $fieldParts 
)
                                        && $fieldParts[0] !== 'PRIMARY KEY' ) {
                                        $fields[] = $this->getField( 
$fieldParts );
                                }
@@ -93,7 +91,15 @@
                $type = $this->getFieldType( $fieldParts[2] );
                $default = $this->getFieldDefault( $fieldParts[4] );
                $null = $this->getFieldCanNull( $fieldParts[6] );
-               return new FieldDefinition( $fieldParts[1], $type, $null, 
$default );
+               $attr = FieldDefinition::NO_ATTRIB; //todo read ATTRIBS
+
+               if( array_key_exists( 9, $fieldParts ) ){
+                       $autoInc = $this->getAutoInc( $fieldParts[9] );
+               } else {
+                       $autoInc = FieldDefinition::NO_AUTOINCREMENT;
+               }
+
+               return new FieldDefinition( $fieldParts[1], $type, $null, 
$default, $attr, $autoInc );
        }
 
        private function getFieldType( $type ) {
@@ -105,6 +111,9 @@
                                return 'str';
                                break;
                        case 'INT':
+                               return 'int';
+                               break;
+                       case 'INTEGER':
                                return 'int';
                                break;
                        case 'FLOAT':
@@ -119,16 +128,23 @@
                if( !empty( $default ) ){
                        return $default;
                } else {
-                       return null;
+                       return FieldDefinition::NO_DEFAULT;
                }
        }
 
        private function getFieldCanNull( $canNull ) {
                if( $canNull === 'NOT NULL' ){
-                       return false;
+                       return FieldDefinition::NOT_NULL;
                } else {
-                       return true;
+                       return FieldDefinition::NULL;
                }
+       }
+
+       private function getAutoInc( $autoInc ){
+               if( $autoInc === 'PRIMARY KEY AUTOINCREMENT' ){
+                       return FieldDefinition::AUTOINCREMENT;
+               }
+               return FieldDefinition::NO_AUTOINCREMENT;
        }
 
        /**
@@ -204,6 +220,8 @@
                                        $columns[ trim( $columnName ) ] = 0;
                                }
                                $keys[] = new IndexDefinition( 'PRIMARY', 
$columns , IndexDefinition::TYPE_PRIMARY );
+                       } else if( preg_match( '/(\(|,| )+([^ ]+)[a-z0-9 
_]+PRIMARY KEY AUTOINCREMENT/i', $result->sql, $createParts ) ){
+                               $keys[] = new IndexDefinition( 'PRIMARY', 
array( $createParts[2] => 0 ) , IndexDefinition::TYPE_PRIMARY );
                        }
                }
 
diff --git a/src/SQLite/SQLiteTableSqlBuilder.php 
b/src/SQLite/SQLiteTableSqlBuilder.php
index 0e55e69..a382b96 100644
--- a/src/SQLite/SQLiteTableSqlBuilder.php
+++ b/src/SQLite/SQLiteTableSqlBuilder.php
@@ -58,6 +58,14 @@
 
                $sql .= implode( ', ', $fields );
 
+               $primaryKey = $this->getPrimaryIndex( $table->getIndexes() );
+               if( !is_null( $primaryKey ) ){
+                       $table = $table->mutateIndexAway( 
$primaryKey->getName() );
+                       if( !strstr( $sql, 'PRIMARY KEY' ) ){
+                               $sql .= $this->getPrimaryKey( $primaryKey );
+                       }
+               }
+
                $sql .= ');';
 
                foreach ( $table->getIndexes() as $index ){
@@ -67,4 +75,34 @@
                return $sql;
        }
 
+       /**
+        * @param IndexDefinition[] $indexes
+        * @return IndexDefinition|null
+        */
+       protected function getPrimaryIndex( $indexes ){
+               foreach( $indexes as $index ){
+                       if( $index->getType() === IndexDefinition::TYPE_PRIMARY 
){
+                               return $index;
+                       }
+               }
+               return null;
+       }
+
+       /**
+        * @param IndexDefinition $index
+        * @return string
+        */
+       protected function getPrimaryKey( $index ){
+               if( $index instanceof IndexDefinition ){
+
+                       $cols = array();
+                       foreach( $index->getColumns() as $col => $length ){
+                               $cols[] = $col;
+                       }
+
+                       return ',PRIMARY KEY (' . implode( ', ', $cols ) . ')';
+               }
+               return '';
+       }
+
 }
diff --git a/src/Schema/Definitions/TableDefinition.php 
b/src/Schema/Definitions/TableDefinition.php
index 8d750e3..b7c5ba7 100644
--- a/src/Schema/Definitions/TableDefinition.php
+++ b/src/Schema/Definitions/TableDefinition.php
@@ -194,4 +194,36 @@
                return $this->mutateFields( $newFields );
        }
 
+       /**
+        * Returns a clone of the table, though with the provided indexes 
rather then the original ones.
+        *
+        * @since 0.1
+        *
+        * @param IndexDefinition[] $indexes
+        *
+        * @return TableDefinition
+        */
+       public function mutateIndexes( array $indexes ) {
+               return new self( $this->name, $this->fields, $indexes );
+       }
+
+       /**
+        * Returns a clone of the table, though with the provided index removed.
+        *
+        * @since 0.1
+        *
+        * @param string $indexName
+        *
+        * @return TableDefinition
+        */
+       public function mutateIndexAway( $indexName ){
+               $newIndexes = array();
+               foreach( $this->getIndexes() as $index ){
+                       if( $index->getName() !== $indexName ){
+                               $newIndexes[] = $index;
+                       }
+               }
+               return $this->mutateIndexes( $newIndexes );
+       }
+
 }
diff --git a/tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php 
b/tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php
index 862fd34..3f925fb 100644
--- a/tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php
+++ b/tests/integration/MediaWiki/Schema/TableCreateReadDeleteTest.php
@@ -89,15 +89,13 @@
                        )
                );
 
-               if( $this->getType() === 'mysql' ){ //TODO add support for 
AutoInc cols in sqlite and remove this condition
-                       $tables[] = new TableDefinition( 'autoinc_field', array(
-                                       new FieldDefinition( 'autoinc', 
FieldDefinition::TYPE_INTEGER, FieldDefinition::NOT_NULL, 
FieldDefinition::NO_DEFAULT, FieldDefinition::NO_ATTRIB, 
FieldDefinition::AUTOINCREMENT ),
-                               ),
-                               array(
-                                       new IndexDefinition( 'PRIMARY', array( 
'autoinc' => 0 ), IndexDefinition::TYPE_PRIMARY ),
-                               )
-                       );
-               }
+               $tables[] = new TableDefinition( 'autoinc_field', array(
+                               new FieldDefinition( 'autoinc', 
FieldDefinition::TYPE_INTEGER, FieldDefinition::NOT_NULL, 
FieldDefinition::NO_DEFAULT, FieldDefinition::NO_ATTRIB, 
FieldDefinition::AUTOINCREMENT ),
+                       ),
+                       array(
+                               new IndexDefinition( 'PRIMARY', array( 
'autoinc' => 0 ), IndexDefinition::TYPE_PRIMARY ),
+                       )
+               );
 
                $tables[] = new TableDefinition( 'not_null_fields', array(
                                new FieldDefinition( 'intfield', 
FieldDefinition::TYPE_INTEGER, FieldDefinition::NOT_NULL, 42 ),
diff --git a/tests/phpunit/SQLite/SQLiteFieldSqlBuilderTest.php 
b/tests/phpunit/SQLite/SQLiteFieldSqlBuilderTest.php
index aa0a35d..f3b4c5a 100644
--- a/tests/phpunit/SQLite/SQLiteFieldSqlBuilderTest.php
+++ b/tests/phpunit/SQLite/SQLiteFieldSqlBuilderTest.php
@@ -48,6 +48,19 @@
 
                $argLists[] = array(
                        new FieldDefinition(
+                               'autoInc',
+                               FieldDefinition::TYPE_INTEGER,
+                               FieldDefinition::NOT_NULL,
+                               FieldDefinition::NO_DEFAULT,
+                               FieldDefinition::NO_ATTRIB,
+                               FieldDefinition::AUTOINCREMENT
+
+                       ),
+                       'autoInc INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT'
+               );
+
+               $argLists[] = array(
+                       new FieldDefinition(
                                'fieldName',
                                FieldDefinition::TYPE_BOOLEAN,
                                FieldDefinition::NOT_NULL,
diff --git a/tests/phpunit/SQLite/SQLiteSchemaSqlBuilderTest.php 
b/tests/phpunit/SQLite/SQLiteSchemaSqlBuilderTest.php
index b13e7da..c61a0ee 100644
--- a/tests/phpunit/SQLite/SQLiteSchemaSqlBuilderTest.php
+++ b/tests/phpunit/SQLite/SQLiteSchemaSqlBuilderTest.php
@@ -69,7 +69,7 @@
                $sql = $instance->getRemoveFieldSql( 'tableName', 'textField' );
                $this->assertEquals(
                        'ALTER TABLE tableName RENAME TO tableName_tmp;' . 
PHP_EOL
-                       . 'CREATE TABLE tableName (primaryField INT NOT NULL, 
intField INT DEFAULT 42 NOT NULL);' . PHP_EOL
+                       . 'CREATE TABLE tableName (primaryField INTEGER NOT 
NULL, intField INTEGER DEFAULT 42 NOT NULL);' . PHP_EOL
                        . 'CREATE INDEX INDEX ON tableName 
(intField,primaryField);' . PHP_EOL
                        . 'INSERT INTO tableName(primaryField, intField) SELECT 
primaryField, intField FROM tableName_tmp;' . PHP_EOL
                        . 'DROP TABLE tableName_tmp;' ,
@@ -79,7 +79,7 @@
        public function testGetAddFieldSql(){
                $instance = $this->newInstance( );
                $sql = $instance->getAddFieldSql( 'tableName', new 
FieldDefinition( 'intField',FieldDefinition::TYPE_INTEGER) );
-               $this->assertEquals( "ALTER TABLE tableName ADD COLUMN intField 
INT NULL", $sql );
+               $this->assertEquals( "ALTER TABLE tableName ADD COLUMN intField 
INTEGER NULL", $sql );
        }
 
        public function testGetRemoveIndexSql(){
diff --git a/tests/phpunit/SQLite/SQLiteTableSqlBuilderTest.php 
b/tests/phpunit/SQLite/SQLiteTableSqlBuilderTest.php
index 725698b..a6fee0e 100644
--- a/tests/phpunit/SQLite/SQLiteTableSqlBuilderTest.php
+++ b/tests/phpunit/SQLite/SQLiteTableSqlBuilderTest.php
@@ -64,7 +64,7 @@
                                        new FieldDefinition( 'fieldName', 
FieldDefinition::TYPE_INTEGER )
                                )
                        ),
-                       'CREATE TABLE tableName (fieldName INT NULL);'
+                       'CREATE TABLE tableName (fieldName INTEGER NULL);'
                );
 
                $argLists[] = array(
@@ -89,7 +89,7 @@
                                        ),
                                )
                        ),
-                       'CREATE TABLE tableName (primaryField INT NOT NULL, 
textField BLOB NULL, intField INT DEFAULT 42 NOT NULL);'
+                       'CREATE TABLE tableName (primaryField INTEGER NOT NULL, 
textField BLOB NULL, intField INTEGER DEFAULT 42 NOT NULL);'
                );
 
                $argLists[] = array(
@@ -121,7 +121,7 @@
                                        ),
                                )
                        ),
-                       'CREATE TABLE tableName (primaryField INT NOT NULL, 
textField BLOB NULL, intField INT DEFAULT 42 NOT NULL);' . PHP_EOL
+                       'CREATE TABLE tableName (primaryField INTEGER NOT NULL, 
textField BLOB NULL, intField INTEGER DEFAULT 42 NOT NULL);' . PHP_EOL
                        . 'CREATE INDEX indexName ON tableName 
(intField,textField);'
                );
 
diff --git a/tests/phpunit/Schema/Definition/TableDefinitionTest.php 
b/tests/phpunit/Schema/Definition/TableDefinitionTest.php
index 4d9cb63..9c80a18 100644
--- a/tests/phpunit/Schema/Definition/TableDefinitionTest.php
+++ b/tests/phpunit/Schema/Definition/TableDefinitionTest.php
@@ -273,4 +273,12 @@
                return $args;
        }
 
+       public function testMutateIndexes(){
+               $this->markTestIncomplete( 'Test Me!' );
+       }
+
+       public function testMutateIndexAway(){
+               $this->markTestIncomplete( 'Test Me!' );
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0668aee1ba61f39f117d4fe7056a652af0d4f4c0
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseDatabase
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to