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