Subversion Repositories oidplus

Rev

Rev 507 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2021 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. if (!defined('INSIDE_OIDPLUS')) die();
  21.  
  22. class OIDplusSqlSlangPluginAccess extends OIDplusSqlSlangPlugin {
  23.  
  24.         public static function id(): string {
  25.                 return 'access';
  26.         }
  27.  
  28.         public function natOrder($fieldname, $order='asc'): string {
  29.  
  30.                 // TODO: Implement
  31.                 return "$fieldname $order";
  32.  
  33.         }
  34.  
  35.         public function sqlDate(): string {
  36.                 return 'date()';
  37.         }
  38.  
  39.         public function detect(OIDplusDatabaseConnection $db): bool {
  40.                 /*
  41.                 if ($tables = @odbc_tables($db->conn)) {
  42.                         while ($row = @odbc_fetch_array($tables)) {
  43.                                 if (($row['TABLE_NAME'] == 'MSysACEs') ||
  44.                                         ($row['TABLE_NAME'] == 'MSysObjects') ||
  45.                                         ($row['TABLE_NAME'] == 'MSysQueries') ||
  46.                                         ($row['TABLE_NAME'] == 'MSysRelationships'))
  47.                                 {
  48.                                         return true;
  49.                                 }
  50.                         }
  51.                 }
  52.                 return false;
  53.                 */
  54.  
  55.                 $err_a = '';
  56.                 try {
  57.                         // On this table, there are often no read permissions, so we need to find out if the error message is different
  58.                         $db->query("select * from MSysObjects");
  59.                 } catch (Exception $e) {
  60.                         $err_a = $db->error();
  61.                 }
  62.                 $err_a = str_replace('MSysObjects', '', $err_a);
  63.  
  64.                 $err_b = '';
  65.                 try {
  66.                         $db->query("select * from XYZObjects");
  67.                 } catch (Exception $e) {
  68.                         $err_b = $db->error();
  69.                 }
  70.                 $err_b = str_replace('XYZObjects', '', $err_b);
  71.  
  72.                 return (!empty($err_a) && !empty($err_b) && ($err_a != $err_b));
  73.         }
  74.  
  75.         public function insert_id(OIDplusDatabaseConnection $db): int {
  76.                 $res = $db->query("SELECT @@IDENTITY AS ID");
  77.                 $row = $res->fetch_array();
  78.                 return (int)$row['ID'];
  79.         }
  80.  
  81.         public function setupSetTablePrefix($cont, $table, $prefix): string {
  82.                 $cont = str_replace('['.$table.']', '['.$prefix.$table.']', $cont);
  83.                 $cont = str_replace('PK_'.$table, 'PK_'.$prefix.$table, $cont);
  84.                 $cont = str_replace('IX_'.$table, 'PK_'.$prefix.$table, $cont);
  85.                 return $cont;
  86.         }
  87.  
  88.         public function setupCreateDbIfNotExists($database): string {
  89.                 return "";
  90.         }
  91.  
  92.         public function setupUseDatabase($database): string {
  93.                 return "";
  94.         }
  95.  
  96.         public function isNullFunction($expr1, $expr2): string {
  97.                 return "iif($expr1 is null, $expr2, $expr1)";
  98.         }
  99.  
  100.         public function filterQuery($sql): string {
  101.                 // value => [value]
  102.                 $sql = preg_replace('@\\b(value)\\b@i', '[\\1]', $sql);
  103.  
  104.                 // This function does following:
  105.                 // Input:  select * from   T left join X on ...  left join Y on ...  left join Z on ...
  106.                 // Output: select * from ((T left join X on ...) left join Y on ...) left join Z on ...
  107.                 $ary = preg_split("@\\bunion\\b@i", $sql);
  108.                 foreach ($ary as &$x) {
  109.                         $INVALIDATE_SEQUENCE = '~X~X~X~X~X~X';
  110.                         $REGEX_JOIN = '(?<!'.$INVALIDATE_SEQUENCE.')(left|right|full|inner)\\s+(outer\\s+){0,1}join';
  111.                         do {
  112.                                 $count = 0;
  113.                                 $x = preg_replace("@from\\s+(.+)\\s+(".$REGEX_JOIN.")\\s+(.+)(".$REGEX_JOIN.")@ismU",
  114.                                                                   'from (\1 '.$INVALIDATE_SEQUENCE.'\2 \5) \6', $x, 1, $count);
  115.                         } while ($count > 0);
  116.                         $x = str_replace($INVALIDATE_SEQUENCE,'',$x);
  117.                 }
  118.                 $sql = implode(' union ', $ary);
  119.                 return $sql;
  120.         }
  121.  
  122.         public function getSQLBool($bool): string {
  123.                 return $bool ? '-1' : '0';
  124.         }
  125.  
  126.         public function escapeString($str): string {
  127.                 return str_replace("'", "''", $str);
  128.         }
  129. }
  130.