Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
PSDI Army
yii2
Commits
4602b575
Commit
4602b575
authored
May 29, 2013
by
Gevik Babakhani
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Save point: WIP implementing db schema functionality.
parent
c30eae62
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
146 additions
and
17 deletions
+146
-17
Connection.php
framework/yii/db/Connection.php
+2
-0
PDO.php
framework/yii/db/pgsql/PDO.php
+61
-0
Schema.php
framework/yii/db/pgsql/Schema.php
+32
-5
config.php
tests/unit/data/config.php
+4
-1
DatabaseTestCase.php
tests/unit/framework/db/DatabaseTestCase.php
+3
-0
PostgreSQLConnectionTest.php
tests/unit/framework/db/pgsql/PostgreSQLConnectionTest.php
+44
-11
No files found.
framework/yii/db/Connection.php
View file @
4602b575
...
@@ -352,6 +352,8 @@ class Connection extends Component
...
@@ -352,6 +352,8 @@ class Connection extends Component
$driver
=
strtolower
(
substr
(
$this
->
dsn
,
0
,
$pos
));
$driver
=
strtolower
(
substr
(
$this
->
dsn
,
0
,
$pos
));
if
(
$driver
===
'mssql'
||
$driver
===
'dblib'
||
$driver
===
'sqlsrv'
)
{
if
(
$driver
===
'mssql'
||
$driver
===
'dblib'
||
$driver
===
'sqlsrv'
)
{
$pdoClass
=
'yii\db\mssql\PDO'
;
$pdoClass
=
'yii\db\mssql\PDO'
;
}
else
if
(
$driver
===
'pgsql'
)
{
$pdoClass
=
'yii\db\pgsql\PDO'
;
}
}
}
}
return
new
$pdoClass
(
$this
->
dsn
,
$this
->
username
,
$this
->
password
,
$this
->
attributes
);
return
new
$pdoClass
(
$this
->
dsn
,
$this
->
username
,
$this
->
password
,
$this
->
attributes
);
...
...
framework/yii/db/pgsql/PDO.php
0 → 100644
View file @
4602b575
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\db\pgsql
;
/**
* This is an extension of the default PDO class for PostgreSQL drivers.
* It provides additional low level functionality for setting database
* configuration parameters.
*
* @author Gevik babakhani <gevikb@gmail.com>
* @since 2.0
*/
class
PDO
extends
\PDO
{
/**
* Here we override the default PDO constructor in order to
* find and set the default schema search path.
*/
public
function
__construct
(
$dsn
,
$username
,
$passwd
,
$options
)
{
$searchPath
=
null
;
if
(
is_array
(
$options
)
&&
isset
(
$options
[
'search_path'
]))
{
$matches
=
null
;
if
(
preg_match
(
"/(\s?)+(\w)+((\s+)?,(\s+)?\w+)*/"
,
$options
[
'search_path'
],
$matches
)
===
1
)
{
$searchPath
=
$matches
[
0
];
}
}
parent
::
__construct
(
$dsn
,
$username
,
$passwd
,
$options
);
if
(
!
is_null
(
$searchPath
))
{
$this
->
setSchemaSearchPath
(
$searchPath
);
}
}
/**
* Sets the schema search path of the current users session.
* The syntax of the path is a comma separated string with
* your custom search path at the beginning and the "public"
* schema at the end.
*
* This method automatically adds the "public" schema at the
* end of the search path if it is not provied.
* @param string custom schema search path. defaults to public
*/
public
function
setSchemaSearchPath
(
$searchPath
=
'public'
)
{
$schemas
=
explode
(
','
,
str_replace
(
' '
,
''
,
$searchPath
));
if
(
end
(
$schemas
)
!==
'public'
)
{
$schemas
[]
=
'public'
;
}
foreach
(
$schemas
as
$k
=>
$item
)
{
$schemas
[
$k
]
=
'"'
.
str_replace
(
array
(
'"'
,
"'"
,
';'
),
''
,
$item
)
.
'"'
;
}
$path
=
implode
(
', '
,
$schemas
);
$this
->
exec
(
'SET search_path TO '
.
$path
);
}
}
framework/yii/db/pgsql/Schema.php
View file @
4602b575
<?php
<?php
/**
/**
* @link http://www.yiiframework.com/
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @copyright Copyright (c) 2008 Yii Software LLC
...
@@ -16,15 +17,39 @@ use yii\db\ColumnSchema;
...
@@ -16,15 +17,39 @@ use yii\db\ColumnSchema;
* @author Gevik Babakhani <gevikb@gmail.com>
* @author Gevik Babakhani <gevikb@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
class
Schema
extends
\yii\db\Schema
class
Schema
extends
\yii\db\Schema
{
{
/**
* Resolves the table name and schema name (if any).
* @param TableSchema $table the table metadata object
* @param string $name the table name
*/
protected
function
resolveTableNames
(
$table
,
$name
)
{
$parts
=
explode
(
'.'
,
str_replace
(
'"'
,
''
,
$name
));
if
(
isset
(
$parts
[
1
]))
{
$table
->
schemaName
=
$parts
[
0
];
$table
->
name
=
$parts
[
1
];
}
else
{
$table
->
name
=
$parts
[
0
];
}
}
/**
* Quotes a table name for use in a query.
* A simple table name has no schema prefix.
* @param string $name table name
* @return string the properly quoted table name
*/
public
function
quoteSimpleTableName
(
$name
)
{
return
strpos
(
$name
,
'"'
)
!==
false
?
$name
:
'"'
.
$name
.
'"'
;
}
/**
/**
* Loads the metadata for the specified table.
* Loads the metadata for the specified table.
* @param string $name table name
* @param string $name table name
* @return TableSchema|null driver dependent table metadata. Null if the table does not exist.
* @return TableSchema|null driver dependent table metadata. Null if the table does not exist.
*/
*/
public
function
loadTableSchema
(
$name
)
public
function
loadTableSchema
(
$name
)
{
{
$table
=
new
TableSchema
();
$table
=
new
TableSchema
();
$this
->
resolveTableNames
(
$table
,
$name
);
$this
->
resolveTableNames
(
$table
,
$name
);
$this
->
findPrimaryKeys
(
$table
);
$this
->
findPrimaryKeys
(
$table
);
...
@@ -32,5 +57,6 @@ class Schema extends \yii\db\Schema
...
@@ -32,5 +57,6 @@ class Schema extends \yii\db\Schema
$this
->
findForeignKeys
(
$table
);
$this
->
findForeignKeys
(
$table
);
return
$table
;
return
$table
;
}
}
}
}
}
}
\ No newline at end of file
tests/unit/data/config.php
View file @
4602b575
...
@@ -21,9 +21,12 @@ return array(
...
@@ -21,9 +21,12 @@ return array(
'fixture'
=>
__DIR__
.
'/mssql.sql'
,
'fixture'
=>
__DIR__
.
'/mssql.sql'
,
),
),
'pgsql'
=>
array
(
'pgsql'
=>
array
(
'dsn'
=>
'pgsql:host=localhost;dbname=yiitest;port=5432'
,
'dsn'
=>
'pgsql:host=localhost;dbname=yiitest;port=5432
;
'
,
'username'
=>
'postgres'
,
'username'
=>
'postgres'
,
'password'
=>
'postgres'
,
'password'
=>
'postgres'
,
'attributes'
=>
array
(
'search_path'
=>
'master,hello'
),
'fixture'
=>
__DIR__
.
'/postgres.sql'
,
'fixture'
=>
__DIR__
.
'/postgres.sql'
,
)
)
)
)
...
...
tests/unit/framework/db/DatabaseTestCase.php
View file @
4602b575
...
@@ -37,6 +37,9 @@ abstract class DatabaseTestCase extends TestCase
...
@@ -37,6 +37,9 @@ abstract class DatabaseTestCase extends TestCase
$db
->
username
=
$this
->
database
[
'username'
];
$db
->
username
=
$this
->
database
[
'username'
];
$db
->
password
=
$this
->
database
[
'password'
];
$db
->
password
=
$this
->
database
[
'password'
];
}
}
if
(
isset
(
$this
->
database
[
'attributes'
]))
{
$db
->
attributes
=
$this
->
database
[
'attributes'
];
}
if
(
$open
)
{
if
(
$open
)
{
$db
->
open
();
$db
->
open
();
$lines
=
explode
(
';'
,
file_get_contents
(
$this
->
database
[
'fixture'
]));
$lines
=
explode
(
';'
,
file_get_contents
(
$this
->
database
[
'fixture'
]));
...
...
tests/unit/framework/db/pgsql/PostgreSQLConnectionTest.php
View file @
4602b575
...
@@ -4,15 +4,48 @@ namespace yiiunit\framework\db\pgsql;
...
@@ -4,15 +4,48 @@ namespace yiiunit\framework\db\pgsql;
use
yiiunit\framework\db\ConnectionTest
;
use
yiiunit\framework\db\ConnectionTest
;
class
PostgreSQLConnectionTest
extends
ConnectionTest
class
PostgreSQLConnectionTest
extends
ConnectionTest
{
{
public
function
setUp
()
public
function
setUp
()
{
{
$this
->
driverName
=
'pgsql'
;
$this
->
driverName
=
'pgsql'
;
parent
::
setUp
();
parent
::
setUp
();
}
}
public
function
testConnection
()
{
public
function
testConnection
()
{
$connection
=
$this
->
getConnection
(
true
);
$connection
=
$this
->
getConnection
(
true
);
}
}
function
testQuoteValue
()
{
$connection
=
$this
->
getConnection
(
false
);
$this
->
assertEquals
(
123
,
$connection
->
quoteValue
(
123
));
$this
->
assertEquals
(
"'string'"
,
$connection
->
quoteValue
(
'string'
));
$this
->
assertEquals
(
"'It''s interesting'"
,
$connection
->
quoteValue
(
"It's interesting"
));
}
function
testQuoteTableName
()
{
$connection
=
$this
->
getConnection
(
false
);
$this
->
assertEquals
(
'"table"'
,
$connection
->
quoteTableName
(
'table'
));
$this
->
assertEquals
(
'"table"'
,
$connection
->
quoteTableName
(
'"table"'
));
$this
->
assertEquals
(
'"schema"."table"'
,
$connection
->
quoteTableName
(
'schema.table'
));
$this
->
assertEquals
(
'"schema"."table"'
,
$connection
->
quoteTableName
(
'schema."table"'
));
$this
->
assertEquals
(
'"schema"."table"'
,
$connection
->
quoteTableName
(
'"schema"."table"'
));
$this
->
assertEquals
(
'{{table}}'
,
$connection
->
quoteTableName
(
'{{table}}'
));
$this
->
assertEquals
(
'(table)'
,
$connection
->
quoteTableName
(
'(table)'
));
}
function
testQuoteColumnName
()
{
$connection
=
$this
->
getConnection
(
false
);
$this
->
assertEquals
(
'"column"'
,
$connection
->
quoteColumnName
(
'column'
));
$this
->
assertEquals
(
'"column"'
,
$connection
->
quoteColumnName
(
'"column"'
));
$this
->
assertEquals
(
'"table"."column"'
,
$connection
->
quoteColumnName
(
'table.column'
));
$this
->
assertEquals
(
'"table"."column"'
,
$connection
->
quoteColumnName
(
'table."column"'
));
$this
->
assertEquals
(
'"table"."column"'
,
$connection
->
quoteColumnName
(
'"table"."column"'
));
$this
->
assertEquals
(
'[[column]]'
,
$connection
->
quoteColumnName
(
'[[column]]'
));
$this
->
assertEquals
(
'{{column}}'
,
$connection
->
quoteColumnName
(
'{{column}}'
));
$this
->
assertEquals
(
'(column)'
,
$connection
->
quoteColumnName
(
'(column)'
));
}
}
}
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