Subversion Repositories oidplus

Rev

Rev 635 | Rev 1086 | 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
5
 * Copyright 2019 - 2021 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
 
1050 daniel-mar 20
namespace ViaThinkSoft\OIDplus;
635 daniel-mar 21
 
22
class OIDplusSqlSlangPluginMsSQL extends OIDplusSqlSlangPlugin {
23
 
24
        public static function id(): string {
25
                return 'mssql';
26
        }
27
 
28
        public function natOrder($fieldname, $order='asc'): string {
29
 
30
                $order = strtolower($order);
31
                if (($order != 'asc') && ($order != 'desc')) {
32
                        throw new OIDplusException(_L('Invalid order "%1" (needs to be "asc" or "desc")',$order));
33
                }
34
 
35
                $out = array();
36
 
37
                $max_arc_len = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE');
38
 
39
                // 1. sort by namespace (oid, guid, ...)
40
                $out[] = "SUBSTRING($fieldname,1,CHARINDEX(':',$fieldname)-1) $order";
41
 
42
                for ($i=1; $i<=OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH'); $i++) {
43
                        // 2. Sort by the rest arcs one by one; note that MySQL can only handle decimal(65), not decimal($max_arc_len)
44
                        $out[] = "dbo.getOidArc($fieldname, $max_arc_len, $i) $order";
45
                }
46
 
47
                // 3. as last resort, sort by the identifier itself, e.g. if the function getOidArc always return 0 (happens if it is not an OID)
48
                $out[] = "$fieldname $order";
49
 
50
                return implode(', ', $out);
51
 
52
        }
53
 
54
        public function sqlDate(): string {
55
                return 'getdate()';
56
        }
57
 
58
        public function detect(OIDplusDatabaseConnection $db): bool {
59
                try {
60
                        $vers = $db->query("select @@version as dbms_version")->fetch_object()->dbms_version;
61
                        $vers = strtolower($vers);
62
                        return strpos($vers, 'microsoft sql server') !== false;
1050 daniel-mar 63
                } catch (\Exception $e) {
635 daniel-mar 64
                        return false;
65
                }
66
        }
67
 
68
        public function insert_id(OIDplusDatabaseConnection $db): int {
69
                // Note: SCOPE_IDENTITY() does not work, does only give 0.
70
                // $res = $db->query("SELECT SCOPE_IDENTITY() AS ID");
71
                $res = $db->query("SELECT @@IDENTITY AS ID");
72
                $row = $res->fetch_array();
73
                return (int)$row['ID'];
74
        }
75
 
76
        public function setupSetTablePrefix($cont, $table, $prefix): string {
77
                $cont = str_replace('['.$table.']', '['.$prefix.$table.']', $cont);
78
                $cont = str_replace("'dbo.$table'", "'dbo.$prefix$table'", $cont);
79
                $cont = str_replace('PK_'.$table, 'PK_'.$prefix.$table, $cont);
80
                $cont = str_replace('IX_'.$table, 'PK_'.$prefix.$table, $cont);
81
                $cont = str_replace('DF__'.$table, 'DF__'.$prefix.$table, $cont);
82
                return $cont;
83
        }
84
 
85
        public function setupCreateDbIfNotExists($database): string {
86
                return "";
87
        }
88
 
89
        public function setupUseDatabase($database): string {
90
                return "USE [$database]\n\nGO\n\n";
91
        }
92
 
93
        public function isNullFunction($expr1, $expr2): string {
94
                return "isnull($expr1, $expr2)";
95
        }
96
 
97
        public function filterQuery($sql): string {
98
                return $sql;
99
        }
100
 
101
        public function getSQLBool($bool): string {
102
                return $bool ? '1' : '0';
103
        }
104
 
105
        public function escapeString($str): string {
106
                return str_replace("'", "''", $str);
107
        }
108
}