File: adodb-pdo_sqlite.inc.php

package info (click to toggle)
libphp-adodb 5.21.4-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 2,276 kB
  • sloc: php: 35,839; xml: 52; sql: 32; makefile: 5
file content (212 lines) | stat: -rw-r--r-- 6,246 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
 * PDO SQLite driver
 *
 * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
 *
 * @package ADOdb
 * @link https://adodb.org Project's web site and documentation
 * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
 *
 * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
 * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
 * any later version. This means you can use it in proprietary products.
 * See the LICENSE.md file distributed with this source code for details.
 * @license BSD-3-Clause
 * @license LGPL-2.1-or-later
 *
 * @copyright 2000-2013 John Lim
 * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
 * @author Diogo Toscano <diogo@scriptcase.net>
 * @author Sid Dunayer <sdunayer@interserv.com>
 */

class ADODB_pdo_sqlite extends ADODB_pdo {
	var $metaTablesSQL   = "SELECT name FROM sqlite_master WHERE type='table'";
	var $sysDate         = 'current_date';
	var $sysTimeStamp    = 'current_timestamp';
	var $nameQuote       = '`';
	var $replaceQuote    = "''";
	var $hasGenID        = true;
	var $_genIDSQL       = "UPDATE %s SET id=id+1 WHERE id=%s";
	var $_genSeqSQL      = "CREATE TABLE %s (id integer)";
	var $_genSeqCountSQL = 'SELECT COUNT(*) FROM %s';
	var $_genSeq2SQL     = 'INSERT INTO %s VALUES(%s)';
	var $_dropSeqSQL     = 'DROP TABLE %s';
	var $concat_operator = '||';
    var $pdoDriver       = false;
	var $random='abs(random())';

	function _init($parentDriver)
	{
		$this->pdoDriver = $parentDriver;
		$parentDriver->_bindInputArray = true;
		$parentDriver->hasTransactions = false; // // should be set to false because of PDO SQLite driver not supporting changing autocommit mode
		$parentDriver->hasInsertID = true;
	}

	function ServerInfo()
	{
		$parent = $this->pdoDriver;
		@($ver = array_pop($parent->GetCol("SELECT sqlite_version()")));
		@($enc = array_pop($parent->GetCol("PRAGMA encoding")));

		$arr['version']     = $ver;
		$arr['description'] = 'SQLite ';
		$arr['encoding']    = $enc;

		return $arr;
	}

	function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
	{
		$nrows = (int) $nrows;
		$offset = (int) $offset;
		$parent = $this->pdoDriver;
		$offsetStr = ($offset >= 0) ? " OFFSET $offset" : '';
		$limitStr  = ($nrows >= 0)  ? " LIMIT $nrows" : ($offset >= 0 ? ' LIMIT 999999999' : '');
		if ($secs2cache)
			$rs = $parent->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
		else
			$rs = $parent->Execute($sql."$limitStr$offsetStr",$inputarr);

		return $rs;
	}

	function GenID($seq='adodbseq',$start=1)
	{
		$parent = $this->pdoDriver;
		// if you have to modify the parameter below, your database is overloaded,
		// or you need to implement generation of id's yourself!
		$MAXLOOPS = 100;
		while (--$MAXLOOPS>=0) {
			@($num = array_pop($parent->GetCol("SELECT id FROM {$seq}")));
			if ($num === false || !is_numeric($num)) {
				@$parent->Execute(sprintf($this->_genSeqSQL ,$seq));
				$start -= 1;
				$num = '0';
				$cnt = $parent->GetOne(sprintf($this->_genSeqCountSQL,$seq));
				if (!$cnt) {
					$ok = $parent->Execute(sprintf($this->_genSeq2SQL,$seq,$start));
				}
				if (!$ok) return false;
			}
			$parent->Execute(sprintf($this->_genIDSQL,$seq,$num));

			if ($parent->affected_rows() > 0) {
                	        $num += 1;
                		$parent->genID = intval($num);
                		return intval($num);
			}
		}
		if ($fn = $parent->raiseErrorFn) {
			$fn($parent->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
		}
		return false;
	}

	function CreateSequence($seqname='adodbseq',$start=1)
	{
		$parent = $this->pdoDriver;
		$ok = $parent->Execute(sprintf($this->_genSeqSQL,$seqname));
		if (!$ok) return false;
		$start -= 1;
		return $parent->Execute("insert into $seqname values($start)");
	}

	function SetTransactionMode($transaction_mode)
	{
		$parent = $this->pdoDriver;
		$parent->_transmode = strtoupper($transaction_mode);
	}

	function BeginTrans()
	{
		$parent = $this->pdoDriver;
		if ($parent->transOff) return true;
		$parent->transCnt += 1;
		$parent->_autocommit = false;
		return $parent->Execute("BEGIN {$parent->_transmode}");
	}

	function CommitTrans($ok=true)
	{
		$parent = $this->pdoDriver;
		if ($parent->transOff) return true;
		if (!$ok) return $parent->RollbackTrans();
		if ($parent->transCnt) $parent->transCnt -= 1;
		$parent->_autocommit = true;

		$ret = $parent->Execute('COMMIT');
		return $ret;
	}

	function RollbackTrans()
	{
		$parent = $this->pdoDriver;
		if ($parent->transOff) return true;
		if ($parent->transCnt) $parent->transCnt -= 1;
		$parent->_autocommit = true;

		$ret = $parent->Execute('ROLLBACK');
		return $ret;
	}


    // mark newnham
	function MetaColumns($tab,$normalize=true)
	{
	  global $ADODB_FETCH_MODE;

	  $parent = $this->pdoDriver;
	  $false = false;
	  $save = $ADODB_FETCH_MODE;
	  $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
	  if ($parent->fetchMode !== false) $savem = $parent->SetFetchMode(false);
	  $rs = $parent->Execute("PRAGMA table_info('$tab')");
	  if (isset($savem)) $parent->SetFetchMode($savem);
	  if (!$rs) {
	    $ADODB_FETCH_MODE = $save;
	    return $false;
	  }
	  $arr = array();
	  while ($r = $rs->FetchRow()) {
	    $type = explode('(',$r['type']);
	    $size = '';
	    if (sizeof($type)==2)
	    $size = trim($type[1],')');
	    $fn = strtoupper($r['name']);
	    $fld = new ADOFieldObject;
	    $fld->name = $r['name'];
	    $fld->type = $type[0];
	    $fld->max_length = $size;
	    $fld->not_null = $r['notnull'];
	    $fld->primary_key = $r['pk'];
	    $fld->default_value = $r['dflt_value'];
	    $fld->scale = 0;
	    if ($save == ADODB_FETCH_NUM) $arr[] = $fld;
	    else $arr[strtoupper($fld->name)] = $fld;
	  }
	  $rs->Close();
	  $ADODB_FETCH_MODE = $save;
	  return $arr;
	}

	function MetaTables($ttype=false,$showSchema=false,$mask=false)
	{
		$parent = $this->pdoDriver;

		if ($mask) {
			$save = $this->metaTablesSQL;
			$mask = $this->qstr(strtoupper($mask));
			$this->metaTablesSQL .= " AND name LIKE $mask";
		}

		$ret = $parent->GetCol($this->metaTablesSQL);

		if ($mask) {
			$this->metaTablesSQL = $save;
		}
		return $ret;
   }
}