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
f580f0a2
Commit
f580f0a2
authored
May 30, 2010
by
Juozas Kaziukenas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added pdo_sqlsrv, sqlsrv drivers and mssql updates
Signed-off-by:
Juozas Kaziukenas
<
juozas@juokaz.com
>
parent
390717ed
Changes
13
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
1128 additions
and
523 deletions
+1128
-523
Connection.php
lib/Doctrine/DBAL/Driver/PDOMsSql/Connection.php
+12
-17
Connection.php
lib/Doctrine/DBAL/Driver/PDOSqlsrv/Connection.php
+60
-0
Driver.php
lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php
+95
-0
Driver.php
lib/Doctrine/DBAL/Driver/Sqlsrv/Driver.php
+81
-0
SqlsrvConnection.php
lib/Doctrine/DBAL/Driver/Sqlsrv/SqlsrvConnection.php
+130
-0
SqlsrvException.php
lib/Doctrine/DBAL/Driver/Sqlsrv/SqlsrvException.php
+30
-0
SqlsrvStatement.php
lib/Doctrine/DBAL/Driver/Sqlsrv/SqlsrvStatement.php
+249
-0
DriverManager.php
lib/Doctrine/DBAL/DriverManager.php
+2
-0
MsSqlPlatform.php
lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php
+218
-177
SqlsrvPlatform.php
lib/Doctrine/DBAL/Platforms/SqlsrvPlatform.php
+45
-0
MsSqlSchemaManager.php
lib/Doctrine/DBAL/Schema/MsSqlSchemaManager.php
+167
-325
SqlsrvSchemaManager.php
lib/Doctrine/DBAL/Schema/SqlsrvSchemaManager.php
+35
-0
MsSqlPlatformTest.php
tests/Doctrine/Tests/DBAL/Platforms/MsSqlPlatformTest.php
+4
-4
No files found.
lib/Doctrine/DBAL/Driver/PDOMsSql/Connection.php
View file @
f580f0a2
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
...
...
@@ -19,17 +21,17 @@
namespace
Doctrine\DBAL\Driver\PDOMsSql
;
use
PDO
,
Doctrine\DBAL\Driver\Connection
as
DriverConnection
;
/**
* MsSql Connection implementation.
*
* @since 2.0
*/
class
Connection
extends
PDO
implements
Driver
Connection
class
Connection
extends
\PDO
implements
\Doctrine\DBAL\Driver\
Connection
{
/**
* {@inheritdoc}
* Performs the rollback.
*
* @override
*/
public
function
rollback
()
{
...
...
@@ -37,7 +39,9 @@ class Connection extends PDO implements DriverConnection
}
/**
* {@inheritdoc}
* Performs the commit.
*
* @override
*/
public
function
commit
()
{
...
...
@@ -45,21 +49,12 @@ class Connection extends PDO implements DriverConnection
}
/**
* {@inheritdoc}
* Begins a database transaction.
*
* @override
*/
public
function
beginTransaction
()
{
$this
->
exec
(
'BEGIN TRANSACTION'
);
}
/**
* {@inheritdoc}
*/
public
function
lastInsertId
(
$name
=
null
)
{
$stmt
=
$this
->
query
(
'SELECT SCOPE_IDENTITY()'
);
$id
=
$stmt
->
fetchColumn
();
$stmt
->
closeCursor
();
return
$id
;
}
}
\ No newline at end of file
lib/Doctrine/DBAL/Driver/PDOSqlsrv/Connection.php
0 → 100644
View file @
f580f0a2
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\DBAL\Driver\PDOSqlsrv
;
/**
* Sqlsrv Connection implementation.
*
* @since 2.0
*/
class
Connection
extends
\PDO
implements
\Doctrine\DBAL\Driver\Connection
{
/**
* Performs the rollback.
*
* @override
*/
public
function
rollback
()
{
$this
->
exec
(
'ROLLBACK TRANSACTION'
);
}
/**
* Performs the commit.
*
* @override
*/
public
function
commit
()
{
$this
->
exec
(
'COMMIT TRANSACTION'
);
}
/**
* Begins a database transaction.
*
* @override
*/
public
function
beginTransaction
()
{
$this
->
exec
(
'BEGIN TRANSACTION'
);
}
}
\ No newline at end of file
lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php
0 → 100644
View file @
f580f0a2
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\DBAL\Driver\PDOSqlsrv
;
/**
* The PDO-based Sqlsrv driver.
*
* @since 2.0
*/
class
Driver
implements
\Doctrine\DBAL\Driver
{
public
function
connect
(
array
$params
,
$username
=
null
,
$password
=
null
,
array
$driverOptions
=
array
())
{
if
(
isset
(
$params
[
'dbname'
]))
{
$driverOptions
[
'Database'
]
=
$params
[
'dbname'
];
}
return
new
Connection
(
$this
->
_constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
}
/**
* Constructs the Sqlsrv PDO DSN.
*
* @return string The DSN.
*/
private
function
_constructPdoDsn
(
array
$params
)
{
// TODO: This might need to be revisted once we have access to a sql server
$dsn
=
'sqlsrv:('
;
if
(
isset
(
$params
[
'host'
]))
{
$dsn
.=
$params
[
'host'
];
}
$dsn
.=
')'
;
if
(
stripos
(
$dsn
,
'\sqlexpress'
)
!==
false
)
{
$dsn
=
str_ireplace
(
'\sqlexpress'
,
''
,
$dsn
);
$dsn
.=
'\sqlexpress'
;
}
$dsn
=
str_ireplace
(
'localhost'
,
'local'
,
$dsn
);
if
(
isset
(
$params
[
'port'
])
&&
!
empty
(
$params
[
'port'
]))
{
$dsn
.=
','
.
$params
[
'port'
];
}
return
$dsn
;
}
public
function
getDatabasePlatform
()
{
return
new
\Doctrine\DBAL\Platforms\SqlsrvPlatform
();
}
public
function
getSchemaManager
(
\Doctrine\DBAL\Connection
$conn
)
{
return
new
\Doctrine\DBAL\Schema\SqlsrvSchemaManager
(
$conn
);
}
public
function
getName
()
{
return
'pdo_sqlsrv'
;
}
public
function
getDatabase
(
\Doctrine\DBAL\Connection
$conn
)
{
$params
=
$conn
->
getParams
();
return
$params
[
'dbname'
];
}
}
\ No newline at end of file
lib/Doctrine/DBAL/Driver/Sqlsrv/Driver.php
0 → 100644
View file @
f580f0a2
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\DBAL\Driver\Sqlsrv
;
use
Doctrine\DBAL\Platforms
;
/**
* A Doctrine DBAL driver for the Microsoft SQL Native Client PHP extension.
*
* @since 2.0
* @author Juozas Kaziukenas <juozas@juokaz.com>
*/
class
Driver
implements
\Doctrine\DBAL\Driver
{
public
function
connect
(
array
$params
,
$username
=
null
,
$password
=
null
,
array
$driverOptions
=
array
())
{
$serverName
=
''
;
// construct server name
$serverName
=
$params
[
'host'
];
if
(
isset
(
$params
[
'port'
])
&&
!
empty
(
$params
[
'port'
]))
{
$port
=
(
integer
)
$params
[
'port'
];
$serverName
.=
', '
.
$port
;
}
$connectionInfo
=
array
(
'Database'
=>
$params
[
'dbname'
],
);
if
(
isset
(
$username
)
&&
!
empty
(
$username
)
&&
isset
(
$password
)
&&
!
empty
(
$password
))
{
$connectionInfo
+=
array
(
'UID'
=>
$username
,
'PWD'
=>
$password
,
);
}
$connectionInfo
+=
array
(
'ReturnDatesAsStrings'
=>
true
);
return
new
SqlsrvConnection
(
$serverName
,
$connectionInfo
);
}
public
function
getDatabasePlatform
()
{
return
new
\Doctrine\DBAL\Platforms\SqlsrvPlatform
();
}
public
function
getSchemaManager
(
\Doctrine\DBAL\Connection
$conn
)
{
return
new
\Doctrine\DBAL\Schema\SqlsrvSchemaManager
(
$conn
);
}
public
function
getName
()
{
return
'sqlsrv'
;
}
public
function
getDatabase
(
\Doctrine\DBAL\Connection
$conn
)
{
$params
=
$conn
->
getParams
();
return
$params
[
'dbname'
];
}
}
\ No newline at end of file
lib/Doctrine/DBAL/Driver/Sqlsrv/SqlsrvConnection.php
0 → 100644
View file @
f580f0a2
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\DBAL\Driver\Sqlsrv
;
/**
* Sqlsrv implementation of the Connection interface.
*
* @since 2.0
* @author Juozas Kaziukenas <juozas@juokaz.com>
*/
class
SqlsrvConnection
implements
\Doctrine\DBAL\Driver\Connection
{
private
$_dbh
;
public
function
__construct
(
$serverName
,
array
$connectionInfo
)
{
$this
->
_dbh
=
@
sqlsrv_connect
(
$serverName
,
$connectionInfo
);
if
(
!
is_resource
(
$this
->
_dbh
))
{
throw
SqlsrvException
::
fromErrorInfo
(
$this
->
errorInfo
());
}
}
public
function
prepare
(
$prepareString
)
{
return
new
SqlsrvStatement
(
$this
->
_dbh
,
$prepareString
);
}
public
function
query
()
{
$args
=
func_get_args
();
$sql
=
$args
[
0
];
//$fetchMode = $args[1];
$stmt
=
$this
->
prepare
(
$sql
);
$stmt
->
execute
();
return
$stmt
;
}
public
function
quote
(
$input
,
$type
=
\PDO
::
PARAM_STR
)
{
return
is_numeric
(
$input
)
?
$input
:
"'"
.
str_replace
(
"'"
,
"''"
,
$input
)
.
"'"
;
}
public
function
exec
(
$statement
)
{
$stmt
=
$this
->
prepare
(
$statement
);
$stmt
->
execute
();
return
$stmt
->
rowCount
();
}
public
function
lastInsertId
(
$name
=
null
)
{
$id
=
$this
->
query
(
'SELECT @@IDENTITY'
)
->
fetchColumn
();
if
(
!
$id
)
{
return
1
;
}
return
$id
;
}
public
function
beginTransaction
()
{
if
(
!
sqlsrv_begin_transaction
(
$this
->
_dbh
))
{
throw
SqlsrvException
::
fromErrorInfo
(
$this
->
errorInfo
());
}
return
true
;
}
public
function
commit
()
{
if
(
!
sqlsrv_commit
(
$this
->
_dbh
))
{
throw
SqlsrvException
::
fromErrorInfo
(
$this
->
errorInfo
());
}
return
true
;
}
public
function
rollBack
()
{
if
(
!
sqlsrv_rollback
(
$this
->
_dbh
))
{
throw
SqlsrvException
::
fromErrorInfo
(
$this
->
errorInfo
());
}
return
true
;
}
public
function
errorCode
()
{
$errors
=
sqlsrv_errors
();
if
(
false
===
isset
(
$errors
[
0
][
'code'
]))
{
return
null
;
}
$error
=
$errors
[
0
];
return
$error
[
'code'
];
}
public
function
errorInfo
()
{
$errors
=
sqlsrv_errors
();
if
(
false
===
isset
(
$errors
[
0
][
'message'
]))
{
return
null
;
}
return
$errors
[
0
];
}
}
\ No newline at end of file
lib/Doctrine/DBAL/Driver/Sqlsrv/SqlsrvException.php
0 → 100644
View file @
f580f0a2
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\DBAL\Driver\Sqlsrv
;
class
SqlsrvException
extends
\Exception
{
static
public
function
fromErrorInfo
(
$error
)
{
return
new
self
(
$error
[
'message'
],
$error
[
'code'
]);
}
}
lib/Doctrine/DBAL/Driver/Sqlsrv/SqlsrvStatement.php
0 → 100644
View file @
f580f0a2
<?php
/*
* $Id: Interface.php 3882 2008-02-22 18:11:35Z jwage $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\DBAL\Driver\Sqlsrv
;
use
\PDO
;
/**
* The Sql server implementation of the Statement interface.
*
* @since 2.0
* @author Juozas Kaziukenas <juozas@juokaz.com>
*/
class
SqlsrvStatement
implements
\Doctrine\DBAL\Driver\Statement
{
/** Statement handle. */
private
$_sth
;
private
$_dbh
=
null
;
private
$_query
=
null
;
private
static
$fetchStyleMap
=
array
(
PDO
::
FETCH_BOTH
=>
SQLSRV_FETCH_BOTH
,
PDO
::
FETCH_ASSOC
=>
SQLSRV_FETCH_ASSOC
,
PDO
::
FETCH_NUM
=>
SQLSRV_FETCH_NUMERIC
);
private
$_paramMap
=
array
();
private
$_bindParams
=
array
();
/**
* Creates a new SqlsrvStatement that uses the given connection handle and SQL statement.
*
* @param resource $dbh The connection handle.
* @param string $statement The SQL statement.
*/
public
function
__construct
(
$dbh
,
$statement
)
{
$this
->
_dbh
=
$dbh
;
$this
->
_query
=
$this
->
_convertPositionalToNamedPlaceholders
(
$statement
);
}
/**
* Sqlsrv doesnt support bamed params and these should be replaced
* to question marks
*
* @param string $statement The SQL statement to convert.
*/
private
function
_convertPositionalToNamedPlaceholders
(
$statement
)
{
// reset bind params
$this
->
bindParams
=
array
();
// get params count
$param_count
=
substr_count
(
$statement
,
'?'
);
// prepare bind params
if
(
$param_count
>
0
)
{
for
(
$i
=
0
;
$i
<
$param_count
;
$i
++
)
{
// preapre bind param for later usage
$this
->
_bindParams
[
$i
]
=
null
;
}
}
else
{
// parse statement and get named bind params
if
(
preg_match_all
(
'/[= ]:([a-z]+)\b/'
,
$statement
,
$matches
))
{
for
(
$i
=
0
,
$c
=
count
(
$matches
[
1
]);
$i
<
$c
;
$i
++
)
{
// preapre bind param for later usage
$this
->
_bindParams
[
$i
]
=
null
;
// save name for later usage
$this
->
_paramMap
[
$matches
[
1
][
$i
]]
=
$i
+
1
;
// replace to question mark (:name => ?)
$statement
=
str_replace
(
':'
.
$matches
[
1
][
$i
],
'?'
,
$statement
);
}
}
}
return
$statement
;
}
/**
* {@inheritdoc}
*/
public
function
bindValue
(
$param
,
$value
,
$type
=
null
)
{
return
$this
->
bindParam
(
$param
,
$value
,
$type
);
}
/**
* {@inheritdoc}
*/
public
function
bindParam
(
$column
,
&
$variable
,
$type
=
null
)
{
$column
=
isset
(
$this
->
_paramMap
[
$column
])
?
$this
->
_paramMap
[
$column
]
:
$column
;
if
(
$column
>
0
&&
$column
<=
count
(
$this
->
_bindParams
))
{
$this
->
_bindParams
[
$column
-
1
]
=
&
$variable
;
}
else
{
throw
SqlsrvException
::
fromErrorInfo
(
array
(
'message'
=>
"Parameter out of bounds"
));
}
}
/**
* Closes the cursor, enabling the statement to be executed again.
*
* @return boolean Returns TRUE on success or FALSE on failure.
*/
public
function
closeCursor
()
{
return
sqlsrv_free_stmt
(
$this
->
_sth
);
}
/**
* {@inheritdoc}
*/
public
function
columnCount
()
{
return
sqlsrv_num_fields
(
$this
->
_sth
);
}
/**
* {@inheritdoc}
*/
public
function
errorCode
()
{
$errors
=
sqlsrv_errors
();
if
(
false
===
isset
(
$errors
[
0
][
'code'
]))
{
return
null
;
}
$error
=
$errors
[
0
];
return
$error
[
'code'
];
}
/**
* {@inheritdoc}
*/
public
function
errorInfo
()
{
$errors
=
sqlsrv_errors
();
if
(
false
===
isset
(
$errors
[
0
][
'message'
]))
{
return
null
;
}
return
$errors
[
0
];
}
/**
* {@inheritdoc}
*/
public
function
execute
(
$params
=
null
)
{
if
(
$params
)
{
$hasZeroIndex
=
isset
(
$params
[
0
]);
foreach
(
$params
as
$key
=>
$val
)
{
if
(
$hasZeroIndex
&&
is_numeric
(
$key
))
{
$this
->
bindValue
(
$key
+
1
,
$val
);
}
else
{
$this
->
bindValue
(
$key
,
$val
);
}
}
}
// prepare statement
$this
->
_sth
=
sqlsrv_prepare
(
$this
->
_dbh
,
$this
->
_query
,
$this
->
_bindParams
);
if
(
!
$this
->
_sth
)
{
throw
SqlsrvException
::
fromErrorInfo
(
$this
->
errorInfo
());
}
$ret
=
@
sqlsrv_execute
(
$this
->
_sth
);
if
(
!
$ret
)
{
throw
SqlsrvException
::
fromErrorInfo
(
$this
->
errorInfo
());
}
return
$ret
;
}
/**
* {@inheritdoc}
*/
public
function
fetch
(
$fetchStyle
=
PDO
::
FETCH_BOTH
)
{
if
(
!
isset
(
self
::
$fetchStyleMap
[
$fetchStyle
]))
{
throw
new
\InvalidArgumentException
(
"Invalid fetch style: "
.
$fetchStyle
);
}
return
sqlsrv_fetch_array
(
$this
->
_sth
,
self
::
$fetchStyleMap
[
$fetchStyle
]);
}
/**
* {@inheritdoc}
*/
public
function
fetchAll
(
$fetchStyle
=
PDO
::
FETCH_BOTH
)
{
if
(
!
isset
(
self
::
$fetchStyleMap
[
$fetchStyle
]))
{
throw
new
\InvalidArgumentException
(
"Invalid fetch style: "
.
$fetchStyle
);
}
$result
=
array
();
while
(
$row
=
$this
->
fetch
(
$fetchStyle
))
{
$result
[]
=
$row
;
}
return
$result
;
}
/**
* {@inheritdoc}
*/
public
function
fetchColumn
(
$columnIndex
=
0
)
{
return
sqlsrv_get_field
(
$this
->
_sth
,
$columnIndex
);
}
/**
* {@inheritdoc}
*/
public
function
rowCount
()
{
return
sqlsrv_num_rows
(
$this
->
_sth
);
}
}
\ No newline at end of file
lib/Doctrine/DBAL/DriverManager.php
View file @
f580f0a2
...
...
@@ -44,6 +44,8 @@ final class DriverManager
'oci8'
=>
'Doctrine\DBAL\Driver\OCI8\Driver'
,
'ibm_db2'
=>
'Doctrine\DBAL\Driver\IBMDB2\DB2Driver'
,
'pdo_ibm'
=>
'Doctrine\DBAL\Driver\PDOIbm\Driver'
,
'pdo_sqlsrv'
=>
'Doctrine\DBAL\Driver\PDOSqlsrv\Driver'
,
'sqlsrv'
=>
'Doctrine\DBAL\Driver\Sqlsrv\Driver'
,
);
/** Private constructor. This class cannot be instantiated. */
...
...
lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php
View file @
f580f0a2
...
...
@@ -21,6 +21,7 @@ namespace Doctrine\DBAL\Platforms;
use
Doctrine\DBAL\Schema\TableDiff
;
use
Doctrine\DBAL\DBALException
;
use
Doctrine\DBAL\Schema\Index
;
/**
* The MsSqlPlatform provides the behavior, features and SQL dialect of the
...
...
@@ -35,50 +36,79 @@ use Doctrine\DBAL\DBALException;
class
MsSqlPlatform
extends
AbstractPlatform
{
/**
* Adds an adapter-specific LIMIT clause to the SELECT statement.
* [ borrowed from Zend Framework ]
* Whether the platform prefers identity columns for ID generation.
* MsSql prefers "autoincrement" identity columns since sequences can only
* be emulated with a table.
*
* @param string $query
* @param mixed $limit
* @param mixed $offset
* @link http://lists.bestpractical.com/pipermail/rt-devel/2005-June/007339.html
* @return string
* @return boolean
* @override
*/
public
function
writeLimitClause
(
$query
,
$limit
=
false
,
$offset
=
false
)
public
function
prefersIdentityColumns
(
)
{
if
(
$limit
>
0
)
{
$count
=
intval
(
$limit
);
$offset
=
intval
(
$offset
);
if
(
$offset
<
0
)
{
throw
DBALException
::
limitOffsetInvalid
(
$offset
);
return
true
;
}
$orderby
=
stristr
(
$query
,
'ORDER BY'
);
if
(
$orderby
!==
false
)
{
$sort
=
(
stripos
(
$orderby
,
'desc'
)
!==
false
)
?
'desc'
:
'asc'
;
$order
=
str_ireplace
(
'ORDER BY'
,
''
,
$orderby
);
$order
=
trim
(
preg_replace
(
'/ASC|DESC/i'
,
''
,
$order
));
/**
* Whether the platform supports identity columns.
* MsSql supports this through AUTO_INCREMENT columns.
*
* @return boolean
* @override
*/
public
function
supportsIdentityColumns
()
{
return
true
;
}
$query
=
preg_replace
(
'/^SELECT\s/i'
,
'SELECT TOP '
.
(
$count
+
$offset
)
.
' '
,
$query
);
/**
* Whether the platform supports savepoints. MsSql does not.
*
* @return boolean
* @override
*/
public
function
supportsSavepoints
()
{
return
false
;
}
$query
=
'SELECT * FROM (SELECT TOP '
.
$count
.
' * FROM ('
.
$query
.
') AS inner_tbl'
;
if
(
$orderby
!==
false
)
{
$query
.=
' ORDER BY '
.
$order
.
' '
;
$query
.=
(
stripos
(
$sort
,
'asc'
)
!==
false
)
?
'DESC'
:
'ASC'
;
/**
* create a new database
*
* @param string $name name of the database that should be created
* @return string
* @override
*/
public
function
getCreateDatabaseSQL
(
$name
)
{
return
'CREATE DATABASE '
.
$name
;
}
$query
.=
') AS outer_tbl'
;
if
(
$orderby
!==
false
)
{
$query
.=
' ORDER BY '
.
$order
.
' '
.
$sort
;
/**
* drop an existing database
*
* @param string $name name of the database that should be dropped
* @return string
* @override
*/
public
function
getDropDatabaseSQL
(
$name
)
{
return
'DROP DATABASE '
.
$name
;
}
return
$query
;
/**
* @override
*/
public
function
getDropForeignKeySQL
(
$foreignKey
,
$table
)
{
if
(
$foreignKey
instanceof
\Doctrine\DBAL\Schema\ForeignKeyConstraint
)
{
$foreignKey
=
$foreignKey
->
getName
();
}
if
(
$table
instanceof
\Doctrine\DBAL\Schema\Table
)
{
$table
=
$table
->
getName
();
}
return
$quer
y
;
return
'ALTER TABLE '
.
$table
.
' DROP CONSTRAINT '
.
$foreignKe
y
;
}
/**
...
...
@@ -125,64 +155,75 @@ class MsSqlPlatform extends AbstractPlatform
}
/**
* Returns the regular expression operator.
*
* @return string
* @override
*/
public
function
get
RegexpExpression
(
)
public
function
get
EmptyIdentityInsertSQL
(
$quotedTableName
,
$quotedIdentifierColumnName
)
{
return
'
RLIKE
'
;
return
'
INSERT INTO '
.
$quotedTableName
.
' DEFAULT VALUES
'
;
}
/**
* Return string to call a variable with the current timestamp inside an SQL statement
* There are three special variables for current date and time:
* - CURRENT_TIMESTAMP (date and time, TIMESTAMP type)
* - CURRENT_DATE (date, DATE type)
* - CURRENT_TIME (time, TIME type)
*
* @return string to call a variable with the current timestamp
* @override
*/
public
function
get
NowExpression
(
$type
=
'timestamp'
)
public
function
get
ShowDatabasesSQL
(
)
{
switch
(
$type
)
{
case
'time'
:
case
'date'
:
case
'timestamp'
:
default
:
return
'GETDATE()'
;
return
'SHOW DATABASES'
;
}
/**
* @override
*/
public
function
getListTablesSQL
()
{
return
"SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"
;
}
/**
* return string to call a function to get a substring inside an SQL statement
*
* @return string to call a function to get a substring
* @override
*/
public
function
get
SubstringExpression
(
$value
,
$position
,
$length
=
null
)
public
function
get
ListTableColumnsSQL
(
$table
)
{
if
(
!
is_null
(
$length
))
{
return
'SUBSTRING('
.
$value
.
', '
.
$position
.
', '
.
$length
.
')'
;
return
'exec sp_columns @table_name = '
.
$table
;
}
return
'SUBSTRING('
.
$value
.
', '
.
$position
.
', LEN('
.
$value
.
') - '
.
$position
.
' + 1)'
;
/**
* @override
*/
public
function
getListTableForeignKeysSQL
(
$table
,
$database
=
null
)
{
return
"SELECT f.name AS ForeignKey,
SCHEMA_NAME (f.SCHEMA_ID) AS SchemaName,
OBJECT_NAME (f.parent_object_id) AS TableName,
COL_NAME (fc.parent_object_id,fc.parent_column_id) AS ColumnName,
SCHEMA_NAME (o.SCHEMA_ID) ReferenceSchemaName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS ReferenceColumnName,
f.delete_referential_action_desc,
f.update_referential_action_desc
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
INNER JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id
ON f.OBJECT_ID = fc.constraint_object_id
WHERE OBJECT_NAME (f.parent_object_id) = '"
.
$table
.
"'"
;
}
/**
* @override
*/
public
function
getListTableIndexesSQL
(
$table
)
{
return
"exec sp_helpindex '"
.
$table
.
"'"
;
}
/**
* Returns
string to concatenate two or more string parameters
* Returns
the regular expression operator.
*
* @param string $arg1
* @param string $arg2
* @param string $values...
* @return string to concatenate two strings
* @return string
* @override
*/
public
function
get
Concat
Expression
()
public
function
get
Regexp
Expression
()
{
$args
=
func_get_args
();
return
'('
.
implode
(
' + '
,
$args
)
.
')'
;
return
'RLIKE'
;
}
/**
...
...
@@ -193,79 +234,80 @@ class MsSqlPlatform extends AbstractPlatform
*/
public
function
getGuidExpression
()
{
return
'
NEW
ID()'
;
return
'
UU
ID()'
;
}
/**
* Whether the platform prefers identity columns for ID generation.
* MsSql prefers "autoincrement" identity columns since sequences can only
* be emulated with a table.
*
* @return boolean
* @override
*/
public
function
prefersIdentityColumns
(
)
public
function
getLocateExpression
(
$str
,
$substr
,
$startPos
=
false
)
{
return
true
;
if
(
$startPos
==
false
)
{
return
'CHARINDEX('
.
$substr
.
', '
.
$str
.
')'
;
}
else
{
return
'CHARINDEX('
.
$substr
.
', '
.
$str
.
', '
.
$startPos
.
')'
;
}
}
/**
* Whether the platform supports identity columns.
* MsSql supports this through AUTO_INCREMENT columns.
*
* @return boolean
* @override
*/
public
function
supportsIdentityColumns
(
)
public
function
getModExpression
(
$expression1
,
$expression2
)
{
return
true
;
return
$expression1
.
' % '
.
$expression2
;
}
/**
* Whether the platform supports savepoints. MsSql does not.
*
* @return boolean
* @override
*/
public
function
supportsSavepoints
(
)
public
function
getTrimExpression
(
$str
,
$pos
=
self
::
TRIM_UNSPECIFIED
,
$char
=
false
)
{
return
false
;
// @todo
$trimFn
=
''
;
$trimChar
=
(
$char
!=
false
)
?
(
', '
.
$char
)
:
''
;
if
(
$pos
==
self
::
TRIM_LEADING
)
{
$trimFn
=
'LTRIM'
;
}
else
if
(
$pos
==
self
::
TRIM_TRAILING
)
{
$trimFn
=
'RTRIM'
;
}
else
{
return
'LTRIM(RTRIM('
.
$str
.
'))'
;
}
public
function
getShowDatabasesSQL
()
{
return
'SHOW DATABASES'
;
return
$trimFn
.
'('
.
$str
.
')'
;
}
public
function
getListTablesSQL
()
/**
* @override
*/
public
function
getConcatExpression
()
{
return
'SHOW TABLES'
;
$args
=
func_get_args
();
return
'('
.
implode
(
' + '
,
$args
)
.
')'
;
}
/**
* create a new database
*
* @param string $name name of the database that should be created
* @return string
* @override
*/
public
function
get
CreateDatabaseSQL
(
$name
)
public
function
get
SubstringExpression
(
$value
,
$from
,
$len
=
null
)
{
return
'CREATE DATABASE '
.
$name
;
if
(
!
is_null
(
$len
))
{
return
'SUBSTRING('
.
$value
.
', '
.
$from
.
', '
.
$len
.
')'
;
}
return
'SUBSTRING('
.
$value
.
', '
.
$from
.
', LEN('
.
$value
.
') - '
.
$from
.
' + 1)'
;
}
/**
* drop an existing database
*
* @param string $name name of the database that should be dropped
* @return string
* @override
*/
public
function
get
DropDatabaseSQL
(
$name
)
public
function
get
LengthExpression
(
$column
)
{
return
'
DROP DATABASE '
.
$name
;
return
'
LEN('
.
$column
.
')'
;
}
/**
* @override
*/
public
function
getSetTransactionIsolationSQL
(
$level
)
{
return
'SET TRANSACTION ISOLATION LEVEL '
.
$this
->
_getTransactionIsolationLevelSQL
(
$level
);
...
...
@@ -295,6 +337,7 @@ class MsSqlPlatform extends AbstractPlatform
return
'SMALLINT'
.
$this
->
_getCommonIntegerTypeDeclarationSQL
(
$field
);
}
/** @override */
public
function
getVarcharTypeDeclarationSQL
(
array
$field
)
{
if
(
!
isset
(
$field
[
'length'
]))
{
...
...
@@ -325,7 +368,7 @@ class MsSqlPlatform extends AbstractPlatform
{
$autoinc
=
''
;
if
(
!
empty
(
$columnDef
[
'autoincrement'
]))
{
$autoinc
=
'
AUTO_INCREMENT
'
;
$autoinc
=
'
IDENTITY
'
;
}
$unsigned
=
(
isset
(
$columnDef
[
'unsigned'
])
&&
$columnDef
[
'unsigned'
])
?
' UNSIGNED'
:
''
;
...
...
@@ -337,7 +380,8 @@ class MsSqlPlatform extends AbstractPlatform
*/
public
function
getDateTimeTypeDeclarationSQL
(
array
$fieldDeclaration
)
{
return
'CHAR('
.
strlen
(
'YYYY-MM-DD HH:MM:SS'
)
.
')'
;
// 6 - microseconds precision length
return
'DATETIME2(6)'
;
}
/**
...
...
@@ -345,7 +389,7 @@ class MsSqlPlatform extends AbstractPlatform
*/
public
function
getDateTypeDeclarationSQL
(
array
$fieldDeclaration
)
{
return
'
CHAR('
.
strlen
(
'YYYY-MM-DD'
)
.
')
'
;
return
'
DATE
'
;
}
/**
...
...
@@ -353,7 +397,7 @@ class MsSqlPlatform extends AbstractPlatform
*/
public
function
getTimeTypeDeclarationSQL
(
array
$fieldDeclaration
)
{
return
'
CHAR('
.
strlen
(
'HH:MM:SS'
)
.
')
'
;
return
'
TIME
'
;
}
/**
...
...
@@ -364,16 +408,6 @@ class MsSqlPlatform extends AbstractPlatform
return
'BIT'
;
}
/**
* Get the platform name for this instance
*
* @return string
*/
public
function
getName
()
{
return
'mssql'
;
}
/**
* Adds an adapter-specific LIMIT clause to the SELECT statement.
*
...
...
@@ -463,51 +497,58 @@ class MsSqlPlatform extends AbstractPlatform
}
/**
* Get the insert sql for an empty insert statement
*
* @param string $tableName
* @param string $identifierColumnName
* @return string $sql
* @override
*/
public
function
getEmptyIdentityInsertSQL
(
$quotedTableName
,
$quotedIdentifierColumnName
)
public
function
convertBooleans
(
$item
)
{
return
'INSERT INTO '
.
$quotedTableName
.
' DEFAULT VALUES'
;
if
(
is_array
(
$item
))
{
foreach
(
$item
as
$key
=>
$value
)
{
if
(
is_bool
(
$value
)
||
is_numeric
(
$item
))
{
$item
[
$key
]
=
(
$value
)
?
'TRUE'
:
'FALSE'
;
}
}
}
else
{
if
(
is_bool
(
$item
)
||
is_numeric
(
$item
))
{
$item
=
(
$item
)
?
'TRUE'
:
'FALSE'
;
}
}
return
$item
;
}
/**
* @
inheritdoc
* @
override
*/
public
function
get
TruncateTableSQL
(
$tableName
,
$cascade
=
false
)
public
function
get
CreateTemporaryTableSnippetSQL
(
)
{
return
'TRUNCATE TABLE '
.
$tableName
;
// @todo change to be a real temporary table
return
"CREATE TABLE"
;
}
/**
* MsSql uses Table Hints for locking strategies instead of the ANSI SQL FOR UPDATE like hints.
*
* @return string
* @override
*/
public
function
get
ForUpdateSQL
()
public
function
get
DateTimeFormatString
()
{
return
''
;
return
'
Y-m-d H:i:s.u
'
;
}
/**
* @license LGPL
* @author Hibernate
* @param string $fromClause
* @param int $lockMode
* @return string
* @override
*/
public
function
appendLockHint
(
$fromClause
,
$lockMode
)
public
function
getIndexDeclarationSQL
(
$name
,
Index
$index
)
{
if
(
$lockMode
==
\Doctrine\DBAL\LockMode
::
PESSIMISTIC_WRITE
)
{
return
$fromClause
.
" WITH (UPDLOCK, ROWLOCK)"
;
}
else
if
(
$lockMode
==
\Doctrine\DBAL\LockMode
::
PESSIMISTIC_READ
)
{
return
$fromClause
.
" WITH (HOLDLOCK, ROWLOCK)"
;
}
else
{
return
$fromClause
;
// @todo
return
$this
->
getUniqueConstraintDeclarationSQL
(
$name
,
$index
);
}
/**
* Get the platform name for this instance
*
* @return string
*/
public
function
getName
()
{
return
'mssql'
;
}
protected
function
initializeDoctrineTypeMappings
()
...
...
lib/Doctrine/DBAL/Platforms/SqlsrvPlatform.php
0 → 100644
View file @
f580f0a2
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\DBAL\Platforms
;
use
Doctrine\DBAL\Schema\TableDiff
;
use
Doctrine\DBAL\DBALException
;
/**
* The SqlsrvPlatform provides the behavior, features and SQL dialect of the
* Microsoft SQL database platform.
*
* @since 2.0
* @author Juozas Kaziukenas <juozas@juokaz.com>
*/
class
SqlsrvPlatform
extends
MsSqlPlatform
{
/**
* Get the platform name for this instance
*
* @return string
*/
public
function
getName
()
{
return
'sqlsrv'
;
}
}
lib/Doctrine/DBAL/Schema/MsSqlSchemaManager.php
View file @
f580f0a2
...
...
@@ -27,373 +27,215 @@ namespace Doctrine\DBAL\Schema;
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
* @author Juozas Kaziukenas <juozas@juokaz.com>
* @version $Revision$
* @since 2.0
*/
class
MsSqlSchemaManager
extends
AbstractSchemaManager
{
/**
* create a new database
*
* @param string $name name of the database that should be created
* @return void
*/
public
function
createDatabase
(
$name
)
{
$query
=
"CREATE DATABASE
$name
"
;
if
(
$this
->
conn
->
options
[
'database_device'
])
{
$query
.=
' ON '
.
$this
->
conn
->
options
[
'database_device'
];
$query
.=
$this
->
conn
->
options
[
'database_size'
]
?
'='
.
$this
->
conn
->
options
[
'database_size'
]
:
''
;
}
return
$this
->
conn
->
standaloneQuery
(
$query
,
null
,
true
);
}
/**
* drop an existing database
*
* @param string $name name of the database that should be dropped
* @return void
* @override
*/
p
ublic
function
dropDatabase
(
$name
)
p
rotected
function
_getPortableTableColumnDefinition
(
$tableColumn
)
{
return
$this
->
conn
->
standaloneQuery
(
'DROP DATABASE '
.
$name
,
null
,
true
);
}
$dbType
=
strtolower
(
$tableColumn
[
'TYPE_NAME'
]);
/**
* alter an existing table
*
* @param string $name name of the table that is intended to be changed.
* @param array $changes associative array that contains the details of each type
* of change that is intended to be performed. The types of
* changes that are currently supported are defined as follows:
*
* name
*
* New name for the table.
*
* add
*
* Associative array with the names of fields to be added as
* indexes of the array. The value of each entry of the array
* should be set to another associative array with the properties
* of the fields to be added. The properties of the fields should
* be the same as defined by the Metabase parser.
*
*
* remove
*
* Associative array with the names of fields to be removed as indexes
* of the array. Currently the values assigned to each entry are ignored.
* An empty array should be used for future compatibility.
*
* rename
*
* Associative array with the names of fields to be renamed as indexes
* of the array. The value of each entry of the array should be set to
* another associative array with the entry named name with the new
* field name and the entry named Declaration that is expected to contain
* the portion of the field declaration already in DBMS specific SQL code
* as it is used in the CREATE TABLE statement.
*
* change
*
* Associative array with the names of the fields to be changed as indexes
* of the array. Keep in mind that if it is intended to change either the
* name of a field and any other properties, the change array entries
* should have the new names of the fields as array indexes.
*
* The value of each entry of the array should be set to another associative
* array with the properties of the fields to that are meant to be changed as
* array entries. These entries should be assigned to the new values of the
* respective properties. The properties of the fields should be the same
* as defined by the Metabase parser.
*
* Example
* array(
* 'name' => 'userlist',
* 'add' => array(
* 'quota' => array(
* 'type' => 'integer',
* 'unsigned' => 1
* )
* ),
* 'remove' => array(
* 'file_limit' => array(),
* 'time_limit' => array()
* ),
* 'change' => array(
* 'name' => array(
* 'length' => '20',
* 'definition' => array(
* 'type' => 'text',
* 'length' => 20,
* ),
* )
* ),
* 'rename' => array(
* 'sex' => array(
* 'name' => 'gender',
* 'definition' => array(
* 'type' => 'text',
* 'length' => 1,
* 'default' => 'M',
* ),
* )
* )
* )
*
* @param boolean $check indicates whether the function should just check if the DBMS driver
* can perform the requested table alterations if the value is true or
* actually perform them otherwise.
* @return void
*/
public
function
alterTable
(
$name
,
array
$changes
,
$check
=
false
)
{
foreach
(
$changes
as
$changeName
=>
$change
)
{
switch
(
$changeName
)
{
case
'add'
:
break
;
case
'remove'
:
break
;
case
'name'
:
case
'rename'
:
case
'change'
:
default
:
throw
SchemaException
::
alterTableChangeNotSupported
(
$changeName
);
}
if
(
stripos
(
$dbType
,
'identity'
))
{
$dbType
=
trim
(
str_ireplace
(
'identity'
,
''
,
$dbType
));
}
$query
=
''
;
if
(
!
empty
(
$changes
[
'add'
])
&&
is_array
(
$changes
[
'add'
]))
{
foreach
(
$changes
[
'add'
]
as
$fieldName
=>
$field
)
{
if
(
$query
)
{
$query
.=
', '
;
}
$query
.=
'ADD '
.
$this
->
getDeclaration
(
$fieldName
,
$field
);
}
}
$type
=
array
();
$unsigned
=
$fixed
=
null
;
if
(
!
empty
(
$changes
[
'remove'
])
&&
is_array
(
$changes
[
'remove'
]))
{
foreach
(
$changes
[
'remove'
]
as
$fieldName
=>
$field
)
{
if
(
$query
)
{
$query
.=
', '
;
}
$query
.=
'DROP COLUMN '
.
$fieldName
;
}
if
(
!
isset
(
$tableColumn
[
'name'
]))
{
$tableColumn
[
'name'
]
=
''
;
}
if
(
!
$query
)
{
return
false
;
// Map db type to Doctrine mapping type
switch
(
$dbType
)
{
case
'tinyint'
:
$type
=
'boolean'
;
break
;
case
'smallint'
:
$type
=
'smallint'
;
break
;
case
'mediumint'
:
$type
=
'integer'
;
break
;
case
'int'
:
case
'integer'
:
$type
=
'integer'
;
break
;
case
'bigint'
:
$type
=
'bigint'
;
break
;
case
'tinytext'
:
case
'mediumtext'
:
case
'longtext'
:
case
'text'
:
$type
=
'text'
;
$fixed
=
false
;
break
;
case
'varchar'
:
$fixed
=
false
;
case
'string'
:
case
'char'
:
$type
=
'string'
;
if
(
$tableColumn
[
'LENGTH'
]
==
'1'
)
{
$type
=
'boolean'
;
if
(
preg_match
(
'/^(is|has)/'
,
$tableColumn
[
'name'
]))
{
$type
=
array_reverse
(
$type
);
}
return
$this
->
conn
->
exec
(
'ALTER TABLE '
.
$name
.
' '
.
$query
);
}
else
if
(
strstr
(
$dbType
,
'text'
))
{
$type
=
'text'
;
if
(
$decimal
==
'binary'
)
{
$type
=
'blob'
;
}
/**
* {@inheritdoc}
*/
public
function
createSequence
(
$seqName
,
$start
=
1
,
$allocationSize
=
1
)
{
$seqcolName
=
'seq_col'
;
$query
=
'CREATE TABLE '
.
$seqName
.
' ('
.
$seqcolName
.
' INT PRIMARY KEY CLUSTERED IDENTITY('
.
$start
.
', 1) NOT NULL)'
;
$res
=
$this
->
conn
->
exec
(
$query
);
if
(
$start
==
1
)
{
return
true
;
}
try
{
$query
=
'SET IDENTITY_INSERT '
.
$sequenceName
.
' ON '
.
'INSERT INTO '
.
$sequenceName
.
' ('
.
$seqcolName
.
') VALUES ( '
.
$start
.
')'
;
$res
=
$this
->
conn
->
exec
(
$query
);
}
catch
(
Exception
$e
)
{
$result
=
$this
->
conn
->
exec
(
'DROP TABLE '
.
$sequenceName
);
if
(
$fixed
!==
false
)
{
$fixed
=
true
;
}
return
true
;
break
;
case
'set'
:
$fixed
=
false
;
$type
=
'text'
;
$type
=
'integer'
;
//FIXME:???
break
;
case
'date'
:
$type
=
'date'
;
break
;
case
'datetime'
:
case
'timestamp'
:
$type
=
'datetime'
;
break
;
case
'time'
:
$type
=
'time'
;
break
;
case
'float'
:
case
'double'
:
case
'real'
:
case
'numeric'
:
case
'decimal'
:
$type
=
'decimal'
;
break
;
case
'tinyblob'
:
case
'mediumblob'
:
case
'longblob'
:
case
'blob'
:
case
'binary'
:
case
'varbinary'
:
$type
=
'blob'
;
break
;
case
'year'
:
$type
=
'integer'
;
$type
=
'date'
;
break
;
case
'geometry'
:
case
'geometrycollection'
:
case
'point'
:
case
'multipoint'
:
case
'linestring'
:
case
'multilinestring'
:
case
'polygon'
:
case
'multipolygon'
:
$type
=
'blob'
;
break
;
default
:
$type
=
'string'
;
}
/**
* This function drops an existing sequence
*
* @param string $seqName name of the sequence to be dropped
* @return void
*/
public
function
dropSequenceSql
(
$seqName
)
{
return
'DROP TABLE '
.
$seqName
;
}
$def
=
array
(
'type'
=>
$type
,
'length'
=>
((
int
)
$tableColumn
[
'LENGTH'
]
==
0
)
?
null
:
(
int
)
$tableColumn
[
'LENGTH'
],
'unsigned'
=>
(
bool
)
$unsigned
,
'fixed'
=>
(
bool
)
$fixed
);
/**
* lists all database sequences
*
* @param string|null $database
* @return array
*/
public
function
listSequences
(
$database
=
null
)
{
$query
=
"SELECT name FROM sysobjects WHERE xtype = 'U'"
;
$tableNames
=
$this
->
conn
->
fetchColumn
(
$query
);
// @todo
$options
=
array
(
'length'
=>
((
int
)
$tableColumn
[
'LENGTH'
]
==
0
)
?
null
:
(
int
)
$tableColumn
[
'LENGTH'
],
'unsigned'
=>
(
bool
)
$unsigned
,
'fixed'
=>
(
bool
)
$fixed
,
'default'
=>
$tableColumn
[
'COLUMN_DEF'
]
!==
'(NULL)'
?
$tableColumn
[
'COLUMN_DEF'
]
:
null
,
'notnull'
=>
(
bool
)
(
$tableColumn
[
'IS_NULLABLE'
]
!=
'YES'
),
'scale'
=>
$tableColumn
[
'SCALE'
],
'precision'
=>
$tableColumn
[
'PRECISION'
],
'platformOptions'
=>
array
(
'primary'
=>
false
,
'unique'
=>
false
,
'autoincrement'
=>
false
,
),
);
return
array_map
(
array
(
$this
->
conn
->
formatter
,
'fixSequenceName'
),
$tableName
s
);
return
new
Column
(
$tableColumn
[
'COLUMN_NAME'
],
\Doctrine\DBAL\Types\Type
::
getType
(
$type
),
$option
s
);
}
/**
* lists table constraints
*
* @param string $table database table name
* @return array
* @override
*/
p
ublic
function
listTableColumns
(
$table
)
p
rotected
function
_getPortableTableIndexesList
(
$tableIndexRows
,
$tableName
=
null
)
{
$sql
=
'EXEC sp_columns @table_name = '
.
$table
;
$result
=
$this
->
conn
->
fetchAssoc
(
$sql
);
$columns
=
array
();
foreach
(
$result
as
$key
=>
$val
)
{
$val
=
array_change_key_case
(
$val
,
CASE_LOWER
);
if
(
strstr
(
$val
[
'type_name'
],
' '
))
{
list
(
$type
,
$identity
)
=
explode
(
' '
,
$val
[
'type_name'
]);
}
else
{
$type
=
$val
[
'type_name'
];
$identity
=
''
;
}
if
(
$type
==
'varchar'
)
{
$type
.=
'('
.
$val
[
'length'
]
.
')'
;
$result
=
array
();
foreach
(
$tableIndexRows
AS
$tableIndex
)
{
$indexName
=
$keyName
=
$tableIndex
[
'index_name'
];
if
(
strpos
(
$tableIndex
[
'index_description'
],
'primary key'
)
!==
false
)
{
$keyName
=
'primary'
;
}
$keyName
=
strtolower
(
$keyName
);
$val
[
'type'
]
=
$type
;
$val
[
'identity'
]
=
$identity
;
$decl
=
$this
->
conn
->
getDatabasePlatform
()
->
getPortableDeclaration
(
$val
);
$description
=
array
(
'name'
=>
$val
[
'column_name'
],
'ntype'
=>
$type
,
'type'
=>
$decl
[
'type'
][
0
],
'alltypes'
=>
$decl
[
'type'
],
'length'
=>
$decl
[
'length'
],
'fixed'
=>
$decl
[
'fixed'
],
'unsigned'
=>
$decl
[
'unsigned'
],
'notnull'
=>
(
bool
)
(
trim
(
$val
[
'is_nullable'
])
===
'NO'
),
'default'
=>
$val
[
'column_def'
],
'primary'
=>
(
strtolower
(
$identity
)
==
'identity'
),
$result
[
$keyName
]
=
array
(
'name'
=>
$indexName
,
'columns'
=>
explode
(
', '
,
$tableIndex
[
'index_keys'
]),
'unique'
=>
strpos
(
$tableIndex
[
'index_description'
],
'unique'
)
!==
false
,
'primary'
=>
strpos
(
$tableIndex
[
'index_description'
],
'primary key'
)
!==
false
,
);
$columns
[
$val
[
'column_name'
]]
=
$description
;
}
return
$columns
;
$indexes
=
array
();
foreach
(
$result
AS
$indexKey
=>
$data
)
{
$indexes
[
$indexKey
]
=
new
Index
(
$data
[
'name'
],
$data
[
'columns'
],
$data
[
'unique'
],
$data
[
'primary'
]);
}
/**
* lists table constraints
*
* @param string $table database table name
* @return array
*/
public
function
listTableIndexes
(
$table
)
{
return
$indexes
;
}
/**
* lists tables
*
* @param string|null $database
* @return array
* @override
*/
public
function
listTables
(
$database
=
null
)
public
function
_getPortableTableForeignKeyDefinition
(
$tableForeignKey
)
{
$sql
=
"SELECT name FROM sysobjects WHERE type = 'U' AND name <> 'dtproperties' ORDER BY name"
;
return
$this
->
conn
->
fetchColumn
(
$sql
);
return
new
ForeignKeyConstraint
(
(
array
)
$tableForeignKey
[
'ColumnName'
],
$tableForeignKey
[
'ReferenceTableName'
],
(
array
)
$tableForeignKey
[
'ReferenceColumnName'
],
$tableForeignKey
[
'ForeignKey'
],
array
(
'onUpdate'
=>
str_replace
(
'_'
,
' '
,
$tableForeignKey
[
'update_referential_action_desc'
]),
'onDelete'
=>
str_replace
(
'_'
,
' '
,
$tableForeignKey
[
'delete_referential_action_desc'
]),
)
);
}
/**
* lists all triggers
*
* @return array
* @override
*/
p
ublic
function
listTriggers
(
$database
=
null
)
p
rotected
function
_getPortableTableDefinition
(
$table
)
{
$query
=
"SELECT name FROM sysobjects WHERE xtype = 'TR'"
;
$result
=
$this
->
conn
->
fetchColumn
(
$query
);
return
$result
;
return
$table
[
'name'
];
}
/**
* lists table triggers
*
* @param string $table database table name
* @return array
* @override
*/
public
function
listTableTriggers
(
$tabl
e
)
public
function
dropDatabase
(
$nam
e
)
{
$table
=
$this
->
conn
->
quote
(
$table
,
'text'
);
$query
=
"SELECT name FROM sysobjects WHERE xtype = 'TR' AND object_name(parent_obj) = "
.
$table
;
$result
=
$this
->
conn
->
fetchColumn
(
$query
);
return
$result
;
}
/**
* lists table views
*
* @param string $table database table name
* @return array
*/
public
function
listTableViews
(
$table
)
try
{
$keyName
=
'INDEX_NAME'
;
$pkName
=
'PK_NAME'
;
if
(
$this
->
conn
->
getAttribute
(
Doctrine
::
ATTR_PORTABILITY
)
&
Doctrine
::
PORTABILITY_FIX_CASE
)
{
if
(
$this
->
conn
->
getAttribute
(
Doctrine
::
ATTR_FIELD_CASE
)
==
CASE_LOWER
)
{
$keyName
=
strtolower
(
$keyName
);
$pkName
=
strtolower
(
$pkName
);
}
else
{
$keyName
=
strtoupper
(
$keyName
);
$pkName
=
strtoupper
(
$pkName
);
return
parent
::
dropDatabase
(
$name
);
}
}
$table
=
$this
->
conn
->
quote
(
$table
,
'text'
);
$query
=
'EXEC sp_statistics @table_name = '
.
$table
;
$indexes
=
$this
->
conn
->
fetchColumn
(
$query
,
$keyName
);
$query
=
'EXEC sp_pkeys @table_name = '
.
$table
;
$pkAll
=
$this
->
conn
->
fetchColumn
(
$query
,
$pkName
);
$result
=
array
();
foreach
(
$indexes
as
$index
)
{
if
(
!
in_array
(
$index
,
$pkAll
)
&&
$index
!=
null
)
{
$result
[]
=
$this
->
conn
->
formatter
->
fixIndexName
(
$index
);
}
}
return
$result
;
}
/**
* lists database views
*
* @param string|null $database
* @return array
*/
public
function
listViews
(
$database
=
null
)
catch
(
\Exception
$e
)
{
$query
=
"SELECT name FROM sysobjects WHERE xtype = 'V'"
;
return
$this
->
conn
->
fetchColumn
(
$query
);
print
$e
->
getMessage
();
}
protected
function
_getPortableTableColumnDefinition
(
$tableColumn
)
{
return
$column
;
}
}
\ No newline at end of file
lib/Doctrine/DBAL/Schema/SqlsrvSchemaManager.php
0 → 100644
View file @
f580f0a2
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.org>.
*/
namespace
Doctrine\DBAL\Schema
;
/**
* xxx
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Juozas Kaziukenas <juozas@juokaz.com>
* @version $Revision$
* @since 2.0
*/
class
SqlsrvSchemaManager
extends
MsSqlSchemaManager
{
}
\ No newline at end of file
tests/Doctrine/Tests/DBAL/Platforms/MsSqlPlatformTest.php
View file @
f580f0a2
...
...
@@ -16,13 +16,13 @@ class MsSqlPlatformTest extends AbstractPlatformTestCase
public
function
getGenerateTableSql
()
{
return
'CREATE TABLE test (id INT
AUTO_INCREMENT
NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))'
;
return
'CREATE TABLE test (id INT
IDENTITY
NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))'
;
}
public
function
getGenerateTableWithMultiColumnUniqueIndexSql
()
{
return
array
(
'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL,
UNIQUE INDEX test_foo_bar_uniq
(foo, bar))'
'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL,
CONSTRAINT test_foo_bar_uniq UNIQUE
(foo, bar))'
);
}
...
...
@@ -75,11 +75,11 @@ class MsSqlPlatformTest extends AbstractPlatformTestCase
$this
->
_platform
->
getIntegerTypeDeclarationSQL
(
array
())
);
$this
->
assertEquals
(
'INT
AUTO_INCREMENT
'
,
'INT
IDENTITY
'
,
$this
->
_platform
->
getIntegerTypeDeclarationSQL
(
array
(
'autoincrement'
=>
true
)
));
$this
->
assertEquals
(
'INT
AUTO_INCREMENT
'
,
'INT
IDENTITY
'
,
$this
->
_platform
->
getIntegerTypeDeclarationSQL
(
array
(
'autoincrement'
=>
true
,
'primary'
=>
true
)
));
...
...
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