Skip to content

Commit

Permalink
4th alpha release
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc A Hester committed Sep 21, 2015
1 parent 7f3d0a0 commit 8c7a1cd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.mb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 47 additions & 20 deletions src/sqlshim.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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=[] )
Expand All @@ -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<br>"; }
// if ( !$bound ) { echo "fail $i:$var<br>"; }
$i++;
}
$stmt->conn = $conn; // for ref
return $stmt;
}
catch ( \PDOException $e )
Expand Down
9 changes: 5 additions & 4 deletions tests/GeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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]);
}
}
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8c7a1cd

Please sign in to comment.