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 OIDplusSqlSlangPluginMsSQL extends OIDplusSqlSlangPlugin {
27
 
1116 daniel-mar 28
        /**
29
         * @return string
30
         */
635 daniel-mar 31
        public static function id(): string {
32
                return 'mssql';
33
        }
34
 
1116 daniel-mar 35
        /**
36
         * @param string $fieldname
37
         * @param string $order
38
         * @return string
39
         * @throws OIDplusException
40
         */
41
        public function natOrder(string $fieldname, string $order='asc'): string {
635 daniel-mar 42
 
43
                $order = strtolower($order);
44
                if (($order != 'asc') && ($order != 'desc')) {
45
                        throw new OIDplusException(_L('Invalid order "%1" (needs to be "asc" or "desc")',$order));
46
                }
47
 
48
                $out = array();
49
 
50
                $max_arc_len = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_ARC_SIZE');
51
 
52
                // 1. sort by namespace (oid, guid, ...)
53
                $out[] = "SUBSTRING($fieldname,1,CHARINDEX(':',$fieldname)-1) $order";
54
 
55
                for ($i=1; $i<=OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH'); $i++) {
56
                        // 2. Sort by the rest arcs one by one; note that MySQL can only handle decimal(65), not decimal($max_arc_len)
57
                        $out[] = "dbo.getOidArc($fieldname, $max_arc_len, $i) $order";
58
                }
59
 
60
                // 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)
61
                $out[] = "$fieldname $order";
62
 
63
                return implode(', ', $out);
64
 
65
        }
66
 
1116 daniel-mar 67
        /**
68
         * @return string
69
         */
635 daniel-mar 70
        public function sqlDate(): string {
71
                return 'getdate()';
72
        }
73
 
1116 daniel-mar 74
        /**
75
         * @param OIDplusDatabaseConnection $db
76
         * @return bool
77
         */
635 daniel-mar 78
        public function detect(OIDplusDatabaseConnection $db): bool {
79
                try {
80
                        $vers = $db->query("select @@version as dbms_version")->fetch_object()->dbms_version;
81
                        $vers = strtolower($vers);
82
                        return strpos($vers, 'microsoft sql server') !== false;
1050 daniel-mar 83
                } catch (\Exception $e) {
635 daniel-mar 84
                        return false;
85
                }
86
        }
87
 
1116 daniel-mar 88
        /**
89
         * @param OIDplusDatabaseConnection $db
90
         * @return int
91
         * @throws OIDplusException
92
         */
635 daniel-mar 93
        public function insert_id(OIDplusDatabaseConnection $db): int {
94
                // Note: SCOPE_IDENTITY() does not work, does only give 0.
95
                // $res = $db->query("SELECT SCOPE_IDENTITY() AS ID");
96
                $res = $db->query("SELECT @@IDENTITY AS ID");
97
                $row = $res->fetch_array();
98
                return (int)$row['ID'];
99
        }
100
 
1116 daniel-mar 101
        /**
102
         * @param string $cont
103
         * @param string $table
104
         * @param string $prefix
105
         * @return string
106
         */
107
        public function setupSetTablePrefix(string $cont, string $table, string $prefix): string {
635 daniel-mar 108
                $cont = str_replace('['.$table.']', '['.$prefix.$table.']', $cont);
109
                $cont = str_replace("'dbo.$table'", "'dbo.$prefix$table'", $cont);
110
                $cont = str_replace('PK_'.$table, 'PK_'.$prefix.$table, $cont);
111
                $cont = str_replace('IX_'.$table, 'PK_'.$prefix.$table, $cont);
1130 daniel-mar 112
                return str_replace('DF__'.$table, 'DF__'.$prefix.$table, $cont);
635 daniel-mar 113
        }
114
 
1116 daniel-mar 115
        /**
116
         * @param string $database
117
         * @return string
118
         */
119
        public function setupCreateDbIfNotExists(string $database): string {
635 daniel-mar 120
                return "";
121
        }
122
 
1116 daniel-mar 123
        /**
124
         * @param string $database
125
         * @return string
126
         */
127
        public function setupUseDatabase(string $database): string {
635 daniel-mar 128
                return "USE [$database]\n\nGO\n\n";
129
        }
130
 
1116 daniel-mar 131
        /**
132
         * @param string $expr1
133
         * @param string $expr2
134
         * @return string
135
         */
136
        public function isNullFunction(string $expr1, string $expr2): string {
635 daniel-mar 137
                return "isnull($expr1, $expr2)";
138
        }
139
 
1116 daniel-mar 140
        /**
141
         * @param string $sql
142
         * @return string
143
         */
144
        public function filterQuery(string $sql): string {
635 daniel-mar 145
                return $sql;
146
        }
147
 
1116 daniel-mar 148
        /**
149
         * @param bool $bool
150
         * @return string
151
         */
152
        public function getSQLBool(bool $bool): string {
635 daniel-mar 153
                return $bool ? '1' : '0';
154
        }
155
 
1116 daniel-mar 156
        /**
157
         * @param string $str
158
         * @return string
159
         */
160
        public function escapeString(string $str): string {
635 daniel-mar 161
                return str_replace("'", "''", $str);
162
        }
163
}