Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
cb440e1e
Commit
cb440e1e
authored
Nov 16, 2012
by
Martin Hasoň
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support for simple ALTER TABLE command (only rename table and add columns) in Sqlite platform
parent
33555d36
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
113 additions
and
0 deletions
+113
-0
AbstractPlatform.php
lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
+4
-0
SqlitePlatform.php
lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
+63
-0
SqlitePlatformTest.php
tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php
+46
-0
No files found.
lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
View file @
cb440e1e
...
@@ -1833,6 +1833,10 @@ abstract class AbstractPlatform
...
@@ -1833,6 +1833,10 @@ abstract class AbstractPlatform
$default
=
" DEFAULT "
.
$field
[
'default'
];
$default
=
" DEFAULT "
.
$field
[
'default'
];
}
else
if
((
string
)
$field
[
'type'
]
==
'DateTime'
&&
$field
[
'default'
]
==
$this
->
getCurrentTimestampSQL
())
{
}
else
if
((
string
)
$field
[
'type'
]
==
'DateTime'
&&
$field
[
'default'
]
==
$this
->
getCurrentTimestampSQL
())
{
$default
=
" DEFAULT "
.
$this
->
getCurrentTimestampSQL
();
$default
=
" DEFAULT "
.
$this
->
getCurrentTimestampSQL
();
}
else
if
((
string
)
$field
[
'type'
]
==
'Time'
&&
$field
[
'default'
]
==
$this
->
getCurrentTimeSQL
())
{
$default
=
" DEFAULT "
.
$this
->
getCurrentTimeSQL
();
}
else
if
((
string
)
$field
[
'type'
]
==
'Date'
&&
$field
[
'default'
]
==
$this
->
getCurrentDateSQL
())
{
$default
=
" DEFAULT "
.
$this
->
getCurrentDateSQL
();
}
else
if
((
string
)
$field
[
'type'
]
==
'Boolean'
)
{
}
else
if
((
string
)
$field
[
'type'
]
==
'Boolean'
)
{
$default
=
" DEFAULT '"
.
$this
->
convertBooleans
(
$field
[
'default'
])
.
"'"
;
$default
=
" DEFAULT '"
.
$this
->
convertBooleans
(
$field
[
'default'
])
.
"'"
;
}
}
...
...
lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
View file @
cb440e1e
...
@@ -24,6 +24,7 @@ use Doctrine\DBAL\Schema\TableDiff;
...
@@ -24,6 +24,7 @@ use Doctrine\DBAL\Schema\TableDiff;
use
Doctrine\DBAL\Schema\Table
;
use
Doctrine\DBAL\Schema\Table
;
use
Doctrine\DBAL\Schema\ForeignKeyConstraint
;
use
Doctrine\DBAL\Schema\ForeignKeyConstraint
;
use
Doctrine\DBAL\Schema\Index
;
use
Doctrine\DBAL\Schema\Index
;
use
Doctrine\DBAL\Schema\Constraint
;
/**
/**
* The SqlitePlatform class describes the specifics and dialects of the SQLite
* The SqlitePlatform class describes the specifics and dialects of the SQLite
...
@@ -32,6 +33,7 @@ use Doctrine\DBAL\Schema\Index;
...
@@ -32,6 +33,7 @@ use Doctrine\DBAL\Schema\Index;
* @since 2.0
* @since 2.0
* @author Roman Borschel <roman@code-factory.org>
* @author Roman Borschel <roman@code-factory.org>
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Martin Hasoň <martin.hason@gmail.com>
* @todo Rename: SQLitePlatform
* @todo Rename: SQLitePlatform
*/
*/
class
SqlitePlatform
extends
AbstractPlatform
class
SqlitePlatform
extends
AbstractPlatform
...
@@ -599,6 +601,14 @@ class SqlitePlatform extends AbstractPlatform
...
@@ -599,6 +601,14 @@ class SqlitePlatform extends AbstractPlatform
throw
new
DBALException
(
'Sqlite platform does not support alter foreign key.'
);
throw
new
DBALException
(
'Sqlite platform does not support alter foreign key.'
);
}
}
/**
* {@inheritDoc}
*/
public
function
getCreateConstraintSQL
(
Constraint
$constraint
,
$table
)
{
throw
new
DBALException
(
'Sqlite platform does not support alter constraint.'
);
}
/**
/**
* {@inheritDoc}
* {@inheritDoc}
*/
*/
...
@@ -614,6 +624,11 @@ class SqlitePlatform extends AbstractPlatform
...
@@ -614,6 +624,11 @@ class SqlitePlatform extends AbstractPlatform
*/
*/
public
function
getAlterTableSQL
(
TableDiff
$diff
)
public
function
getAlterTableSQL
(
TableDiff
$diff
)
{
{
$sql
=
$this
->
getSimpleAlterTableSQL
(
$diff
);
if
(
false
!==
$sql
)
{
return
$sql
;
}
$fromTable
=
$diff
->
fromTable
;
$fromTable
=
$diff
->
fromTable
;
if
(
!
$fromTable
instanceof
Table
)
{
if
(
!
$fromTable
instanceof
Table
)
{
throw
new
DBALException
(
'Sqlite platform requires for alter table the table diff with reference to original table schema'
);
throw
new
DBALException
(
'Sqlite platform requires for alter table the table diff with reference to original table schema'
);
...
@@ -704,6 +719,54 @@ class SqlitePlatform extends AbstractPlatform
...
@@ -704,6 +719,54 @@ class SqlitePlatform extends AbstractPlatform
return
array_merge
(
$sql
,
$tableSql
,
$columnSql
);
return
array_merge
(
$sql
,
$tableSql
,
$columnSql
);
}
}
private
function
getSimpleAlterTableSQL
(
TableDiff
$diff
)
{
if
(
!
empty
(
$diff
->
renamedColumns
)
||
!
empty
(
$diff
->
addedForeignKeys
)
||
!
empty
(
$diff
->
addedIndexes
)
||
!
empty
(
$diff
->
changedColumns
)
||
!
empty
(
$diff
->
changedForeignKeys
)
||
!
empty
(
$diff
->
changedIndexes
)
||
!
empty
(
$diff
->
removedColumns
)
||
!
empty
(
$diff
->
removedForeignKeys
)
||
!
empty
(
$diff
->
removedIndexes
)
)
{
return
false
;
}
$table
=
new
Table
(
$diff
->
name
);
$sql
=
array
();
$tableSql
=
array
();
$columnSql
=
array
();
foreach
(
$diff
->
addedColumns
as
$columnName
=>
$column
)
{
if
(
$this
->
onSchemaAlterTableAddColumn
(
$column
,
$diff
,
$columnSql
))
{
continue
;
}
$field
=
array_merge
(
array
(
'unique'
=>
null
,
'autoincrement'
=>
null
,
'default'
=>
null
),
$column
->
toArray
());
$type
=
(
string
)
$field
[
'type'
];
switch
(
true
)
{
case
isset
(
$field
[
'columnDefinition'
])
||
$field
[
'autoincrement'
]
||
$field
[
'unique'
]
:
case
$type
==
'DateTime'
&&
$field
[
'default'
]
==
$this
->
getCurrentTimestampSQL
()
:
case
$type
==
'Date'
&&
$field
[
'default'
]
==
$this
->
getCurrentDateSQL
()
:
case
$type
==
'Time'
&&
$field
[
'default'
]
==
$this
->
getCurrentTimeSQL
()
:
return
false
;
}
$field
[
'name'
]
=
$column
->
getQuotedName
(
$this
);
if
(
strtolower
(
$field
[
'type'
])
==
'string'
&&
$field
[
'length'
]
===
null
)
{
$field
[
'length'
]
=
255
;
}
$sql
[]
=
'ALTER TABLE '
.
$table
->
getQuotedName
(
$this
)
.
' ADD COLUMN '
.
$this
->
getColumnDeclarationSQL
(
$field
[
'name'
],
$field
);
}
if
(
!
$this
->
onSchemaAlterTable
(
$diff
,
$tableSql
))
{
if
(
$diff
->
newName
!==
false
)
{
$newTable
=
new
Table
(
$diff
->
newName
);
$sql
[]
=
'ALTER TABLE '
.
$table
->
getQuotedName
(
$this
)
.
' RENAME TO '
.
$newTable
->
getQuotedName
(
$this
);
}
}
return
array_merge
(
$sql
,
$tableSql
,
$columnSql
);
}
private
function
getPrimaryIndex
(
TableDiff
$diff
)
private
function
getPrimaryIndex
(
TableDiff
$diff
)
{
{
$primaryIndex
=
array
();
$primaryIndex
=
array
();
...
...
tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php
View file @
cb440e1e
...
@@ -2,8 +2,11 @@
...
@@ -2,8 +2,11 @@
namespace
Doctrine\Tests\DBAL\Platforms
;
namespace
Doctrine\Tests\DBAL\Platforms
;
use
Doctrine\DBAL\Schema\Column
;
use
Doctrine\DBAL\Schema\TableDiff
;
use
Doctrine\DBAL\Platforms\SqlitePlatform
;
use
Doctrine\DBAL\Platforms\SqlitePlatform
;
use
Doctrine\DBAL\Types\Type
;
use
Doctrine\DBAL\Types\Type
;
use
Doctrine\DBAL\DBALException
;
require_once
__DIR__
.
'/../../TestInit.php'
;
require_once
__DIR__
.
'/../../TestInit.php'
;
...
@@ -113,6 +116,14 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
...
@@ -113,6 +116,14 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
parent
::
testGeneratesForeignKeyCreationSql
();
parent
::
testGeneratesForeignKeyCreationSql
();
}
}
/**
* @expectedException \Doctrine\DBAL\DBALException
*/
public
function
testGeneratesConstraintCreationSql
()
{
parent
::
testGeneratesConstraintCreationSql
();
}
public
function
getGenerateForeignKeySql
()
public
function
getGenerateForeignKeySql
()
{
{
return
null
;
return
null
;
...
@@ -156,6 +167,41 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
...
@@ -156,6 +167,41 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
);
);
}
}
public
function
testAlterTableAddColumns
()
{
$diff
=
new
TableDiff
(
'user'
);
$diff
->
addedColumns
[
'foo'
]
=
new
Column
(
'foo'
,
Type
::
getType
(
'string'
));
$diff
->
addedColumns
[
'count'
]
=
new
Column
(
'count'
,
Type
::
getType
(
'integer'
),
array
(
'notnull'
=>
false
,
'default'
=>
1
));
$expected
=
array
(
'ALTER TABLE user ADD COLUMN foo VARCHAR(255) NOT NULL'
,
'ALTER TABLE user ADD COLUMN count INTEGER DEFAULT 1'
,
);
$this
->
assertEquals
(
$expected
,
$this
->
_platform
->
getAlterTableSQL
(
$diff
));
}
public
function
testAlterTableAddComplexColumns
()
{
$diff
=
new
TableDiff
(
'user'
);
$diff
->
addedColumns
[
'time'
]
=
new
Column
(
'time'
,
Type
::
getType
(
'date'
),
array
(
'default'
=>
'CURRENT_DATE'
));
try
{
$this
->
_platform
->
getAlterTableSQL
(
$diff
);
$this
->
fail
();
}
catch
(
DBALException
$e
)
{
}
$diff
=
new
TableDiff
(
'user'
);
$diff
->
addedColumns
[
'id'
]
=
new
Column
(
'id'
,
Type
::
getType
(
'integer'
),
array
(
'autoincrement'
=>
true
));
try
{
$this
->
_platform
->
getAlterTableSQL
(
$diff
);
$this
->
fail
();
}
catch
(
DBALException
$e
)
{
}
}
protected
function
getQuotedColumnInPrimaryKeySQL
()
protected
function
getQuotedColumnInPrimaryKeySQL
()
{
{
return
array
(
return
array
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment