MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
mdatabase.class
Ir para a documentação deste ficheiro.
1<?php
3{
4 var $conf; // identifies db configuration in miolo.conf
5 var $system; // what driver?
6 var $host; // host configuration
7 var $db; // db identifier in miolo.conf
8 var $user; // user in miolo.conf
9 var $pass; // password in miolo.conf
10
14 public $conn;
15
16 var $parameters; // parameters for connection
17 var $_miolo; // MIOLO object
18
19 private $idResult;
20
21 function __construct($conf, $system, $host, $db, $user, $pass, $parameters = NULL, $port=NULL)
22 {
23 $this->_miolo = MIOLO::GetInstance();
24
25 $this->_miolo->Assert($system, "Missing DB system!");
26 $this->_miolo->Assert($host, "Missing DB host!");
27 $this->_miolo->Assert($db, "Missing DB name!");
28
29 $this->_miolo->Uses('database/mconnection.class');
30 $this->_miolo->Uses('database/mdataset.class');
31 $this->_miolo->Uses('database/mquery.class');
32 $this->_miolo->Uses('database/midgenerator.class');
33 $this->_miolo->Uses('database/mtransaction.class');
34 $this->_miolo->Uses('database/msqljoin.class');
35 $this->_miolo->Uses('database/mschema.class');
36
37 $this->_miolo->Uses('database/' . $system . '/mconnection.class');
38
39 $this->conf = $conf;
40 $this->system = $system;
41 $this->host = $host;
42 $this->port = $port;
43 $this->db = $db;
44 $this->user = $user;
45 $this->pass = $pass;
46 $this->parameters = $parameters;
47
48 if ( $system == 'postgres' )
49 {
50 $this->conn = new PostgresConnection($this);
51 }
52 else
53 {
54 $className = "{$system}Connection";
55 $this->conn = new $className($this);
56 }
57
58 $this->conn->Open($host, $db, $user, $pass, $parameters, $port);
59
60 if ($err = $this->GetError())
61 {
62 throw new EDatabaseException($conf, $err);
63 }
64 }
65
66 function Close()
67 {
68 $this->conn->Close();
69 }
70
71 function GetError()
72 {
73 $err = $this->GetErrors();
74 return $err ? $err[0] : false;
75 }
76
77 function GetErrors()
78 {
79 return $this->conn->GetErrors();
80 }
81
82 function GetTransaction()
83 {
84 $this->_miolo->Uses('database/' . $this->system . '/mtransaction.class');
85 $className = "{$this->system}Transaction";
86 $transaction = new $className($this->conn);
87 return $transaction;
88 }
89
90 function GetISR()
91 {
92 $this->_miolo->Uses('database/misr.class');
93 $isr = new MISR($this->conn);
94 return $isr;
95 }
96
105 function Execute($sql)
106 {
107 $this->_miolo->ProfileEnter('Database::Execute');
108
109 if (is_array($sql))
110 {
111 $ok = $this->ExecuteBatch($sql);
112 }
113 else
114 {
115 try
116 {
117 $ok = @$this->conn->Execute($sql);
118 }
119 catch ( MDatabaseException $e )
120 {
121 $this->_miolo->logError($e, $this->conf);
123 }
124 catch( Exception $e )
125 {
126 $err = trim($this->GetError());
127 $this->_miolo->LogError($err . '; SQL:' . $sql, $this->conf);
128
130 if ( MUtil::getBooleanValue($MIOLO->getConf('options.backtrace')) )
131 {
132 $trace = $e->getTrace();
133 $query = $trace[1]['args'][0];
134
135 $err .= "<br /><br />$query<br /><br />";
136 $err .= str_replace("\n", '<br />', $e->getTraceAsString());
137 }
138
139 throw new EDatabaseException($this->conf, $err);
140 }
141 }
142
143 $this->_miolo->ProfileExit('Database::Execute');
144 return $ok;
145 }
146
154 function ExecuteBatch($sql_array)
155 {
156 $transaction = $this->GetTransaction();
157
158 if (!is_array($sql_array))
159 $sql_array = array($sql_array);
160
161 foreach ($sql_array as $sql)
162 {
163 $transaction->addCommand($sql);
164 }
165
166 try
167 {
168 $ok = $transaction->process();
169 }
170 catch( Exception $e )
171 {
172 throw new EDatabaseException($this->conf, $e->GetMessage());
173 }
174
175 return $ok;
176 }
177
178 function Count($sql)
179 {
180 $query = $this->QueryChunk($sql, 0, 0, $total);
181 return $total;
182 }
183
184 function GetNewId($sequence = 'admin', $tableGenerator = 'miolo_sequence')
185 {
186 $this->_miolo->Uses('database/' . $this->system . '/midgenerator.class');
187 $className = "{$this->system}IdGenerator";
188 $idgenerator = new $className($this);
189 try
190 {
191 $value = $idgenerator->getNewId($sequence, $tableGenerator);
192 }
193 catch( Exception $e )
194 {
195 throw new EDatabaseException('DB::GetNewId: ' . trim($this->getError()), $this->conf);
196 }
197
198 return $value;
199 }
200
201 function Query($sql, $maxrows = 0, $offset = 0) // backward compatibility
202 {
203 try
204 {
206
207 if ( $MIOLO->getCacheSQL() )
208 {
209 static $cache = array();
210
211 if ( isset($cache[$sql]) )
212 {
213 $result = $cache[$sql];
214 $this->idResult = $cache[$sql . "<id_result>"];
215 }
216 else
217 {
218 $query = $this->conn->getQueryCommand($sql, $maxrows, $offset);
219 $result = $query->result;
220 if ( $query instanceof PostgresQuery )
221 {
222 $this->idResult = $query->id_result;
223 }
224 $cache[$sql] = $result;
225 $cache[$sql . "<id_result>"] = $this->idResult;
226 }
227 }
228 else
229 {
230 $query = $this->conn->getQueryCommand($sql, $maxrows, $offset);
231 $result = $query->result;
232 if ( $query instanceof PostgresQuery )
233 {
234 $this->idResult = $query->id_result;
235 }
236 }
237 }
238 catch ( MDatabaseException $e )
239 {
240 $this->_miolo->logError($e, $this->conf);
242 }
243 catch ( EDatabaseQueryException $e )
244 {
245 $message = $e->getMessage();
247 if ( MUtil::getBooleanValue($MIOLO->getConf('options.backtrace')) )
248 {
249 $trace = $e->getTrace();
250 $query = $trace[2]['args'][0];
251 $message .= "<br /><br />$query<br /><br />";
252 $message .= str_replace("\n", '<br />', $e->getTraceAsString());
253 }
254 throw new EDatabaseQueryException($message);
255 }
256 return $result;
257 }
258
259 public function getIdResult()
260 {
261 return $this->idResult;
262 }
263
264 public function setIdResult($idResult)
265 {
266 $this->idResult = $idResult;
267 }
268
269 function GetQuery($sql, $maxrows = 0)
270 {
271 if (isset($sql->range))
272 {
273 $query = $this->QueryChunk($sql, $sql->range->rows, $sql->range->offset, $sql->range->total);
274 }
275 else
276 {
277 $query = $this->QueryChunk($sql, $maxrows, 0, $total);
278 }
279
280 return $query;
281 }
282
286 function objQuery($sql, $maxrows = 0) // backward compatibility
287 {
288 if ( is_object($sql) )
289 {
290 if ( get_class($sql) == 'sql' )
291 {
292 return $this->GetQuery($sql, $maxrows);
293 }
294 else
295 {
296 return false;
297 }
298 }
299 else
300 {
301 $oSql = new sql();
302 $oSql->CreateFrom($sql);
303 $query = $this->QueryChunk($oSql, $maxrows, 0, $total);
304 return $query;
305 }
306 }
307
308 function GetTable($tablename)
309 {
310 $sql = new sql("*", $tablename);
311 $query = $this->GetQuery($sql);
312 return $query;
313 }
314
315 function GetTableInfo($tablename)
316 {
317 $this->_miolo->Uses('database/' . $this->system . '/mschema.class');
318 $className = "{$this->system}Schema";
319 $schema = new $className($this->conn);
320 return $schema->getTableInfo($tablename);
321 }
322
323 function QueryRange($sql, &$range) // backward compatibility
324 {
325 $oSql = new sql();
326 $oSql->CreateFrom($sql);
327 $query = $this->QueryChunk($oSql, $range->rows, $range->offset, $range->total);
328 return $query->result;
329 }
330
331 function QueryChunk($sql, $maxrows, $offset, &$total)
332 {
333 $this->_miolo->ProfileEnter('Database::QueryChunk');
334 $sql->SetDb($this);
335
336 try
337 {
338 $query = @$this->conn->getQuery($sql, $maxrows, $offset);
339 $total = $query->GetRowCount();
340
341 if (!$sql->bind)
342 $query->Close();
343
344 $this->_miolo->ProfileExit('Database::QueryChunk');
345 return $query;
346 }
347 catch( Exception $e )
348 {
349 $err = trim($this->GetError());
350 $this->_miolo->LogError($err . '; SQL:' . $sql->command, $this->conf);
351
352 throw new EDatabaseException($this->conf, $err . ';' . $e->getMessage());
353 }
354 }
355
356 function ExecProc($sql, $aParams = null)
357 {
358 $this->_miolo->ProfileEnter('Database::ExecProc');
359 $result = @$this->conn->ExecProc($sql, $aParams);
360 $this->_miolo->ProfileExit('Database::ExecProc');
361 return $result;
362 }
363
364 //
365 // This function checks, if the last executed command caused a database
366 // error. If this is the case, an exception is raised, informing the
367 // reason(s).
368 //
369 function Assert($info = false)
370 {
371 $err = $this->conn->GetErrors();
372 if ($err)
373 {
374 throw new EDatabaseException($conf, $info . $err);
375 }
376 }
377
379 {
380 return $this->conn->affectedrows;
381 }
382
383 function CharToTimestamp($timestamp, $format='DD/MM/YYYY HH24:MI:SS')
384 {
385 return $this->conn->_chartotimestamp($timestamp, $format);
386 }
387
388 function CharToDate($date, $format='DD/MM/YYYY')
389 {
390 return $this->conn->_chartodate($date, $format);
391 }
392
393 function TimestampToChar($timestamp, $format='DD/MM/YYYY HH24:MI:SS')
394 {
395 return $this->conn->_timestamptochar($timestamp, $format);
396 }
397
398 function DateToChar($date, $format='DD/MM/YYYY')
399 {
400 return $this->conn->_datetochar($date, $format);
401 }
402}
403?>
QueryRange($sql, &$range)
Query($sql, $maxrows=0, $offset=0)
QueryChunk($sql, $maxrows, $offset, &$total)
Assert($info=false)
GetNewId($sequence='admin', $tableGenerator='miolo_sequence')
__construct($conf, $system, $host, $db, $user, $pass, $parameters=NULL, $port=NULL)
Definição mdatabase.class:21
Execute($sql)
ExecProc($sql, $aParams=null)
objQuery($sql, $maxrows=0)
DateToChar($date, $format='DD/MM/YYYY')
GetTransaction()
Definição mdatabase.class:82
CharToTimestamp($timestamp, $format='DD/MM/YYYY HH24:MI:SS')
TimestampToChar($timestamp, $format='DD/MM/YYYY HH24:MI:SS')
GetQuery($sql, $maxrows=0)
CharToDate($date, $format='DD/MM/YYYY')
GetTable($tablename)
Count($sql)
ExecuteBatch($sql_array)
setIdResult($idResult)
GetTableInfo($tablename)
static getInstance()
Definição miolo.class:134
Definição misr.class:3
static getBooleanValue($value)
Definição mutil.class:100