1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: namespace Cake\Database\Driver;
16:
17: use Cake\Database\Dialect\SqlserverDialectTrait;
18: use Cake\Database\Driver;
19: use Cake\Database\Query;
20: use Cake\Database\Statement\SqlserverStatement;
21: use PDO;
22:
23: 24: 25:
26: class Sqlserver extends Driver
27: {
28: use SqlserverDialectTrait;
29:
30: 31: 32: 33: 34:
35: protected $_baseConfig = [
36: 'host' => 'localhost\SQLEXPRESS',
37: 'username' => '',
38: 'password' => '',
39: 'database' => 'cake',
40: 'port' => '',
41:
42: 'encoding' => 65001,
43: 'flags' => [],
44: 'init' => [],
45: 'settings' => [],
46: 'attributes' => [],
47: 'app' => null,
48: 'connectionPooling' => null,
49: 'failoverPartner' => null,
50: 'loginTimeout' => null,
51: 'multiSubnetFailover' => null,
52: ];
53:
54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64:
65: public function connect()
66: {
67: if ($this->_connection) {
68: return true;
69: }
70: $config = $this->_config;
71:
72: if (isset($config['persistent']) && $config['persistent']) {
73: throw new \InvalidArgumentException('Config setting "persistent" cannot be set to true, as the Sqlserver PDO driver does not support PDO::ATTR_PERSISTENT');
74: }
75:
76: $config['flags'] += [
77: PDO::ATTR_EMULATE_PREPARES => false,
78: PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
79: ];
80:
81: if (!empty($config['encoding'])) {
82: $config['flags'][PDO::SQLSRV_ATTR_ENCODING] = $config['encoding'];
83: }
84: $port = '';
85: if (strlen($config['port'])) {
86: $port = ',' . $config['port'];
87: }
88:
89: $dsn = "sqlsrv:Server={$config['host']}{$port};Database={$config['database']};MultipleActiveResultSets=false";
90: if ($config['app'] !== null) {
91: $dsn .= ";APP={$config['app']}";
92: }
93: if ($config['connectionPooling'] !== null) {
94: $dsn .= ";ConnectionPooling={$config['connectionPooling']}";
95: }
96: if ($config['failoverPartner'] !== null) {
97: $dsn .= ";Failover_Partner={$config['failoverPartner']}";
98: }
99: if ($config['loginTimeout'] !== null) {
100: $dsn .= ";LoginTimeout={$config['loginTimeout']}";
101: }
102: if ($config['multiSubnetFailover'] !== null) {
103: $dsn .= ";MultiSubnetFailover={$config['multiSubnetFailover']}";
104: }
105: $this->_connect($dsn, $config);
106:
107: $connection = $this->getConnection();
108: if (!empty($config['init'])) {
109: foreach ((array)$config['init'] as $command) {
110: $connection->exec($command);
111: }
112: }
113: if (!empty($config['settings']) && is_array($config['settings'])) {
114: foreach ($config['settings'] as $key => $value) {
115: $connection->exec("SET {$key} {$value}");
116: }
117: }
118: if (!empty($config['attributes']) && is_array($config['attributes'])) {
119: foreach ($config['attributes'] as $key => $value) {
120: $connection->setAttribute($key, $value);
121: }
122: }
123:
124: return true;
125: }
126:
127: 128: 129: 130: 131:
132: public function enabled()
133: {
134: return in_array('sqlsrv', PDO::getAvailableDrivers(), true);
135: }
136:
137: 138: 139: 140: 141: 142:
143: public function prepare($query)
144: {
145: $this->connect();
146: $options = [PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL];
147: $isObject = $query instanceof Query;
148: if ($isObject && $query->isBufferedResultsEnabled() === false) {
149: $options = [];
150: }
151: $statement = $this->_connection->prepare($isObject ? $query->sql() : $query, $options);
152:
153: return new SqlserverStatement($statement, $this);
154: }
155:
156: 157: 158:
159: public function supportsDynamicConstraints()
160: {
161: return true;
162: }
163: }
164: