|
1 |
| -<?php namespace emericklaw\SQLAnywhere; |
| 1 | +<?php |
| 2 | + |
| 3 | +namespace emericklaw\SQLAnywhere; |
2 | 4 |
|
3 | 5 | use Illuminate\Database\Connection;
|
4 | 6 | use \emericklaw\SQLAnywhereClient;
|
5 | 7 |
|
6 |
| -class SQLAnywhereConnection extends Connection { |
7 |
| - |
8 |
| - /** |
9 |
| - * Create a new database connection instance. |
10 |
| - * |
11 |
| - * @param PDO $pdo |
12 |
| - * @param string $database |
13 |
| - * @param string $tablePrefix |
14 |
| - * @param array $config |
15 |
| - * @return void |
16 |
| - */ |
17 |
| - public function __construct(SQLAnywhereClient $pdo, $database = '', $tablePrefix = '', array $config = array()) |
18 |
| - { |
19 |
| - $this->pdo = $pdo; |
20 |
| - |
21 |
| - // First we will setup the default properties. We keep track of the DB |
22 |
| - // name we are connected to since it is needed when some reflective |
23 |
| - // type commands are run such as checking whether a table exists. |
24 |
| - $this->database = $database; |
25 |
| - |
26 |
| - $this->tablePrefix = $tablePrefix; |
27 |
| - |
28 |
| - $this->config = $config; |
29 |
| - |
30 |
| - // We need to initialize a query grammar and the query post processors |
31 |
| - // which are both very important parts of the database abstractions |
32 |
| - // so we initialize these to their default values while starting. |
33 |
| - $this->useDefaultQueryGrammar(); |
34 |
| - |
35 |
| - $this->useDefaultPostProcessor(); |
36 |
| - } |
37 |
| - |
38 |
| - /** |
39 |
| - * Run a select statement against the database. |
40 |
| - * |
41 |
| - * @param string $query |
42 |
| - * @param array $bindings |
43 |
| - * @return array |
44 |
| - */ |
45 |
| - public function select($query, $bindings = array(), $useReadPdo = true) |
46 |
| - { |
47 |
| - // new version since Laravel 5.4 |
48 |
| - // /vendor/laravel/framework/src/Illuminate/Database/Connection.php |
49 |
| - // --> function: select(...) |
50 |
| - return $this->run($query, $bindings, function($query, $bindings) |
51 |
| - { |
52 |
| - if ($this->pretending()) return array(); |
53 |
| - |
54 |
| - // For select statements, we'll simply execute the query and return an array |
55 |
| - // of the database result set. Each element in the array will be a single |
56 |
| - // row from the database table, and will either be an array or objects. |
57 |
| - $statement = $this->getReadPdo()->prepare($query); |
58 |
| - |
59 |
| - $statement->execute($this->prepareBindings($bindings)); |
60 |
| - |
61 |
| - return $statement->fetchAll(); |
62 |
| - }); |
63 |
| - } |
64 |
| - |
65 |
| - /** |
66 |
| - * Run a select statement against the database and returns a generator. |
67 |
| - * |
68 |
| - * @param string $query |
69 |
| - * @param array $bindings |
70 |
| - * @param bool $useReadPdo |
71 |
| - * @return \Generator |
72 |
| - */ |
73 |
| - public function cursor($query, $bindings = [], $useReadPdo = true) |
74 |
| - { |
75 |
| - $statement = $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) { |
76 |
| - if ($this->pretending()) { |
77 |
| - return []; |
78 |
| - } |
79 |
| - |
80 |
| - // First we will create a statement for the query. Then, we will set the fetch |
81 |
| - // mode and prepare the bindings for the query. Once that's done we will be |
82 |
| - // ready to execute the query against the database and return the cursor. |
83 |
| - $statement = $this->getReadPdo()->prepare($query); |
84 |
| - |
85 |
| - $statement->execute($this->prepareBindings($bindings)); |
86 |
| - |
87 |
| - return $statement; |
88 |
| - }); |
89 |
| - |
90 |
| - while ($record = $statement->fetch()) { |
91 |
| - yield $record; |
92 |
| - } |
93 |
| - } |
94 |
| - |
95 |
| - /** |
96 |
| - * Run an SQL statement and get the number of rows affected. |
97 |
| - * |
98 |
| - * @param string $query |
99 |
| - * @param array $bindings |
100 |
| - * @return int |
101 |
| - */ |
102 |
| - public function affectingStatement($query, $bindings = array()) |
103 |
| - { |
104 |
| - return $this->run($query, $bindings, function($query, $bindings) |
105 |
| - { |
106 |
| - if ($this->pretending()) return 0; |
107 |
| - |
108 |
| - // For update or delete statements, we want to get the number of rows affected |
109 |
| - // by the statement and return that back to the developer. We'll first need |
110 |
| - // to execute the statement and then we'll use PDO to fetch the affected. |
111 |
| - $statement = $this->getPdo()->prepare($query); |
112 |
| - |
113 |
| - $statement->execute($this->prepareBindings($bindings)); |
114 |
| - |
115 |
| - return $statement->affectedRows(); |
116 |
| - }); |
117 |
| - } |
118 |
| - |
119 |
| - /** |
120 |
| - * Get the default query grammar instance. |
121 |
| - * |
122 |
| - * @return Illuminate\Database\Query\Grammars\Grammars\Grammar |
123 |
| - */ |
124 |
| - protected function getDefaultQueryGrammar() |
125 |
| - { |
| 8 | +class SQLAnywhereConnection extends Connection |
| 9 | +{ |
| 10 | + |
| 11 | + /** |
| 12 | + * Create a new database connection instance. |
| 13 | + * |
| 14 | + * @param PDO $pdo |
| 15 | + * @param string $database |
| 16 | + * @param string $tablePrefix |
| 17 | + * @param array $config |
| 18 | + * @return void |
| 19 | + */ |
| 20 | + public function __construct(SQLAnywhereClient $pdo, $database = '', $tablePrefix = '', array $config = []) |
| 21 | + { |
| 22 | + $this->pdo = $pdo; |
| 23 | + |
| 24 | + // First we will setup the default properties. We keep track of the DB |
| 25 | + // name we are connected to since it is needed when some reflective |
| 26 | + // type commands are run such as checking whether a table exists. |
| 27 | + $this->database = $database; |
| 28 | + |
| 29 | + $this->tablePrefix = $tablePrefix; |
| 30 | + |
| 31 | + $this->config = $config; |
| 32 | + |
| 33 | + // We need to initialize a query grammar and the query post processors |
| 34 | + // which are both very important parts of the database abstractions |
| 35 | + // so we initialize these to their default values while starting. |
| 36 | + $this->useDefaultQueryGrammar(); |
| 37 | + |
| 38 | + $this->useDefaultPostProcessor(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Run a select statement against the database. |
| 43 | + * |
| 44 | + * @param string $query |
| 45 | + * @param array $bindings |
| 46 | + * @return array |
| 47 | + */ |
| 48 | + public function select($query, $bindings = [], $useReadPdo = true) |
| 49 | + { |
| 50 | + // new version since Laravel 5.4 |
| 51 | + // /vendor/laravel/framework/src/Illuminate/Database/Connection.php |
| 52 | + // --> function: select(...) |
| 53 | + return $this->run($query, $bindings, function ($query, $bindings) { |
| 54 | + if ($this->pretending()) return []; |
| 55 | + |
| 56 | + // For select statements, we'll simply execute the query and return an array |
| 57 | + // of the database result set. Each element in the array will be a single |
| 58 | + // row from the database table, and will either be an array or objects. |
| 59 | + $statement = $this->getReadPdo()->prepare($query); |
| 60 | + |
| 61 | + $statement->execute($this->prepareBindings($bindings)); |
| 62 | + |
| 63 | + return $statement->fetchAll(); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Run a select statement against the database and returns a generator. |
| 69 | + * |
| 70 | + * @param string $query |
| 71 | + * @param array $bindings |
| 72 | + * @param bool $useReadPdo |
| 73 | + * @return \Generator |
| 74 | + */ |
| 75 | + public function cursor($query, $bindings = [], $useReadPdo = true) |
| 76 | + { |
| 77 | + $statement = $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) { |
| 78 | + if ($this->pretending()) { |
| 79 | + return []; |
| 80 | + } |
| 81 | + |
| 82 | + // First we will create a statement for the query. Then, we will set the fetch |
| 83 | + // mode and prepare the bindings for the query. Once that's done we will be |
| 84 | + // ready to execute the query against the database and return the cursor. |
| 85 | + $statement = $this->getReadPdo()->prepare($query); |
| 86 | + |
| 87 | + $statement->execute($this->prepareBindings($bindings)); |
| 88 | + |
| 89 | + return $statement; |
| 90 | + }); |
| 91 | + |
| 92 | + while ($record = $statement->fetch()) { |
| 93 | + yield $record; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Run an SQL statement and get the number of rows affected. |
| 99 | + * |
| 100 | + * @param string $query |
| 101 | + * @param array $bindings |
| 102 | + * @return int |
| 103 | + */ |
| 104 | + public function affectingStatement($query, $bindings = []) |
| 105 | + { |
| 106 | + return $this->run($query, $bindings, function ($query, $bindings) { |
| 107 | + if ($this->pretending()) return 0; |
| 108 | + |
| 109 | + // For update or delete statements, we want to get the number of rows affected |
| 110 | + // by the statement and return that back to the developer. We'll first need |
| 111 | + // to execute the statement and then we'll use PDO to fetch the affected. |
| 112 | + $statement = $this->getPdo()->prepare($query); |
| 113 | + |
| 114 | + $statement->execute($this->prepareBindings($bindings)); |
| 115 | + |
| 116 | + return $statement->affectedRows(); |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Get the default query grammar instance. |
| 122 | + * |
| 123 | + * @return Illuminate\Database\Query\Grammars\Grammars\Grammar |
| 124 | + */ |
| 125 | + protected function getDefaultQueryGrammar() |
| 126 | + { |
126 | 127 | return $this->withTablePrefix(new SQLAnywhereQueryGrammar);
|
127 |
| - } |
128 |
| - |
129 |
| - /** |
130 |
| - * Get the default schema grammar instance. |
131 |
| - * |
132 |
| - * @return Illuminate\Database\Schema\Grammars\Grammar |
133 |
| - */ |
134 |
| - protected function getDefaultSchemaGrammar() |
135 |
| - { |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Get the default schema grammar instance. |
| 132 | + * |
| 133 | + * @return Illuminate\Database\Schema\Grammars\Grammar |
| 134 | + */ |
| 135 | + protected function getDefaultSchemaGrammar() |
| 136 | + { |
136 | 137 | return $this->withTablePrefix(new SQLAnywhereSchemaGrammar);
|
137 |
| - } |
138 |
| - |
| 138 | + } |
139 | 139 | }
|
0 commit comments