Subversion Repositories oidplus

Rev

Rev 1086 | Rev 1130 | 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
         * @param string $fieldname
37
         * @param string $order
38
         * @return string
39
         * @throws OIDplusException
40
         */
41
        public function natOrder(string $fieldname, string $order='asc'): string {
783 daniel-mar 42
 
43
                $order = strtolower($order);
44
                if (($order != 'asc') && ($order != 'desc')) {
45
                        throw new OIDplusException(_L('Invalid order "%1" (needs to be "asc" or "desc")',$order));
46
                }
47
 
48
                $out = array();
49
 
50
                $max_arc_len = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE') > 65 ? 65 : OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE'); // Limit of "decimal()" type
51
 
52
                // 1. sort by namespace (oid, guid, ...)
53
                $out[] = "regexp_substr($fieldname, '(.*?)(:|\$)', 1, 1, NULL, 1) $order";
54
 
55
                // 2. sort by first arc (0,1,2)
56
                $tmp = "regexp_substr($fieldname, '(.*?)(:|\$)', 1, 2, NULL, 1)";
57
                $i = 1;
58
                $out[] = "lpad(regexp_substr($tmp, '(.*?)(\\.|\$)', 1, $i, NULL, 1),$max_arc_len,'0') $order";
59
 
60
                for ($i=2; $i<=OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH'); $i++) {
61
                        // 3. Sort by the rest arcs one by one, not that MySQL can only handle decimal(65), not decimal($max_arc_len)
62
                        $out[] = "lpad(regexp_substr($fieldname, '(.*?)(\\.|\$)', 1, $i, NULL, 1),$max_arc_len,'0') $order";
63
                }
64
 
65
                // 4. as last resort, sort by the identifier itself, e.g. if the casts above did fail (happens if it is not an OID)
66
                $out[] = "$fieldname $order";
67
 
68
                return implode(', ', $out);
69
 
70
        }
71
 
1116 daniel-mar 72
        /**
73
         * @return string
74
         */
783 daniel-mar 75
        public function sqlDate(): string {
76
                return 'SYSDATE';
77
        }
78
 
1116 daniel-mar 79
        /**
80
         * @param OIDplusDatabaseConnection $db
81
         * @return bool
82
         */
783 daniel-mar 83
        public function detect(OIDplusDatabaseConnection $db): bool {
84
                try {
85
                        $vers = $db->query("SELECT banner FROM v\$version WHERE banner LIKE 'Oracle%'")->fetch_object()->banner;
86
                        $vers = strtolower($vers);
87
                        return (strpos($vers, 'oracle') !== false);
1050 daniel-mar 88
                } catch (\Exception $e) {
783 daniel-mar 89
                        return false;
90
                }
91
        }
92
 
1116 daniel-mar 93
        /**
94
         * @var ?string
95
         */
783 daniel-mar 96
        private $last_insert_table = null;
97
 
1116 daniel-mar 98
        /**
99
         * @param OIDplusDatabaseConnection $db
100
         * @return int
101
         * @throws OIDplusException
102
         */
783 daniel-mar 103
        public function insert_id(OIDplusDatabaseConnection $db): int {
104
                if (!$this->last_insert_table) return 0;
105
                $res = $db->query("select sequence_name from user_tab_identity_cols where table_name = '".strtoupper($this->last_insert_table)."'");
106
                $row = $res->fetch_array();
107
 
108
                if (!isset($row['sequence_name'])) return 0;
109
                $res = $db->query("select ".$row['sequence_name'].".currval from dual");
110
                $row = $res->fetch_array();
111
                return (int)$row['CURRVAL'];
112
        }
113
 
1116 daniel-mar 114
        /**
115
         * @param string $cont
116
         * @param string $table
117
         * @param string $prefix
118
         * @return string
119
         */
120
        public function setupSetTablePrefix(string $cont, string $table, string $prefix): string {
788 daniel-mar 121
                $table = strtoupper($table);
122
                $prefix = strtoupper($prefix);
783 daniel-mar 123
                $cont = str_replace('"'.$table.'"', '"'.$prefix.$table.'"', $cont);
124
                return $cont;
125
        }
126
 
1116 daniel-mar 127
        /**
128
         * @param string $database
129
         * @return string
130
         */
131
        public function setupCreateDbIfNotExists(string $database): string {
783 daniel-mar 132
                // TODO! Implement
133
                return "";
134
        }
135
 
1116 daniel-mar 136
        /**
137
         * @param string $database
138
         * @return string
139
         */
140
        public function setupUseDatabase(string $database): string {
783 daniel-mar 141
                // TODO! Implement
142
                return "";
143
        }
144
 
1116 daniel-mar 145
        /**
146
         * @param string $expr1
147
         * @param string $expr2
148
         * @return string
149
         */
150
        public function isNullFunction(string $expr1, string $expr2): string {
783 daniel-mar 151
                // Test via "SELECT NVL(null, 'foo') FROM DUAL;"
152
                return "NVL($expr1, $expr2)";
153
        }
154
 
1116 daniel-mar 155
        /**
156
         * @param string $sql
157
         * @return string
158
         */
159
        public function filterQuery(string $sql): string {
783 daniel-mar 160
 
161
                // "select 1" is not valid. You need to add "from dual"
162
                if ((stripos($sql,'select') !== false) && (stripos($sql,'from') === false)) {
163
                        $sql .= ' from dual';
164
                }
165
 
787 daniel-mar 166
                // SQL-Queries MUST NOT end with a ";", otherwise error "SQL command not property ended"
167
                $sql = rtrim(trim($sql), "; \n\r\t\v\x00");
168
                // SQL/PL-Programs MUST end with a ";"
169
                if (strtolower(substr($sql,-3)) == 'end') $sql .= ';';
783 daniel-mar 170
 
171
                // Dirty hack!!! We need the name of the last inserted table so that insert_id()
172
                // works. This is a dirty hack, because the invokation of filterQuery() does
173
                // not guarantee that the query was actually executed...
174
                if (preg_match("@insert into (.+) @ismU", $sql, $m)) {
175
                        $this->last_insert_table = $m[1];
176
                } else {
177
                        $this->last_insert_table = null;
178
                }
179
 
180
                // Comment is a keyword and cannot be used as column name
1116 daniel-mar 181
                return str_ireplace('comment', '"COMMENT"', $sql);
783 daniel-mar 182
        }
183
 
1116 daniel-mar 184
        /**
185
         * @param bool $bool
186
         * @return string
187
         */
188
        public function getSQLBool(bool $bool): string {
783 daniel-mar 189
                return $bool ? '1' : '0';
190
        }
191
 
1116 daniel-mar 192
        /**
193
         * @param string $str
194
         * @return string
195
         */
196
        public function escapeString(string $str): string {
783 daniel-mar 197
                return str_replace("'", "''", $str);
198
        }
199
}