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 OIDplusSqlSlangPluginSQLite extends OIDplusSqlSlangPlugin {
  27.  
  28.         public static function id(): string {
  29.                 return 'sqlite';
  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.                 // If the SQLite database is accessed through the SQLite3 plugin,
  42.                 // we use an individual collation, therefore OIDplusDatabasePluginSQLite3 overrides
  43.                 // natOrder().
  44.                 // If we connected to SQLite using ODBC or PDO, we need to do something else,
  45.                 // but that solution is complex, slow and wrong (since it does not support
  46.                 // UUIDs etc.)
  47.  
  48.                 $max_depth = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH');
  49.                 if ($max_depth > 11) $max_depth = 11; // SQLite3 will crash if max depth > 11 (parser stack overflow); (TODO: can we do something else?!?!)
  50.  
  51.                 // 1. sort by namespace (oid, guid, ...)
  52.                 $out[] = "substr($fieldname,0,instr($fieldname,':')) $order";
  53.  
  54.                 // 2. Sort by the rest arcs one by one
  55.                 for ($i=1; $i<=$max_depth; $i++) {
  56.                         if ($i==1) {
  57.                                 $arc = "substr($fieldname,5)||'.'";
  58.                                 $fieldname = $arc;
  59.                                 $arc = "substr($arc,0,instr($arc,'.'))";
  60.                                 $out[] = "cast($arc as integer) $order";
  61.                         } else {
  62.                                 $arc = "ltrim(ltrim($fieldname,'0123456789'),'.')";
  63.                                 $fieldname = $arc;
  64.                                 $arc = "substr($arc,0,instr($arc,'.'))";
  65.                                 $out[] = "cast($arc as integer)  $order";
  66.                         }
  67.                 }
  68.  
  69.                 // 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)
  70.                 $out[] = "$fieldname $order";
  71.  
  72.                 return implode(', ', $out);
  73.  
  74.         }
  75.  
  76.         public function sqlDate(): string {
  77.                 return 'datetime()';
  78.         }
  79.  
  80.         public function detect(OIDplusDatabaseConnection $db): bool {
  81.                 try {
  82.                         $db->query("select sqlite_version as dbms_version");
  83.                         return true;
  84.                 } catch (\Exception $e) {
  85.                         return false;
  86.                 }
  87.         }
  88.  
  89.         public function insert_id(OIDplusDatabaseConnection $db): int {
  90.                 $res = $db->query("SELECT last_insert_rowid() AS ID");
  91.                 $row = $res->fetch_array();
  92.                 return (int)$row['ID'];
  93.         }
  94.  
  95.         public function setupSetTablePrefix($cont, $table, $prefix): string {
  96.                 $cont = str_replace('`'.$table.'`', '`'.$prefix.$table.'`', $cont);
  97.                 return $cont;
  98.         }
  99.  
  100.         public function setupCreateDbIfNotExists($database): string {
  101.                 return "";
  102.         }
  103.  
  104.         public function setupUseDatabase($database): string {
  105.                 return "";
  106.         }
  107.  
  108.         public function isNullFunction($expr1, $expr2): string {
  109.                 return "ifnull($expr1, $expr2)";
  110.         }
  111.  
  112.         public function filterQuery($sql): string {
  113.                 return $sql;
  114.         }
  115.  
  116.         public function getSQLBool($bool): string {
  117.                 return $bool ? '1' : '0';
  118.         }
  119.  
  120.         public function escapeString($str): string {
  121.                 return str_replace("'", "''", $str);
  122.         }
  123. }
  124.