Subversion Repositories oidplus

Rev

Rev 1050 | Rev 1116 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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