Rev 1225 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
783 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
783 | 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; |
783 | 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 | |||
783 | daniel-mar | 26 | class OIDplusSqlSlangPluginOracle extends OIDplusSqlSlangPlugin { |
27 | |||
1116 | daniel-mar | 28 | /** |
29 | * @return string |
||
30 | */ |
||
783 | daniel-mar | 31 | public static function id(): string { |
32 | return 'oracle'; |
||
33 | } |
||
34 | |||
1116 | daniel-mar | 35 | /** |
36 | * @return string |
||
37 | */ |
||
783 | daniel-mar | 38 | public function sqlDate(): string { |
39 | return 'SYSDATE'; |
||
40 | } |
||
41 | |||
1116 | daniel-mar | 42 | /** |
43 | * @param OIDplusDatabaseConnection $db |
||
44 | * @return bool |
||
45 | */ |
||
783 | daniel-mar | 46 | public function detect(OIDplusDatabaseConnection $db): bool { |
47 | try { |
||
48 | $vers = $db->query("SELECT banner FROM v\$version WHERE banner LIKE 'Oracle%'")->fetch_object()->banner; |
||
1225 | daniel-mar | 49 | return (stripos($vers, 'Oracle') !== false); |
1050 | daniel-mar | 50 | } catch (\Exception $e) { |
783 | daniel-mar | 51 | return false; |
52 | } |
||
53 | } |
||
54 | |||
1116 | daniel-mar | 55 | /** |
1152 | daniel-mar | 56 | * @var string|null |
1116 | daniel-mar | 57 | */ |
783 | daniel-mar | 58 | private $last_insert_table = null; |
59 | |||
1116 | daniel-mar | 60 | /** |
61 | * @param OIDplusDatabaseConnection $db |
||
62 | * @return int |
||
63 | * @throws OIDplusException |
||
64 | */ |
||
783 | daniel-mar | 65 | public function insert_id(OIDplusDatabaseConnection $db): int { |
66 | if (!$this->last_insert_table) return 0; |
||
67 | $res = $db->query("select sequence_name from user_tab_identity_cols where table_name = '".strtoupper($this->last_insert_table)."'"); |
||
68 | $row = $res->fetch_array(); |
||
69 | |||
70 | if (!isset($row['sequence_name'])) return 0; |
||
71 | $res = $db->query("select ".$row['sequence_name'].".currval from dual"); |
||
72 | $row = $res->fetch_array(); |
||
73 | return (int)$row['CURRVAL']; |
||
74 | } |
||
75 | |||
1116 | daniel-mar | 76 | /** |
77 | * @param string $cont |
||
78 | * @param string $table |
||
79 | * @param string $prefix |
||
80 | * @return string |
||
81 | */ |
||
82 | public function setupSetTablePrefix(string $cont, string $table, string $prefix): string { |
||
788 | daniel-mar | 83 | $table = strtoupper($table); |
84 | $prefix = strtoupper($prefix); |
||
1130 | daniel-mar | 85 | return str_replace('"'.$table.'"', '"'.$prefix.$table.'"', $cont); |
783 | daniel-mar | 86 | } |
87 | |||
1116 | daniel-mar | 88 | /** |
89 | * @param string $database |
||
90 | * @return string |
||
91 | */ |
||
92 | public function setupCreateDbIfNotExists(string $database): string { |
||
783 | daniel-mar | 93 | // TODO! Implement |
94 | return ""; |
||
95 | } |
||
96 | |||
1116 | daniel-mar | 97 | /** |
98 | * @param string $database |
||
99 | * @return string |
||
100 | */ |
||
101 | public function setupUseDatabase(string $database): string { |
||
783 | daniel-mar | 102 | // TODO! Implement |
103 | return ""; |
||
104 | } |
||
105 | |||
1116 | daniel-mar | 106 | /** |
107 | * @param string $expr1 |
||
108 | * @param string $expr2 |
||
109 | * @return string |
||
110 | */ |
||
111 | public function isNullFunction(string $expr1, string $expr2): string { |
||
783 | daniel-mar | 112 | // Test via "SELECT NVL(null, 'foo') FROM DUAL;" |
113 | return "NVL($expr1, $expr2)"; |
||
114 | } |
||
115 | |||
1116 | daniel-mar | 116 | /** |
117 | * @param string $sql |
||
118 | * @return string |
||
119 | */ |
||
120 | public function filterQuery(string $sql): string { |
||
1240 | daniel-mar | 121 | $sql = trim($sql); |
783 | daniel-mar | 122 | |
1240 | daniel-mar | 123 | // SQL-Queries MUST NOT end with a ";", otherwise error "SQL command not property ended" |
124 | $sql = rtrim($sql, "; \n\r\t\v\x00"); |
||
125 | // SQL/PL-Programs MUST end with a ";" |
||
126 | if (strtolower(substr($sql,-3)) == 'end') $sql .= ';'; |
||
127 | |||
783 | daniel-mar | 128 | // "select 1" is not valid. You need to add "from dual" |
1240 | daniel-mar | 129 | if (str_starts_with(trim(strtolower($sql)),'select') && (stripos($sql,'from') === false)) { |
783 | daniel-mar | 130 | $sql .= ' from dual'; |
131 | } |
||
132 | |||
133 | // Dirty hack!!! We need the name of the last inserted table so that insert_id() |
||
134 | // works. This is a dirty hack, because the invokation of filterQuery() does |
||
135 | // not guarantee that the query was actually executed... |
||
136 | if (preg_match("@insert into (.+) @ismU", $sql, $m)) { |
||
137 | $this->last_insert_table = $m[1]; |
||
138 | } else { |
||
139 | $this->last_insert_table = null; |
||
140 | } |
||
141 | |||
142 | // Comment is a keyword and cannot be used as column name |
||
1116 | daniel-mar | 143 | return str_ireplace('comment', '"COMMENT"', $sql); |
783 | daniel-mar | 144 | } |
145 | |||
1116 | daniel-mar | 146 | /** |
147 | * @param bool $bool |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getSQLBool(bool $bool): string { |
||
783 | daniel-mar | 151 | return $bool ? '1' : '0'; |
152 | } |
||
153 | |||
1116 | daniel-mar | 154 | /** |
155 | * @param string $str |
||
156 | * @return string |
||
157 | */ |
||
158 | public function escapeString(string $str): string { |
||
783 | daniel-mar | 159 | return str_replace("'", "''", $str); |
160 | } |
||
161 | } |