Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
274 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 OIDplusSqlSlangPluginMsSQL extends OIDplusSqlSlangPlugin {
21
 
22
        public static function id(): string {
23
                return 'mssql';
24
        }
25
 
26
        public function natOrder($fieldname, $order='asc'): string {
27
 
28
                $order = strtolower($order);
29
                if (($order != 'asc') && ($order != 'desc')) {
30
                        throw new OIDplusException("Invalid order '$order' (needs to be 'asc' or 'desc')");
31
                }
32
 
33
                $out = array();
34
 
35
                $max_arc_len = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE');
36
 
37
                // 1. sort by namespace (oid, guid, ...)
38
                $out[] = "SUBSTRING($fieldname,1,CHARINDEX(':',$fieldname)-1) $order";
39
 
40
                for ($i=1; $i<=OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH'); $i++) {
41
                        // 2. Sort by the rest arcs one by one; note that MySQL can only handle decimal(65), not decimal($max_arc_len)
42
                        $out[] = "dbo.getOidArc($fieldname, $max_arc_len, $i) $order";
43
                }
44
 
45
                // 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)
46
                $out[] = "$fieldname $order";
47
 
48
                return implode(', ', $out);
49
 
50
        }
51
 
52
        public function sqlDate(): string {
53
                return 'getdate()';
54
        }
55
 
56
        public function detect(): bool {
57
                try {
58
                        $vers = $this->query("select @@version as dbms_version")->fetch_object()->dbms_version;
59
                        $vers = strtolower($vers);
60
                        return strpos($vers, 'microsoft sql server') !== false;
61
                } catch (Exception $e) {
62
                        return false;
63
                }
64
        }
65
 
66
        public function insert_id(): int {
67
                // Note: SCOPE_IDENTITY() does not work, does only give 0.
68
                // $res = $this->query("SELECT SCOPE_IDENTITY() AS ID");
69
                $res = $this->query("SELECT @@IDENTITY AS ID");
70
                $row = $res->fetch_array();
71
                return (int)$row['ID'];
72
        }
73
 
74
        public function setupSetTablePrefix($cont, $table, $prefix): string {
75
                $cont = str_replace('['.$table.']', '['.$prefix.$table.']', $cont);
76
                $cont = str_replace("'dbo.$table'", "'dbo.$prefix$table'", $cont);
77
                $cont = str_replace('PK_'.$table, 'PK_'.$prefix.$table, $cont);
78
                $cont = str_replace('IX_'.$table, 'PK_'.$prefix.$table, $cont);
79
                $cont = str_replace('DF__'.$table, 'DF__'.$prefix.$table, $cont);
80
                return $cont;
81
        }
82
 
83
        public function setupCreateDbIfNotExists($database): string {
84
                return "";
85
        }
86
 
87
        public function setupUseDatabase($database): string {
88
                return "USE [$database]\n\nGO\n\n";
89
        }
90
 
91
}