From 3d8e15bb00996ecbe3f272e6296d9732a07119ee Mon Sep 17 00:00:00 2001 From: Roly Rudy Gutierrez Pinto Date: Thu, 20 Dec 2018 16:45:51 -0400 Subject: [PATCH] PMC-337 --- .../creole/drivers/mssql/MSSQLConnection.php | 67 +++++++++++-------- .../creole/drivers/mssql/MSSQLResultSet.php | 10 +-- .../mssql/metadata/MSSQLDatabaseInfo.php | 33 +++++---- workflow/engine/classes/Net.php | 32 ++++----- 4 files changed, 77 insertions(+), 65 deletions(-) diff --git a/thirdparty/creole/drivers/mssql/MSSQLConnection.php b/thirdparty/creole/drivers/mssql/MSSQLConnection.php index b9097ee4f..27a08c1fc 100644 --- a/thirdparty/creole/drivers/mssql/MSSQLConnection.php +++ b/thirdparty/creole/drivers/mssql/MSSQLConnection.php @@ -191,16 +191,18 @@ class MSSQLConnection extends ConnectionCommon implements Connection { $this->lastQuery = $sql; 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 { if (!@mssql_select_db($this->database, $this->dblink)) { throw new SQLException('No database selected'); } - $result = @mssql_query($sql, $this->dblink); - } - if (!$result) { - throw new SQLException('Could not execute query', mssql_get_last_message()); + if (!$result) { + throw new SQLException('Could not execute query', mssql_get_last_message()); + } } return new MSSQLResultSet($this, $result, $fetchmode); } @@ -210,23 +212,23 @@ class MSSQLConnection extends ConnectionCommon implements Connection */ function executeUpdate($sql) { + $this->lastQuery = $sql; 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 { - $this->lastQuery = $sql; if (!mssql_select_db($this->database, $this->dblink)) { throw new SQLException('No database selected'); } - $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() { 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 { $result = @mssql_query('BEGIN TRAN', $this->dblink); - } - if (!$result) { - throw new SQLException('Could not begin transaction', mssql_get_last_message()); + if (!$result) { + throw new SQLException('Could not begin transaction', mssql_get_last_message()); + } } } - + /** * Commit the current transaction. * @throws SQLException @@ -254,15 +259,18 @@ class MSSQLConnection extends ConnectionCommon implements Connection protected function commitTrans() { 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 { if (!@mssql_select_db($this->database, $this->dblink)) { throw new SQLException('No database selected'); } $result = @mssql_query('COMMIT TRAN', $this->dblink); - } - if (!$result) { - throw new SQLException('Could not commit transaction', mssql_get_last_message()); + if (!$result) { + throw new SQLException('Could not commit transaction', mssql_get_last_message()); + } } } @@ -274,15 +282,18 @@ class MSSQLConnection extends ConnectionCommon implements Connection protected function rollbackTrans() { 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 { if (!@mssql_select_db($this->database, $this->dblink)) { throw new SQLException('no database selected'); } $result = @mssql_query('ROLLBACK TRAN', $this->dblink); - } - if (!$result) { - throw new SQLException('Could not rollback transaction', mssql_get_last_message()); + if (!$result) { + throw new SQLException('Could not rollback transaction', mssql_get_last_message()); + } } } diff --git a/thirdparty/creole/drivers/mssql/MSSQLResultSet.php b/thirdparty/creole/drivers/mssql/MSSQLResultSet.php index b75c740e5..b66008179 100644 --- a/thirdparty/creole/drivers/mssql/MSSQLResultSet.php +++ b/thirdparty/creole/drivers/mssql/MSSQLResultSet.php @@ -154,12 +154,14 @@ class MSSQLResultSet extends ResultSetCommon implements ResultSet { if (extension_loaded('sqlsrv')) { $rows = @sqlsrv_num_rows($this->result); + if ($rows === null) { + throw new SQLException('Error getting record count', print_r(sqlsrv_errors(), true)); + } } else { $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 $rows -= $this->offset; diff --git a/thirdparty/creole/drivers/mssql/metadata/MSSQLDatabaseInfo.php b/thirdparty/creole/drivers/mssql/metadata/MSSQLDatabaseInfo.php index 959c1e321..3e557eabc 100644 --- a/thirdparty/creole/drivers/mssql/metadata/MSSQLDatabaseInfo.php +++ b/thirdparty/creole/drivers/mssql/metadata/MSSQLDatabaseInfo.php @@ -38,31 +38,30 @@ class MSSQLDatabaseInfo extends DatabaseInfo protected function initTables() { include_once 'creole/drivers/mssql/metadata/MSSQLTableInfo.php'; - $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')) { - $result = sqlsrv_query( - "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME <> 'dtproperties'", - $this->conn->getResource() - ); + $result = sqlsrv_query($sql, $this->conn->getResource()); + if (!$result) { + 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 { if (!@mssql_select_db($this->dbname, $this->conn->getResource())) { 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()); - } - - 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]); + $result = mssql_query($sql, $this->conn->getResource()); + 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]); + } } } - + /** * * @return void diff --git a/workflow/engine/classes/Net.php b/workflow/engine/classes/Net.php index c410abeed..d5950d92e 100644 --- a/workflow/engine/classes/Net.php +++ b/workflow/engine/classes/Net.php @@ -253,14 +253,7 @@ class Net break; case 'mssql': //todo - 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 (extension_loaded('sqlsrv')) { if ($this->db_instance != "") { $server = $this->ip . "\\" . $this->db_instance; } else { @@ -274,6 +267,13 @@ class Net 'Database' => $this->db_sourcename ]; $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) { @@ -397,14 +397,7 @@ class Net } break; case 'mssql': - 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 (extension_loaded('sqlsrv')) { if ($this->db_instance != "") { $server = $this->ip . "\\" . $this->db_instance; } else { @@ -418,6 +411,13 @@ class Net 'Database' => $this->db_sourcename ]; $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 (!extension_loaded('sqlsrv')) {