Pluf Framework

Pluf Framework Git Source Tree


Root/src/Pluf/DB.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
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
213
214
215
216
217
218
219
<?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 ***** */
 
/**
 * Database class.
 *
 * Depending of the parameters, the database object will connect to a MySQL
 * or SQLite database.
 */
class Pluf_DB
{
 
    /**
     * Get a database connection.
     */
    static function get($engine, $server, $database, $login, $password,
                        $prefix, $debug=false, $version='')
    {
        $engine = 'Pluf_DB_'.$engine;
        $con = new $engine($login, $password, $server, $database,
                           $prefix, $debug, $version);
        return $con;
    }
}
 
/**
 * Get the default DB connection.
 *
 * The default database connection is defined in the configuration file
 * through the following configuration variables:
 * - db_login : Login to connect to the database
 * - db_password : Password to the database
 * - db_server : Name of the server
 * - db_database : Name of the database
 * - db_table_prefix : Prefix for the table names
 * - db_version : Version of the database engine
 * - db_engine : Engine for exampe 'MySQL', 'SQLite'
 *
 * Once the first connection is created the following calls to Pluf::db()
 * are getting the same connection.
 *
 */
function Pluf_DB_getConnection($extra=null)
{
    if (isset($GLOBALS['_PX_db']) &&
        (is_resource($GLOBALS['_PX_db']->con_id) or is_object($GLOBALS['_PX_db']->con_id))) {
        return $GLOBALS['_PX_db'];
    }
    $GLOBALS['_PX_db'] = Pluf_DB::get(Pluf::f('db_engine'),
                                      Pluf::f('db_server'),   
                                      Pluf::f('db_database'),
                                      Pluf::f('db_login'),
                                      Pluf::f('db_password'),
                                      Pluf::f('db_table_prefix'),
                                      Pluf::f('debug'),
                                      Pluf::f('db_version'));
    return $GLOBALS['_PX_db'];
}
 
/**
 * Returns an array of default typecast and quoting for the database ORM.
 *
 * Foreach field type you need to provide an array with 2 functions,
 * the from_db, the to_db.
 *
 * $value = from_db($value);
 * $escaped_value = to_db($value, $dbobject);
 *
 * $escaped_value is ready to be put in the SQL, that is if this is a
 * string, the value is quoted and escaped for example with SQLite:
 * 'my string'' is escaped' or with MySQL 'my string\' is escaped' the
 * starting ' and ending ' are included!
 *
 * @return array Default typecast.
 */
function Pluf_DB_defaultTypecast()
{
    return array(
                 'Pluf_DB_Field_Boolean' =>
                     array('Pluf_DB_BooleanFromDb', 'Pluf_DB_BooleanToDb'),
                 'Pluf_DB_Field_Date' =>
                     array('Pluf_DB_IdentityFromDb', 'Pluf_DB_IdentityToDb'),
                 'Pluf_DB_Field_Datetime' =>
                     array('Pluf_DB_IdentityFromDb', 'Pluf_DB_IdentityToDb'),
                 'Pluf_DB_Field_Email' =>
                     array('Pluf_DB_IdentityFromDb', 'Pluf_DB_IdentityToDb'),
                 'Pluf_DB_Field_File' =>
                     array('Pluf_DB_IdentityFromDb', 'Pluf_DB_IdentityToDb'),
                 'Pluf_DB_Field_Float' =>
                     array('Pluf_DB_IdentityFromDb', 'Pluf_DB_IdentityToDb'),
                 'Pluf_DB_Field_Foreignkey' =>
                     array('Pluf_DB_IntegerFromDb', 'Pluf_DB_IntegerToDb'),
                 'Pluf_DB_Field_Integer' =>
                     array('Pluf_DB_IntegerFromDb', 'Pluf_DB_IntegerToDb'),
                 'Pluf_DB_Field_Password' =>
                     array('Pluf_DB_IdentityFromDb', 'Pluf_DB_PasswordToDb'),
                 'Pluf_DB_Field_Sequence' =>
                     array('Pluf_DB_IntegerFromDb', 'Pluf_DB_IntegerToDb'),
                 'Pluf_DB_Field_Text' =>
                     array('Pluf_DB_IdentityFromDb', 'Pluf_DB_IdentityToDb'),
                 'Pluf_DB_Field_Varchar' =>
                     array('Pluf_DB_IdentityFromDb', 'Pluf_DB_IdentityToDb'),
                 'Pluf_DB_Field_Serialized' =>
                     array('Pluf_DB_SerializedFromDb', 'Pluf_DB_SerializedToDb'),
                 'Pluf_DB_Field_Compressed' =>
                     array('Pluf_DB_CompressedFromDb', 'Pluf_DB_CompressedToDb'),
                 );
}
 
/**
 * Identity function.
 *
 * @param mixed Value
 * @return mixed Value
 */
function Pluf_DB_IdentityFromDb($val)
{
    return $val;
}
 
/**
 * Identity function.
 *
 * @param mixed Value.
 * @param object Database handler.
 * @return string Ready to use for SQL.
 */
function Pluf_DB_IdentityToDb($val, $db)
{
    if (is_null($val)) {
        return 'NULL';
    }
    return $db->esc($val);
}
 
function Pluf_DB_SerializedFromDb($val)
{
    if ($val) {
        return unserialize($val);
    }
    return $val;
}
 
function Pluf_DB_SerializedToDb($val, $db)
{
    if (is_null($val)) {
        return 'NULL';
    }
    return $db->esc(serialize($val));
}
 
function Pluf_DB_CompressedFromDb($val)
{
    return ($val) ? gzinflate($val) : $val;
}
 
function Pluf_DB_CompressedToDb($val, $db)
{
    return (is_null($val)) ? 'NULL' : $db->esc(gzdeflate($val, 9));
}
 
function Pluf_DB_BooleanFromDb($val) {
    if ($val) {
        return true;
    }
    return false;
}
 
function Pluf_DB_BooleanToDb($val, $db) {
    if (is_null($val)) {
        return 'NULL';
    }
    if ($val) {
        return $db->esc('1');
    }
    return $db->esc('0');
}
 
function Pluf_DB_IntegerFromDb($val) {
    if (is_null($val)) return null;
    return (int) $val;
}
 
function Pluf_DB_IntegerToDb($val, $db) {
    if (is_null($val)) {
        return 'NULL';
    }
    return (string)(int)$val;
}
 
function Pluf_DB_PasswordToDb($val, $db) {
    $exp = explode(':', $val);
    if (in_array($exp[0], array('sha1', 'md5', 'crc32'))) {
        return $db->esc($val);
    }
    // We need to hash the value.
    $salt = Pluf_Utils::getRandomString(5);
    return $db->esc('sha1:'.$salt.':'.sha1($salt.$val));
}

Archive Download this file

Branches

Tags

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