Subversion Repositories oidplus

Rev

Rev 1225 | 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.         /**
  29.          * @return string
  30.          */
  31.         public static function id(): string {
  32.                 return 'mssql';
  33.         }
  34.  
  35.         /**
  36.          * @return string
  37.          */
  38.         public function sqlDate(): string {
  39.                 return 'getdate()';
  40.         }
  41.  
  42.         /**
  43.          * @param OIDplusDatabaseConnection $db
  44.          * @return bool
  45.          */
  46.         public function detect(OIDplusDatabaseConnection $db): bool {
  47.                 try {
  48.                         $vers = $db->query("select @@version as dbms_version")->fetch_object()->dbms_version;
  49.                         return stripos($vers, 'Microsoft SQL Server') !== false;
  50.                 } catch (\Exception $e) {
  51.                         return false;
  52.                 }
  53.         }
  54.  
  55.         /**
  56.          * @param OIDplusDatabaseConnection $db
  57.          * @return int
  58.          * @throws OIDplusException
  59.          */
  60.         public function insert_id(OIDplusDatabaseConnection $db): int {
  61.                 // Note: SCOPE_IDENTITY() does not work, does only give 0.
  62.                 // $res = $db->query("SELECT SCOPE_IDENTITY() AS ID");
  63.  
  64.                 // DM 04 Apr 2023 : Added @@ROWCOUNT , see https://stackoverflow.com/questions/4571911/reset-scope-identity
  65.                 $rc = $db->rowsAffected();
  66.                 if ($rc == -1) {
  67.                         // -1 means: it is not implemented or possible with that driver
  68.                         $res = $db->query("SELECT @@ROWCOUNT AS RC");
  69.                         $row = $res->fetch_array();
  70.                         $rc = (int)$row['RC'];
  71.                 }
  72.                 if ($rc == 0) return 0;
  73.  
  74.                 $res = $db->query("SELECT @@IDENTITY AS ID");
  75.                 $row = $res->fetch_array();
  76.                 return (int)$row['ID'];
  77.         }
  78.  
  79.         /**
  80.          * @param string $cont
  81.          * @param string $table
  82.          * @param string $prefix
  83.          * @return string
  84.          */
  85.         public function setupSetTablePrefix(string $cont, string $table, string $prefix): string {
  86.                 $cont = str_replace('['.$table.']', '['.$prefix.$table.']', $cont);
  87.                 $cont = str_replace("'dbo.$table'", "'dbo.$prefix$table'", $cont);
  88.                 $cont = str_replace('PK_'.$table, 'PK_'.$prefix.$table, $cont);
  89.                 $cont = str_replace('IX_'.$table, 'PK_'.$prefix.$table, $cont);
  90.                 return str_replace('DF__'.$table, 'DF__'.$prefix.$table, $cont);
  91.         }
  92.  
  93.         /**
  94.          * @param string $database
  95.          * @return string
  96.          */
  97.         public function setupCreateDbIfNotExists(string $database): string {
  98.                 return "";
  99.         }
  100.  
  101.         /**
  102.          * @param string $database
  103.          * @return string
  104.          */
  105.         public function setupUseDatabase(string $database): string {
  106.                 return "USE [$database]\n\nGO\n\n";
  107.         }
  108.  
  109.         /**
  110.          * @param string $expr1
  111.          * @param string $expr2
  112.          * @return string
  113.          */
  114.         public function isNullFunction(string $expr1, string $expr2): string {
  115.                 return "isnull($expr1, $expr2)";
  116.         }
  117.  
  118.         /**
  119.          * @param string $sql
  120.          * @return string
  121.          */
  122.         public function filterQuery(string $sql): string {
  123.                 return $sql;
  124.         }
  125.  
  126.         /**
  127.          * @param bool $bool
  128.          * @return string
  129.          */
  130.         public function getSQLBool(bool $bool): string {
  131.                 return $bool ? '1' : '0';
  132.         }
  133.  
  134.         /**
  135.          * @param string $str
  136.          * @return string
  137.          */
  138.         public function escapeString(string $str): string {
  139.                 return str_replace("'", "''", $str);
  140.         }
  141. }
  142.