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
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 OIDplusSqlSlangPluginAccess extends OIDplusSqlSlangPlugin {
27
 
1116 daniel-mar 28
        /**
29
         * @return string
30
         */
635 daniel-mar 31
        public static function id(): string {
32
                return 'access';
33
        }
34
 
1116 daniel-mar 35
        /**
36
         * @param string $fieldname
37
         * @param string $order
38
         * @return string
39
         */
40
        public function natOrder(string $fieldname, string $order='asc'): string {
635 daniel-mar 41
 
42
                // TODO: Implement
43
                return "$fieldname $order";
44
 
45
        }
46
 
1116 daniel-mar 47
        /**
48
         * @return string
49
         */
635 daniel-mar 50
        public function sqlDate(): string {
51
                return 'date()';
52
        }
53
 
1116 daniel-mar 54
        /**
55
         * @param OIDplusDatabaseConnection $db
56
         * @return bool
57
         */
635 daniel-mar 58
        public function detect(OIDplusDatabaseConnection $db): bool {
59
                /*
60
                if ($tables = @odbc_tables($db->conn)) {
61
                        while ($row = @odbc_fetch_array($tables)) {
62
                                if (($row['TABLE_NAME'] == 'MSysACEs') ||
63
                                        ($row['TABLE_NAME'] == 'MSysObjects') ||
64
                                        ($row['TABLE_NAME'] == 'MSysQueries') ||
65
                                        ($row['TABLE_NAME'] == 'MSysRelationships'))
66
                                {
67
                                        return true;
68
                                }
69
                        }
70
                }
71
                return false;
72
                */
73
 
74
                $err_a = '';
75
                try {
76
                        // On this table, there are often no read permissions, so we need to find out if the error message is different
77
                        $db->query("select * from MSysObjects");
1050 daniel-mar 78
                } catch (\Exception $e) {
635 daniel-mar 79
                        $err_a = $db->error();
80
                }
81
                $err_a = str_replace('MSysObjects', '', $err_a);
82
 
83
                $err_b = '';
84
                try {
85
                        $db->query("select * from XYZObjects");
1050 daniel-mar 86
                } catch (\Exception $e) {
635 daniel-mar 87
                        $err_b = $db->error();
88
                }
89
                $err_b = str_replace('XYZObjects', '', $err_b);
90
 
91
                return (!empty($err_a) && !empty($err_b) && ($err_a != $err_b));
92
        }
93
 
1116 daniel-mar 94
        /**
95
         * @param OIDplusDatabaseConnection $db
96
         * @return int
97
         * @throws OIDplusException
98
         */
635 daniel-mar 99
        public function insert_id(OIDplusDatabaseConnection $db): int {
100
                $res = $db->query("SELECT @@IDENTITY AS ID");
101
                $row = $res->fetch_array();
102
                return (int)$row['ID'];
103
        }
104
 
1116 daniel-mar 105
        /**
106
         * @param string $cont
107
         * @param string $table
108
         * @param string $prefix
109
         * @return string
110
         */
111
        public function setupSetTablePrefix(string $cont, string $table, string $prefix): string {
635 daniel-mar 112
                $cont = str_replace('['.$table.']', '['.$prefix.$table.']', $cont);
113
                $cont = str_replace('PK_'.$table, 'PK_'.$prefix.$table, $cont);
1130 daniel-mar 114
                return str_replace('IX_'.$table, 'PK_'.$prefix.$table, $cont);
635 daniel-mar 115
        }
116
 
1116 daniel-mar 117
        /**
118
         * @param string $database
119
         * @return string
120
         */
121
        public function setupCreateDbIfNotExists(string $database): string {
635 daniel-mar 122
                return "";
123
        }
124
 
1116 daniel-mar 125
        /**
126
         * @param string $database
127
         * @return string
128
         */
129
        public function setupUseDatabase(string $database): string {
635 daniel-mar 130
                return "";
131
        }
132
 
1116 daniel-mar 133
        /**
134
         * @param string $expr1
135
         * @param string $expr2
136
         * @return string
137
         */
138
        public function isNullFunction(string $expr1, string $expr2): string {
635 daniel-mar 139
                return "iif($expr1 is null, $expr2, $expr1)";
140
        }
141
 
1116 daniel-mar 142
        /**
143
         * @param string $sql
144
         * @return string
145
         */
146
        public function filterQuery(string $sql): string {
635 daniel-mar 147
                // value => [value]
148
                $sql = preg_replace('@\\b(value)\\b@i', '[\\1]', $sql);
149
 
150
                // This function does following:
151
                // Input:  select * from   T left join X on ...  left join Y on ...  left join Z on ...
152
                // Output: select * from ((T left join X on ...) left join Y on ...) left join Z on ...
153
                $ary = preg_split("@\\bunion\\b@i", $sql);
154
                foreach ($ary as &$x) {
155
                        $INVALIDATE_SEQUENCE = '~X~X~X~X~X~X';
156
                        $REGEX_JOIN = '(?<!'.$INVALIDATE_SEQUENCE.')(left|right|full|inner)\\s+(outer\\s+){0,1}join';
157
                        do {
158
                                $count = 0;
159
                                $x = preg_replace("@from\\s+(.+)\\s+(".$REGEX_JOIN.")\\s+(.+)(".$REGEX_JOIN.")@ismU",
160
                                                                  'from (\1 '.$INVALIDATE_SEQUENCE.'\2 \5) \6', $x, 1, $count);
161
                        } while ($count > 0);
162
                        $x = str_replace($INVALIDATE_SEQUENCE,'',$x);
163
                }
1130 daniel-mar 164
                return implode(' union ', $ary);
635 daniel-mar 165
        }
166
 
1116 daniel-mar 167
        /**
168
         * @param bool $bool
169
         * @return string
170
         */
171
        public function getSQLBool(bool $bool): string {
635 daniel-mar 172
                return $bool ? '-1' : '0';
173
        }
174
 
1116 daniel-mar 175
        /**
176
         * @param string $str
177
         * @return string
178
         */
179
        public function escapeString(string $str): string {
635 daniel-mar 180
                return str_replace("'", "''", $str);
181
        }
182
}