Rev 1226 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
635 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
635 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
635 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
635 | daniel-mar | 26 | class OIDplusSqlSlangPluginMsSQL extends OIDplusSqlSlangPlugin { |
27 | |||
1116 | daniel-mar | 28 | /** |
29 | * @return string |
||
30 | */ |
||
635 | daniel-mar | 31 | public static function id(): string { |
32 | return 'mssql'; |
||
33 | } |
||
34 | |||
1116 | daniel-mar | 35 | /** |
36 | * @return string |
||
37 | */ |
||
635 | daniel-mar | 38 | public function sqlDate(): string { |
39 | return 'getdate()'; |
||
40 | } |
||
41 | |||
1116 | daniel-mar | 42 | /** |
43 | * @param OIDplusDatabaseConnection $db |
||
44 | * @return bool |
||
45 | */ |
||
635 | daniel-mar | 46 | public function detect(OIDplusDatabaseConnection $db): bool { |
47 | try { |
||
48 | $vers = $db->query("select @@version as dbms_version")->fetch_object()->dbms_version; |
||
1225 | daniel-mar | 49 | return stripos($vers, 'Microsoft SQL Server') !== false; |
1050 | daniel-mar | 50 | } catch (\Exception $e) { |
635 | daniel-mar | 51 | return false; |
52 | } |
||
53 | } |
||
54 | |||
1116 | daniel-mar | 55 | /** |
56 | * @param OIDplusDatabaseConnection $db |
||
57 | * @return int |
||
58 | * @throws OIDplusException |
||
59 | */ |
||
635 | daniel-mar | 60 | public function insert_id(OIDplusDatabaseConnection $db): int { |
61 | // Note: SCOPE_IDENTITY() does not work, does only give 0. |
||
62 | // $res = $db->query("SELECT SCOPE_IDENTITY() AS ID"); |
||
1158 | daniel-mar | 63 | |
64 | // DM 04 Apr 2023 : Added @@ROWCOUNT , see https://stackoverflow.com/questions/4571911/reset-scope-identity |
||
1226 | daniel-mar | 65 | $rc = $db->rowsAffected(); |
66 | if ($rc == -1) { |
||
67 | // -1 means: it is not implemented or possible with that driver |
||
68 | $res = $db->query("SELECT @@ROWCOUNT AS RC"); |
||
69 | $row = $res->fetch_array(); |
||
70 | $rc = (int)$row['RC']; |
||
71 | } |
||
1158 | daniel-mar | 72 | if ($rc == 0) return 0; |
73 | |||
635 | daniel-mar | 74 | $res = $db->query("SELECT @@IDENTITY AS ID"); |
75 | $row = $res->fetch_array(); |
||
76 | return (int)$row['ID']; |
||
77 | } |
||
78 | |||
1116 | daniel-mar | 79 | /** |
80 | * @param string $cont |
||
81 | * @param string $table |
||
82 | * @param string $prefix |
||
83 | * @return string |
||
84 | */ |
||
85 | public function setupSetTablePrefix(string $cont, string $table, string $prefix): string { |
||
635 | daniel-mar | 86 | $cont = str_replace('['.$table.']', '['.$prefix.$table.']', $cont); |
87 | $cont = str_replace("'dbo.$table'", "'dbo.$prefix$table'", $cont); |
||
88 | $cont = str_replace('PK_'.$table, 'PK_'.$prefix.$table, $cont); |
||
89 | $cont = str_replace('IX_'.$table, 'PK_'.$prefix.$table, $cont); |
||
1130 | daniel-mar | 90 | return str_replace('DF__'.$table, 'DF__'.$prefix.$table, $cont); |
635 | daniel-mar | 91 | } |
92 | |||
1116 | daniel-mar | 93 | /** |
94 | * @param string $database |
||
95 | * @return string |
||
96 | */ |
||
97 | public function setupCreateDbIfNotExists(string $database): string { |
||
635 | daniel-mar | 98 | return ""; |
99 | } |
||
100 | |||
1116 | daniel-mar | 101 | /** |
102 | * @param string $database |
||
103 | * @return string |
||
104 | */ |
||
105 | public function setupUseDatabase(string $database): string { |
||
635 | daniel-mar | 106 | return "USE [$database]\n\nGO\n\n"; |
107 | } |
||
108 | |||
1116 | daniel-mar | 109 | /** |
110 | * @param string $expr1 |
||
111 | * @param string $expr2 |
||
112 | * @return string |
||
113 | */ |
||
114 | public function isNullFunction(string $expr1, string $expr2): string { |
||
635 | daniel-mar | 115 | return "isnull($expr1, $expr2)"; |
116 | } |
||
117 | |||
1116 | daniel-mar | 118 | /** |
119 | * @param string $sql |
||
120 | * @return string |
||
121 | */ |
||
122 | public function filterQuery(string $sql): string { |
||
635 | daniel-mar | 123 | return $sql; |
124 | } |
||
125 | |||
1116 | daniel-mar | 126 | /** |
127 | * @param bool $bool |
||
128 | * @return string |
||
129 | */ |
||
130 | public function getSQLBool(bool $bool): string { |
||
635 | daniel-mar | 131 | return $bool ? '1' : '0'; |
132 | } |
||
133 | |||
1116 | daniel-mar | 134 | /** |
135 | * @param string $str |
||
136 | * @return string |
||
137 | */ |
||
138 | public function escapeString(string $str): string { |
||
635 | daniel-mar | 139 | return str_replace("'", "''", $str); |
140 | } |
||
1311 | daniel-mar | 141 | |
142 | /** |
||
143 | * @param string $sql |
||
144 | * @return string |
||
145 | */ |
||
146 | public function lowerCase(string $sql): string { |
||
147 | return "lower($sql)"; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param string $sql |
||
152 | * @return string |
||
153 | */ |
||
154 | public function upperCase(string $sql): string { |
||
155 | return "upper($sql)"; |
||
156 | } |
||
635 | daniel-mar | 157 | } |