pluf2

pluf2 Commit Details


Date:2008-11-06 15:40:34 (16 years 1 month ago)
Author:Loic d'Anterroches
Branch:master
Commit:38d90e17142e1f1a20ba75ee2c1b0d41d1c1cf83
Parents: b8379bfae171cc1eb94f51ca68e24f342f3e0e35
Message:Added a compressed DB field.

The compressed DB field will automatically deflate the data before
storage in the DB and inflate again when retrieving the data. This is
done transparently for the end user. This is good if you need to store
large body of text and do not need to search into it with the SQL
commands.
Changes:

File differences

src/Pluf/DB.php
122122
123123
124124
125
126
125127
126128
127129
......
159161
160162
161163
162
163
164
165
166
167
168
169164
170165
171166
......
174169
175170
176171
172
173
174
175
176
177
178
179
180
181
177182
178183
179184
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'),
);
}
return $val;
}
/**
* Identity function.
*
* @param mixed Value.
* @param object Database handler.
* @return string Ready to use for SQL.
*/
function Pluf_DB_SerializedToDb($val, $db)
{
if (is_null($val)) {
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;
src/Pluf/DB/Field/Compressed.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
<?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 ***** */
/**
* This field will automatically inflate/deflate its content.
*
* This can be used to store large text bodies where you do not need
* to directly search into the content using the standard SQL
* functions.
*/
class Pluf_DB_Field_Compressed extends Pluf_DB_Field
{
public $type = 'text';
function formField($def, $form_field='Pluf_Form_Field_Varchar')
{
if (!isset($def['widget'])) {
$def['widget'] = 'Pluf_Form_Widget_TextareaInput';
}
return parent::formField($def, $form_field);
}
}
src/Pluf/Tests/Model/CompressedField.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
<?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 ***** */
class Pluf_Tests_Model_CompressedField_Model extends Pluf_Model
{
public $_model = __CLASS__;
function init()
{
$this->_a['verbose'] = 'compressed';
$this->_a['table'] = 'compressed';
$this->_a['model'] = __CLASS__;
$this->_a['cols'] = array(
// It is mandatory to have an "id" column.
'id' =>
array(
'type' => 'Pluf_DB_Field_Sequence',
//It is automatically added.
'blank' => true,
),
'compressed' =>
array(
'type' => 'Pluf_DB_Field_Compressed',
),
);
}
}
class Pluf_Tests_Model_CompressedField extends UnitTestCase {
function __construct()
{
parent::__construct('Test the compressed field.');
}
function testCreate()
{
$db = Pluf::db();
$schema = new Pluf_DB_Schema($db);
$m = new Pluf_Tests_Model_CompressedField_Model();
$schema->model = $m;
$schema->createTables();
$m->compressed = 'Youplaboum';
$m->create();
$this->assertEqual(1, $m->id);
$m = new Pluf_Tests_Model_CompressedField_Model(1);
$this->assertEqual('Youplaboum', $m->compressed);
$schema->dropTables();
}
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.05970s using 13 queries.