~registry/webmasterkit/development

« back to all changes in this revision

Viewing changes to database/wk.lib.php

  • Committer: Pietro Albini
  • Date: 2013-03-29 08:37:36 UTC
  • Revision ID: pietro98.albini@gmail.com-20130329083736-tg4sd5tx9h9eyb3j
Start to implement wkDatabase module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * @name        wk.database
 
4
 * @version     0.1
 
5
 * @description Database handler for WebmasterKit
 
6
 * @author      Pietro Albini <pietro98.albini@gmail.com>
 
7
 * @license     GNU-LGPL v3
 
8
 */
 
9
defined("WEBMASTERKIT") or die("Restricted access");
 
10
 
 
11
interface wkDatabaseBase
 
12
{
 
13
        public function GetDSN($args);
 
14
}
 
15
 
 
16
class wkDatabase
 
17
{
 
18
        /* System */
 
19
        public $handler;                // PDO handler
 
20
        protected $type;                // Database type
 
21
        protected $db_tools;            // Database engine tools
 
22
        
 
23
        /* Status */
 
24
        public $in_transaction;         // Transation is running or not
 
25
        public $is_connect;             // Is connected
 
26
        public $num_queries;            // Numer of queries
 
27
        public $error;                  // Last error
 
28
        
 
29
        /* Information */
 
30
        public $engines;                // Available engines
 
31
        public $insert_id;              // The last insered item's id
 
32
        public $affected_rows;          // Affected rows
 
33
        
 
34
        /**
 
35
         * __construct method
 
36
         * Initialize the class
 
37
         * 
 
38
         * @param       $auto_connect   Auto connect to the database
 
39
         * @return      bool
 
40
         */
 
41
        
 
42
        public function __construct($auto_connect=true)
 
43
        {
 
44
                $this->handler = false;
 
45
                $this->type = false;
 
46
                $this->db_tool = false;
 
47
                
 
48
                $this->in_transation = false;
 
49
                $this->is_connect = false;
 
50
                $this->num_queries = 0;
 
51
                $this->error = "";
 
52
                
 
53
                $this->engines = $this->GetEngines();
 
54
                $this->insert_id = 0;
 
55
                $this->affected_rows = 0;
 
56
                
 
57
                if($auto_connect and wk::$config->database_auto_connect)
 
58
                {
 
59
                        $args = array_merge(array(wk::$config->database_engine, wk::$config->database_username, wk::$config->database_password), wk::$config->database_options);
 
60
                }
 
61
                
 
62
                return true;
 
63
        }
 
64
        
 
65
        /**
 
66
         * __destruct method
 
67
         * Destruct the class
 
68
         * 
 
69
         * @return      bool
 
70
         */
 
71
        
 
72
        public function __destruct()
 
73
        {
 
74
                if($this->handler)
 
75
                {
 
76
                        $this->Close();
 
77
                }
 
78
                return true;
 
79
        }
 
80
        
 
81
        /**
 
82
         * __clone method
 
83
         * Prevent to cloning the class
 
84
         * 
 
85
         * @return      bool
 
86
         */
 
87
        
 
88
        public function __clone()
 
89
        {
 
90
                if(wk::$config->database_prevent_cloning)
 
91
                {
 
92
                        $this->Close();
 
93
                        trigger_error("You cannot clone a database instance", E_USER_ERROR);
 
94
                }
 
95
        }
 
96
        
 
97
        /**
 
98
         * GetEngines method
 
99
         * Get the list of available engines
 
100
         * 
 
101
         * @return      array
 
102
         */
 
103
        
 
104
        public function GetEngines()
 
105
        {
 
106
                $engines = PDO::getAvailableDrivers();
 
107
                foreach($engines as &$engine)
 
108
                {
 
109
                        if(!wk::$tools->HasLibrary('database', $engine))
 
110
                        {
 
111
                                unset($engine);
 
112
                        }
 
113
                }
 
114
                return $engines;
 
115
        }
 
116
        
 
117
        /**
 
118
         * Connect method
 
119
         * Connect to a database engine
 
120
         * 
 
121
         * @param       $engine         The database engine
 
122
         * @param       $username       Your username
 
123
         * @param       $password       Your password
 
124
         * @param       *               Connection params
 
125
         * @return      bool
 
126
         */
 
127
        
 
128
        public function Connect()
 
129
        {
 
130
                if(!$this->handler)
 
131
                {
 
132
                        $args = func_get_args();
 
133
                        $engine = array_shift($args);
 
134
                        $username = array_shift($args);
 
135
                        $password = array_shift($args);
 
136
                
 
137
                        if(in_array($engine, $this->GetEngines()))
 
138
                        {
 
139
                                $db_tools = 'wkDatabase'.ucfirst($engine);
 
140
                                $this->db_tools = new $db_tools();
 
141
                                $dsn = $this->db_tools->GetDSN($args);
 
142
                        
 
143
                                try
 
144
                                {
 
145
                                        $this->handler = new PDO($dsn, $username, $password);
 
146
                                        $this->handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 
147
                                        $this->type = $engine;
 
148
                                        $this->is_connect = true;
 
149
                                }
 
150
                                catch (PDOException $e)
 
151
                                {
 
152
                                        $this->error = $e->getMessage();
 
153
                                        trigger_error('Impossibile connettersi al database', E_USER_ERROR);
 
154
                                }
 
155
                                return true;
 
156
                        }
 
157
                        else
 
158
                        {
 
159
                                trigger_error('Il database engine richiesto non è disponibile', E_USER_WARNING);
 
160
                                return false;
 
161
                        }
 
162
                }
 
163
                else
 
164
                {
 
165
                        trigger_error('Non è possibile connettersi poiché si è già connessi', E_USER_WARNING);
 
166
                        return false;
 
167
                }
 
168
        }
 
169
        
 
170
        /**
 
171
         * Close method
 
172
         * Close the connection
 
173
         * 
 
174
         * @return      bool
 
175
         */
 
176
        
 
177
        public function Close()
 
178
        {
 
179
                if($this->handler)
 
180
                {
 
181
                        $this->handler = false;
 
182
                        $this->is_connect = false;
 
183
                        $this->type = "";
 
184
                        $this->db_tools = false;
 
185
                        return true;
 
186
                }
 
187
                else
 
188
                {
 
189
                        trigger_error('Non è possibile chiudere la connessione poiché non si è connessi', E_USER_WARNING);
 
190
                        return false;
 
191
                }
 
192
        }
 
193
        
 
194
        /**
 
195
         * Query method
 
196
         * Execute a query
 
197
         * 
 
198
         * @param       $sql    The query
 
199
         * @return      bool
 
200
         */
 
201
        
 
202
        public function Query($sql)
 
203
        {
 
204
                if($this->handler)
 
205
                {
 
206
                        if(substr(trim($sql), -1) == ";")
 
207
                        {
 
208
                                $sql = substr(trim($sql), 0, -1);
 
209
                        }
 
210
                        $queries = explode($sql, ";");
 
211
                        if(count($queries) > 1)
 
212
                        {
 
213
                                trigger_error("Non sono permesse pi&ugrave; query contemporaneamente", E_USER_ERROR);
 
214
                                return false;
 
215
                        }
 
216
                        $sql .= ";";
 
217
                        
 
218
                        $is_select = preg_match("/^select$/i", $sql);
 
219
                        if($is_select)
 
220
                        {
 
221
                                try
 
222
                                {
 
223
                                        $result = $this->handler->query($sql);
 
224
                                }
 
225
                                catch(PDOException $e)
 
226
                                {
 
227
                                        $this->error = $e->getMessage();
 
228
                                        trigger_error("Si &egrave; verificato un errore durante l'esecuzione della query", E_USER_ERROR);
 
229
                                        return false;
 
230
                                }
 
231
                                
 
232
                                if($result)
 
233
                                {
 
234
                                        $return = new wkDatabaseResult($this, $this->handler, $result);
 
235
                                }
 
236
                                else
 
237
                                {
 
238
                                        $return = false;
 
239
                                }
 
240
                        }
 
241
                        else
 
242
                        {
 
243
                                try
 
244
                                {
 
245
                                        $result = $this->handler->exec($sql);
 
246
                                }
 
247
                                catch(PDOException $e)
 
248
                                {
 
249
                                        $this->error = $e->getMessage();
 
250
                                        trigger_error("Si &egrave; verificato un errore durante l'esecuzione della query", E_USER_ERROR);
 
251
                                        return false;
 
252
                                }
 
253
                                
 
254
                                if($affected)
 
255
                                {
 
256
                                        $this->affected_rows = $affected;
 
257
                                        $is_insert = preg_match("/^insert$/i", $sql);
 
258
                                        if($is_insert)
 
259
                                        {
 
260
                                                $this->insert_id = $this->handler->lastInsertId();
 
261
                                        }
 
262
                                        return true;
 
263
                                }
 
264
                                else
 
265
                                {
 
266
                                        return false;
 
267
                                }
 
268
                        }
 
269
                }
 
270
        }
 
271
}
 
272
?>