This commit is contained in:
Roly Rudy Gutierrez Pinto
2018-12-20 16:45:51 -04:00
parent 8a854f7657
commit 3d8e15bb00
4 changed files with 77 additions and 65 deletions

View File

@@ -191,16 +191,18 @@ class MSSQLConnection extends ConnectionCommon implements Connection
{ {
$this->lastQuery = $sql; $this->lastQuery = $sql;
if (extension_loaded('sqlsrv')) { if (extension_loaded('sqlsrv')) {
$result = sqlsrv_query($this->dblink, $sql); $result = @sqlsrv_query($this->dblink, $sql);
if (!$result) {
throw new SQLException('Could not execute query', print_r(sqlsrv_errors(), true));
}
} else { } else {
if (!@mssql_select_db($this->database, $this->dblink)) { if (!@mssql_select_db($this->database, $this->dblink)) {
throw new SQLException('No database selected'); throw new SQLException('No database selected');
} }
$result = @mssql_query($sql, $this->dblink); $result = @mssql_query($sql, $this->dblink);
} if (!$result) {
if (!$result) { throw new SQLException('Could not execute query', mssql_get_last_message());
throw new SQLException('Could not execute query', mssql_get_last_message()); }
} }
return new MSSQLResultSet($this, $result, $fetchmode); return new MSSQLResultSet($this, $result, $fetchmode);
} }
@@ -210,23 +212,23 @@ class MSSQLConnection extends ConnectionCommon implements Connection
*/ */
function executeUpdate($sql) function executeUpdate($sql)
{ {
$this->lastQuery = $sql;
if (extension_loaded('sqlsrv')) { if (extension_loaded('sqlsrv')) {
$result = sqlsrv_query($this->dblink, $sql); $result = @sqlsrv_query($this->dblink, $sql);
if (!$result) {
throw new SQLException('Could not execute update', print_r(sqlsrv_errors(), true), $sql);
}
return (int) sqlsrv_rows_affected($this->dblink);
} else { } else {
$this->lastQuery = $sql;
if (!mssql_select_db($this->database, $this->dblink)) { if (!mssql_select_db($this->database, $this->dblink)) {
throw new SQLException('No database selected'); throw new SQLException('No database selected');
} }
$result = @mssql_query($sql, $this->dblink); $result = @mssql_query($sql, $this->dblink);
if (!$result) {
throw new SQLException('Could not execute update', mssql_get_last_message(), $sql);
}
return (int) mssql_rows_affected($this->dblink);
} }
if (!$result) {
throw new SQLException('Could not execute update', mssql_get_last_message(), $sql);
}
return (int) mssql_rows_affected($this->dblink);
// return $this->getUpdateCount();
} }
/** /**
@@ -237,15 +239,18 @@ class MSSQLConnection extends ConnectionCommon implements Connection
protected function beginTrans() protected function beginTrans()
{ {
if (extension_loaded('sqlsrv')) { if (extension_loaded('sqlsrv')) {
$result = sqlsrv_begin_transaction($this->dblink); $result = @sqlsrv_begin_transaction($this->dblink);
if (!$result) {
throw new SQLException('Could not begin transaction', print_r(sqlsrv_errors(), true));
}
} else { } else {
$result = @mssql_query('BEGIN TRAN', $this->dblink); $result = @mssql_query('BEGIN TRAN', $this->dblink);
} if (!$result) {
if (!$result) { throw new SQLException('Could not begin transaction', mssql_get_last_message());
throw new SQLException('Could not begin transaction', mssql_get_last_message()); }
} }
} }
/** /**
* Commit the current transaction. * Commit the current transaction.
* @throws SQLException * @throws SQLException
@@ -254,15 +259,18 @@ class MSSQLConnection extends ConnectionCommon implements Connection
protected function commitTrans() protected function commitTrans()
{ {
if (extension_loaded('sqlsrv')) { if (extension_loaded('sqlsrv')) {
$result = sqlsrv_commit($this->dblink); $result = @sqlsrv_commit($this->dblink);
if (!$result) {
throw new SQLException('Could not commit transaction', print_r(sqlsrv_errors(), true));
}
} else { } else {
if (!@mssql_select_db($this->database, $this->dblink)) { if (!@mssql_select_db($this->database, $this->dblink)) {
throw new SQLException('No database selected'); throw new SQLException('No database selected');
} }
$result = @mssql_query('COMMIT TRAN', $this->dblink); $result = @mssql_query('COMMIT TRAN', $this->dblink);
} if (!$result) {
if (!$result) { throw new SQLException('Could not commit transaction', mssql_get_last_message());
throw new SQLException('Could not commit transaction', mssql_get_last_message()); }
} }
} }
@@ -274,15 +282,18 @@ class MSSQLConnection extends ConnectionCommon implements Connection
protected function rollbackTrans() protected function rollbackTrans()
{ {
if (extension_loaded('sqlsrv')) { if (extension_loaded('sqlsrv')) {
$result = sqlsrv_rollback($this->dblink); $result = @sqlsrv_rollback($this->dblink);
if (!$result) {
throw new SQLException('Could not rollback transaction', print_r(sqlsrv_errors(), true));
}
} else { } else {
if (!@mssql_select_db($this->database, $this->dblink)) { if (!@mssql_select_db($this->database, $this->dblink)) {
throw new SQLException('no database selected'); throw new SQLException('no database selected');
} }
$result = @mssql_query('ROLLBACK TRAN', $this->dblink); $result = @mssql_query('ROLLBACK TRAN', $this->dblink);
} if (!$result) {
if (!$result) { throw new SQLException('Could not rollback transaction', mssql_get_last_message());
throw new SQLException('Could not rollback transaction', mssql_get_last_message()); }
} }
} }

View File

@@ -154,12 +154,14 @@ class MSSQLResultSet extends ResultSetCommon implements ResultSet
{ {
if (extension_loaded('sqlsrv')) { if (extension_loaded('sqlsrv')) {
$rows = @sqlsrv_num_rows($this->result); $rows = @sqlsrv_num_rows($this->result);
if ($rows === null) {
throw new SQLException('Error getting record count', print_r(sqlsrv_errors(), true));
}
} else { } else {
$rows = @mssql_num_rows($this->result); $rows = @mssql_num_rows($this->result);
} if ($rows === null) {
throw new SQLException('Error getting record count', mssql_get_last_message());
if ($rows === null) { }
throw new SQLException('Error getting record count', mssql_get_last_message());
} }
// adjust count based on emulated LIMIT/OFFSET // adjust count based on emulated LIMIT/OFFSET
$rows -= $this->offset; $rows -= $this->offset;

View File

@@ -38,31 +38,30 @@ class MSSQLDatabaseInfo extends DatabaseInfo
protected function initTables() protected function initTables()
{ {
include_once 'creole/drivers/mssql/metadata/MSSQLTableInfo.php'; include_once 'creole/drivers/mssql/metadata/MSSQLTableInfo.php';
$dsn = $this->conn->getDSN(); $dsn = $this->conn->getDSN();
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME <> 'dtproperties'";
if (extension_loaded('sqlsrv')) { if (extension_loaded('sqlsrv')) {
$result = sqlsrv_query( $result = sqlsrv_query($sql, $this->conn->getResource());
"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME <> 'dtproperties'", if (!$result) {
$this->conn->getResource() throw new SQLException("Could not list tables", print_r(sqlsrv_errors(), true));
); }
while ($row = sqlsrv_fetch_array($result)) {
$this->tables[strtoupper($row[0])] = new MSSQLTableInfo($this, $row[0]);
}
} else { } else {
if (!@mssql_select_db($this->dbname, $this->conn->getResource())) { if (!@mssql_select_db($this->dbname, $this->conn->getResource())) {
throw new SQLException('No database selected'); throw new SQLException('No database selected');
} }
$result = mssql_query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME <> 'dtproperties'", $this->conn->getResource()); $result = mssql_query($sql, $this->conn->getResource());
} if (!$result) {
throw new SQLException("Could not list tables", mssql_get_last_message());
if (!$result) { }
throw new SQLException("Could not list tables", mssql_get_last_message()); while ($row = mssql_fetch_row($result)) {
} $this->tables[strtoupper($row[0])] = new MSSQLTableInfo($this, $row[0]);
}
while ($row = mssql_fetch_row($result)) {
$this->tables[strtoupper($row[0])] = new MSSQLTableInfo($this, $row[0]);
} }
} }
/** /**
* *
* @return void * @return void

View File

@@ -253,14 +253,7 @@ class Net
break; break;
case 'mssql': case 'mssql':
//todo //todo
if (!extension_loaded('sqlsrv')) { if (extension_loaded('sqlsrv')) {
if ($this->db_instance != "") {
$link = @mssql_connect($this->ip . "\\" . $this->db_instance, $this->db_user, $this->db_passwd);
} else {
$port = (($this->db_port == "") || ($this->db_port == 0) || ($this->db_port == 1433)) ? "" : ":" . $this->db_port;
$link = @mssql_connect($this->ip . $port, $this->db_user, $this->db_passwd);
}
} else {
if ($this->db_instance != "") { if ($this->db_instance != "") {
$server = $this->ip . "\\" . $this->db_instance; $server = $this->ip . "\\" . $this->db_instance;
} else { } else {
@@ -274,6 +267,13 @@ class Net
'Database' => $this->db_sourcename 'Database' => $this->db_sourcename
]; ];
$link = @sqlsrv_connect($server, $opt); $link = @sqlsrv_connect($server, $opt);
} else {
if ($this->db_instance != "") {
$link = @mssql_connect($this->ip . "\\" . $this->db_instance, $this->db_user, $this->db_passwd);
} else {
$port = (($this->db_port == "") || ($this->db_port == 0) || ($this->db_port == 1433)) ? "" : ":" . $this->db_port;
$link = @mssql_connect($this->ip . $port, $this->db_user, $this->db_passwd);
}
} }
if ($link) { if ($link) {
@@ -397,14 +397,7 @@ class Net
} }
break; break;
case 'mssql': case 'mssql':
if (!extension_loaded('sqlsrv')) { if (extension_loaded('sqlsrv')) {
if ($this->db_instance != "") {
$link = @mssql_connect($this->ip . "\\" . $this->db_instance, $this->db_user, $this->db_passwd);
} else {
$port = (($this->db_port == "") || ($this->db_port == 0) || ($this->db_port == 1433)) ? "" : ":" . $this->db_port;
$link = @mssql_connect($this->ip . $port, $this->db_user, $this->db_passwd);
}
} else {
if ($this->db_instance != "") { if ($this->db_instance != "") {
$server = $this->ip . "\\" . $this->db_instance; $server = $this->ip . "\\" . $this->db_instance;
} else { } else {
@@ -418,6 +411,13 @@ class Net
'Database' => $this->db_sourcename 'Database' => $this->db_sourcename
]; ];
$link = $db = @sqlsrv_connect($server, $opt); $link = $db = @sqlsrv_connect($server, $opt);
} else {
if ($this->db_instance != "") {
$link = @mssql_connect($this->ip . "\\" . $this->db_instance, $this->db_user, $this->db_passwd);
} else {
$port = (($this->db_port == "") || ($this->db_port == 0) || ($this->db_port == 1433)) ? "" : ":" . $this->db_port;
$link = @mssql_connect($this->ip . $port, $this->db_user, $this->db_passwd);
}
} }
if ($link) { if ($link) {
if (!extension_loaded('sqlsrv')) { if (!extension_loaded('sqlsrv')) {