Subversion Repositories oidplus

Rev

Rev 1086 | Rev 1130 | 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.         /**
  29.          * @return string
  30.          */
  31.         public static function id(): string {
  32.                 return 'sqlite';
  33.         }
  34.  
  35.         /**
  36.          * @param string $fieldname
  37.          * @param string $order
  38.          * @return string
  39.          * @throws OIDplusException
  40.          */
  41.         public function natOrder(string $fieldname, string $order='asc'): string {
  42.  
  43.                 $order = strtolower($order);
  44.                 if (($order != 'asc') && ($order != 'desc')) {
  45.                         throw new OIDplusException(_L('Invalid order "%1" (needs to be "asc" or "desc")',$order));
  46.                 }
  47.  
  48.                 $out = array();
  49.  
  50.                 // If the SQLite database is accessed through the SQLite3 plugin,
  51.                 // we use an individual collation, therefore OIDplusDatabasePluginSQLite3 overrides
  52.                 // natOrder().
  53.                 // If we connected to SQLite using ODBC or PDO, we need to do something else,
  54.                 // but that solution is complex, slow and wrong (since it does not support
  55.                 // UUIDs etc.)
  56.  
  57.                 $max_depth = OIDplus::baseConfig()->getValue('LIMITS_MAX_OID_DEPTH');
  58.                 if ($max_depth > 11) $max_depth = 11; // SQLite3 will crash if max depth > 11 (parser stack overflow); (TODO: can we do something else?!?!)
  59.  
  60.                 // 1. sort by namespace (oid, guid, ...)
  61.                 $out[] = "substr($fieldname,0,instr($fieldname,':')) $order";
  62.  
  63.                 // 2. Sort by the rest arcs one by one
  64.                 for ($i=1; $i<=$max_depth; $i++) {
  65.                         if ($i==1) {
  66.                                 $arc = "substr($fieldname,5)||'.'";
  67.                                 $fieldname = $arc;
  68.                                 $arc = "substr($arc,0,instr($arc,'.'))";
  69.                                 $out[] = "cast($arc as integer) $order";
  70.                         } else {
  71.                                 $arc = "ltrim(ltrim($fieldname,'0123456789'),'.')";
  72.                                 $fieldname = $arc;
  73.                                 $arc = "substr($arc,0,instr($arc,'.'))";
  74.                                 $out[] = "cast($arc as integer)  $order";
  75.                         }
  76.                 }
  77.  
  78.                 // 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)
  79.                 $out[] = "$fieldname $order";
  80.  
  81.                 return implode(', ', $out);
  82.  
  83.         }
  84.  
  85.         /**
  86.          * @return string
  87.          */
  88.         public function sqlDate(): string {
  89.                 return 'datetime()';
  90.         }
  91.  
  92.         /**
  93.          * @param OIDplusDatabaseConnection $db
  94.          * @return bool
  95.          */
  96.         public function detect(OIDplusDatabaseConnection $db): bool {
  97.                 try {
  98.                         $db->query("select sqlite_version as dbms_version");
  99.                         return true;
  100.                 } catch (\Exception $e) {
  101.                         return false;
  102.                 }
  103.         }
  104.  
  105.         /**
  106.          * @param OIDplusDatabaseConnection $db
  107.          * @return int
  108.          * @throws OIDplusException
  109.          */
  110.         public function insert_id(OIDplusDatabaseConnection $db): int {
  111.                 $res = $db->query("SELECT last_insert_rowid() AS ID");
  112.                 $row = $res->fetch_array();
  113.                 return (int)$row['ID'];
  114.         }
  115.  
  116.         /**
  117.          * @param string $cont
  118.          * @param string $table
  119.          * @param string $prefix
  120.          * @return string
  121.          */
  122.         public function setupSetTablePrefix(string $cont, string $table, string $prefix): string {
  123.                 $cont = str_replace('`'.$table.'`', '`'.$prefix.$table.'`', $cont);
  124.                 return $cont;
  125.         }
  126.  
  127.         /**
  128.          * @param string $database
  129.          * @return string
  130.          */
  131.         public function setupCreateDbIfNotExists(string $database): string {
  132.                 return "";
  133.         }
  134.  
  135.         /**
  136.          * @param string $database
  137.          * @return string
  138.          */
  139.         public function setupUseDatabase(string $database): string {
  140.                 return "";
  141.         }
  142.  
  143.         /**
  144.          * @param string $expr1
  145.          * @param string $expr2
  146.          * @return string
  147.          */
  148.         public function isNullFunction(string $expr1, string $expr2): string {
  149.                 return "ifnull($expr1, $expr2)";
  150.         }
  151.  
  152.         /**
  153.          * @param string $sql
  154.          * @return string
  155.          */
  156.         public function filterQuery(string $sql): string {
  157.                 return $sql;
  158.         }
  159.  
  160.         /**
  161.          * @param bool $bool
  162.          * @return string
  163.          */
  164.         public function getSQLBool(bool $bool): string {
  165.                 return $bool ? '1' : '0';
  166.         }
  167.  
  168.         /**
  169.          * @param string $str
  170.          * @return string
  171.          */
  172.         public function escapeString(string $str): string {
  173.                 return str_replace("'", "''", $str);
  174.         }
  175. }
  176.