root/trunk/patForms/Storage/DB.php

Revision 351, 6.4 kB (checked in by schst, 3 years ago)

Fixed bug #205: semicolon on end of SQL,
replaced tabs with spaces

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * patForms storage DB
4  *
5  * $Id$
6  *
7  * @package        patForms
8  * @subpackage    Storage
9  * @author        Stephan Schmidt <schst@php-tools.net>
10  */
11
12 /**
13  * needs PEAR::DB
14  */
15 require_once 'DB.php';
16
17 /**
18  * patForms storage DB
19  *
20  * Stores form data in a database.
21  *
22  * @access        protected
23  * @package        patForms
24  * @subpackage    Storage
25  * @author        Stephan Schmidt <schst@php-tools.net>
26  * @license        LGPL, see license.txt for details
27  * @link        http://www.php-tools.net
28  */
29 class patForms_Storage_DB extends patForms_Storage
30 {
31    /**
32     * datasource name
33     *
34     * @access    private
35     * @var        string
36     */
37     var $_dsn;
38
39    /**
40     * table name
41     *
42     * @access    private
43     * @var        string
44     */
45     var $_table;
46
47    /**
48     * instance of PEAR::DB
49     *
50     * @access    private
51     * @var        object
52     */
53     var $_db;
54
55    /**
56     * field map
57     *
58     * @access    private
59     * @var        string
60     */
61     var $_fieldMap    =    array();
62
63    /**
64     * set the dsn and table
65     *
66     * @access    public
67     * @param    string        datasource name
68     * @param    string        table
69     */
70     function setStorageLocation( $dsn, $table )
71     {
72         $this->_dsn        =    $dsn;
73         $this->_table    =    $table;
74     }
75
76    /**
77     * set the field map
78     *
79     * The field map is an associative array, that defines how
80     * the form elements (key) map to fields in the
81     * table (value)
82     *
83     * @access    public
84     * @param    array        field map
85     */
86     function setFieldMap($fieldMap)
87     {
88         $this->_fieldMap = $fieldMap;
89     }
90
91    /**
92     * get an entry
93     *
94     * This tries to find an entry in the storage container
95     * that matches the current data that has been set in the
96     * form and populates the form with the data of this
97     * entry
98     *
99     * @access    public
100     * @param    object patForms        patForms object that should be stored
101     * @return    boolean                true on success
102     */
103     function loadEntry(&$form)
104     {
105         $values  = $form->getValues();
106         $primary = $this->getPrimary($values);
107
108         // no primary key, storage will only add entries
109         if (empty($primary)) {
110             return array();
111         }
112
113         /**
114          * entry does not exists
115          */
116         if (!$data = $this->_entryExists($primary)) {
117             return array();
118         }
119
120         $values = $this->_unmapFields($data);
121
122         $form->setValues($values);
123         return true;
124     }
125
126    /**
127     * adds an entry to the storage
128     *
129     * The entry will be appended at the end of the file.
130     *
131     * @abstract
132     * @param    object patForms        patForms object that should be stored
133     * @return    boolean                true on success
134     */
135     function _addEntry(&$form)
136     {
137         $values = $form->getValues();
138
139         $result = $this->_prepareConnection();
140         if (PEAR::isError($result)) {
141             return patErrorManager::raiseError(PATFORMS_STORAGE_ERROR_STORAGE_INVALID, 'Could not connect to database: ' . $result->getMessage());
142         }
143         $values    = $this->_mapFields($values);
144
145         $values = array_merge($values, $this->_staticValues);
146
147         $fields = array_keys($values);
148         $values = array_map(array($this->_db, 'quoteSmart'), array_values($values));
149
150         $query  = sprintf('INSERT INTO %s (%s) VALUES (%s)',
151                           $this->_table,
152                           implode(',', $fields),
153                           implode(',', $values)
154                       );
155
156         $result = $this->_db->query($query);
157
158         if (PEAR::isError($result)) {
159             return patErrorManager::raiseError(PATFORMS_STORAGE_ERROR_STORAGE_INVALID, 'Query failed: ' . $result->getMessage());
160         }
161         return true;
162     }
163
164    /**
165     * updates an entry in the storage
166     *
167     * Implement this in the concrete storage container.
168     *
169     * @abstract
170     * @param    object patForms        patForms object that should be stored
171     * @return    boolean                true on success
172     */
173     function _updateEntry( &$form, $primary )
174     {
175         $values = $form->getValues();
176
177         $result = $this->_prepareConnection();
178         if (PEAR::isError($result)) {
179             return patErrorManager::raiseError(PATFORMS_STORAGE_ERROR_STORAGE_INVALID, 'Could not connect to database: ' . $result->getMessage());
180         }
181         $values     = $this->_mapFields($values);
182         $primary = $this->_mapFields($primary);
183
184         $tmp = array();
185         foreach ($values as $key => $value) {
186             array_push( $tmp, $key.'='.$this->_db->quoteSmart( $value ) );
187         }
188
189         $ptmp = array();
190         foreach ($primary as $key => $value) {
191             array_push( $ptmp, $key.'='.$this->_db->quoteSmart( $value ) );
192         }
193
194         $query  = 'UPDATE '.$this->_table.' SET '.implode( ', ', $tmp ).' WHERE '.implode( ' AND ', $ptmp );
195         $result = $this->_db->query($query);
196         if (PEAR::isError($result)) {
197             return patErrorManager::raiseError(PATFORMS_STORAGE_ERROR_STORAGE_INVALID, 'Query failed: ' . $result->getMessage());
198         }
199         return true;
200     }
201
202    /**
203     * check, whether an entry exists
204     *
205     * @access    private
206     * @param    array
207     */
208     function _entryExists($primary)
209     {
210         $result = $this->_prepareConnection();
211         if (PEAR::isError($result)) {
212             return patErrorManager::raiseError(PATFORMS_STORAGE_ERROR_STORAGE_INVALID, 'Could not connect to database: ' . $result->getMessage());
213         }
214         $primary = $this->_mapFields($primary);
215
216         $tmp = array();
217         foreach ($primary as $key => $value) {
218             array_push($tmp, $key.'='.$this->_db->quoteSmart($value));
219         }
220
221         $query    = 'SELECT * FROM '.$this->_table.' WHERE '.implode( ' AND ', $tmp );
222         $result    = $this->_db->getRow( $query, array(), DB_FETCHMODE_ASSOC );
223         if (PEAR::isError($result)) {
224             return patErrorManager::raiseError(PATFORMS_STORAGE_ERROR_STORAGE_INVALID, 'Could not connect to database: ' . $result->getMessage());
225         }
226
227         if (empty($result)) {
228             return false;
229         }
230
231         return $result;
232     }
233
234    /**
235     * map the values to the correct fields
236     *
237     * @access    private
238     * @param    array        values
239     * @return    array        values mapped to the correct fields
240     */
241     function _mapFields( $values )
242     {
243         if (empty($this->_fieldMap)) {
244             return $values;
245         }
246
247         $fields    =    array();
248         foreach ($this->_fieldMap as $el => $field) {
249             if (!isset($values[$el])) {
250                 continue;
251             }
252
253             $fields[$field] = $values[$el];
254         }
255         return $fields;
256     }
257
258    /**
259     * map the fields to the correct elements
260     *
261     * @access    private
262     * @param    array        values
263     * @return    array        values mapped to the correct fields
264     */
265     function _unmapFields( $values )
266     {
267         if (empty($this->_fieldMap)) {
268             return $values;
269         }
270
271         $fields    = array();
272         foreach ($this->_fieldMap as $el => $field) {
273             if( !isset($values[$field])) {
274                 continue;
275             }
276
277             $fields[$el] = $values[$field];
278         }
279         return $fields;
280     }
281
282    /**
283     * prepare the DB connection
284     *
285     * @access    private
286     */
287     function _prepareConnection()
288     {
289         if ($this->_db != null) {
290             return true;
291         }
292
293         if (is_object($this->_dsn)) {
294             $this->_db = &$this->_dsn;
295             return true;
296         }
297
298         $this->_db = &DB::connect($this->_dsn);
299         if (PEAR::isError($this->_db)) {
300             $error = $this->_db;
301             $this->_db = null;
302             return $error;
303         }
304         return true;
305     }
306 }
307 ?>
Note: See TracBrowser for help on using the browser.