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