srchub

srchub Git Source Tree


Root/pluf/src/Pluf/DB/SQLite.php

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
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * SQLite connection class
 */
class Pluf_DB_SQLite
{
    public $con_id;
    public $pfx = '';
    private $debug = false;
    /** The last query, set with debug(). Used when an error is returned. */
    public $lastquery = '';
    public $engine = 'SQLite';
    public $type_cast = array();
 
    function __construct($user, $pwd, $server, $dbname, $pfx='', $debug=false)
    {
        Pluf::loadFunction('Pluf_DB_defaultTypecast');
        $this->type_cast = Pluf_DB_defaultTypecast();
        $this->debug = $debug;
        $this->pfx = $pfx;
        $this->debug('* SQLITE OPEN');
        $this->type_cast['Pluf_DB_Field_Compressed'] = array('Pluf_DB_CompressedFromDb', 'Pluf_DB_SQLite_CompressedToDb');
        // Connect and let the Exception be thrown in case of problem
        try {
            $this->con_id = new PDO('sqlite:'.$dbname);
        } catch (PDOException $e) {
            throw $e;
        }
    }
 
    /**
     * Get the version of the SQLite library.
     *
     * @return string Version string
     */
    function getServerInfo()
    {
        return $this->con_id->getAttribute(PDO::ATTR_SERVER_INFO);
    }
 
    /**
     * Log the queries. Keep track of the last query and if in debug mode
     * keep track of all the queries in
     * $GLOBALS['_PX_debug_data']['sql_queries']
     *
     * @param string Query to keep track
     * @return bool true
     */
    function debug($query)
    {
        $this->lastquery = $query;
        if (!$this->debug) return true;
        if (!isset($GLOBALS['_PX_debug_data']['sql_queries']))
            $GLOBALS['_PX_debug_data']['sql_queries'] = array();
        $GLOBALS['_PX_debug_data']['sql_queries'][] = $query;
        return true;
    }
 
    function close()
    {
        $this->con_id = null;
        return true;
    }
 
    function select($query)
    {
        $this->debug($query);
        if (false === ($cur = $this->con_id->query($query))) {
            throw new Exception($this->getError());
        }
        return $cur->fetchAll(PDO::FETCH_ASSOC);
    }
 
    function execute($query)
    {
        $this->debug($query);
        if (false === ($cur = $this->con_id->exec($query))) {
            throw new Exception($this->getError());
        }
        return $cur;
 
    }
 
    function getLastID()
    {
        $this->debug('* GET LAST ID');
        return (int) $this->con_id->lastInsertId();
    }
 
    /**
     * Returns a string ready to be used in the exception.
     *
     * @return string Error string
     */
    function getError()
    {
        $err = $this->con_id->errorInfo();
        $err[] = $this->lastquery;
        return implode(' - ', $err);
    }
 
    function esc($str)
    {
        return $this->con_id->quote($str);
    }
 
    /**
     * Quote the column name.
     *
     * @param string Name of the column
     * @return string Escaped name
     */
    function qn($col)
    {
        return '"'.$col.'"';
    }
 
    /**
     * Start a transaction.
     */
    function begin()
    {
        $this->execute('BEGIN');
    }
 
    /**
     * Commit a transaction.
     */
    function commit()
    {
        $this->execute('COMMIT');
    }
 
    /**
     * Rollback a transaction.
     */
    function rollback()
    {
        $this->execute('ROLLBACK');
    }
 
    function __toString()
    {
        // According to PHPStorm \PDO does not define __toString
        return '<Pluf_DB_SQLite()>';
    }
 
}
 
 
function Pluf_DB_SQLite_CompressedToDb($val, $con)
{
    if (is_null($val)) {
        return 'NULL';
    }
    return 'X'.$con->esc(bin2hex(gzdeflate($val, 9)));
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.14128s using 11 queries.