From 8c7a1cd1da880520679206ddcf16aa916ee8e803 Mon Sep 17 00:00:00 2001 From: Marc A Hester Date: Mon, 21 Sep 2015 14:50:08 -0500 Subject: [PATCH] 4th alpha release --- CHANGELOG.mb | 11 +++++++ src/sqlshim.php | 67 ++++++++++++++++++++++++++++++------------- tests/GeneralTest.php | 9 +++--- 3 files changed, 63 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.mb b/CHANGELOG.mb index 9bdc98b..af376ec 100644 --- a/CHANGELOG.mb +++ b/CHANGELOG.mb @@ -11,6 +11,17 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Make client_info() more reliable. - Figure out how/why sqlsrv's PHPTYPE_STR(EAM|ING) function return values are randomly wrong. When wrong, they are always the same wrong values. +## [0.0.4] - +### Added +- option parsing for prepare() +- connection ref variable. + +### Removed +- has_rows() and num_rows() were assumed to be working, but do not. no longer pretending. + +### Fixed +- bug improperly handling invalid parameters. + ## [0.0.3] - 2015-06-17 ### Added diff --git a/src/sqlshim.php b/src/sqlshim.php index 9e8e6f8..e35f0c2 100644 --- a/src/sqlshim.php +++ b/src/sqlshim.php @@ -648,6 +648,7 @@ public static function get_field( \PDOStatement $stmt, $fieldIndex=0, $getAsType public static function has_rows( \PDOStatement $stmt ) { + // REVEW: this doesn't work unless $stmt->rowCount() works return (bool)$stmt->rowCount(); } @@ -663,15 +664,22 @@ public static function num_fields( \PDOStatement $stmt ) public static function num_rows( \PDOStatement $stmt ) { - // REVIEW: num_rows() - test this. - return $stmt->rowCount(); - // "SELECT @@ROWCOUNT;" - // $row = $stmt->fetch(\PDO::FETCH_NUM); - // if ( is_array($row) && count($row) ) - // { - // return $row[key($row)]; - // } - // return false; + // REVIEW: num_rows() - $stmt->rowCount DOES NOT work for SELECTs in MSSQL PDO. + $conn = $stmt->conn; + $sql = $stmt->queryString; + if ( stripos($sql, "select")>=0 ) + { + $sql = preg_replace("/SELECT .* FROM/", "SELECT COUNT(*) AS count FROM", $sql); + + var_dump($sql); + $$cnt = $conn->query($sql); + $row = $cnt->fetch(\PDO::FETCH_NUM); + if ( is_array($row) && count($row) ) + { + return $row[key($row)]; + } + } + return false; } public static function prepare( \PDO $conn, $sql, $params=[], $options=[] ) @@ -686,26 +694,45 @@ public static function prepare( \PDO $conn, $sql, $params=[], $options=[] ) } while ( $found ); // translate options array + $optionsin = $options; + $options = []; + foreach ( $options as $opt=>$val ) + { + switch ( $opt ) + { + case 'QueryTimeout': + if ( is_numeric($val) ) + { + $conn->setAttribute(\PDO::SQLSRV_ATTR_QUERY_TIMEOUT, intval($val)); + } + break; + case 'SendStreamParamsAtExec': + // ??? + break; + case 'Scrollable': + if ( isset(self::$tabcursor[$val]) ) + { + $options[\PDO::ATTR_CURSOR] = self::$tabcursor[$val]; + } + break; + default: + break; + } + } + + if ( !is_array($params) ) $params = []; try { - $stmt = $conn->prepare($sql); + $stmt = $conn->prepare($sql, $options); $i = 1; foreach ( array_slice($params, 0, $count) as $var ) { if ( $i>$count ) break; - // $type = \PDO::PARAM_STR; - // if ( is_null($var) ) - // { - // echo "WEE DOO";exit; - // $type = \PDO::PARAM_NULL; - // $var = "NULL"; - // } - $bound = $stmt->bindValue(":var$i", $var); - - if ( !$bound ) { echo "fail $i:$var
"; } + // if ( !$bound ) { echo "fail $i:$var
"; } $i++; } + $stmt->conn = $conn; // for ref return $stmt; } catch ( \PDOException $e ) diff --git a/tests/GeneralTest.php b/tests/GeneralTest.php index a3110d1..9ab93e2 100644 --- a/tests/GeneralTest.php +++ b/tests/GeneralTest.php @@ -52,7 +52,7 @@ public function testConstants( $init ) $cval = constant(SqlShim::NAME . "::" . str_replace('SQLSRV_', '', $const)); $val = constant($const); $compare = ($val===$cval); - echo !$compare ? "$const: c$cval vs g$val" : ""; + echo !$compare ? "$const: c$cval vs g$val\n" : ""; $this->assertTrue($compare); } } @@ -77,14 +77,14 @@ public function testConstants( $init ) { for( $p1=$args[0][0]; $p1<=$args[0][1]; $p1++ ) { for( $p2=$args[1][0]; $p2<=$args[1][1]; $p2++ ) { - self::tryfunction($func, [$p1, $p2]); + $this->tryfunction($func, [$p1, $p2]); }} } else // if ( strstr($func, 'STREAM') || strstr($func, 'STRING') ) { foreach ( $args as $arg ) { - self::tryfunction($func, [$arg]); + $this->tryfunction($func, [$arg]); } } } @@ -167,8 +167,9 @@ public function testQueries( $con ) { if ( $con!==false ) { - $stmt = sqlsrv_query($con, "SELECT * FROM Northwind.Customers;"); + $stmt = sqlsrv_query($con, "SELECT * FROM Northwind.Customers;", null, ['Scrollable'=>SQLSRV_CURSOR_KEYSET]); $rows = []; + var_dump(sqlsrv_num_rows($stmt));exit; while ( $row = sqlsrv_fetch_array($stmt) ) { $rows[] = $row;