Skip to content

Commit 9bebc7d

Browse files
author
Alex Wijnholds
committed
Added SQLSrv adapter from funkjedi/php-activerecord fork
1 parent ad17f9e commit 9bebc7d

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

lib/adapters/SqlsrvAdapter.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
/**
3+
* @package ActiveRecord
4+
*/
5+
namespace ActiveRecord;
6+
7+
use PDO;
8+
9+
/**
10+
* Adapter for MSSQL.
11+
* http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=20098
12+
*
13+
* @package ActiveRecord
14+
*/
15+
class SqlsrvAdapter extends Connection
16+
{
17+
static $QUOTE_CHARACTER = '';
18+
static $DEFAULT_PORT = 1433;
19+
20+
protected function __construct($info)
21+
{
22+
try {
23+
$host = isset($info->port) ? "$info->host, $info->port" : $info->host;
24+
$this->connection = new PDO("sqlsrv:Server=$host;Database=$info->db", $info->user, $info->pass, static::$PDO_OPTIONS);
25+
} catch (PDOException $e) {
26+
throw new DatabaseException($e);
27+
}
28+
}
29+
30+
public function quote_name($string)
31+
{
32+
return $string[0] === '[' || $string[strlen($string) - 1] === ']' ?
33+
$string : "[$string]";
34+
}
35+
36+
// based on the implementation from the Zend Db adapter
37+
public function limit($sql, $offset, $limit)
38+
{
39+
$limit = intval($limit);
40+
$offset = intval($offset);
41+
42+
if ($offset == 0)
43+
return preg_replace('/^SELECT\s/i', 'SELECT TOP ' . $limit . ' ', $sql);
44+
45+
$orderby = stristr($sql, 'ORDER BY');
46+
$over = $orderby ? preg_replace('/\"[^,]*\".\"([^,]*)\"/i', '"inner_tbl"."$1"', $orderby) : 'ORDER BY (SELECT 0)';
47+
48+
// Remove ORDER BY clause from $sql
49+
$sql = preg_replace('/\s+ORDER BY(.*)/', '', $sql);
50+
51+
// Add ORDER BY clause as an argument for ROW_NUMBER()
52+
$sql = "SELECT ROW_NUMBER() OVER ($over) AS \"AR_ROWNUM\", * FROM ($sql) AS inner_tbl";
53+
54+
$start = $offset + 1;
55+
$end = $offset + $limit;
56+
return "WITH outer_tbl AS ($sql) SELECT * FROM outer_tbl WHERE \"AR_ROWNUM\" BETWEEN $start AND $end";
57+
}
58+
59+
public function query_column_info($table)
60+
{
61+
$sql =
62+
"SELECT c.COLUMN_NAME as field, c.DATA_TYPE as data_type, c.CHARACTER_MAXIMUM_LENGTH AS length, c.NUMERIC_PRECISION_RADIX AS radix, c.COLUMN_DEFAULT AS data_default, c.IS_NULLABLE AS nullable, " .
63+
"COLUMNPROPERTY(OBJECT_ID(TABLE_NAME), c.COLUMN_NAME, 'IsIdentity') AS extra, " .
64+
"(SELECT a.CONSTRAINT_TYPE " .
65+
"FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS a, INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE b " .
66+
"WHERE a.CONSTRAINT_TYPE='PRIMARY KEY' " .
67+
"AND a.CONSTRAINT_NAME = b.CONSTRAINT_NAME " .
68+
"AND a.TABLE_NAME = b.TABLE_NAME AND b.COLUMN_NAME = c.COLUMN_NAME) AS PK " .
69+
"FROM INFORMATION_SCHEMA.COLUMNS c " .
70+
"WHERE c.TABLE_NAME=?";
71+
72+
$values = array($table);
73+
return $this->query($sql,$values);
74+
}
75+
76+
public function query_for_tables()
77+
{
78+
return $this->query('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES');
79+
}
80+
81+
public function create_column(&$column)
82+
{
83+
$c = new Column();
84+
$c->inflected_name = Inflector::instance()->variablize($column['field']);
85+
$c->name = $column['field'];
86+
$c->nullable = ($column['nullable'] === 'YES' ? true : false);
87+
$c->auto_increment = ($column['extra'] === '1' ? true : false);
88+
$c->pk = ($column['pk'] === 'PRIMARY KEY' ? true : false);
89+
$c->raw_type = $column['data_type'];
90+
$c->length = ($column['length'] ? $column['length'] : $column['radix']);
91+
92+
if ($c->raw_type == 'text')
93+
$c->length = null;
94+
95+
if ($c->raw_type == 'datetime')
96+
$c->length = 19;
97+
98+
$c->map_raw_type();
99+
$c->default = $c->cast(preg_replace("#\(+'?(.*?)'?\)+#", '$1', $column['data_default']),$this);
100+
101+
return $c;
102+
}
103+
104+
public function set_encoding($charset)
105+
{
106+
throw new ActiveRecordException("SqlsrvAdapter::set_charset not supported.");
107+
}
108+
109+
public function accepts_limit_and_order_for_update_and_delete() { return false; }
110+
111+
public function native_database_types()
112+
{
113+
return array(
114+
'primary_key' => 'int NOT NULL IDENTITY(1,1) PRIMARY KEY',
115+
'string' => array('name' => 'varchar', 'length' => 255),
116+
'text' => array('name' => 'text'),
117+
'integer' => array('name' => 'int'),
118+
'float' => array('name' => 'float'),
119+
'datetime' => array('name' => 'datetime'),
120+
'timestamp' => array('name' => 'datetime'),
121+
'time' => array('name' => 'time'),
122+
'date' => array('name' => 'date'),
123+
'binary' => array('name' => 'blob'),
124+
'boolean' => array('name' => 'bit')
125+
);
126+
}
127+
128+
}
129+
?>

0 commit comments

Comments
 (0)