Subversion Repositories oidplus

Rev

Rev 502 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
502 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
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
 
20
class OIDplusSqlSlangPluginAccess extends OIDplusSqlSlangPlugin {
21
 
22
        public static function id(): string {
23
                return 'access';
24
        }
25
 
26
        public function natOrder($fieldname, $order='asc'): string {
27
 
28
                // TODO: Implement
29
                return "$fieldname $order";
30
 
31
        }
32
 
33
        public function sqlDate(): string {
34
                return 'date()';
35
        }
36
 
37
        public function detect(OIDplusDatabaseConnection $db): bool {
38
                /*
39
                if ($tables = @odbc_tables($db->conn)) {
40
                        while ($row = @odbc_fetch_array($tables)) {
41
                                if (($row['TABLE_NAME'] == 'MSysACEs') ||
42
                                        ($row['TABLE_NAME'] == 'MSysObjects') ||
43
                                        ($row['TABLE_NAME'] == 'MSysQueries') ||
44
                                        ($row['TABLE_NAME'] == 'MSysRelationships'))
45
                                {
46
                                        return true;
47
                                }
48
                        }
49
                }
50
                return false;
51
                */
52
 
53
                $err_a = '';
54
                try {
55
                        // On this table, there are often no read permissions, so we need to find out if the error message is different
56
                        $db->query("select * from MSysObjects");
57
                } catch (Exception $e) {
58
                        $err_a = $db->error();
59
                }
60
                $err_a = str_replace('MSysObjects', '', $err_a);
61
 
62
                $err_b = '';
63
                try {
64
                        $db->query("select * from XYZObjects");
65
                } catch (Exception $e) {
66
                        $err_b = $db->error();
67
                }
68
                $err_b = str_replace('XYZObjects', '', $err_b);
69
 
70
                return (!empty($err_a) && !empty($err_b) && ($err_a != $err_b));
71
        }
72
 
73
        public function insert_id(OIDplusDatabaseConnection $db): int {
74
                $res = $db->query("SELECT @@IDENTITY AS ID");
75
                $row = $res->fetch_array();
76
                return (int)$row['ID'];
77
        }
78
 
79
        public function setupSetTablePrefix($cont, $table, $prefix): string {
80
                $cont = str_replace('['.$table.']', '['.$prefix.$table.']', $cont);
81
                $cont = str_replace('PK_'.$table, 'PK_'.$prefix.$table, $cont);
82
                $cont = str_replace('IX_'.$table, 'PK_'.$prefix.$table, $cont);
83
                return $cont;
84
        }
85
 
86
        public function setupCreateDbIfNotExists($database): string {
87
                return "";
88
        }
89
 
90
        public function setupUseDatabase($database): string {
91
                return "";
92
        }
93
 
94
        public function isNullFunction($expr1, $expr2): string {
95
                return "iif($expr1 is null, $expr2, $expr1)";
96
        }
97
 
98
        public function filterQuery($sql): string {
99
                // value => [value]
100
                $sql = preg_replace('@\\b(value)\\b@i', '[\\1]', $sql);
101
 
102
                // This function does following:
103
                // Input:  select * from   T left join X on ...  left join Y on ...  left join Z on ...
104
                // Output: select * from ((T left join X on ...) left join Y on ...) left join Z on ...
105
                $ary = preg_split("@\\bunion\\b@i", $sql);
106
                foreach ($ary as &$x) {
107
                        $INVALIDATE_SEQUENCE = '~X~X~X~X~X~X';
108
                        $REGEX_JOIN = '(?<!'.$INVALIDATE_SEQUENCE.')(left|right|full|inner)\\s+(outer\\s+){0,1}join';
109
                        do {
110
                                $count = 0;
111
                                $x = preg_replace("@from\\s+(.+)\\s+(".$REGEX_JOIN.")\\s+(.+)(".$REGEX_JOIN.")@ismU",
112
                                                                  'from (\1 '.$INVALIDATE_SEQUENCE.'\2 \5) \6', $x, 1, $count);
113
                        } while ($count > 0);
114
                        $x = str_replace($INVALIDATE_SEQUENCE,'',$x);
115
                }
116
                $sql = implode(' union ', $ary);
117
                return $sql;
118
        }
119
 
120
        public function getSQLBool($bool): string {
121
                return $bool ? '-1' : '0';
122
        }
507 daniel-mar 123
 
124
        public function escapeString($str): string {
125
                return str_replace("'", "''", $str);
126
        }
127
}