MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
mconnection.class
Ir para a documentação deste ficheiro.
1<?php
7{
17 function __construct($conf)
18 {
19 parent::__construct($conf);
20 }
21
35 function _connect($dbhost, $LoginDB, $LoginUID, $LoginPWD, $persistent = true, $port='5432')
36 {
37 if( ! $port ) $port = '5432';
38 $arg = "host=$dbhost " . "dbname=$LoginDB " . "port=$port " . "user=$LoginUID " . "password=$LoginPWD";
39
40 $encoding = $this->_miolo->getConf("i18n.db_client_encoding");
41
42 if ( strlen($encoding) )
43 {
44 $arg .= " options='--client_encoding=$encoding'";
45 }
46
47 if (false && $persistent)
48 {
49 $this->id = pg_pconnect($arg);
50 }
51 else
52 {
53 $this->id = pg_connect($arg);
54 }
55 }
56
64 function _close()
65 {
66 pg_close($this->id);
67 }
68
78 function _error($resource = null)
79 {
80 return $this->getErrorField($resource ? $resource : $this->id);
81 }
82
92 function _execute($sql)
93 {
94 pg_send_query($this->id, $sql);
95 while (true)
96 {
97 $rs = pg_get_result($this->id);
98 if ($rs == false)
99 {
100 $rs = $lastRs;
101 break;
102 }
103 $lastRs = $rs;
104 }
105
106 $this->throwError($rs);
107
108 $success = false;
109
110 if ($rs)
111 {
112 $success = true;
113 $this->affectedrows = pg_affected_rows($rs);
114 pg_free_result($rs);
115 }
116 else
117 {
118 $this->traceback[] = pg_last_error($this->id);
119 }
120
121 return $success;
122 }
123
131 function _createquery()
132 {
133 return new PostgresQuery();
134 }
135
145 function _chartotimestamp($timestamp, $format='DD/MM/YYYY HH24:MI:SS')
146 {
147 return ":TO_TIMESTAMP('" . $timestamp . "','$format') ";
148 }
149
159 function _chartodate($date, $format='DD/MM/YYYY')
160 {
161 return ":TO_DATE('" . $date . "','$format') ";
162 }
163
173 function _timestamptochar($timestamp, $format='DD/MM/YYYY HH24:MI:SS')
174 {
175 return "TO_CHAR(" . $timestamp . ",'$format') ";
176 }
177
187 function _datetochar($date, $format='DD/MM/YYYY')
188 {
189 return "TO_CHAR(" . $date . ",'$format') ";
190 }
191
196 public function throwError($result)
197 {
198 $error = pg_result_error($result);
199
200 // Debug
201 if ( $error && MIOLO::isDebugMode() )
202 {
203 try
204 {
205 //echo '<br><br><pre>ERRO</pre>'; var_dump(MUtil::NVL($sql, $this->sql, MIOLO::getInstance()->getConf('lastsql')));
206 throw new Exception("Erro SQL");
207 }
208 catch (Exception $e)
209 {
210 var_dump($e->getMessage());
211 }
212 }
213
214 switch ( $this->getErrorField($result, PGSQL_DIAG_SQLSTATE) )
215 {
216 // Class 00 - Successful Completion
217 case '00000': // SUCCESSFUL COMPLETION
218 // throw nothing
219 break;
220
221 // Class 01 ? Warning
222 case '01000': // WARNING
223 case '0100C': // DYNAMIC RESULT SETS RETURNED
224 case '01008': // IMPLICIT ZERO BIT PADDING
225 case '01003': // NULL VALUE ELIMINATED IN SET FUNCTION
226 case '01007': // PRIVILEGE NOT GRANTED
227 case '01006': // PRIVILEGE NOT REVOKED
228 case '01004': // STRING DATA RIGHT TRUNCATION
229 case '01P01': // DEPRECATED FEATURE
230 // throw nothing
231 break;
232
233 // Class 02 ? No Data (this is also a warning class per the SQL standard)
234 case '02000': // NO DATA
235 case '02001': // NO ADDITIONAL DYNAMIC RESULT SETS RETURNED
236 // throw nothing
237 break;
238
239 // Class 03 ? SQL Statement Not Yet Complete
240 case '03000': // SQL STATEMENT NOT YET COMPLETE
241 // throw nothing
242 break;
243
244 // Class 08 ? Connection Exception
245 case '08000': // CONNECTION EXCEPTION
246 case '08003': // CONNECTION DOES NOT EXIST
247 case '08006': // CONNECTION FAILURE
248 case '08001': // SQLCLIENT UNABLE TO ESTABLISH SQLCONNECTION
249 case '08004': // SQLSERVER REJECTED ESTABLISHMENT OF SQLCONNECTION
250 case '08007': // TRANSACTION RESOLUTION UNKNOWN
251 case '08P01': // PROTOCOL VIOLATION
252 throw new MDatabaseException($this->getErrorField($result));
253 break;
254
255 // Class 09 ? Triggered Action Exception
256 case '09000': // TRIGGERED ACTION EXCEPTION
257 throw new MDatabaseException($this->getErrorField($result));
258 break;
259
260 // Class 0A ? Feature Not Supported
261 case '0A000': // FEATURE NOT SUPPORTED
262 throw new MDatabaseException($this->getErrorField($result));
263 break;
264
265 // Class 0B ? Invalid Transaction Initiation
266 case '0B000': // INVALID TRANSACTION INITIATION
267 throw new MDatabaseException($this->getErrorField($result));
268 break;
269
270 // Class 0F ? Locator Exception
271 case '0F000': // LOCATOR EXCEPTION
272 case '0F001': // INVALID LOCATOR SPECIFICATION
273 throw new MDatabaseException($this->getErrorField($result));
274 break;
275
276 // Class 0L ? Invalid Grantor
277 case '0L000': // INVALID GRANTOR
278 case '0LP01': // INVALID GRANT OPERATION
279 throw new MDatabaseException($this->getErrorField($result));
280 break;
281
282 // Class 0P ? Invalid Role Specification
283 case '0P000': // INVALID ROLE SPECIFICATION
284 throw new MDatabaseException($this->getErrorField($result));
285 break;
286
287 // Class 21 ? Cardinality Violation
288 case '21000': // CARDINALITY VIOLATION
289 throw new MDatabaseException($this->getErrorField($result));
290 break;
291
292 // Class 22 ? Data Exception
293 case '22000': // DATA EXCEPTION
294 case '2202E': // ARRAY SUBSCRIPT ERROR
295 case '22021': // CHARACTER NOT IN REPERTOIRE
296 case '22008': // DATETIME FIELD OVERFLOW
297 case '22012': // DIVISION BY ZERO
298 case '22005': // ERROR IN ASSIGNMENT
299 case '2200B': // ESCAPE CHARACTER CONFLICT
300 case '22022': // INDICATOR OVERFLOW
301 case '22015': // INTERVAL FIELD OVERFLOW
302 case '2201E': // INVALID ARGUMENT FOR LOGARITHM
303 case '2201F': // INVALID ARGUMENT FOR POWER FUNCTION
304 case '2201G': // INVALID ARGUMENT FOR WIDTH BUCKET FUNCTION
305 case '22018': // INVALID CHARACTER VALUE FOR CAST
306 case '22007': // INVALID DATETIME FORMAT
307 case '22019': // INVALID ESCAPE CHARACTER
308 case '2200D': // INVALID ESCAPE OCTET
309 case '22025': // INVALID ESCAPE SEQUENCE
310 case '22P06': // NONSTANDARD USE OF ESCAPE CHARACTER
311 case '22010': // INVALID INDICATOR PARAMETER VALUE
312 case '22020': // INVALID LIMIT VALUE
313 case '22023': // INVALID PARAMETER VALUE
314 case '2201B': // INVALID REGULAR EXPRESSION
315 case '22009': // INVALID TIME ZONE DISPLACEMENT VALUE
316 case '2200C': // INVALID USE OF ESCAPE CHARACTER
317 case '2200G': // MOST SPECIFIC TYPE MISMATCH
318 case '22004': // NULL VALUE NOT ALLOWED
319 case '22002': // NULL VALUE NO INDICATOR PARAMETER
320 case '22003': // NUMERIC VALUE OUT OF RANGE
321 case '22026': // STRING DATA LENGTH MISMATCH
322 case '22001': // STRING DATA RIGHT TRUNCATION
323 case '22011': // SUBSTRING ERROR
324 case '22027': // TRIM ERROR
325 case '22024': // UNTERMINATED C STRING
326 case '2200F': // ZERO LENGTH CHARACTER STRING
327 case '22P01': // FLOATING POINT EXCEPTION
328 case '22P02': // INVALID TEXT REPRESENTATION
329 case '22P03': // INVALID BINARY REPRESENTATION
330 case '22P04': // BAD COPY FILE FORMAT
331 case '22P05': // UNTRANSLATABLE CHARACTER
332 throw new MDatabaseException($this->getErrorField($result));
333 break;
334
335 // Class 23 ? Integrity Constraint Violation
336 case '23000': // INTEGRITY CONSTRAINT VIOLATION
337 case '23001': // RESTRICT VIOLATION
338 case '23502': // NOT NULL VIOLATION
339 throw new MDatabaseException($this->getErrorField($result));
340 break;
341
342 case '23503': // FOREIGN KEY VIOLATION
343
344 preg_match_all('/\"[^"]*\"/', $this->getErrorField($result), $messageData);
345
346 $messageData = $messageData[0];
347
348 // If there is a third quoted string in the messsage, it's a delete operation
349 if ( $messageData[2] )
350 {
351 $table = trim($messageData[0], '"');
352 $fkTable = trim($messageData[2], '"');
353 $message = _M('@1 references @2. It cannot be removed.', NULL, $fkTable, $table);
354 }
355 // Else it's an insert or an update
356 else
357 {
358 // Get the message detail to know which table has the absent reference
359 preg_match_all('/\"[^"]*\"/', $this->getErrorField($result), $messageData);
360 $table = trim($messageData[0][0], '"');
361
362 preg_match_all('/(\"[^"]*\"|\‍([^)]*\‍))/', $this->getErrorField($result, PGSQL_DIAG_MESSAGE_DETAIL), $messageData);
363 $fkColumn = trim($messageData[0][0], '()');
364 $fkTable = trim($messageData[0][2], '"');
365
366 $message = _M('Reference @1 from @2 is invalid on @3', NULL, $fkColumn, $fkTable, $table);
367 }
368
370 break;
371
372
373 case '23505': // UNIQUE VIOLATION
374
375 $message = _M('The identifier already exists on database');
376
378 break;
379
380
381 case '23514': // CHECK VIOLATION
382
383 // Get the error message to know which table has the business rule
384 preg_match_all('/\"[^"]*\"/', $this->getErrorField($result), $messageData);
385
386 $table = trim($messageData[0][0], '"');
387 $message = _M('Operation violates check constraint at table @1', NULL, $table);
388
390 break;
391
392
393 // Class 24 ? Invalid Cursor State
394 case '24000': // INVALID CURSOR STATE
395 throw new MDatabaseException($this->getErrorField($result));
396 break;
397
398 // Class 25 ? Invalid Transaction State
399 case '25000': // INVALID TRANSACTION STATE
400 case '25001': // ACTIVE SQL TRANSACTION
401 case '25002': // BRANCH TRANSACTION ALREADY ACTIVE
402 case '25008': // HELD CURSOR REQUIRES SAME ISOLATION LEVEL
403 case '25003': // INAPPROPRIATE ACCESS MODE FOR BRANCH TRANSACTION
404 case '25004': // INAPPROPRIATE ISOLATION LEVEL FOR BRANCH TRANSACTION
405 case '25005': // NO ACTIVE SQL TRANSACTION FOR BRANCH TRANSACTION
406 case '25006': // READ ONLY SQL TRANSACTION
407 case '25007': // SCHEMA AND DATA STATEMENT MIXING NOT SUPPORTED
408 case '25P01': // NO ACTIVE SQL TRANSACTION
409 case '25P02': // IN FAILED SQL TRANSACTION
410 throw new MDatabaseException($this->getErrorField($result));
411 break;
412
413 // Class 26 ? Invalid SQL Statement Name
414 case '26000': // INVALID SQL STATEMENT NAME
415 throw new MDatabaseException($this->getErrorField($result));
416 break;
417
418 // Class 27 ? Triggered Data Change Violation
419 case '27000': // TRIGGERED DATA CHANGE VIOLATION
420 throw new MDatabaseException($this->getErrorField($result));
421 break;
422
423 // Class 28 ? Invalid Authorization Specification
424 case '28000': // INVALID AUTHORIZATION SPECIFICATION
425 throw new MDatabaseException($this->getErrorField($result));
426 break;
427
428 // Class 2B ? Dependent Privilege Descriptors Still Exist
429 case '2B000': // DEPENDENT PRIVILEGE DESCRIPTORS STILL EXIST
430 case '2BP01': // DEPENDENT OBJECTS STILL EXIST
431 throw new MDatabaseException($this->getErrorField($result));
432 break;
433
434 // Class 2D ? Invalid Transaction Termination
435 case '2D000': // INVALID TRANSACTION TERMINATION
436 throw new MDatabaseException($this->getErrorField($result));
437 break;
438
439 // Class 2F ? SQL Routine Exception
440 case '2F000': // SQL ROUTINE EXCEPTION
441 case '2F005': // FUNCTION EXECUTED NO RETURN STATEMENT
442 case '2F002': // MODIFYING SQL DATA NOT PERMITTED
443 case '2F003': // PROHIBITED SQL STATEMENT ATTEMPTED
444 case '2F004': // READING SQL DATA NOT PERMITTED
445 throw new MDatabaseException($this->getErrorField($result));
446 break;
447
448 // Class 34 ? Invalid Cursor Name
449 case '34000': // INVALID CURSOR NAME
450 throw new MDatabaseException($this->getErrorField($result));
451 break;
452
453 // Class 38 ? External Routine Exception
454 case '38000': // EXTERNAL ROUTINE EXCEPTION
455 case '38001': // CONTAINING SQL NOT PERMITTED
456 case '38002': // MODIFYING SQL DATA NOT PERMITTED
457 case '38003': // PROHIBITED SQL STATEMENT ATTEMPTED
458 case '38004': // READING SQL DATA NOT PERMITTED
459 throw new MDatabaseException($this->getErrorField($result));
460 break;
461
462 // Class 39 ? External Routine Invocation Exception
463 case '39000': // EXTERNAL ROUTINE INVOCATION EXCEPTION
464 case '39001': // INVALID SQLSTATE RETURNED
465 case '39004': // NULL VALUE NOT ALLOWED
466 case '39P01': // TRIGGER PROTOCOL VIOLATED
467 case '39P02': // SRF PROTOCOL VIOLATED
468 throw new MDatabaseException($this->getErrorField($result));
469 break;
470
471 // Class 3B ? Savepoint Exception
472 case '3B000': // SAVEPOINT EXCEPTION
473 case '3B001': // INVALID SAVEPOINT SPECIFICATION
474 throw new MDatabaseException($this->getErrorField($result));
475 break;
476
477 // Class 3D ? Invalid Catalog Name
478 case '3D000': // INVALID CATALOG NAME
479 throw new MDatabaseException($this->getErrorField($result));
480 break;
481
482 // Class 3F ? Invalid Schema Name
483 case '3F000': // INVALID SCHEMA NAME
484 throw new MDatabaseException($this->getErrorField($result));
485 break;
486
487 // Class 40 ? Transaction Rollback
488 case '40000': // TRANSACTION ROLLBACK
489 case '40002': // TRANSACTION INTEGRITY CONSTRAINT VIOLATION
490 case '40001': // SERIALIZATION FAILURE
491 case '40003': // STATEMENT COMPLETION UNKNOWN
492 case '40P01': // DEADLOCK DETECTED
493 throw new MDatabaseException($this->getErrorField($result));
494 break;
495
496 // Class 42 ? Syntax Error or Access Rule Violation
497 case '42000': // SYNTAX ERROR OR ACCESS RULE VIOLATION
498 case '42601': // SYNTAX ERROR
499 case '42501': // INSUFFICIENT PRIVILEGE
500 case '42846': // CANNOT COERCE
501 case '42803': // GROUPING ERROR
502 case '42830': // INVALID FOREIGN KEY
503 case '42602': // INVALID NAME
504 case '42622': // NAME TOO LONG
505 case '42939': // RESERVED NAME
506 case '42804': // DATATYPE MISMATCH
507 case '42P18': // INDETERMINATE DATATYPE
508 case '42809': // WRONG OBJECT TYPE
509 case '42703': // UNDEFINED COLUMN
510 case '42883': // UNDEFINED FUNCTION
511 case '42P01': // UNDEFINED TABLE
512 case '42P02': // UNDEFINED PARAMETER
513 case '42704': // UNDEFINED OBJECT
514 case '42701': // DUPLICATE COLUMN
515 case '42P03': // DUPLICATE CURSOR
516 case '42P04': // DUPLICATE DATABASE
517 case '42723': // DUPLICATE FUNCTION
518 case '42P05': // DUPLICATE PREPARED STATEMENT
519 case '42P06': // DUPLICATE SCHEMA
520 case '42P07': // DUPLICATE TABLE
521 case '42712': // DUPLICATE ALIAS
522 case '42710': // DUPLICATE OBJECT
523 case '42702': // AMBIGUOUS COLUMN
524 case '42725': // AMBIGUOUS FUNCTION
525 case '42P08': // AMBIGUOUS PARAMETER
526 case '42P09': // AMBIGUOUS ALIAS
527 case '42P10': // INVALID COLUMN REFERENCE
528 case '42611': // INVALID COLUMN DEFINITION
529 case '42P11': // INVALID CURSOR DEFINITION
530 case '42P12': // INVALID DATABASE DEFINITION
531 case '42P13': // INVALID FUNCTION DEFINITION
532 case '42P14': // INVALID PREPARED STATEMENT DEFINITION
533 case '42P15': // INVALID SCHEMA DEFINITION
534 case '42P16': // INVALID TABLE DEFINITION
535 case '42P17': // INVALID OBJECT DEFINITION
536 throw new MDatabaseException($this->getErrorField($result));
537 break;
538
539 // Class 44 ? WITH CHECK OPTION Violation
540 case '44000': // WITH CHECK OPTION VIOLATION
541 throw new MDatabaseException($this->getErrorField($result));
542 break;
543
544 // Class 53 ? Insufficient Resources
545 case '53000': // INSUFFICIENT RESOURCES
546 case '53100': // DISK FULL
547 case '53200': // OUT OF MEMORY
548 case '53300': // TOO MANY CONNECTIONS
549 throw new MDatabaseException($this->getErrorField($result));
550 break;
551
552 // Class 54 ? Program Limit Exceeded
553 case '54000': // PROGRAM LIMIT EXCEEDED
554 case '54001': // STATEMENT TOO COMPLEX
555 case '54011': // TOO MANY COLUMNS
556 case '54023': // TOO MANY ARGUMENTS
557 throw new MDatabaseException($this->getErrorField($result));
558 break;
559
560 // Class 55 ? Object Not In Prerequisite State
561 case '55000': // OBJECT NOT IN PREREQUISITE STATE
562 case '55006': // OBJECT IN USE
563 case '55P02': // CANT CHANGE RUNTIME PARAM
564 case '55P03': // LOCK NOT AVAILABLE
565 throw new MDatabaseException($this->getErrorField($result));
566 break;
567
568 // Class 57 ? Operator Intervention
569 case '57000': // OPERATOR INTERVENTION
570 case '57014': // QUERY CANCELED
571 case '57P01': // ADMIN SHUTDOWN
572 case '57P02': // CRASH SHUTDOWN
573 case '57P03': // CANNOT CONNECT NOW
574 throw new MDatabaseException($this->getErrorField($result));
575 break;
576
577 // Class 58 ? System Error (errors external to PostgreSQL itself)
578 case '58030': // IO ERROR
579 case '58P01': // UNDEFINED FILE
580 case '58P02': // DUPLICATE FILE
581 throw new MDatabaseException($this->getErrorField($result));
582 break;
583
584 // Class F0 ? Configuration File Error
585 case 'F0000': // CONFIG FILE ERROR
586 case 'F0001': // LOCK FILE EXISTS
587 throw new MDatabaseException($this->getErrorField($result));
588 break;
589
590 // Class P0 ? PL/pgSQL Error
591 case 'P0000': // PLPGSQL ERROR
592 throw new MDatabaseException($this->getErrorField($result));
593 break;
594
595 case 'P0001': // RAISE EXCEPTION
597 break;
598
599 // Class XX ? Internal Error
600 case 'XX000': // INTERNAL ERROR
601 case 'XX001': // DATA CORRUPTED
602 case 'XX002': // INDEX CORRUPTED
603 throw new MDatabaseException($this->getErrorField($result));
604 break;
605 }
606 }
607
615 public function getErrorField($result, $fieldcode = PGSQL_DIAG_MESSAGE_PRIMARY)
616 {
617 return pg_result_error_field($result, $fieldcode);
618 }
619}
620?>
static isDebugMode()
Definição miolo.class:1825
_chartotimestamp($timestamp, $format='DD/MM/YYYY HH24:MI:SS')
_error($resource=null)
_chartodate($date, $format='DD/MM/YYYY')
_connect($dbhost, $LoginDB, $LoginUID, $LoginPWD, $persistent=true, $port='5432')
_timestamptochar($timestamp, $format='DD/MM/YYYY HH24:MI:SS')
_datetochar($date, $format='DD/MM/YYYY')
getErrorField($result, $fieldcode=PGSQL_DIAG_MESSAGE_PRIMARY)