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
28c7e09d
Commit
28c7e09d
authored
Feb 20, 2011
by
Benjamin Eberlei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[DBAL-42] Added support for marking doctrine types as to be commented, added support for MySQL
parent
4c4c778d
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
115 additions
and
3 deletions
+115
-3
AbstractPlatform.php
lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
+63
-1
AbstractSchemaManager.php
lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
+21
-0
MySqlSchemaManager.php
lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php
+3
-0
SchemaManagerFunctionalTestCase.php
...BAL/Functional/Schema/SchemaManagerFunctionalTestCase.php
+28
-2
No files found.
lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
View file @
28c7e09d
...
...
@@ -25,7 +25,8 @@ use Doctrine\DBAL\DBALException,
Doctrine\DBAL\Schema\Table
,
Doctrine\DBAL\Schema\Index
,
Doctrine\DBAL\Schema\ForeignKeyConstraint
,
Doctrine\DBAL\Schema\TableDiff
;
Doctrine\DBAL\Schema\TableDiff
,
Doctrine\DBAL\Types\Type
;
/**
* Base class for all DatabasePlatforms. The DatabasePlatforms are the central
...
...
@@ -80,6 +81,14 @@ abstract class AbstractPlatform
*/
protected
$doctrineTypeMapping
=
null
;
/**
* Contains a list of all columns that should generate parseable column comments for type-detection
* in reverse engineering scenarios.
*
* @var array
*/
protected
$doctrineTypeComments
=
null
;
/**
* Constructor.
*/
...
...
@@ -209,6 +218,56 @@ abstract class AbstractPlatform
return
isset
(
$this
->
doctrineTypeMapping
[
$dbType
]);
}
/**
* Initialize the Doctrine Type comments instance variable for in_array() checks.
*
* @return void
*/
protected
function
initializeCommentedDoctrineTypes
()
{
$this
->
doctrineTypeComments
=
array
(
Type
::
TARRAY
,
Type
::
OBJECT
);
}
/**
* Is it necessary for the platform to add a parsable type comment to allow reverse engineering the given type?
*
* @param Type $doctrineType
* @return bool
*/
public
function
isCommentedDoctrineType
(
Type
$doctrineType
)
{
if
(
$this
->
doctrineTypeComments
===
null
)
{
$this
->
initializeCommentedDoctrineTypes
();
}
return
in_array
(
$doctrineType
->
getName
(),
$this
->
doctrineTypeComments
);
}
/**
* Mark this type as to be commented in ALTER TABLE and CREATE TABLE statements.
*
* @param Type $doctrineType
* @return void
*/
public
function
markDoctrineTypeCommented
(
Type
$doctrineType
)
{
if
(
$this
->
doctrineTypeComments
===
null
)
{
$this
->
initializeCommentedDoctrineTypes
();
}
$this
->
doctrineTypeComments
[]
=
$doctrineType
->
getName
();
}
/**
* Get the comment to append to a column comment that helps parsing this type in reverse engineering.
*
* @param Type $doctrineType
* @return string
*/
public
function
getDoctrineTypeComment
(
Type
$doctrineType
)
{
return
'(DC2Type:'
.
$doctrineType
->
getName
()
.
')'
;
}
/**
* Gets the character used for identifier quoting.
*
...
...
@@ -798,6 +857,9 @@ abstract class AbstractPlatform
$columnData
[
'columnDefinition'
]
=
$column
->
getColumnDefinition
();
$columnData
[
'autoincrement'
]
=
$column
->
getAutoincrement
();
$columnData
[
'comment'
]
=
$column
->
getComment
();
if
(
$this
->
isCommentedDoctrineType
(
$column
->
getType
()))
{
$columnData
[
'comment'
]
.=
$this
->
getDoctrineTypeComment
(
$column
->
getType
());
}
if
(
in_array
(
$column
->
getName
(),
$options
[
'primary'
]))
{
$columnData
[
'primary'
]
=
true
;
...
...
lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
View file @
28c7e09d
...
...
@@ -779,4 +779,25 @@ abstract class AbstractSchemaManager
return
$schemaConfig
;
}
/**
* Given a table comment this method tries to extract a typehint for Doctrine Type, or returns
* the type given as default.
*
* @param string $comment
* @param string $currentType
* @return string
*/
public
function
extractDoctrineTypeFromComment
(
$comment
,
$currentType
)
{
if
(
preg_match
(
"(\(DC2Type:([a-zA-Z0-9]+)\))"
,
$comment
,
$match
))
{
$currentType
=
$match
[
1
];
}
return
$currentType
;
}
public
function
removeDoctrineTypeFromComment
(
$comment
,
$type
)
{
return
str_replace
(
'(DC2Type:'
.
$type
.
')'
,
''
,
$comment
);
}
}
\ No newline at end of file
lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php
View file @
28c7e09d
...
...
@@ -107,6 +107,9 @@ class MySqlSchemaManager extends AbstractSchemaManager
$precision
=
null
;
$type
=
$this
->
_platform
->
getDoctrineTypeMapping
(
$dbType
);
$type
=
$this
->
extractDoctrineTypeFromComment
(
$tableColumn
[
'comment'
],
$type
);
$tableColumn
[
'comment'
]
=
$this
->
removeDoctrineTypeFromComment
(
$tableColumn
[
'comment'
],
$type
);
switch
(
$dbType
)
{
case
'char'
:
$fixed
=
true
;
...
...
tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php
View file @
28c7e09d
...
...
@@ -408,17 +408,43 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this
->
markTestSkipped
(
'Database does not support column comments.'
);
}
$table
=
new
\Doctrine\DBAL\Schema\Table
(
'test'
);
$table
=
new
\Doctrine\DBAL\Schema\Table
(
'
column_comment_
test'
);
$table
->
addColumn
(
'id'
,
'integer'
,
array
(
'comment'
=>
'This is a comment'
));
$table
->
setPrimaryKey
(
array
(
'id'
));
$this
->
_sm
->
createTable
(
$table
);
$columns
=
$this
->
_sm
->
listTableColumns
(
"test"
);
$columns
=
$this
->
_sm
->
listTableColumns
(
"
column_comment_
test"
);
$this
->
assertEquals
(
1
,
count
(
$columns
));
$this
->
assertEquals
(
'This is a comment'
,
$columns
[
'id'
]
->
getComment
());
}
/**
* @group DBAL-42
*/
public
function
testAutomaticallyAppendCommentOnMarkedColumns
()
{
if
(
!
$this
->
_conn
->
getDatabasePlatform
()
->
supportsInlineColumnComments
()
&&
!
$this
->
_conn
->
getDatabasePlatform
()
->
supportsCommentOnStatement
())
{
$this
->
markTestSkipped
(
'Database does not support column comments.'
);
}
$table
=
new
\Doctrine\DBAL\Schema\Table
(
'column_comment_test2'
);
$table
->
addColumn
(
'id'
,
'integer'
,
array
(
'comment'
=>
'This is a comment'
));
$table
->
addColumn
(
'obj'
,
'object'
,
array
(
'comment'
=>
'This is a comment'
));
$table
->
addColumn
(
'arr'
,
'array'
,
array
(
'comment'
=>
'This is a comment'
));
$table
->
setPrimaryKey
(
array
(
'id'
));
$this
->
_sm
->
createTable
(
$table
);
$columns
=
$this
->
_sm
->
listTableColumns
(
"column_comment_test2"
);
$this
->
assertEquals
(
3
,
count
(
$columns
));
$this
->
assertEquals
(
'This is a comment'
,
$columns
[
'id'
]
->
getComment
());
$this
->
assertEquals
(
'This is a comment'
,
$columns
[
'obj'
]
->
getComment
(),
"The Doctrine2 Typehint should be stripped from comment."
);
$this
->
assertInstanceOf
(
'Doctrine\DBAL\Types\ObjectType'
,
$columns
[
'obj'
]
->
getType
(),
"The Doctrine2 should be detected from comment hint."
);
$this
->
assertEquals
(
'This is a comment'
,
$columns
[
'arr'
]
->
getComment
(),
"The Doctrine2 Typehint should be stripped from comment."
);
$this
->
assertInstanceOf
(
'Doctrine\DBAL\Types\ArrayType'
,
$columns
[
'arr'
]
->
getType
(),
"The Doctrine2 should be detected from comment hint."
);
}
/**
* @param string $name
* @param array $data
...
...
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