Subversion Repositories oidplus

Rev

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