Subversion Repositories oidplus

Rev

Rev 1240 | 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 OIDplusSqlSlangPluginFirebird extends OIDplusSqlSlangPlugin {
  27.  
  28.         /**
  29.          * @return string
  30.          */
  31.         public static function id(): string {
  32.                 return 'firebird';
  33.         }
  34.  
  35.         /**
  36.          * @return string
  37.          */
  38.         public function sqlDate(): string {
  39.                 // Firebird 3 :  LOCALTIMESTAMP == current_timestamp
  40.                 // Firebird 4 :  LOCALTIMESTAMP is without timezone and
  41.                 //               current_timestamp is with timezone.
  42.                 // PDO seems to have big problems with the "time stamp with time zone"
  43.                 // data type, since the plugin "adminPages => Systeminfo" shows an
  44.                 // empty string for "select current_timestamp from ###config".
  45.                 // Passing current_timestamp into a "insert into" query works however...
  46.                 // For now, we use LOCALTIMESTAMP. It does not seem to make a difference
  47.                 //return 'current_timestamp';
  48.                 return 'LOCALTIMESTAMP';
  49.         }
  50.  
  51.         /**
  52.          * @param OIDplusDatabaseConnection $db
  53.          * @return bool
  54.          */
  55.         public function detect(OIDplusDatabaseConnection $db): bool {
  56.                 try {
  57.                         return $db->query('select * from rdb$character_sets;')->num_rows() > 0;
  58.                 } catch (\Exception $e) {
  59.                         return false;
  60.                 }
  61.         }
  62.  
  63.         /**
  64.          * @var mixed
  65.          */
  66.         private $last_insert_id = null;
  67.  
  68.         /**
  69.          * @param OIDplusDatabaseConnection $db
  70.          * @return int
  71.          * @throws OIDplusException
  72.          */
  73.         public function insert_id(OIDplusDatabaseConnection $db): int {
  74.                 if (!is_numeric($this->last_insert_id)) return -1;
  75.                 return $this->last_insert_id ?? -1;
  76.         }
  77.  
  78.         /**
  79.          * This gives the SQL slang plugin the chance to review the result before it is passed to the application.
  80.          * @param OIDplusQueryResult $res
  81.          * @param string $sql
  82.          * @param array|null $prepared_args
  83.          * @return void
  84.          */
  85.         public function reviewResult(OIDplusQueryResult $res, string $sql, array $prepared_args=null) {
  86.                 if (str_starts_with(trim(strtolower($sql)),'insert')) {
  87.                         $this->last_insert_id = $res->fetch_array()["id"];
  88.                 } else {
  89.                         $this->last_insert_id = null;
  90.                 }
  91.         }
  92.  
  93.         /**
  94.          * @param string $sql
  95.          * @return bool
  96.          */
  97.         public function fetchableRowsExpected(string $sql): bool {
  98.                 return str_starts_with(trim(strtolower($sql)),'select') || str_starts_with(trim(strtolower($sql)),'insert');
  99.         }
  100.  
  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 {
  108.                 $table = strtoupper($table);
  109.                 $prefix = strtoupper($prefix);
  110.                 return str_replace('"'.$table.'"', '"'.$prefix.$table.'"', $cont);
  111.         }
  112.  
  113.         /**
  114.          * @param string $database
  115.          * @return string
  116.          */
  117.         public function setupCreateDbIfNotExists(string $database): string {
  118.                 // TODO! Implement
  119.                 return "";
  120.         }
  121.  
  122.         /**
  123.          * @param string $database
  124.          * @return string
  125.          */
  126.         public function setupUseDatabase(string $database): string {
  127.                 // TODO! Implement
  128.                 return "";
  129.         }
  130.  
  131.         /**
  132.          * @param string $expr1
  133.          * @param string $expr2
  134.          * @return string
  135.          */
  136.         public function isNullFunction(string $expr1, string $expr2): string {
  137.                 return "COALESCE($expr1, $expr2)";
  138.         }
  139.  
  140.         /**
  141.          * @param string $sql
  142.          * @return string
  143.          */
  144.         public function filterQuery(string $sql): string {
  145.                 // Make sure that the query does not end with ";", otherwise we cannot append stuff to it
  146.                 $sql = trim($sql);
  147.                 $sql = rtrim($sql, "; \n\r\t\v\x00");
  148.  
  149.                 // Rewrite INSERT queries, so we can extract the ID in reviewResult()
  150.                 if (str_starts_with(trim(strtolower($sql)),'insert')) {
  151.                         $sql .= " returning id";
  152.                 }
  153.  
  154.                 // "select 1" is not valid. You need to add "from RDB$DATABASE"
  155.                 if (str_starts_with(trim(strtolower($sql)),'select') && (stripos($sql,'from') === false)) {
  156.                         $sql .= ' from RDB$DATABASE';
  157.                 }
  158.  
  159.                 // Value is a keyword and cannot be used as column name
  160.                 $sql = str_ireplace('value', '"VALUE"', $sql);
  161.                 $sql = str_ireplace('"VALUE"s', 'values', $sql);
  162.  
  163.                 return $sql;
  164.         }
  165.  
  166.         /**
  167.          * @param bool $bool
  168.          * @return string
  169.          */
  170.         public function getSQLBool(bool $bool): string {
  171.                 return $bool ? '1' : '0';
  172.         }
  173.  
  174.         /**
  175.          * @param string $str
  176.          * @return string
  177.          */
  178.         public function escapeString(string $str): string {
  179.                 return str_replace("'", "''", $str);
  180.         }
  181.  
  182.         /**
  183.          * @param string $sql
  184.          * @return string
  185.          */
  186.         public function lowerCase(string $sql): string {
  187.                 return "lower($sql)";
  188.         }
  189.  
  190.         /**
  191.          * @param string $sql
  192.          * @return string
  193.          */
  194.         public function upperCase(string $sql): string {
  195.                 return "upper($sql)";
  196.         }
  197. }
  198.