Subversion Repositories oidplus

Rev

Rev 1160 | 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 OIDplusSqlSlangPluginAccess extends OIDplusSqlSlangPlugin {
  27.  
  28.         /**
  29.          * @return string
  30.          */
  31.         public static function id(): string {
  32.                 return 'access';
  33.         }
  34.  
  35.         /**
  36.          * @return string
  37.          */
  38.         public function sqlDate(): string {
  39.                 return 'now()';
  40.         }
  41.  
  42.         /**
  43.          * @param OIDplusDatabaseConnection $db
  44.          * @return bool
  45.          */
  46.         public function detect(OIDplusDatabaseConnection $db): bool {
  47.                 /*
  48.                 if ($tables = @odbc_tables($db->conn)) {
  49.                         while ($row = @odbc_fetch_array($tables)) {
  50.                                 if (($row['TABLE_NAME'] == 'MSysACEs') ||
  51.                                         ($row['TABLE_NAME'] == 'MSysObjects') ||
  52.                                         ($row['TABLE_NAME'] == 'MSysQueries') ||
  53.                                         ($row['TABLE_NAME'] == 'MSysRelationships'))
  54.                                 {
  55.                                         return true;
  56.                                 }
  57.                         }
  58.                 }
  59.                 return false;
  60.                 */
  61.  
  62.                 $err_a = '';
  63.                 try {
  64.                         // On this table, there are often no read permissions, so we need to find out if the error message is different
  65.                         $db->query("select * from MSysObjects");
  66.                 } catch (\Exception $e) {
  67.                         $err_a = $db->error();
  68.                 }
  69.                 $err_a = str_ireplace('MSysObjects', '', $err_a);
  70.  
  71.                 $err_b = '';
  72.                 try {
  73.                         $db->query("select * from XYZObjects");
  74.                 } catch (\Exception $e) {
  75.                         $err_b = $db->error();
  76.                 }
  77.                 $err_b = str_ireplace('XYZObjects', '', $err_b);
  78.  
  79.                 return (!empty($err_a) && !empty($err_b) && ($err_a != $err_b));
  80.         }
  81.  
  82.         /**
  83.          * @param OIDplusDatabaseConnection $db
  84.          * @return int
  85.          * @throws OIDplusException
  86.          */
  87.         public function insert_id(OIDplusDatabaseConnection $db): int {
  88.                 $res = $db->query("SELECT @@IDENTITY AS ID");
  89.                 $row = $res->fetch_array();
  90.                 return (int)$row['ID'];
  91.         }
  92.  
  93.         /**
  94.          * @param string $cont
  95.          * @param string $table
  96.          * @param string $prefix
  97.          * @return string
  98.          */
  99.         public function setupSetTablePrefix(string $cont, string $table, string $prefix): string {
  100.                 $cont = str_replace('['.$table.']', '['.$prefix.$table.']', $cont);
  101.                 $cont = str_replace('PK_'.$table, 'PK_'.$prefix.$table, $cont);
  102.                 return str_replace('IX_'.$table, 'PK_'.$prefix.$table, $cont);
  103.         }
  104.  
  105.         /**
  106.          * @param string $database
  107.          * @return string
  108.          */
  109.         public function setupCreateDbIfNotExists(string $database): string {
  110.                 return "";
  111.         }
  112.  
  113.         /**
  114.          * @param string $database
  115.          * @return string
  116.          */
  117.         public function setupUseDatabase(string $database): string {
  118.                 return "";
  119.         }
  120.  
  121.         /**
  122.          * @param string $expr1
  123.          * @param string $expr2
  124.          * @return string
  125.          */
  126.         public function isNullFunction(string $expr1, string $expr2): string {
  127.                 return "iif($expr1 is null, $expr2, $expr1)";
  128.         }
  129.  
  130.         /**
  131.          * @param string $sql
  132.          * @return string
  133.          */
  134.         public function filterQuery(string $sql): string {
  135.                 // value => [value]
  136.                 $sql = preg_replace('@\\b(value)\\b@i', '[\\1]', $sql);
  137.  
  138.                 // This function does following:
  139.                 // Input:  select * from   T left join X on ...  left join Y on ...  left join Z on ...
  140.                 // Output: select * from ((T left join X on ...) left join Y on ...) left join Z on ...
  141.                 $ary = preg_split("@\\bunion\\b@i", $sql);
  142.                 foreach ($ary as &$x) {
  143.                         $INVALIDATE_SEQUENCE = '~X~X~X~X~X~X';
  144.                         $REGEX_JOIN = '(?<!'.$INVALIDATE_SEQUENCE.')(left|right|full|inner)\\s+(outer\\s+){0,1}join';
  145.                         do {
  146.                                 $count = 0;
  147.                                 $x = preg_replace("@from\\s+(.+)\\s+(".$REGEX_JOIN.")\\s+(.+)(".$REGEX_JOIN.")@ismU",
  148.                                                                   'from (\1 '.$INVALIDATE_SEQUENCE.'\2 \5) \6', $x, 1, $count);
  149.                         } while ($count > 0);
  150.                         $x = str_replace($INVALIDATE_SEQUENCE,'',$x);
  151.                 }
  152.                 return implode(' union ', $ary);
  153.         }
  154.  
  155.         /**
  156.          * @param bool $bool
  157.          * @return string
  158.          */
  159.         public function getSQLBool(bool $bool): string {
  160.                 return $bool ? '-1' : '0';
  161.         }
  162.  
  163.         /**
  164.          * @param string $str
  165.          * @return string
  166.          */
  167.         public function escapeString(string $str): string {
  168.                 return str_replace("'", "''", $str);
  169.         }
  170. }
  171.