Subversion Repositories oidplus

Rev

Rev 1116 | Rev 1148 | 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);
1130 daniel-mar 123
                return str_replace('"'.$table.'"', '"'.$prefix.$table.'"', $cont);
783 daniel-mar 124
        }
125
 
1116 daniel-mar 126
        /**
127
         * @param string $database
128
         * @return string
129
         */
130
        public function setupCreateDbIfNotExists(string $database): string {
783 daniel-mar 131
                // TODO! Implement
132
                return "";
133
        }
134
 
1116 daniel-mar 135
        /**
136
         * @param string $database
137
         * @return string
138
         */
139
        public function setupUseDatabase(string $database): string {
783 daniel-mar 140
                // TODO! Implement
141
                return "";
142
        }
143
 
1116 daniel-mar 144
        /**
145
         * @param string $expr1
146
         * @param string $expr2
147
         * @return string
148
         */
149
        public function isNullFunction(string $expr1, string $expr2): string {
783 daniel-mar 150
                // Test via "SELECT NVL(null, 'foo') FROM DUAL;"
151
                return "NVL($expr1, $expr2)";
152
        }
153
 
1116 daniel-mar 154
        /**
155
         * @param string $sql
156
         * @return string
157
         */
158
        public function filterQuery(string $sql): string {
783 daniel-mar 159
 
160
                // "select 1" is not valid. You need to add "from dual"
161
                if ((stripos($sql,'select') !== false) && (stripos($sql,'from') === false)) {
162
                        $sql .= ' from dual';
163
                }
164
 
787 daniel-mar 165
                // SQL-Queries MUST NOT end with a ";", otherwise error "SQL command not property ended"
166
                $sql = rtrim(trim($sql), "; \n\r\t\v\x00");
167
                // SQL/PL-Programs MUST end with a ";"
168
                if (strtolower(substr($sql,-3)) == 'end') $sql .= ';';
783 daniel-mar 169
 
170
                // Dirty hack!!! We need the name of the last inserted table so that insert_id()
171
                // works. This is a dirty hack, because the invokation of filterQuery() does
172
                // not guarantee that the query was actually executed...
173
                if (preg_match("@insert into (.+) @ismU", $sql, $m)) {
174
                        $this->last_insert_table = $m[1];
175
                } else {
176
                        $this->last_insert_table = null;
177
                }
178
 
179
                // Comment is a keyword and cannot be used as column name
1116 daniel-mar 180
                return str_ireplace('comment', '"COMMENT"', $sql);
783 daniel-mar 181
        }
182
 
1116 daniel-mar 183
        /**
184
         * @param bool $bool
185
         * @return string
186
         */
187
        public function getSQLBool(bool $bool): string {
783 daniel-mar 188
                return $bool ? '1' : '0';
189
        }
190
 
1116 daniel-mar 191
        /**
192
         * @param string $str
193
         * @return string
194
         */
195
        public function escapeString(string $str): string {
783 daniel-mar 196
                return str_replace("'", "''", $str);
197
        }
198
}