2013-04-03 06:19:26 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-15 03:57:54 +02:00
|
|
|
namespace SilverStripe\MSSQL;
|
|
|
|
|
|
|
|
use DateTime;
|
2016-09-09 08:55:53 +02:00
|
|
|
use SilverStripe\ORM\Connect\Query;
|
2016-07-15 03:57:54 +02:00
|
|
|
|
2013-04-03 06:19:26 +02:00
|
|
|
/**
|
|
|
|
* A result-set from a MSSQL database.
|
|
|
|
*/
|
2016-09-09 08:55:53 +02:00
|
|
|
class SQLServerQuery extends Query
|
2015-12-17 19:50:59 +01:00
|
|
|
{
|
2013-04-03 06:19:26 +02:00
|
|
|
|
2015-12-17 19:50:59 +01:00
|
|
|
/**
|
|
|
|
* The SQLServerConnector object that created this result set.
|
2016-08-30 03:08:11 +02:00
|
|
|
*
|
2015-12-17 19:50:59 +01:00
|
|
|
* @var SQLServerConnector
|
|
|
|
*/
|
|
|
|
private $connector;
|
2013-04-03 06:19:26 +02:00
|
|
|
|
2015-12-17 19:50:59 +01:00
|
|
|
/**
|
|
|
|
* The internal MSSQL handle that points to the result set.
|
2016-08-30 03:08:11 +02:00
|
|
|
*
|
2015-12-17 19:50:59 +01:00
|
|
|
* @var resource
|
|
|
|
*/
|
|
|
|
private $handle;
|
2013-04-03 06:19:26 +02:00
|
|
|
|
2015-12-17 19:50:59 +01:00
|
|
|
/**
|
|
|
|
* Hook the result-set given into a Query class, suitable for use by sapphire.
|
|
|
|
* @param SQLServerConnector $connector The database object that created this query.
|
|
|
|
* @param resource $handle the internal mssql handle that is points to the resultset.
|
|
|
|
*/
|
|
|
|
public function __construct(SQLServerConnector $connector, $handle)
|
|
|
|
{
|
|
|
|
$this->connector = $connector;
|
|
|
|
$this->handle = $handle;
|
|
|
|
}
|
2013-04-03 06:19:26 +02:00
|
|
|
|
2015-12-17 19:50:59 +01:00
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
if (is_resource($this->handle)) {
|
|
|
|
sqlsrv_free_stmt($this->handle);
|
|
|
|
}
|
|
|
|
}
|
2013-04-03 06:19:26 +02:00
|
|
|
|
2018-03-16 17:52:47 +01:00
|
|
|
public function getIterator()
|
2015-12-17 19:50:59 +01:00
|
|
|
{
|
2018-03-16 17:52:47 +01:00
|
|
|
if (is_resource($this->handle)) {
|
|
|
|
while ($data = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_ASSOC)) {
|
|
|
|
// special case for sqlsrv - date values are DateTime coming out of the sqlsrv drivers,
|
|
|
|
// so we convert to the usual Y-m-d H:i:s value!
|
|
|
|
foreach ($data as $name => $value) {
|
|
|
|
if ($value instanceof DateTime) {
|
|
|
|
$data[$name] = $value->format('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
}
|
2013-04-03 06:19:26 +02:00
|
|
|
|
2018-03-16 17:52:47 +01:00
|
|
|
yield $data;
|
|
|
|
}
|
|
|
|
}
|
2015-12-17 19:50:59 +01:00
|
|
|
}
|
2013-04-03 06:19:26 +02:00
|
|
|
|
2015-12-17 19:50:59 +01:00
|
|
|
public function numRecords()
|
|
|
|
{
|
|
|
|
if (!is_resource($this->handle)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-03 06:19:26 +02:00
|
|
|
|
2015-12-17 19:50:59 +01:00
|
|
|
// WARNING: This will only work if the cursor type is scrollable!
|
|
|
|
if (function_exists('sqlsrv_num_rows')) {
|
|
|
|
return sqlsrv_num_rows($this->handle);
|
|
|
|
} else {
|
|
|
|
user_error('MSSQLQuery::numRecords() not supported in this version of sqlsrv', E_USER_WARNING);
|
|
|
|
}
|
|
|
|
}
|
2013-04-03 06:19:26 +02:00
|
|
|
}
|