| 1 | # -*- perl -*- | ||||||||||
| 2 | ##---------------------------------------------------------------------------- | ||||||||||
| 3 | ## Database Object Interface - ~/lib//mnt/src/perl/DB-Object/lib/DB/Object/Postgres.pm | ||||||||||
| 4 | ## Version v1.6.0 | ||||||||||
| 5 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||||||||||
| 6 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||
| 7 | ## Created 2017/07/19 | ||||||||||
| 8 | ## Modified 2026/09/23 | ||||||||||
| 9 | ## All rights reserved | ||||||||||
| 10 | ## | ||||||||||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||||||||||
| 12 | ## under the same terms as Perl itself. | ||||||||||
| 13 | ##---------------------------------------------------------------------------- | ||||||||||
| 14 | # This is the subclassable module for driver specific ones. | ||||||||||
| 15 | package DB::Object::Postgres; | ||||||||||
| 16 | BEGIN | ||||||||||
| 17 | 1 | ▶ | { | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
| |||||||||||
| 18 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | warnings::register_categories( 'DB::Object' ); | |||||||||
| 21 | 1 | ▶ | use parent qw( DB::Object ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 1 | ▶ | use version; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 1 | ▶ | use vars qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | $VERSION | ||||||||||
| 25 | $DEBUG $ERROR $MOD_PERL $USE_BIND $USE_CACHE | ||||||||||
| 26 | $PLACEHOLDER_REGEXP $DATATYPES_DICT $EXCEPTION_CLASS | ||||||||||
| 27 | 1 | ); | |||||||||
| 28 | 1 | ▶ | use DBI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | eval | ||||||||||
| 30 | 1 | { | |||||||||
| 31 | 1 | require DBD::Pg; | |||||||||
| 32 | 0 | DBD::Pg->import( ':pg_types' ); | |||||||||
| 33 | }; | ||||||||||
| 34 | 1 | ▶ | die( $@ ) if( $@ ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | # the 'constant' property in the dictionary hash is added in structure() | ||||||||||
| 36 | # <https://www.postgresql.org/docs/13/datatype.html> | ||||||||||
| 37 | 0 | our $DATATYPES_DICT = | |||||||||
| 38 | { | ||||||||||
| 39 | bit => { | ||||||||||
| 40 | constant => '', | ||||||||||
| 41 | is_array => 0, | ||||||||||
| 42 | name => 'PG_BIT', | ||||||||||
| 43 | re => qr/^bit(?!>\s+varying)/, | ||||||||||
| 44 | type => 'bit', | ||||||||||
| 45 | }, | ||||||||||
| 46 | bool => { | ||||||||||
| 47 | alias => [qw( boolean )], | ||||||||||
| 48 | constant => '', | ||||||||||
| 49 | is_array => 0, | ||||||||||
| 50 | name => 'PG_BOOL', | ||||||||||
| 51 | re => qr/^(boolean|bool)\b/, | ||||||||||
| 52 | type => 'bool', | ||||||||||
| 53 | }, | ||||||||||
| 54 | box => { | ||||||||||
| 55 | constant => '', | ||||||||||
| 56 | is_array => 0, | ||||||||||
| 57 | name => 'PG_BOX', | ||||||||||
| 58 | re => qr/^box\b/, | ||||||||||
| 59 | type => 'box', | ||||||||||
| 60 | }, | ||||||||||
| 61 | bpchar => { | ||||||||||
| 62 | constant => '', | ||||||||||
| 63 | is_array => 0, | ||||||||||
| 64 | name => 'PG_BPCHAR', | ||||||||||
| 65 | re => qr/^bpchar\b/, | ||||||||||
| 66 | type => 'bpchar', | ||||||||||
| 67 | }, | ||||||||||
| 68 | bytea => { | ||||||||||
| 69 | constant => '', | ||||||||||
| 70 | is_array => 0, | ||||||||||
| 71 | name => 'PG_BYTEA', | ||||||||||
| 72 | re => qr/^bytea\b/ , | ||||||||||
| 73 | type => 'bytea', | ||||||||||
| 74 | }, | ||||||||||
| 75 | # Even if the table field type is CHAR, we use PG_VARCHAR, because PG_CHAR is for a single character, and if we used it, it would truncate the data to one character. | ||||||||||
| 76 | # <https://github.com/bucardo/dbdpg/issues/103> | ||||||||||
| 77 | char => { | ||||||||||
| 78 | alias => [qw( character char )], | ||||||||||
| 79 | constant => '', | ||||||||||
| 80 | is_array => 0, | ||||||||||
| 81 | name => 'PG_VARCHAR', | ||||||||||
| 82 | re => qr/^(character|char)\b(?![[:blank:]]+varying)/, | ||||||||||
| 83 | type => 'char', | ||||||||||
| 84 | }, | ||||||||||
| 85 | cid => { | ||||||||||
| 86 | constant => '', | ||||||||||
| 87 | is_array => 0, | ||||||||||
| 88 | name => 'PG_CID', | ||||||||||
| 89 | re => qr/^cid\b/ , | ||||||||||
| 90 | type => 'cid', | ||||||||||
| 91 | }, | ||||||||||
| 92 | cidr => { | ||||||||||
| 93 | constant => '', | ||||||||||
| 94 | is_array => 0, | ||||||||||
| 95 | name => 'PG_CIDR', | ||||||||||
| 96 | re => qr/^cidr\b/ , | ||||||||||
| 97 | type => 'cidr', | ||||||||||
| 98 | }, | ||||||||||
| 99 | circle => { | ||||||||||
| 100 | constant => '', | ||||||||||
| 101 | is_array => 0, | ||||||||||
| 102 | name => 'PG_CIRCLE', | ||||||||||
| 103 | re => qr/^circle\b/, | ||||||||||
| 104 | type => 'circle', | ||||||||||
| 105 | }, | ||||||||||
| 106 | cstring => { | ||||||||||
| 107 | constant => '', | ||||||||||
| 108 | is_array => 0, | ||||||||||
| 109 | name => 'PG_CSTRING', | ||||||||||
| 110 | re => qr/^cstring\b/, | ||||||||||
| 111 | type => 'cstring', | ||||||||||
| 112 | }, | ||||||||||
| 113 | date => { | ||||||||||
| 114 | constant => '', | ||||||||||
| 115 | is_array => 0, | ||||||||||
| 116 | name => 'PG_DATE', | ||||||||||
| 117 | re => qr/^date\b/, | ||||||||||
| 118 | type => 'date', | ||||||||||
| 119 | }, | ||||||||||
| 120 | # <https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-BUILTIN> | ||||||||||
| 121 | datemultirange => { | ||||||||||
| 122 | constant => '', | ||||||||||
| 123 | is_array => 0, | ||||||||||
| 124 | name => 'PG_DATEMULTIRANGE', | ||||||||||
| 125 | re => qr/^(datemultirange)\b/, | ||||||||||
| 126 | type => 'datemultirange', | ||||||||||
| 127 | }, | ||||||||||
| 128 | # <https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-BUILTIN> | ||||||||||
| 129 | daterange => { | ||||||||||
| 130 | constant => '', | ||||||||||
| 131 | is_array => 0, | ||||||||||
| 132 | name => 'PG_DATERANGE', | ||||||||||
| 133 | re => qr/^(daterange)\b/, | ||||||||||
| 134 | type => 'daterange', | ||||||||||
| 135 | }, | ||||||||||
| 136 | float4 => { | ||||||||||
| 137 | alias => [qw( real )], | ||||||||||
| 138 | constant => '', | ||||||||||
| 139 | is_array => 0, | ||||||||||
| 140 | name => 'PG_FLOAT4', | ||||||||||
| 141 | re => qr/^(real|float4)\b/, | ||||||||||
| 142 | type => 'float4', | ||||||||||
| 143 | }, | ||||||||||
| 144 | float8 => { | ||||||||||
| 145 | alias => ['double', 'double precision'], | ||||||||||
| 146 | constant => '', | ||||||||||
| 147 | is_array => 0, | ||||||||||
| 148 | name => 'PG_FLOAT8', | ||||||||||
| 149 | re => qr/^(double precision|float8)\b/, | ||||||||||
| 150 | type => 'float8', | ||||||||||
| 151 | }, | ||||||||||
| 152 | inet => { | ||||||||||
| 153 | constant => '', | ||||||||||
| 154 | is_array => 0, | ||||||||||
| 155 | name => 'PG_INET', | ||||||||||
| 156 | re => qr/^inet\b/, | ||||||||||
| 157 | type => 'inet', | ||||||||||
| 158 | }, | ||||||||||
| 159 | int2vector => { | ||||||||||
| 160 | constant => '', | ||||||||||
| 161 | is_array => 0, | ||||||||||
| 162 | name => 'PG_INT2VECTOR', | ||||||||||
| 163 | re => qr/^(int2vector)\b/, | ||||||||||
| 164 | type => 'int2vector', | ||||||||||
| 165 | }, | ||||||||||
| 166 | int2 => { | ||||||||||
| 167 | alias => [qw( smallint smallserial serial2 )], | ||||||||||
| 168 | constant => '', | ||||||||||
| 169 | is_array => 0, | ||||||||||
| 170 | name => 'PG_INT2', | ||||||||||
| 171 | re => qr/^(smallint|int2|smallserial|serial2)\b/, | ||||||||||
| 172 | type => 'int2', | ||||||||||
| 173 | }, | ||||||||||
| 174 | int4 => { | ||||||||||
| 175 | alias => [qw( integer int serial serial4 )], | ||||||||||
| 176 | constant => '', | ||||||||||
| 177 | is_array => 0, | ||||||||||
| 178 | name => 'PG_INT4', | ||||||||||
| 179 | re => qr/^(integer|int|int4|serial|serial4)\b/, | ||||||||||
| 180 | type => 'int4', | ||||||||||
| 181 | }, | ||||||||||
| 182 | # <https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-BUILTIN> | ||||||||||
| 183 | int4multirange => { | ||||||||||
| 184 | constant => '', | ||||||||||
| 185 | is_array => 0, | ||||||||||
| 186 | name => 'PG_INT4MULTIRANGE', | ||||||||||
| 187 | re => qr/^(int4multirange)\b/, | ||||||||||
| 188 | type => 'int4multirange', | ||||||||||
| 189 | }, | ||||||||||
| 190 | int4range => { | ||||||||||
| 191 | constant => '', | ||||||||||
| 192 | is_array => 0, | ||||||||||
| 193 | name => 'PG_INT4RANGE', | ||||||||||
| 194 | re => qr/^(int4range)\b/, | ||||||||||
| 195 | type => 'int4range', | ||||||||||
| 196 | }, | ||||||||||
| 197 | int8 => { | ||||||||||
| 198 | alias => [qw( bigint bigserial serial8 )], | ||||||||||
| 199 | constant => '', | ||||||||||
| 200 | is_array => 0, | ||||||||||
| 201 | name => 'PG_INT8', | ||||||||||
| 202 | re => qr/^(bigint|int8|bigserial|serial8)\b/, | ||||||||||
| 203 | type => 'int8', | ||||||||||
| 204 | }, | ||||||||||
| 205 | # <https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-BUILTIN> | ||||||||||
| 206 | int8multirange => { | ||||||||||
| 207 | constant => '', | ||||||||||
| 208 | is_array => 0, | ||||||||||
| 209 | name => 'PG_INT8MULTIRANGE', | ||||||||||
| 210 | re => qr/^(int8multirange)\b/, | ||||||||||
| 211 | type => 'int8multirange', | ||||||||||
| 212 | }, | ||||||||||
| 213 | int8range => { | ||||||||||
| 214 | constant => '', | ||||||||||
| 215 | is_array => 0, | ||||||||||
| 216 | name => 'PG_INT8RANGE', | ||||||||||
| 217 | re => qr/^(int8range)\b/, | ||||||||||
| 218 | type => 'int8range', | ||||||||||
| 219 | }, | ||||||||||
| 220 | interval => { | ||||||||||
| 221 | constant => '', | ||||||||||
| 222 | is_array => 0, | ||||||||||
| 223 | name => 'PG_INTERVAL', | ||||||||||
| 224 | re => qr/^interval\b/, | ||||||||||
| 225 | type => 'interval', | ||||||||||
| 226 | }, | ||||||||||
| 227 | json => { | ||||||||||
| 228 | constant => '', | ||||||||||
| 229 | is_array => 0, | ||||||||||
| 230 | name => 'PG_JSON', | ||||||||||
| 231 | re => qr/^json\b/, | ||||||||||
| 232 | type => 'json', | ||||||||||
| 233 | }, | ||||||||||
| 234 | jsonb => { | ||||||||||
| 235 | constant => '', | ||||||||||
| 236 | is_array => 0, | ||||||||||
| 237 | name => 'PG_JSONB', | ||||||||||
| 238 | re => qr/^jsonb\b/, | ||||||||||
| 239 | type => 'jsonb', | ||||||||||
| 240 | }, | ||||||||||
| 241 | # <https://www.postgresql.org/docs/current/datatype-json.html#DATATYPE-JSONPATH> | ||||||||||
| 242 | jsonpath => { | ||||||||||
| 243 | constant => '', | ||||||||||
| 244 | is_array => 0, | ||||||||||
| 245 | name => 'PG_JSONPATH', | ||||||||||
| 246 | re => qr/^jsonpath\b/, | ||||||||||
| 247 | type => 'jsonpath', | ||||||||||
| 248 | }, | ||||||||||
| 249 | line => { | ||||||||||
| 250 | constant => '', | ||||||||||
| 251 | is_array => 0, | ||||||||||
| 252 | name => 'PG_LINE', | ||||||||||
| 253 | re => qr/^line\b/, | ||||||||||
| 254 | type => 'line', | ||||||||||
| 255 | }, | ||||||||||
| 256 | lseg => { | ||||||||||
| 257 | constant => '', | ||||||||||
| 258 | is_array => 0, | ||||||||||
| 259 | name => 'PG_LSEG', | ||||||||||
| 260 | re => qr/^lseg\b/, | ||||||||||
| 261 | type => 'lseg', | ||||||||||
| 262 | }, | ||||||||||
| 263 | macaddr => { | ||||||||||
| 264 | constant => '', | ||||||||||
| 265 | is_array => 0, | ||||||||||
| 266 | name => 'PG_MACADDR', | ||||||||||
| 267 | re => qr/^macaddr\b/, | ||||||||||
| 268 | type => 'macaddr', | ||||||||||
| 269 | }, | ||||||||||
| 270 | macaddr8 => { | ||||||||||
| 271 | constant => '', | ||||||||||
| 272 | is_array => 0, | ||||||||||
| 273 | name => 'PG_MACADDR8', | ||||||||||
| 274 | re => qr/^macaddr8\b/, | ||||||||||
| 275 | type => 'macaddr8', | ||||||||||
| 276 | }, | ||||||||||
| 277 | money => { | ||||||||||
| 278 | constant => '', | ||||||||||
| 279 | is_array => 0, | ||||||||||
| 280 | name => 'PG_MONEY', | ||||||||||
| 281 | re => qr/^money\b/, | ||||||||||
| 282 | type => 'money', | ||||||||||
| 283 | }, | ||||||||||
| 284 | numeric => { | ||||||||||
| 285 | alias => [qw( decimal )], | ||||||||||
| 286 | constant => '', | ||||||||||
| 287 | is_array => 0, | ||||||||||
| 288 | name => 'PG_NUMERIC', | ||||||||||
| 289 | re => qr/^(numeric|decimal)\b/, | ||||||||||
| 290 | type => 'numeric', | ||||||||||
| 291 | }, | ||||||||||
| 292 | # <https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-BUILTIN> | ||||||||||
| 293 | nummultirange => { | ||||||||||
| 294 | constant => '', | ||||||||||
| 295 | is_array => 0, | ||||||||||
| 296 | name => 'PG_NUMMULTIRANGE', | ||||||||||
| 297 | re => qr/^(nummultirange)\b/, | ||||||||||
| 298 | type => 'nummultirange', | ||||||||||
| 299 | }, | ||||||||||
| 300 | # <https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-BUILTIN> | ||||||||||
| 301 | numrange => { | ||||||||||
| 302 | constant => '', | ||||||||||
| 303 | is_array => 0, | ||||||||||
| 304 | name => 'PG_NUMRANGE', | ||||||||||
| 305 | re => qr/^(numrange)\b/, | ||||||||||
| 306 | type => 'numrange', | ||||||||||
| 307 | }, | ||||||||||
| 308 | oid => { | ||||||||||
| 309 | constant => '', | ||||||||||
| 310 | is_array => 0, | ||||||||||
| 311 | name => 'PG_OID', | ||||||||||
| 312 | re => qr/^(oid)\b/, | ||||||||||
| 313 | type => 'oid', | ||||||||||
| 314 | }, | ||||||||||
| 315 | path => { | ||||||||||
| 316 | constant => '', | ||||||||||
| 317 | is_array => 0, | ||||||||||
| 318 | name => 'PG_PATH', | ||||||||||
| 319 | re => qr/^path\b/, | ||||||||||
| 320 | type => 'path', | ||||||||||
| 321 | }, | ||||||||||
| 322 | pg_lsn => { | ||||||||||
| 323 | constant => '', | ||||||||||
| 324 | is_array => 0, | ||||||||||
| 325 | name => 'PG_PG_LSN', | ||||||||||
| 326 | re => qr/^pg_lsn\b/, | ||||||||||
| 327 | type => 'pg_lsn', | ||||||||||
| 328 | }, | ||||||||||
| 329 | point => { | ||||||||||
| 330 | constant => '', | ||||||||||
| 331 | is_array => 0, | ||||||||||
| 332 | name => 'PG_POINT', | ||||||||||
| 333 | re => qr/^point\b/, | ||||||||||
| 334 | type => 'point', | ||||||||||
| 335 | }, | ||||||||||
| 336 | polygon => { | ||||||||||
| 337 | constant => '', | ||||||||||
| 338 | is_array => 0, | ||||||||||
| 339 | name => 'PG_POLYGON', | ||||||||||
| 340 | re => qr/^polygon\b/, | ||||||||||
| 341 | type => 'polygon', | ||||||||||
| 342 | }, | ||||||||||
| 343 | text => { | ||||||||||
| 344 | constant => '', | ||||||||||
| 345 | is_array => 0, | ||||||||||
| 346 | name => 'PG_TEXT', | ||||||||||
| 347 | re => qr/^text\b/, | ||||||||||
| 348 | type => 'text', | ||||||||||
| 349 | }, | ||||||||||
| 350 | time => { | ||||||||||
| 351 | constant => '', | ||||||||||
| 352 | is_array => 0, | ||||||||||
| 353 | name => 'PG_TIME', | ||||||||||
| 354 | re => qr/^time(\([^\)]+\))?\s+without\s+time\s+zone/, | ||||||||||
| 355 | type => 'time', | ||||||||||
| 356 | }, | ||||||||||
| 357 | timestamp => { | ||||||||||
| 358 | constant => '', | ||||||||||
| 359 | is_array => 0, | ||||||||||
| 360 | name => 'PG_TIMESTAMP', | ||||||||||
| 361 | re => qr/^timestamp(\([^\)]+\))?\s+without\s+time\s+zone/, | ||||||||||
| 362 | type => 'timestamp', | ||||||||||
| 363 | }, | ||||||||||
| 364 | timestamptz => { | ||||||||||
| 365 | constant => '', | ||||||||||
| 366 | is_array => 0, | ||||||||||
| 367 | name => 'PG_TIMESTAMPTZ', | ||||||||||
| 368 | re => qr/^(timestamp(\([^\)]+\))?\s+with\s+time\s+zone)|timestamptz/, | ||||||||||
| 369 | type => 'timestamptz', | ||||||||||
| 370 | }, | ||||||||||
| 371 | timetz => { | ||||||||||
| 372 | constant => '', | ||||||||||
| 373 | is_array => 0, | ||||||||||
| 374 | name => 'PG_TIMETZ', | ||||||||||
| 375 | re => qr/^(time(\([^\)]+\))?\s+with\s+time\s+zone)|timetz/, | ||||||||||
| 376 | type => 'timetz', | ||||||||||
| 377 | }, | ||||||||||
| 378 | # <https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-BUILTIN> | ||||||||||
| 379 | tsmultirange => { | ||||||||||
| 380 | constant => '', | ||||||||||
| 381 | is_array => 0, | ||||||||||
| 382 | name => 'PG_TSMULTIRANGE', | ||||||||||
| 383 | re => qr/^tsmultirange\b/, | ||||||||||
| 384 | type => 'tsmultirange', | ||||||||||
| 385 | }, | ||||||||||
| 386 | tsquery => { | ||||||||||
| 387 | constant => '', | ||||||||||
| 388 | is_array => 0, | ||||||||||
| 389 | name => 'PG_TSQUERY', | ||||||||||
| 390 | re => qr/^tsquery\b/, | ||||||||||
| 391 | type => 'tsquery', | ||||||||||
| 392 | }, | ||||||||||
| 393 | # <https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-BUILTIN> | ||||||||||
| 394 | tsrange => { | ||||||||||
| 395 | constant => '', | ||||||||||
| 396 | is_array => 0, | ||||||||||
| 397 | name => 'PG_TSRANGE', | ||||||||||
| 398 | re => qr/^tsrange\b/, | ||||||||||
| 399 | type => 'tsrange', | ||||||||||
| 400 | }, | ||||||||||
| 401 | # <https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-BUILTIN> | ||||||||||
| 402 | tstzmultirange => { | ||||||||||
| 403 | constant => '', | ||||||||||
| 404 | is_array => 0, | ||||||||||
| 405 | name => 'PG_TSTZMULTIRANGE', | ||||||||||
| 406 | re => qr/^tstzmultirange\b/, | ||||||||||
| 407 | type => 'tstzmultirange', | ||||||||||
| 408 | }, | ||||||||||
| 409 | # <https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-BUILTIN> | ||||||||||
| 410 | tstzrange => { | ||||||||||
| 411 | constant => '', | ||||||||||
| 412 | is_array => 0, | ||||||||||
| 413 | name => 'PG_TSTZRANGE', | ||||||||||
| 414 | re => qr/^tstzrange\b/, | ||||||||||
| 415 | type => 'tstzrange', | ||||||||||
| 416 | }, | ||||||||||
| 417 | tsvector => { | ||||||||||
| 418 | constant => '', | ||||||||||
| 419 | is_array => 0, | ||||||||||
| 420 | name => 'PG_TSVECTOR', | ||||||||||
| 421 | re => qr/^tsvector\b/, | ||||||||||
| 422 | type => 'tsvector', | ||||||||||
| 423 | }, | ||||||||||
| 424 | txid_snapshot => { | ||||||||||
| 425 | constant => '', | ||||||||||
| 426 | is_array => 0, | ||||||||||
| 427 | name => 'PG_TXID_SNAPSHOT', | ||||||||||
| 428 | re => qr/^txid_snapshot\b/, | ||||||||||
| 429 | type => 'txid_snapshot', | ||||||||||
| 430 | }, | ||||||||||
| 431 | uuid => { | ||||||||||
| 432 | constant => '', | ||||||||||
| 433 | is_array => 0, | ||||||||||
| 434 | name => 'PG_UUID', | ||||||||||
| 435 | re => qr/^uuid\b/, | ||||||||||
| 436 | type => 'uuid', | ||||||||||
| 437 | }, | ||||||||||
| 438 | varbit => { | ||||||||||
| 439 | alias => ['bit varying'], | ||||||||||
| 440 | constant => '', | ||||||||||
| 441 | is_array => 0, | ||||||||||
| 442 | name => 'PG_VARBIT', | ||||||||||
| 443 | re => qr/^(bit\s+varying|varbit)\b/, | ||||||||||
| 444 | type => 'varbit', | ||||||||||
| 445 | }, | ||||||||||
| 446 | varchar => { | ||||||||||
| 447 | alias => [qw( varchar ), 'character varying'], | ||||||||||
| 448 | constant => '', | ||||||||||
| 449 | is_array => 0, | ||||||||||
| 450 | name => 'PG_VARCHAR', | ||||||||||
| 451 | re => qr/^(character varying|varchar)\b/, | ||||||||||
| 452 | type => 'varchar', | ||||||||||
| 453 | }, | ||||||||||
| 454 | xml => { | ||||||||||
| 455 | constant => '', | ||||||||||
| 456 | is_array => 0, | ||||||||||
| 457 | name => 'PG_XML', | ||||||||||
| 458 | re => qr/^xml\b/, | ||||||||||
| 459 | type => 'xml', | ||||||||||
| 460 | }, | ||||||||||
| 461 | }; | ||||||||||
| 462 | 0 | our $PLACEHOLDER_REGEXP = qr/(?:(?<![?\w])\?(?![?\w])|(?<!\w)\$(?<index>[1-9]\d*)(?!\w))/; | |||||||||
| 463 | 0 | our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS; | |||||||||
| 464 | 0 | our $VERSION = 'v1.6.0'; | |||||||||
| 465 | }; | ||||||||||
| 466 | |||||||||||
| 467 | use strict; | ||||||||||
| 468 | use warnings; | ||||||||||
| 469 | # require DB::Object::Postgres::Statement; | ||||||||||
| 470 | # require DB::Object::Postgres::Tables; | ||||||||||
| 471 | # require DB::Object::Postgres::Lo; | ||||||||||
| 472 | our $DEBUG = 0; | ||||||||||
| 473 | our $USE_BIND = 0; | ||||||||||
| 474 | our $USE_CACHE = 0; | ||||||||||
| 475 | our $MOD_PERL = 0; | ||||||||||
| 476 | if( $INC{ 'Apache/DBI.pm' } && | ||||||||||
| 477 | substr( $ENV{ 'GATEWAY_INTERFACE' } || '', 0, 8 ) eq 'CGI-Perl' ) | ||||||||||
| 478 | { | ||||||||||
| 479 | $MOD_PERL++; | ||||||||||
| 480 | } | ||||||||||
| 481 | foreach my $type ( keys( %$DATATYPES_DICT ) ) | ||||||||||
| 482 | { | ||||||||||
| 483 | if( CORE::exists( $DATATYPES_DICT->{ $type }->{alias} ) && | ||||||||||
| 484 | ref( $DATATYPES_DICT->{ $type }->{alias} ) eq 'ARRAY' && | ||||||||||
| 485 | scalar( @{$DATATYPES_DICT->{ $type }->{alias}} ) ) | ||||||||||
| 486 | { | ||||||||||
| 487 | foreach my $alias ( @{$DATATYPES_DICT->{ $type }->{alias}} ) | ||||||||||
| 488 | { | ||||||||||
| 489 | next if( CORE::exists( $DATATYPES_DICT->{ $alias } ) ); | ||||||||||
| 490 | $DATATYPES_DICT->{ $alias } = $DATATYPES_DICT->{ $type }; | ||||||||||
| 491 | } | ||||||||||
| 492 | } | ||||||||||
| 493 | } | ||||||||||
| 494 | |||||||||||
| 495 | my $keys = $DBD::Pg::EXPORT_TAGS{pg_types}; | ||||||||||
| 496 | my @constants_to_ignore = qw( | ||||||||||
| 497 | PG_ASYNC PG_OLDQUERY_CANCEL PG_OLDQUERY_WAIT PG_ACLITEM PG_ACLITEMARRAY PG_ANY | ||||||||||
| 498 | PG_ANYARRAY PG_ANYCOMPATIBLE PG_ANYCOMPATIBLEARRAY PG_ANYCOMPATIBLEMULTIRANGE | ||||||||||
| 499 | PG_ANYCOMPATIBLENONARRAY PG_ANYCOMPATIBLERANGE PG_ANYELEMENT PG_ANYENUM PG_ANYMULTIRANGE | ||||||||||
| 500 | PG_ANYNONARRAY PG_ANYRANGE PG_EVENT_TRIGGER PG_FDW_HANDLER PG_GTSVECTOR | ||||||||||
| 501 | PG_GTSVECTORARRAY PG_INDEX_AM_HANDLER PG_INTERNAL PG_LANGUAGE_HANDLER PG_NAME | ||||||||||
| 502 | PG_NAMEARRAY PG_OID8 PG_OID8ARRAY PG_OIDVECTOR PG_OIDVECTORARRAY PG_PG_ATTRIBUTE | ||||||||||
| 503 | PG_PG_ATTRIBUTEARRAY PG_PG_BRIN_BLOOM_SUMMARY PG_PG_BRIN_MINMAX_MULTI_SUMMARY | ||||||||||
| 504 | PG_PG_CLASS PG_PG_CLASSARRAY PG_PG_DDL_COMMAND PG_PG_DEPENDENCIES PG_PG_MCV_LIST | ||||||||||
| 505 | PG_PG_NDISTINCT PG_PG_NODE_TREE PG_PG_PROC PG_PG_PROCARRAY PG_PG_SNAPSHOT | ||||||||||
| 506 | PG_PG_SNAPSHOTARRAY PG_PG_TYPE PG_PG_TYPEARRAY PG_RECORD PG_RECORDARRAY PG_REFCURSOR | ||||||||||
| 507 | PG_REFCURSORARRAY PG_REGCLASS PG_REGCLASSARRAY PG_REGCOLLATION PG_REGCOLLATIONARRAY | ||||||||||
| 508 | PG_REGCONFIG PG_REGCONFIGARRAY PG_REGDATABASE PG_REGDATABASEARRAY PG_REGDICTIONARY | ||||||||||
| 509 | PG_REGDICTIONARYARRAY PG_REGNAMESPACE PG_REGNAMESPACEARRAY PG_REGOPER PG_REGOPERARRAY | ||||||||||
| 510 | PG_REGOPERATOR PG_REGOPERATORARRAY PG_REGPROC PG_REGPROCARRAY PG_REGPROCEDURE | ||||||||||
| 511 | PG_REGPROCEDUREARRAY PG_REGROLE PG_REGROLEARRAY PG_REGTYPE PG_REGTYPEARRAY | ||||||||||
| 512 | PG_TABLE_AM_HANDLER PG_TID PG_TIDARRAY PG_TRIGGER PG_TSM_HANDLER PG_UNKNOWN PG_VOID | ||||||||||
| 513 | PG_XID PG_XID8 PG_XID8ARRAY PG_XIDARRAY | ||||||||||
| 514 | ); | ||||||||||
| 515 | foreach my $c ( @$keys ) | ||||||||||
| 516 | { | ||||||||||
| 517 | next if( scalar( grep( /^$c$/, @constants_to_ignore ) ) ); | ||||||||||
| 518 | if( $c =~ /^PG_(\w+)$/ ) | ||||||||||
| 519 | { | ||||||||||
| 520 | my $type = lc( $1 ); | ||||||||||
| 521 | my $is_array = 0; | ||||||||||
| 522 | if( substr( $type, -5 ) eq 'array' ) | ||||||||||
| 523 | { | ||||||||||
| 524 | $is_array++; | ||||||||||
| 525 | $type = substr( $type, 0, ( length( $type ) - 5 ) ); | ||||||||||
| 526 | } | ||||||||||
| 527 | my $code = \&{"DBD::Pg::$c"}; | ||||||||||
| 528 | my $val = $code->(); | ||||||||||
| 529 | if( !CORE::exists( $DATATYPES_DICT->{ $type } ) ) | ||||||||||
| 530 | { | ||||||||||
| 531 | warn( "Unknown PostgreSQL constant DBD::Pg::${c}" ) if( warnings::enabled( 'DB::Object' ) ); | ||||||||||
| 532 | next; | ||||||||||
| 533 | } | ||||||||||
| 534 | elsif( $is_array ) | ||||||||||
| 535 | { | ||||||||||
| 536 | my $copy = {}; | ||||||||||
| 537 | my $keys = [keys( %{$DATATYPES_DICT->{ $type }} )]; | ||||||||||
| 538 | @$copy{ @$keys } = @{$DATATYPES_DICT->{ $type }}{ @$keys }; | ||||||||||
| 539 | $copy->{type} = "${type}array"; | ||||||||||
| 540 | $copy->{name} = $c; | ||||||||||
| 541 | $copy->{constant} = $val; | ||||||||||
| 542 | $copy->{re} = qr/^(${type}array)\b/; | ||||||||||
| 543 | $copy->{is_array} = $is_array; | ||||||||||
| 544 | $DATATYPES_DICT->{ "${type}array" } = $copy; | ||||||||||
| 545 | next; | ||||||||||
| 546 | } | ||||||||||
| 547 | $DATATYPES_DICT->{ $type }->{is_array} = $is_array; | ||||||||||
| 548 | $DATATYPES_DICT->{ $type }->{constant} = $val; | ||||||||||
| 549 | } | ||||||||||
| 550 | } | ||||||||||
| 551 | |||||||||||
| 552 | # sub new is inherited from DB::Object | ||||||||||
| 553 | sub init | ||||||||||
| 554 | { | ||||||||||
| 555 | my $self = shift( @_ ); | ||||||||||
| 556 | $self->{_exception_class} = $EXCEPTION_CLASS; | ||||||||||
| 557 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||
| 558 | $self->{driver} = 'Pg'; | ||||||||||
| 559 | return( $self ); | ||||||||||
| 560 | } | ||||||||||
| 561 | |||||||||||
| 562 | # Get/set alias | ||||||||||
| 563 | # sub alias | ||||||||||
| 564 | |||||||||||
| 565 | # sub as_string | ||||||||||
| 566 | |||||||||||
| 567 | # sub avoid | ||||||||||
| 568 | |||||||||||
| 569 | sub attribute($;$@) | ||||||||||
| 570 | { | ||||||||||
| 571 | my $self = shift( @_ ); | ||||||||||
| 572 | # $h->{AttributeName} = ...; # set/write | ||||||||||
| 573 | # ... = $h->{AttributeName}; # get/read | ||||||||||
| 574 | # 1 means that the attribute may be modified | ||||||||||
| 575 | # 0 mneas that the attribute may only be read | ||||||||||
| 576 | my $name = shift( @_ ) if( @_ == 1 ); | ||||||||||
| 577 | my %arg = ( @_ ); | ||||||||||
| 578 | my %attr = | ||||||||||
| 579 | ( | ||||||||||
| 580 | ActiveKids => 0, | ||||||||||
| 581 | AutoCommit => 1, | ||||||||||
| 582 | AutoInactiveDestroy => 1, | ||||||||||
| 583 | CachedKids => 0, | ||||||||||
| 584 | ChildHandles => 0, | ||||||||||
| 585 | ChopBlanks => 1, | ||||||||||
| 586 | CursorName => 0, | ||||||||||
| 587 | Driver => 0, | ||||||||||
| 588 | ErrCount => 1, | ||||||||||
| 589 | Executed => 0, | ||||||||||
| 590 | FetchHashKeyName => 1, | ||||||||||
| 591 | HandleError => 1, | ||||||||||
| 592 | HandleSetErr => 1, | ||||||||||
| 593 | InactiveDestroy => 1, | ||||||||||
| 594 | Kids => 0, | ||||||||||
| 595 | NAME => 0, | ||||||||||
| 596 | NULLABLE => 0, | ||||||||||
| 597 | NUM_OF_FIELDS => 0, | ||||||||||
| 598 | NUM_OF_PARAMS => 0, | ||||||||||
| 599 | # Current database name | ||||||||||
| 600 | Name => 0, | ||||||||||
| 601 | PRECISION => 0, | ||||||||||
| 602 | PrintError => 1, | ||||||||||
| 603 | PrintWarn => 1, | ||||||||||
| 604 | Profile => 1, | ||||||||||
| 605 | RaiseError => 1, | ||||||||||
| 606 | # Specifies if the current database connection should be in read-only mode or not. | ||||||||||
| 607 | ReadOnly => 1, | ||||||||||
| 608 | RowCacheSize => 0, | ||||||||||
| 609 | RowsInCache => 0, | ||||||||||
| 610 | SCALE => 0, | ||||||||||
| 611 | ShowErrorStatement => 1, | ||||||||||
| 612 | Statement => 0, | ||||||||||
| 613 | TYPE => 0, | ||||||||||
| 614 | Taint => 1, | ||||||||||
| 615 | TaintIn => 1, | ||||||||||
| 616 | TaintOut => 1, | ||||||||||
| 617 | TraceLevel => 1, | ||||||||||
| 618 | Type => 1, | ||||||||||
| 619 | Username => 0, | ||||||||||
| 620 | Warn => 1, | ||||||||||
| 621 | pg_INV_READ => 0, | ||||||||||
| 622 | pg_INV_WRITE => 0, | ||||||||||
| 623 | pg_async_status => 0, | ||||||||||
| 624 | # If true, boolean values will be returned as the characters 't' and 'f' instead of '1' and '0'. | ||||||||||
| 625 | pg_bool_tf => 1, | ||||||||||
| 626 | pg_db => 0, | ||||||||||
| 627 | pg_default_port => 0, | ||||||||||
| 628 | pg_enable_utf8 => 1, | ||||||||||
| 629 | # Valid entries are 0, 1 and 2 | ||||||||||
| 630 | pg_errorlevel => 1, | ||||||||||
| 631 | pg_expand_array => 1, | ||||||||||
| 632 | pg_host => 0, | ||||||||||
| 633 | pg_lib_version => 0, | ||||||||||
| 634 | pg_options => 0, | ||||||||||
| 635 | pg_pass => 0, | ||||||||||
| 636 | pg_pid => 0, | ||||||||||
| 637 | # When true, question marks inside of statements are not treated as placeholders, e.g. geometric operators | ||||||||||
| 638 | pg_placeholder_dollaronly => 1, | ||||||||||
| 639 | # When true, colons inside of statements are not treated as placeholders | ||||||||||
| 640 | pg_placeholder_nocolons => 1, | ||||||||||
| 641 | pg_port => 0, | ||||||||||
| 642 | pg_prepare_now => 1, | ||||||||||
| 643 | pg_protocol => 0, | ||||||||||
| 644 | # Indicates if DBD::Pg should attempt to use server-side prepared statements. On by default | ||||||||||
| 645 | pg_server_prepare => 1, | ||||||||||
| 646 | pg_server_version => 0, | ||||||||||
| 647 | pg_socket => 0, | ||||||||||
| 648 | pg_standard_conforming_strings => 0, | ||||||||||
| 649 | pg_switch_prepared => 1, | ||||||||||
| 650 | pg_user => 0, | ||||||||||
| 651 | # Not used | ||||||||||
| 652 | # LongReadLen => 1, | ||||||||||
| 653 | # LongTruncOk => 1, | ||||||||||
| 654 | # CompatMode => 1, | ||||||||||
| 655 | ); | ||||||||||
| 656 | # Only those attribute exist | ||||||||||
| 657 | # Using an a non existing attribute produce an exception, so we better avoid | ||||||||||
| 658 | if( $name ) | ||||||||||
| 659 | { | ||||||||||
| 660 | return( $self->{dbh}->{ $name } ) if( exists( $attr{ $name } ) ); | ||||||||||
| 661 | } | ||||||||||
| 662 | else | ||||||||||
| 663 | { | ||||||||||
| 664 | my $value; | ||||||||||
| 665 | while( ( $name, $value ) = each( %arg ) ) | ||||||||||
| 666 | { | ||||||||||
| 667 | # We intend to modifiy the value of an attribute | ||||||||||
| 668 | # we are allowed to modify this value if it is true | ||||||||||
| 669 | if( exists( $attr{ $name } ) && | ||||||||||
| 670 | defined( $value ) && | ||||||||||
| 671 | $attr{ $name } ) | ||||||||||
| 672 | { | ||||||||||
| 673 | $self->{dbh}->{ $name } = $value; | ||||||||||
| 674 | } | ||||||||||
| 675 | } | ||||||||||
| 676 | } | ||||||||||
| 677 | } | ||||||||||
| 678 | |||||||||||
| 679 | # sub available_drivers(@) | ||||||||||
| 680 | |||||||||||
| 681 | sub begin_work($;$@) | ||||||||||
| 682 | { | ||||||||||
| 683 | my $self = shift( @_ ); | ||||||||||
| 684 | local $@; | ||||||||||
| 685 | my $rv = eval | ||||||||||
| 686 | { | ||||||||||
| 687 | return( $self->{dbh}->begin_work( @_ ) ); | ||||||||||
| 688 | }; | ||||||||||
| 689 | if( $@ ) | ||||||||||
| 690 | { | ||||||||||
| 691 | return( $self->error( "Error calling begin_work(). Have you forgotten to turn on AutoCommit?: $@" ) ); | ||||||||||
| 692 | } | ||||||||||
| 693 | elsif( !$rv && $self->{dbh}->err ) | ||||||||||
| 694 | { | ||||||||||
| 695 | return( $self->error({ code => $self->{dbh}->err, message => $self->{dbh}->errstr }) ); | ||||||||||
| 696 | } | ||||||||||
| 697 | |||||||||||
| 698 | if( defined( $rv ) ) | ||||||||||
| 699 | { | ||||||||||
| 700 | $self->_transaction_started; | ||||||||||
| 701 | return( $rv || 1 ); | ||||||||||
| 702 | } | ||||||||||
| 703 | return; | ||||||||||
| 704 | } | ||||||||||
| 705 | |||||||||||
| 706 | # This method is common to DB::Object and DB::Object::Statement | ||||||||||
| 707 | # sub bind | ||||||||||
| 708 | |||||||||||
| 709 | # sub cache | ||||||||||
| 710 | |||||||||||
| 711 | # sub check_driver(@;$@) | ||||||||||
| 712 | |||||||||||
| 713 | sub commit($;$@) | ||||||||||
| 714 | { | ||||||||||
| 715 | my $self = shift( @_ ); | ||||||||||
| 716 | local $@; | ||||||||||
| 717 | my $rv = eval | ||||||||||
| 718 | { | ||||||||||
| 719 | return( $self->{dbh}->commit( @_ ) ); | ||||||||||
| 720 | }; | ||||||||||
| 721 | if( $@ ) | ||||||||||
| 722 | { | ||||||||||
| 723 | return( $self->error( "Error calling commit(): $@" ) ); | ||||||||||
| 724 | } | ||||||||||
| 725 | elsif( !$rv && $self->{dbh}->err ) | ||||||||||
| 726 | { | ||||||||||
| 727 | return( $self->error({ code => $self->{dbh}->err, message => $self->{dbh}->errstr }) ); | ||||||||||
| 728 | } | ||||||||||
| 729 | |||||||||||
| 730 | if( defined( $rv ) ) | ||||||||||
| 731 | { | ||||||||||
| 732 | $self->_transaction_finished; | ||||||||||
| 733 | return( $rv || 1 ); | ||||||||||
| 734 | } | ||||||||||
| 735 | return; | ||||||||||
| 736 | } | ||||||||||
| 737 | |||||||||||
| 738 | # connect() is inherited by DB::Object | ||||||||||
| 739 | |||||||||||
| 740 | # See also datatype_to_constant() | ||||||||||
| 741 | # NOTE: sub constant_to_datatype is inherited from DB::Object | ||||||||||
| 742 | |||||||||||
| 743 | # sub copy | ||||||||||
| 744 | |||||||||||
| 745 | # Load a table with COPY ... FROM STDIN. | ||||||||||
| 746 | # $dbh->copy_from( table => 'nta_staging', from => '/some/file.csv', format => 'csv', encoding => 'UTF8' ) || | ||||||||||
| 747 | # die( $dbh->error ); | ||||||||||
| 748 | # The source can be a file path, a Module::Generic::File object, a filehandle, a scalar reference, or a | ||||||||||
| 749 | # code reference returning successive chunks of data and then undef. | ||||||||||
| 750 | # Returns the number of rows sent (counted on line feeds, so only meaningful for the text and csv | ||||||||||
| 751 | # formats without embedded line feeds), or undef and sets an error. | ||||||||||
| 752 | # It must be called inside a transaction if the caller wants to be able to roll back the load. | ||||||||||
| 753 | sub copy_from | ||||||||||
| 754 | { | ||||||||||
| 755 | my $self = shift( @_ ); | ||||||||||
| 756 | my $opts = $self->_get_args_as_hash( @_ ); | ||||||||||
| 757 | my $table = $opts->{table} || return( $self->error( "No table name was provided to copy into." ) ); | ||||||||||
| 758 | return( $self->error( "No source was provided to copy from." ) ) if( !defined( $opts->{from} ) ); | ||||||||||
| 759 | my $dbh = $self->{dbh} || return( $self->error( "No database handler found." ) ); | ||||||||||
| 760 | my $format = lc( $opts->{format} // 'text' ); | ||||||||||
| 761 | return( $self->error( "Unsupported COPY format '$format'." ) ) if( $format !~ /^(?:text|csv|binary)$/ ); | ||||||||||
| 762 | |||||||||||
| 763 | # Build the statement, quoting identifiers and literals through the DBI handle | ||||||||||
| 764 | my $sql = 'COPY ' . CORE::join( '.', map{ $dbh->quote_identifier( $_ ) } split( /\./, $table ) ); | ||||||||||
| 765 | if( ref( $opts->{columns} ) eq 'ARRAY' && scalar( @{$opts->{columns}} ) ) | ||||||||||
| 766 | { | ||||||||||
| 767 | $sql .= ' ( ' . CORE::join( ', ', map{ $dbh->quote_identifier( $_ ) } @{$opts->{columns}} ) . ' )'; | ||||||||||
| 768 | } | ||||||||||
| 769 | my @with = ( "FORMAT $format" ); | ||||||||||
| 770 | push( @with, 'HEADER' ) if( $opts->{header} ); | ||||||||||
| 771 | for my $k ( qw( delimiter null quote escape encoding ) ) | ||||||||||
| 772 | { | ||||||||||
| 773 | push( @with, sprintf( '%s %s', uc( $k ), $dbh->quote( $opts->{ $k } ) ) ) if( defined( $opts->{ $k } ) ); | ||||||||||
| 774 | } | ||||||||||
| 775 | $sql .= ' FROM STDIN WITH ( ' . CORE::join( ', ', @with ) . ' )'; | ||||||||||
| 776 | |||||||||||
| 777 | # With pg_enable_utf8, which this module enables, DBD::Pg encodes the strings it sends. Raw UTF-8 | ||||||||||
| 778 | # bytes must therefore be decoded into perl characters first, or they would be encoded a second time. | ||||||||||
| 779 | # A block may end in the middle of a multibyte character: FB_QUIET decodes what it can and leaves the | ||||||||||
| 780 | # incomplete bytes in $pending for the next block. Strings that already hold perl characters, such | ||||||||||
| 781 | # as literals from code under 'use utf8', are recognised by their UTF-8 flag and sent as they are. | ||||||||||
| 782 | # The option characters set to true forces this last behaviour for all the data. | ||||||||||
| 783 | my $decode = ( $format ne 'binary' && !$opts->{characters} && ( $opts->{encoding} // 'UTF8' ) =~ /^utf-?8$/i ); | ||||||||||
| 784 | my $pending = ''; | ||||||||||
| 785 | my $rows = 0; | ||||||||||
| 786 | my $put = sub | ||||||||||
| 787 | { | ||||||||||
| 788 | my $data = shift( @_ ); | ||||||||||
| 789 | return(1) unless( defined( $data ) && CORE::length( $data ) ); | ||||||||||
| 790 | if( $decode ) | ||||||||||
| 791 | { | ||||||||||
| 792 | if( utf8::is_utf8( $data ) ) | ||||||||||
| 793 | { | ||||||||||
| 794 | return( $self->error( "Characters received while incomplete UTF-8 bytes are still pending: the COPY source mixes bytes and characters." ) ) if( CORE::length( $pending ) ); | ||||||||||
| 795 | } | ||||||||||
| 796 | else | ||||||||||
| 797 | { | ||||||||||
| 798 | $pending .= $data; | ||||||||||
| 799 | $data = Encode::decode( 'UTF-8', $pending, Encode::FB_QUIET ); | ||||||||||
| 800 | # More than one incomplete character left means the bytes are not valid UTF-8 | ||||||||||
| 801 | return( $self->error( "Invalid UTF-8 data found in the COPY source." ) ) if( CORE::length( $pending ) > 3 ); | ||||||||||
| 802 | return(1) unless( CORE::length( $data ) ); | ||||||||||
| 803 | } | ||||||||||
| 804 | } | ||||||||||
| 805 | $rows += ( $data =~ tr/\n// ) unless( $format eq 'binary' ); | ||||||||||
| 806 | return( $dbh->pg_putcopydata( $data ) || $self->error({ code => $dbh->err, message => $dbh->errstr }) ); | ||||||||||
| 807 | }; | ||||||||||
| 808 | |||||||||||
| 809 | $dbh->do( $sql ) || return( $self->error({ code => $dbh->err, message => "Unable to start COPY: " . $dbh->errstr }) ); | ||||||||||
| 810 | my $src = $opts->{from}; | ||||||||||
| 811 | my $block = $opts->{block_size} || 65536; | ||||||||||
| 812 | my $ok = 1; | ||||||||||
| 813 | my $file; | ||||||||||
| 814 | local $@; | ||||||||||
| 815 | eval | ||||||||||
| 816 | { | ||||||||||
| 817 | if( ref( $src ) eq 'CODE' ) | ||||||||||
| 818 | { | ||||||||||
| 819 | while( defined( my $chunk = $src->() ) ) | ||||||||||
| 820 | { | ||||||||||
| 821 | ( $ok = $put->( $chunk ) ) || last; | ||||||||||
| 822 | } | ||||||||||
| 823 | } | ||||||||||
| 824 | elsif( ref( $src ) eq 'SCALAR' ) | ||||||||||
| 825 | { | ||||||||||
| 826 | $ok = $put->( $$src ); | ||||||||||
| 827 | } | ||||||||||
| 828 | else | ||||||||||
| 829 | { | ||||||||||
| 830 | my $fh; | ||||||||||
| 831 | if( $self->_is_glob( $src ) ) | ||||||||||
| 832 | { | ||||||||||
| 833 | $fh = $src; | ||||||||||
| 834 | } | ||||||||||
| 835 | else | ||||||||||
| 836 | { | ||||||||||
| 837 | # A file path or a Module::Generic::File object, opened here and closed below | ||||||||||
| 838 | $file = $self->new_file( $src ) || die( $self->error, "\n" ); | ||||||||||
| 839 | $fh = $file->open( '<', { binmode => 'raw' } ) || die( $file->error, "\n" ); | ||||||||||
| 840 | } | ||||||||||
| 841 | my $buf; | ||||||||||
| 842 | while( CORE::read( $fh, $buf, $block ) ) | ||||||||||
| 843 | { | ||||||||||
| 844 | ( $ok = $put->( $buf ) ) || last; | ||||||||||
| 845 | } | ||||||||||
| 846 | } | ||||||||||
| 847 | }; | ||||||||||
| 848 | my $err = $@; | ||||||||||
| 849 | $file->close if( defined( $file ) ); | ||||||||||
| 850 | # End the COPY in every case, so that the connection leaves COPY IN mode and a rollback can be sent | ||||||||||
| 851 | my $end = $dbh->pg_putcopyend; | ||||||||||
| 852 | return( $self->error( "Error while sending data to COPY: $err" ) ) if( $err ); | ||||||||||
| 853 | return( $self->pass_error ) if( !$ok ); | ||||||||||
| 854 | return( $self->error( "Invalid UTF-8 data at the end of the COPY source." ) ) if( CORE::length( $pending ) ); | ||||||||||
| 855 | return( $self->error({ code => $dbh->err, message => "Error ending COPY: " . $dbh->errstr }) ) if( !$end ); | ||||||||||
| 856 | return( $rows ); | ||||||||||
| 857 | } | ||||||||||
| 858 | |||||||||||
| 859 | sub create_db | ||||||||||
| 860 | { | ||||||||||
| 861 | my $self = shift( @_ ); | ||||||||||
| 862 | my $name = shift( @_ ) || return( $self->error( "No database name to create was provided." ) ); | ||||||||||
| 863 | my $opts = $self->_get_args_as_hash( @_ ); | ||||||||||
| 864 | my $params = []; | ||||||||||
| 865 | # https://www.postgresql.org/docs/9.5/sql-createdatabase.html | ||||||||||
| 866 | push( @$params, sprintf( 'OWNER=%s', $opts->{owner} ) ) if( $opts->{owner} ); | ||||||||||
| 867 | push( @$params, sprintf( 'TEMPLATE=%s', $opts->{template} ) ) if( $opts->{template} ); | ||||||||||
| 868 | push( @$params, sprintf( 'ENCODING=%s', $opts->{encoding} ) ) if( $opts->{encoding} ); | ||||||||||
| 869 | push( @$params, sprintf( 'LC_COLLATE=%s', $opts->{lc_collate} ) ) if( $opts->{lc_collate} ); | ||||||||||
| 870 | push( @$params, sprintf( 'LC_CTYPE=%s', $opts->{lc_ctype} ) ) if( $opts->{lc_ctype} ); | ||||||||||
| 871 | push( @$params, sprintf( 'TABLESPACE=%s', $opts->{tablespace} ) ) if( $opts->{tablespace} ); | ||||||||||
| 872 | push( @$params, sprintf( 'ALLOW_CONNECTIONS=%s', $opts->{allowcon} ? 'true' : 'false' ) ) if( length( $opts->{allowcon} ) ); | ||||||||||
| 873 | push( @$params, sprintf( 'CONNECTION LIMIT=%s', $opts->{connlimit} ) ) if( length( $opts->{connlimit} ) ); | ||||||||||
| 874 | push( @$params, sprintf( 'IS_TEMPLATE=%s', $opts->{istemplate} ? 'true' : 'false' ) ) if( length( $opts->{istemplate} ) ); | ||||||||||
| 875 | my $sql = "CREATE DATABASE $name"; | ||||||||||
| 876 | if( scalar( @$params ) ) | ||||||||||
| 877 | { | ||||||||||
| 878 | $sql .= ' WITH ' . join( ' ', @$params ); | ||||||||||
| 879 | } | ||||||||||
| 880 | my $dbh = $self->{dbh} || return( $self->error( "Could not find database handler." ) ); | ||||||||||
| 881 | my( $sth, $rc ); | ||||||||||
| 882 | # try-catch | ||||||||||
| 883 | local $@; | ||||||||||
| 884 | $sth = eval | ||||||||||
| 885 | { | ||||||||||
| 886 | $dbh->prepare( $sql ); | ||||||||||
| 887 | }; | ||||||||||
| 888 | if( $@ ) | ||||||||||
| 889 | { | ||||||||||
| 890 | return( $self->error( "An error occured while preparing SQL query to create database: ", $@ ) ); | ||||||||||
| 891 | } | ||||||||||
| 892 | $sth or return( $self->error( "An error occured while preparing SQL query to create database: ", $dbh->errstr ) ); | ||||||||||
| 893 | |||||||||||
| 894 | # try-catch | ||||||||||
| 895 | $rc = eval | ||||||||||
| 896 | { | ||||||||||
| 897 | $sth->execute; | ||||||||||
| 898 | }; | ||||||||||
| 899 | if( $@ ) | ||||||||||
| 900 | { | ||||||||||
| 901 | return( $self->error( "An error occured while executing SQL query to create database: ", $@ ) ); | ||||||||||
| 902 | } | ||||||||||
| 903 | $rc or return( $self->error( "An error occured while executing SQL query to create database: ", $sth->errstr ) ); | ||||||||||
| 904 | |||||||||||
| 905 | # try-catch | ||||||||||
| 906 | eval | ||||||||||
| 907 | { | ||||||||||
| 908 | $sth->finish; | ||||||||||
| 909 | }; | ||||||||||
| 910 | if( $@ ) | ||||||||||
| 911 | { | ||||||||||
| 912 | return( $self->error( "An unexpected error occurred while trying to finish the SQL query to create database: ", $@, "\n$sql" ) ); | ||||||||||
| 913 | } | ||||||||||
| 914 | |||||||||||
| 915 | my $ref = {}; | ||||||||||
| 916 | # my @keys = qw( host port login passwd schema opt debug ); | ||||||||||
| 917 | my $ok_params = $self->_connection_parameters; | ||||||||||
| 918 | foreach my $key ( @$ok_params ) | ||||||||||
| 919 | { | ||||||||||
| 920 | if( my $coderef = $self->can( $key ) ) | ||||||||||
| 921 | { | ||||||||||
| 922 | $ref->{ $key } = $coderef->( $self ); | ||||||||||
| 923 | } | ||||||||||
| 924 | } | ||||||||||
| 925 | $ref->{database} = $name; | ||||||||||
| 926 | $dbh = $self->connect( $ref ) || return( $self->error( "I could create the database \"$name\" but oddly enough, I could not connect to it with user \"$ref->{login}\" on host \"$ref->{host}\" with port \"$ref->{port}\"." ) ); | ||||||||||
| 927 | return( $dbh ); | ||||||||||
| 928 | } | ||||||||||
| 929 | |||||||||||
| 930 | sub create_table | ||||||||||
| 931 | { | ||||||||||
| 932 | my $self = shift( @_ ); | ||||||||||
| 933 | my $name = shift( @_ ) || return( $self->error( "No table name to create was provided." ) ); | ||||||||||
| 934 | my $opts = $self->_get_args_as_hash( @_ ); | ||||||||||
| 935 | return( $self->error( "Table \"$name\" already exists in the database." ) ) if( $self->table_exists( $name ) ); | ||||||||||
| 936 | my $schema = $self->schema; | ||||||||||
| 937 | my $sql = $opts->{sql} || return( $self->error( "No sql query was provided to create table \"$name\"." ) ); | ||||||||||
| 938 | # The schema is missing | ||||||||||
| 939 | if( $schema ) | ||||||||||
| 940 | { | ||||||||||
| 941 | $sql =~ s/\b(CREATE[[:blank:]]+(?:.*?)\bTABLE(?:[[:blank:]]+IF[[:blank:]]+NOT[[:blank:]]+EXISTS)?)[[:blank:]]+$name\b/$1 ${schema}.${name}/si; | ||||||||||
| 942 | } | ||||||||||
| 943 | my $rv = $self->do( $sql ) || return( $self->error( "An error occured while executing sql query to create table \"$name\": ", $self->errstr, "\nOriginal query was: $sql" ) ); | ||||||||||
| 944 | return( $rv ); | ||||||||||
| 945 | } | ||||||||||
| 946 | |||||||||||
| 947 | # sub create_table($;%) | ||||||||||
| 948 | |||||||||||
| 949 | # See DB::Object | ||||||||||
| 950 | # sub data_sources($;\%) | ||||||||||
| 951 | |||||||||||
| 952 | # sub data_type | ||||||||||
| 953 | |||||||||||
| 954 | # NOTE: sub datatype_dict is inherited | ||||||||||
| 955 | |||||||||||
| 956 | # See also constant_to_datatype() | ||||||||||
| 957 | # NOTE: sub datatype_to_constant is inherited from DB::Object | ||||||||||
| 958 | |||||||||||
| 959 | # NOTE: sub datatypes is in inherited | ||||||||||
| 960 | |||||||||||
| 961 | # sub database | ||||||||||
| 962 | |||||||||||
| 963 | sub databases | ||||||||||
| 964 | { | ||||||||||
| 965 | my $self = shift( @_ ); | ||||||||||
| 966 | # return( $self->error( "Not connected to PostgreSQL server yet. Issue $dbh->connect first." ) ) if( !$self->{ 'dbh' } ); | ||||||||||
| 967 | my $dbh; | ||||||||||
| 968 | # If there is no connection yet, then create one using the postgres login. | ||||||||||
| 969 | # There should not be a live user and database just to check what databases there are. | ||||||||||
| 970 | if( !$self->{dbh} ) | ||||||||||
| 971 | { | ||||||||||
| 972 | # try-catch | ||||||||||
| 973 | local $@; | ||||||||||
| 974 | $dbh = eval | ||||||||||
| 975 | { | ||||||||||
| 976 | $self->connect; | ||||||||||
| 977 | }; | ||||||||||
| 978 | if( $@ ) | ||||||||||
| 979 | { | ||||||||||
| 980 | return( $self->error( "Error trying to connect to the PostgreSQL server: $@" ) ); | ||||||||||
| 981 | } | ||||||||||
| 982 | $dbh or return( $self->pass_error ); | ||||||||||
| 983 | } | ||||||||||
| 984 | else | ||||||||||
| 985 | { | ||||||||||
| 986 | $dbh = $self; | ||||||||||
| 987 | } | ||||||||||
| 988 | my $temp = $dbh->do( "SELECT datname FROM pg_database" )->fetchall_arrayref; | ||||||||||
| 989 | return( $self->error( $dbh->errstr ) ) if( !$temp ); | ||||||||||
| 990 | my @dbases = map( $_->[0], @$temp ); | ||||||||||
| 991 | return( @dbases ); | ||||||||||
| 992 | } | ||||||||||
| 993 | |||||||||||
| 994 | # delete() is inherited from DB::Object | ||||||||||
| 995 | # sub delete | ||||||||||
| 996 | |||||||||||
| 997 | # sub disconnect($) | ||||||||||
| 998 | |||||||||||
| 999 | # sub do($;$@) | ||||||||||
| 1000 | |||||||||||
| 1001 | # sub enhance | ||||||||||
| 1002 | |||||||||||
| 1003 | # See DB::Object | ||||||||||
| 1004 | # sub err(@) | ||||||||||
| 1005 | # Meaning of the error code | ||||||||||
| 1006 | # 0 Empty query string | ||||||||||
| 1007 | # 1 A command that returns no data successfully completed. | ||||||||||
| 1008 | # 2 A command that returns data successfully completed. | ||||||||||
| 1009 | # 3 A COPY OUT command is still in progress. | ||||||||||
| 1010 | # 4 A COPY IN command is still in progress. | ||||||||||
| 1011 | # 5 A bad response was received from the backend. | ||||||||||
| 1012 | # 6 A nonfatal error occurred (a notice or warning message) | ||||||||||
| 1013 | # 7 A fatal error was returned: the last query failed. | ||||||||||
| 1014 | |||||||||||
| 1015 | # See DB::Object | ||||||||||
| 1016 | # sub errno | ||||||||||
| 1017 | |||||||||||
| 1018 | # See DB::Object | ||||||||||
| 1019 | # sub errmesg | ||||||||||
| 1020 | |||||||||||
| 1021 | # See DB::Object | ||||||||||
| 1022 | # sub errstr(@) | ||||||||||
| 1023 | # Affected by the pg_errorlevel setting | ||||||||||
| 1024 | |||||||||||
| 1025 | # sub fatal | ||||||||||
| 1026 | |||||||||||
| 1027 | # sub from_unixtime | ||||||||||
| 1028 | |||||||||||
| 1029 | # Inherited from DB::Object | ||||||||||
| 1030 | # sub format_statement($;\%\%@) | ||||||||||
| 1031 | |||||||||||
| 1032 | # sub format_update($;%) | ||||||||||
| 1033 | |||||||||||
| 1034 | sub func | ||||||||||
| 1035 | { | ||||||||||
| 1036 | my $self = shift( @_ ); | ||||||||||
| 1037 | my $table = shift( @_ ); | ||||||||||
| 1038 | # e.g. table_attributes to get the detail information on table columns | ||||||||||
| 1039 | my $func_name = shift( @_ ); | ||||||||||
| 1040 | # Returns: | ||||||||||
| 1041 | # NAME attribute name | ||||||||||
| 1042 | # TYPE attribute type | ||||||||||
| 1043 | # SIZE attribute size (-1 for variable size) | ||||||||||
| 1044 | # NULLABLE flag nullable | ||||||||||
| 1045 | # DEFAULT default value | ||||||||||
| 1046 | # CONSTRAINT constraint | ||||||||||
| 1047 | # PRIMARY_KEY flag is_primary_key | ||||||||||
| 1048 | # REMARKS attribute description | ||||||||||
| 1049 | return( $self->{ 'dbh' }->func( $table, $func_name ) ); | ||||||||||
| 1050 | } | ||||||||||
| 1051 | |||||||||||
| 1052 | # See DB::Object | ||||||||||
| 1053 | # sub getdefault($;%) | ||||||||||
| 1054 | |||||||||||
| 1055 | # sub group | ||||||||||
| 1056 | |||||||||||
| 1057 | sub get_sql_type | ||||||||||
| 1058 | { | ||||||||||
| 1059 | my $self = shift( @_ ); | ||||||||||
| 1060 | my $type = shift( @_ ) || return( $self->error( "No sql type was provided to get its constant." ) ); | ||||||||||
| 1061 | $type = lc( $type ); | ||||||||||
| 1062 | if( CORE::exists( $DATATYPES_DICT->{ $type } ) && | ||||||||||
| 1063 | $type ne $DATATYPES_DICT->{ $type }->{type} ) | ||||||||||
| 1064 | { | ||||||||||
| 1065 | $type = $DATATYPES_DICT->{ $type }->{type}; | ||||||||||
| 1066 | } | ||||||||||
| 1067 | my $const; | ||||||||||
| 1068 | if( substr( $type, 0, 3 ) eq 'pg_' ) | ||||||||||
| 1069 | { | ||||||||||
| 1070 | $const = $self->{dbh}->can( "DBD::Pg::\U${type}\E" ); | ||||||||||
| 1071 | } | ||||||||||
| 1072 | else | ||||||||||
| 1073 | { | ||||||||||
| 1074 | $const = $self->{dbh}->can( "DBD::Pg::PG_\U${type}\E" ); | ||||||||||
| 1075 | } | ||||||||||
| 1076 | return( '' ) if( !defined( $const ) ); | ||||||||||
| 1077 | return( $const->() ); | ||||||||||
| 1078 | } | ||||||||||
| 1079 | |||||||||||
| 1080 | # Specific to Postgres | ||||||||||
| 1081 | sub having | ||||||||||
| 1082 | { | ||||||||||
| 1083 | my $self = shift( @_ ); | ||||||||||
| 1084 | my $q = $self->_reset_query; | ||||||||||
| 1085 | return( $q->having( @_ ) ); | ||||||||||
| 1086 | } | ||||||||||
| 1087 | |||||||||||
| 1088 | # See DB::Object | ||||||||||
| 1089 | # sub insert | ||||||||||
| 1090 | |||||||||||
| 1091 | sub large_object | ||||||||||
| 1092 | { | ||||||||||
| 1093 | my $self = shift( @_ ); | ||||||||||
| 1094 | return( $self->error( "large_object() cannot be called as a class function." ) ) if( !ref( $self ) ); | ||||||||||
| 1095 | $self->_load_class( 'DB::Object::Postgres::Lo' ) || return( $self->pass_error ); | ||||||||||
| 1096 | # Parameter is a bitmask mode | ||||||||||
| 1097 | return( DB::Object::Postgres::Lo->new( $self->{dbh} ) ); | ||||||||||
| 1098 | } | ||||||||||
| 1099 | |||||||||||
| 1100 | # Must be superseded, or better yet, the one in DB::Object should probably be changed to ours | ||||||||||
| 1101 | sub last_insert_id | ||||||||||
| 1102 | { | ||||||||||
| 1103 | my $self = shift( @_ ); | ||||||||||
| 1104 | my $table = shift( @_ ) || $self->{ 'table' }; | ||||||||||
| 1105 | return( $self->{ 'dbh' }->last_insert_id( undef, undef, $table, undef ) ); | ||||||||||
| 1106 | } | ||||||||||
| 1107 | |||||||||||
| 1108 | # http://www.postgresql.org/docs/9.3/interactive/queries-limit.html | ||||||||||
| 1109 | # See DB::Object::Postgres::Query | ||||||||||
| 1110 | # sub limit | ||||||||||
| 1111 | |||||||||||
| 1112 | # sub local | ||||||||||
| 1113 | |||||||||||
| 1114 | # http://www.postgresql.org/docs/current/static/sql-lock.html | ||||||||||
| 1115 | sub lock | ||||||||||
| 1116 | { | ||||||||||
| 1117 | my $self = shift( @_ ); | ||||||||||
| 1118 | my $table = shift( @_ ) || return( $self->error( "You must provide a table name to lock onto." ) ); | ||||||||||
| 1119 | # ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE | | ||||||||||
| 1120 | # SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE | ||||||||||
| 1121 | my $mode = shift( @_ ) || undef(); | ||||||||||
| 1122 | my $nowait= shift( @_ ); | ||||||||||
| 1123 | my $query = "LOCK TABLE $table"; | ||||||||||
| 1124 | $query .= " IN $mode MODE" if( defined( $mode ) ); | ||||||||||
| 1125 | $query .= " NOWAIT" if( $nowait ); | ||||||||||
| 1126 | my $dbh = $self->{dbh} || return( $self->error( "Could not find database handler." ) ); | ||||||||||
| 1127 | my $sth = $dbh->prepare( $query ) || | ||||||||||
| 1128 | return( $self->error( "Error while preparing query to get lock on table '$table': ", $dbh->errstr() ) ); | ||||||||||
| 1129 | $sth->execute() || | ||||||||||
| 1130 | return( $self->error( "Error while executing query to get lock on table '$table': ", $sth->errstr() ) ); | ||||||||||
| 1131 | my $res = $sth->fetchrow; | ||||||||||
| 1132 | $sth->finish(); | ||||||||||
| 1133 | # We do not really need to track that information. | ||||||||||
| 1134 | # $self->{ '_locks' } ||= []; | ||||||||||
| 1135 | # push( @{ $self->{ '_locks' } }, $table ) if( $res && $res ne 'NULL' ); | ||||||||||
| 1136 | return( $res eq 'NULL' ? ( wantarray() ? () : undef() ) : $res ); | ||||||||||
| 1137 | } | ||||||||||
| 1138 | |||||||||||
| 1139 | sub make_schema | ||||||||||
| 1140 | { | ||||||||||
| 1141 | my $self = shift( @_ ); | ||||||||||
| 1142 | my $db = shift( @_ ) || $self->{ 'database' }; | ||||||||||
| 1143 | my $dbh; | ||||||||||
| 1144 | if( $self->{ 'dbh' } ) | ||||||||||
| 1145 | { | ||||||||||
| 1146 | $dbh = $self; | ||||||||||
| 1147 | } | ||||||||||
| 1148 | else | ||||||||||
| 1149 | { | ||||||||||
| 1150 | $dbh = $self->connect( { 'login' => 'postgres', 'database' => $db } ) || return( $self->error( "Cannot connect to Postgres database $db: " . $self->error ) ); | ||||||||||
| 1151 | } | ||||||||||
| 1152 | my $tables = $dbh->tables; | ||||||||||
| 1153 | return( $self->error( "There is no table in database $db." ) ) if( !@$tables ); | ||||||||||
| 1154 | my @schema_tables = (); | ||||||||||
| 1155 | my $max_field_size = <<SQL; | ||||||||||
| 1156 | SELECT MAX(LENGTH(a.attname)) AS "max_length" | ||||||||||
| 1157 | FROM pg_class c, pg_attribute a, pg_authid o | ||||||||||
| 1158 | WHERE c.relkind IN ('r', 'v', 'm', 'f') AND a.attrelid=c.oid AND c.relowner=o.oid AND o.rolname != 'postgres' | ||||||||||
| 1159 | SQL | ||||||||||
| 1160 | my $inherited_fields = <<SQL; | ||||||||||
| 1161 | SELECT c.relname AS table, a.attname AS field | ||||||||||
| 1162 | FROM pg_class c | ||||||||||
| 1163 | JOIN pg_inherits i ON c.oid = i.inhrelid | ||||||||||
| 1164 | JOIN pg_attribute a ON i.inhparent = a.attrelid | ||||||||||
| 1165 | WHERE attnum > 0 | ||||||||||
| 1166 | SQL | ||||||||||
| 1167 | my $table_info = <<SQL; | ||||||||||
| 1168 | SELECT c.oid, c.relchecks, c.relkind, c.relhasindex, c.relhasrules, c.relhastriggers, c.relhasoids, '', c.reltablespace, CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, c.relpersistence FROM pg_catalog.pg_class c | ||||||||||
| 1169 | LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid) | ||||||||||
| 1170 | WHERE c.relname ~ '^(%s)\$' | ||||||||||
| 1171 | SQL | ||||||||||
| 1172 | my $field_info = <<SQL; | ||||||||||
| 1173 | SELECT a.attname, | ||||||||||
| 1174 | pg_catalog.format_type(a.atttypid, a.atttypmod), | ||||||||||
| 1175 | (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128) | ||||||||||
| 1176 | FROM pg_catalog.pg_attrdef d | ||||||||||
| 1177 | WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef), | ||||||||||
| 1178 | a.attnotnull, a.attnum, | ||||||||||
| 1179 | (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t | ||||||||||
| 1180 | WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation, | ||||||||||
| 1181 | NULL AS indexdef, | ||||||||||
| 1182 | NULL AS attfdwoptions | ||||||||||
| 1183 | FROM pg_catalog.pg_attribute a | ||||||||||
| 1184 | WHERE a.attrelid = ? AND a.attnum > 0 AND NOT a.attisdropped | ||||||||||
| 1185 | ORDER BY a.attnum | ||||||||||
| 1186 | SQL | ||||||||||
| 1187 | my $get_constraints = <<SQL; | ||||||||||
| 1188 | SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, i.indisvalid, pg_catalog.pg_get_indexdef(i.indexrelid, 0, true), | ||||||||||
| 1189 | pg_catalog.pg_get_constraintdef(con.oid, true), contype, condeferrable, condeferred, c2.reltablespace | ||||||||||
| 1190 | FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i | ||||||||||
| 1191 | LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x')) | ||||||||||
| 1192 | WHERE c.oid = ? AND c.oid = i.indrelid AND i.indexrelid = c2.oid | ||||||||||
| 1193 | ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname | ||||||||||
| 1194 | SQL | ||||||||||
| 1195 | my $get_check_constraint = <<SQL; | ||||||||||
| 1196 | SELECT r.conname, pg_catalog.pg_get_constraintdef(r.oid, true) | ||||||||||
| 1197 | FROM pg_catalog.pg_constraint r | ||||||||||
| 1198 | WHERE r.conrelid = ? AND r.contype = 'c' | ||||||||||
| 1199 | ORDER BY 1 | ||||||||||
| 1200 | SQL | ||||||||||
| 1201 | my $get_fkey_constraint = <<SQL; | ||||||||||
| 1202 | SELECT conname, | ||||||||||
| 1203 | pg_catalog.pg_get_constraintdef(r.oid, true) as condef | ||||||||||
| 1204 | FROM pg_catalog.pg_constraint r | ||||||||||
| 1205 | WHERE r.conrelid = ? AND r.contype = 'f' ORDER BY 1 | ||||||||||
| 1206 | SQL | ||||||||||
| 1207 | my $inheritance = "SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhparent AND i.inhrelid = ? ORDER BY inhseqno"; | ||||||||||
| 1208 | my $get_tbl_comment = "SELECT description FROM pg_description WHERE (SELECT relname FROM pg_class WHERE oid=objoid) = ? and objsubid = 0"; | ||||||||||
| 1209 | my $get_field_comment = "SELECT d.description, a.attname FROM pg_description d, pg_attribute a WHERE (SELECT relname FROM pg_class WHERE oid=d.objoid) = ? AND a.attnum=d.objsubid AND a.attrelid=d.objoid AND d.objsubid > 0"; | ||||||||||
| 1210 | |||||||||||
| 1211 | # Get the max size of the fields to properly format the schema | ||||||||||
| 1212 | my $sth = $dbh->{dbh}->prepare_cached( $max_field_size ) || return( $self->error( $dbh->{dbh}->errstr ) ); | ||||||||||
| 1213 | $sth->execute() || return( $self->error( $sth->errstr ) ); | ||||||||||
| 1214 | my $fsize = $sth->fetchrow; | ||||||||||
| 1215 | $sth->finish; | ||||||||||
| 1216 | |||||||||||
| 1217 | # Get the list of all inherited fields per table, so we can exclude them in the schema we produce. | ||||||||||
| 1218 | $sth = $dbh->{dbh}->prepare_cached( $inherited_fields ) || return( $self->error( $dbh->{dbh}->errstr ) ); | ||||||||||
| 1219 | $sth->execute() || return( $self->error( $sth->errstr ) ); | ||||||||||
| 1220 | my $all = $sth->fetchall_arrayref( {} ); | ||||||||||
| 1221 | my $inherited = {}; | ||||||||||
| 1222 | foreach my $h ( @$all ) | ||||||||||
| 1223 | { | ||||||||||
| 1224 | $inherited->{ $h->{ 'table' } } = [] if( !exists( $inherited->{ $h->{ 'table' } } ) ); | ||||||||||
| 1225 | push( @{$inherited->{ $h->{ 'table' } }}, $h->{ 'field' } ); | ||||||||||
| 1226 | } | ||||||||||
| 1227 | $sth->finish; | ||||||||||
| 1228 | |||||||||||
| 1229 | foreach my $t ( @$tables ) | ||||||||||
| 1230 | { | ||||||||||
| 1231 | my @table_def = ( "CREATE TABLE $t (" ); | ||||||||||
| 1232 | # Get table overall information | ||||||||||
| 1233 | $dbh->bind( 0 ); | ||||||||||
| 1234 | $sth = $dbh->{dbh}->prepare_cached( sprintf( $table_info, $t ) ) || return( $self->error( $dbh->{dbh}->errstr ) ); | ||||||||||
| 1235 | $sth->execute() || return( $self->error( $sth->errstr ) ); | ||||||||||
| 1236 | my $tbl_info = $sth->fetchrow_hashref(); | ||||||||||
| 1237 | $sth->finish; | ||||||||||
| 1238 | $dbh->bind( 1 ); | ||||||||||
| 1239 | |||||||||||
| 1240 | # Get field info. | ||||||||||
| 1241 | $sth = $dbh->{Dbh}->prepare_cached( $field_info ) || return( $self->error( $dbh->{dbh}->errstr ) ); | ||||||||||
| 1242 | $sth->execute( $tbl_info->{ 'oid' } ) || return( $self->error( $sth->errstr ) ); | ||||||||||
| 1243 | my $rows = $sth->fetchall_arrayref( {} ); | ||||||||||
| 1244 | foreach my $r ( @$rows ) | ||||||||||
| 1245 | { | ||||||||||
| 1246 | next if( exists( $inherited->{ $t } ) && grep( /^$r->{ 'attname' }$/, @{$inherited->{ $t }} ) ); | ||||||||||
| 1247 | my @col_def = ( sprintf( "\t%-${fsize}s %s", $r->{ 'attname' }, uc( $r->{ 'format_type' } ) ) ); | ||||||||||
| 1248 | push( @col_def, 'NOT NULL' ) if( $r->{ 'attnotnull' } ); | ||||||||||
| 1249 | if( $r->{ 'format_type' } =~ /^(integer|int|boolean|decimal)$/i ) | ||||||||||
| 1250 | { | ||||||||||
| 1251 | push( @col_def, sprintf( 'DEFAULT %s', $r->{ 'substring' } ) ) if( $r->{ 'substring' } ); | ||||||||||
| 1252 | } | ||||||||||
| 1253 | else | ||||||||||
| 1254 | { | ||||||||||
| 1255 | push( @col_def, sprintf( "DEFAULT '%s'", $r->{ 'substring' } ) ) if( $r->{ 'substring' } ); | ||||||||||
| 1256 | } | ||||||||||
| 1257 | push( @table_def, join( " ", @col_def ) ); | ||||||||||
| 1258 | } | ||||||||||
| 1259 | $sth->finish; | ||||||||||
| 1260 | my @constraints = (); | ||||||||||
| 1261 | my @index = (); | ||||||||||
| 1262 | # Get primary key and unique index constraints | ||||||||||
| 1263 | $sth = $dbh->{dbh}->prepare_cached( $get_constraints ) || return( $self->error( $dbh->{dbh}->errstr ) ); | ||||||||||
| 1264 | $sth->execute( $tbl_info->{ 'oid' } ) || return( $self->error( $sth->errstr ) ); | ||||||||||
| 1265 | $rows = $sth->fetchall_arrayref( {} ); | ||||||||||
| 1266 | foreach my $r ( @$rows ) | ||||||||||
| 1267 | { | ||||||||||
| 1268 | if( $r->{ 'indisprimary' } || $r->{ 'indisunique' } ) | ||||||||||
| 1269 | { | ||||||||||
| 1270 | push( @constraints, sprintf( "\tCONSTRAINT %s %s", @$r{ qw( relname pg_get_constraintdef ) } ) ); | ||||||||||
| 1271 | } | ||||||||||
| 1272 | else | ||||||||||
| 1273 | { | ||||||||||
| 1274 | push( @index, sprintf( 'CREATE INDEX %s;', $r->{ 'pg_get_indexdef' } ) ); | ||||||||||
| 1275 | } | ||||||||||
| 1276 | } | ||||||||||
| 1277 | $sth->finish; | ||||||||||
| 1278 | # Get CHECK constraints | ||||||||||
| 1279 | $sth = $dbh->{dbh}->prepare_cached( $get_check_constraint ) || return( $self->error( $dbh->{dbh}->errstr ) ); | ||||||||||
| 1280 | $sth->execute( $tbl_info->{ 'oid' } ) || return( $self->error( $sth->errstr ) ); | ||||||||||
| 1281 | $rows = $sth->fetchall_arrayref( {} ); | ||||||||||
| 1282 | foreach my $r ( @$rows ) | ||||||||||
| 1283 | { | ||||||||||
| 1284 | push( @constraints, sprintf( "\tCONSTRAINT %s %s", @$r{ qw( conname pg_get_constraintdef ) } ) ); | ||||||||||
| 1285 | } | ||||||||||
| 1286 | $sth->finish; | ||||||||||
| 1287 | # Get foreign key constraints | ||||||||||
| 1288 | $sth = $dbh->{dbh}->prepare_cached( $get_fkey_constraint ) || return( $self->error( $dbh->{dbh}->errstr ) ); | ||||||||||
| 1289 | $sth->execute( $tbl_info->{ 'oid' } ) || return( $self->error( $sth->errstr ) ); | ||||||||||
| 1290 | $rows = $sth->fetchall_arrayref( {} ); | ||||||||||
| 1291 | foreach my $r ( @$rows ) | ||||||||||
| 1292 | { | ||||||||||
| 1293 | push( @constraints, sprintf( "\tCONSTRAINT %s %s", @$r{ qw( conname condef ) } ) ); | ||||||||||
| 1294 | } | ||||||||||
| 1295 | $sth->finish; | ||||||||||
| 1296 | push( @table_def, @constraints ); | ||||||||||
| 1297 | |||||||||||
| 1298 | # Get table inheritance | ||||||||||
| 1299 | $sth = $dbh->{dbh}->prepare_cached( $inheritance ) || return( $self->error( $dbh->{dbh}->errstr ) ); | ||||||||||
| 1300 | $sth->execute( $tbl_info->{ 'oid' } ) || return( $self->error( $sth->errstr ) ); | ||||||||||
| 1301 | $rows = $sth->fetchall_arrayref( {} ); | ||||||||||
| 1302 | my @inherit = map{ $_->{ 'oid' } } @$rows; | ||||||||||
| 1303 | $sth->finish; | ||||||||||
| 1304 | |||||||||||
| 1305 | # Add a comma at the end of each line now we know how many there are. | ||||||||||
| 1306 | for( my $i = 0; $i < $#table_def; $i++ ) | ||||||||||
| 1307 | { | ||||||||||
| 1308 | $table_def[ $i ] .= ","; | ||||||||||
| 1309 | } | ||||||||||
| 1310 | |||||||||||
| 1311 | if( @inherit ) | ||||||||||
| 1312 | { | ||||||||||
| 1313 | push( @table_def, sprintf( ') INHERITS (%s);', join( ", ", @inherit ) ) ); | ||||||||||
| 1314 | } | ||||||||||
| 1315 | else | ||||||||||
| 1316 | { | ||||||||||
| 1317 | push( @table_def, ');' ); | ||||||||||
| 1318 | } | ||||||||||
| 1319 | |||||||||||
| 1320 | # Put here the table indexes | ||||||||||
| 1321 | push( @table_def, @index ); | ||||||||||
| 1322 | |||||||||||
| 1323 | # Get comments on the table itself, if any | ||||||||||
| 1324 | $sth = $dbh->{dbh}->prepare_cached( $get_tbl_comment ) || return( $self->error( $sth->{dbh}->errstr ) ); | ||||||||||
| 1325 | $sth->execute( $t ) || return( $self->error( $sth->errstr ) ); | ||||||||||
| 1326 | my $ref = $sth->fetchrow_hashref(); | ||||||||||
| 1327 | $sth->finish; | ||||||||||
| 1328 | push( @table_def, sprintf( "COMMENT ON TABLE %s IS '%s';", $t, $ref->{ 'description' } ) ) if( $ref->{ 'description' } ); | ||||||||||
| 1329 | |||||||||||
| 1330 | # Get comments for each field | ||||||||||
| 1331 | $sth = $dbh->{dbh}->prepare_cached( $get_field_comment ) || return( $self->error( $sth->{dbh}->errstr ) ); | ||||||||||
| 1332 | $sth->execute( $t ) || return( $self->error( $sth->errstr ) ); | ||||||||||
| 1333 | $rows = $sth->fetchall_arrayref( {} ); | ||||||||||
| 1334 | $sth->finish; | ||||||||||
| 1335 | foreach my $r ( @$rows ) | ||||||||||
| 1336 | { | ||||||||||
| 1337 | push( @table_def, sprintf( "COMMENT ON COLUMN %s.%s IS '%s';", $t, @$r{ qw( attname description ) } ) ); | ||||||||||
| 1338 | } | ||||||||||
| 1339 | push( @schema_tables, join( "\n", @table_def ) ); | ||||||||||
| 1340 | } | ||||||||||
| 1341 | return( wantarray() ? @schema_tables : join( "\n\n", @schema_tables ) ); | ||||||||||
| 1342 | } | ||||||||||
| 1343 | |||||||||||
| 1344 | # sub no_bind | ||||||||||
| 1345 | |||||||||||
| 1346 | # sub no_cache | ||||||||||
| 1347 | |||||||||||
| 1348 | # sub order | ||||||||||
| 1349 | |||||||||||
| 1350 | # See DB::Object | ||||||||||
| 1351 | # sub param | ||||||||||
| 1352 | |||||||||||
| 1353 | # Thin wrapper around DBD::Pg::pg_getcopydata, to read the output of COPY ... TO STDOUT | ||||||||||
| 1354 | sub pg_getcopydata | ||||||||||
| 1355 | { | ||||||||||
| 1356 | my $self = shift( @_ ); | ||||||||||
| 1357 | local $@; | ||||||||||
| 1358 | # pg_getcopydata writes into its first argument, so we must alias it rather than copy it | ||||||||||
| 1359 | my $rv = eval | ||||||||||
| 1360 | { | ||||||||||
| 1361 | return( $self->{dbh}->pg_getcopydata( $_[0] ) ); | ||||||||||
| 1362 | }; | ||||||||||
| 1363 | return( $self->error( "Error calling pg_getcopydata: $@" ) ) if( $@ ); | ||||||||||
| 1364 | # Returns the number of bytes read, -1 when the COPY is finished, or -2 on error | ||||||||||
| 1365 | return( $self->error({ code => $self->{dbh}->err, message => $self->{dbh}->errstr }) ) if( defined( $rv ) && $rv == -2 ); | ||||||||||
| 1366 | return( $rv ); | ||||||||||
| 1367 | } | ||||||||||
| 1368 | |||||||||||
| 1369 | sub pg_notifies | ||||||||||
| 1370 | { | ||||||||||
| 1371 | my $self = shift( @_ ); | ||||||||||
| 1372 | my $dbh; | ||||||||||
| 1373 | if( $self->{dbh} ) | ||||||||||
| 1374 | { | ||||||||||
| 1375 | $dbh = $self->{dbh}; | ||||||||||
| 1376 | } | ||||||||||
| 1377 | elsif( $self->can( 'database_object' ) ) | ||||||||||
| 1378 | { | ||||||||||
| 1379 | $dbh = $self->database_object || | ||||||||||
| 1380 | return( $self->error( "No database object is set on this table object." ) ); | ||||||||||
| 1381 | $dbh = $dbh->{dbh} || return( $self->error( "No database handler is set on our database object." ) ); | ||||||||||
| 1382 | } | ||||||||||
| 1383 | else | ||||||||||
| 1384 | { | ||||||||||
| 1385 | return( $self->error( "The object of class ", ( ref( $self ) || $self ), " has no property 'dbh'." ) ); | ||||||||||
| 1386 | } | ||||||||||
| 1387 | |||||||||||
| 1388 | # try-catch | ||||||||||
| 1389 | local $@; | ||||||||||
| 1390 | my $ref = eval | ||||||||||
| 1391 | { | ||||||||||
| 1392 | $dbh->pg_notifies; | ||||||||||
| 1393 | }; | ||||||||||
| 1394 | if( $@ ) | ||||||||||
| 1395 | { | ||||||||||
| 1396 | return( $self->error( "Error calling PostgreSQL function pg_notifies: $@" ) ); | ||||||||||
| 1397 | } | ||||||||||
| 1398 | elsif( !$ref && $dbh->err ) | ||||||||||
| 1399 | { | ||||||||||
| 1400 | $self->error({ code => $dbh->err, message => $dbh->errstr }); | ||||||||||
| 1401 | } | ||||||||||
| 1402 | return( $ref ); | ||||||||||
| 1403 | } | ||||||||||
| 1404 | |||||||||||
| 1405 | sub pg_ping(@) | ||||||||||
| 1406 | { | ||||||||||
| 1407 | my $self = shift( @_ ); | ||||||||||
| 1408 | local $@; | ||||||||||
| 1409 | my $rv = eval | ||||||||||
| 1410 | { | ||||||||||
| 1411 | return( $self->{dbh}->pg_ping( @_ ) ); | ||||||||||
| 1412 | }; | ||||||||||
| 1413 | if( $@ ) | ||||||||||
| 1414 | { | ||||||||||
| 1415 | return( $self->error( "Error calling pg_ping: $@" ) ); | ||||||||||
| 1416 | } | ||||||||||
| 1417 | elsif( !$rv && $self->{dbh}->err ) | ||||||||||
| 1418 | { | ||||||||||
| 1419 | $self->error({ code => $self->{dbh}->err, message => $self->{dbh}->errstr }); | ||||||||||
| 1420 | } | ||||||||||
| 1421 | return( $rv ); | ||||||||||
| 1422 | } | ||||||||||
| 1423 | |||||||||||
| 1424 | # Thin wrapper around DBD::Pg::pg_putcopydata, to be used after $dbh->do( 'COPY ... FROM STDIN' ) | ||||||||||
| 1425 | sub pg_putcopydata | ||||||||||
| 1426 | { | ||||||||||
| 1427 | my $self = shift( @_ ); | ||||||||||
| 1428 | local $@; | ||||||||||
| 1429 | my $rv = eval | ||||||||||
| 1430 | { | ||||||||||
| 1431 | return( $self->{dbh}->pg_putcopydata( @_ ) ); | ||||||||||
| 1432 | }; | ||||||||||
| 1433 | return( $self->error( "Error calling pg_putcopydata: $@" ) ) if( $@ ); | ||||||||||
| 1434 | return( $self->error({ code => $self->{dbh}->err, message => $self->{dbh}->errstr }) ) if( !$rv ); | ||||||||||
| 1435 | return( $rv ); | ||||||||||
| 1436 | } | ||||||||||
| 1437 | |||||||||||
| 1438 | # Thin wrapper around DBD::Pg::pg_putcopyend, which ends a COPY ... FROM STDIN | ||||||||||
| 1439 | sub pg_putcopyend | ||||||||||
| 1440 | { | ||||||||||
| 1441 | my $self = shift( @_ ); | ||||||||||
| 1442 | local $@; | ||||||||||
| 1443 | my $rv = eval | ||||||||||
| 1444 | { | ||||||||||
| 1445 | return( $self->{dbh}->pg_putcopyend( @_ ) ); | ||||||||||
| 1446 | }; | ||||||||||
| 1447 | return( $self->error( "Error calling pg_putcopyend: $@" ) ) if( $@ ); | ||||||||||
| 1448 | return( $self->error({ code => $self->{dbh}->err, message => $self->{dbh}->errstr }) ) if( !$rv ); | ||||||||||
| 1449 | return( $rv ); | ||||||||||
| 1450 | } | ||||||||||
| 1451 | |||||||||||
| 1452 | # See DB::Object | ||||||||||
| 1453 | # sub ping(@) | ||||||||||
| 1454 | |||||||||||
| 1455 | # See DB::Object | ||||||||||
| 1456 | # sub prepare($;$) | ||||||||||
| 1457 | |||||||||||
| 1458 | # See DB::Object | ||||||||||
| 1459 | # sub prepare_cached | ||||||||||
| 1460 | |||||||||||
| 1461 | # sub query($$) | ||||||||||
| 1462 | |||||||||||
| 1463 | sub query_object { return( shift->_set_get_object( 'query_object', 'DB::Object::Postgres::Query', @_ ) ); } | ||||||||||
| 1464 | |||||||||||
| 1465 | # sub quote will be automatically loaded via AUTOLOADER | ||||||||||
| 1466 | |||||||||||
| 1467 | sub quote | ||||||||||
| 1468 | { | ||||||||||
| 1469 | my $self = shift( @_ ); | ||||||||||
| 1470 | my( $str, $type ) = @_; | ||||||||||
| 1471 | # my $dbh = $self->{dbh} || return( $self->error( "No database handler was set." ) ); | ||||||||||
| 1472 | my $dbh; | ||||||||||
| 1473 | unless( $dbh = $self->{dbh} ) | ||||||||||
| 1474 | { | ||||||||||
| 1475 | return( $self->SUPER::quote( $str ) ); | ||||||||||
| 1476 | } | ||||||||||
| 1477 | $type = { pg_type => $type } if( $type && ref( $type ) ne 'HASH' ); | ||||||||||
| 1478 | return( $dbh->quote( $str, $type ) ); | ||||||||||
| 1479 | } | ||||||||||
| 1480 | |||||||||||
| 1481 | # This can return an empty string on success, so use defined() to distinguish success from failure. | ||||||||||
| 1482 | sub release | ||||||||||
| 1483 | { | ||||||||||
| 1484 | my $self = shift( @_ ); | ||||||||||
| 1485 | local $@; | ||||||||||
| 1486 | my $rv = eval | ||||||||||
| 1487 | { | ||||||||||
| 1488 | return( $self->{dbh}->pg_release( @_ ) ); | ||||||||||
| 1489 | }; | ||||||||||
| 1490 | if( $@ ) | ||||||||||
| 1491 | { | ||||||||||
| 1492 | return( $self->error( "Error calling release: $@" ) ); | ||||||||||
| 1493 | } | ||||||||||
| 1494 | elsif( !defined( $rv ) && $self->{dbh}->err ) | ||||||||||
| 1495 | { | ||||||||||
| 1496 | return( $self->error({ code => $self->{dbh}->err, message => $self->{dbh}->errstr }) ); | ||||||||||
| 1497 | } | ||||||||||
| 1498 | if( defined( $rv ) ) | ||||||||||
| 1499 | { | ||||||||||
| 1500 | $self->_transaction_started; | ||||||||||
| 1501 | return( $rv || 1 ); | ||||||||||
| 1502 | } | ||||||||||
| 1503 | return; | ||||||||||
| 1504 | } | ||||||||||
| 1505 | |||||||||||
| 1506 | sub replace | ||||||||||
| 1507 | { | ||||||||||
| 1508 | return( shift->error( "Replace queries are not supported in PostgreSQL" ) ); | ||||||||||
| 1509 | } | ||||||||||
| 1510 | |||||||||||
| 1511 | # sub reset | ||||||||||
| 1512 | |||||||||||
| 1513 | # sub reverse | ||||||||||
| 1514 | |||||||||||
| 1515 | # sub rollback will be automatically loaded via AUTOLOADER | ||||||||||
| 1516 | |||||||||||
| 1517 | # Specific to Postgres | ||||||||||
| 1518 | sub returning | ||||||||||
| 1519 | { | ||||||||||
| 1520 | my $self = shift( @_ ); | ||||||||||
| 1521 | my $q = $self->_reset_query; | ||||||||||
| 1522 | return( $q->returning( @_ ) ); | ||||||||||
| 1523 | } | ||||||||||
| 1524 | |||||||||||
| 1525 | sub rollback | ||||||||||
| 1526 | { | ||||||||||
| 1527 | my $self = shift( @_ ); | ||||||||||
| 1528 | local $@; | ||||||||||
| 1529 | my $rv = eval | ||||||||||
| 1530 | { | ||||||||||
| 1531 | return( $self->{dbh}->rollback( @_ ) ); | ||||||||||
| 1532 | }; | ||||||||||
| 1533 | if( $@ ) | ||||||||||
| 1534 | { | ||||||||||
| 1535 | return( $self->error( "Error calling rollback: $@" ) ); | ||||||||||
| 1536 | } | ||||||||||
| 1537 | elsif( !defined( $rv ) && $self->{dbh}->err ) | ||||||||||
| 1538 | { | ||||||||||
| 1539 | return( $self->error({ code => $self->{dbh}->err, message => $self->{dbh}->errstr }) ); | ||||||||||
| 1540 | } | ||||||||||
| 1541 | if( defined( $rv ) ) | ||||||||||
| 1542 | { | ||||||||||
| 1543 | $self->_transaction_finished; | ||||||||||
| 1544 | return( $rv || 1 ); | ||||||||||
| 1545 | } | ||||||||||
| 1546 | return; | ||||||||||
| 1547 | } | ||||||||||
| 1548 | |||||||||||
| 1549 | # This can return an empty string on success, so use defined() to distinguish success from failure. | ||||||||||
| 1550 | sub rollback_to(@) | ||||||||||
| 1551 | { | ||||||||||
| 1552 | my $self = shift( @_ ); | ||||||||||
| 1553 | local $@; | ||||||||||
| 1554 | my $rv = eval | ||||||||||
| 1555 | { | ||||||||||
| 1556 | return( $self->{dbh}->pg_rollback_to( @_ ) ); | ||||||||||
| 1557 | }; | ||||||||||
| 1558 | if( $@ ) | ||||||||||
| 1559 | { | ||||||||||
| 1560 | return( $self->error( "Error calling rollback_to: $@" ) ); | ||||||||||
| 1561 | } | ||||||||||
| 1562 | elsif( !$rv && $self->{dbh}->err ) | ||||||||||
| 1563 | { | ||||||||||
| 1564 | return( $self->error({ code => $self->{dbh}->err, message => $self->{dbh}->errstr }) ); | ||||||||||
| 1565 | } | ||||||||||
| 1566 | if( defined( $rv ) ) | ||||||||||
| 1567 | { | ||||||||||
| 1568 | $self->_transaction_started; | ||||||||||
| 1569 | return( $rv || 1 ); | ||||||||||
| 1570 | } | ||||||||||
| 1571 | return; | ||||||||||
| 1572 | } | ||||||||||
| 1573 | |||||||||||
| 1574 | # This can return an empty string on success, so use defined() to distinguish success from failure. | ||||||||||
| 1575 | sub savepoint(@) | ||||||||||
| 1576 | { | ||||||||||
| 1577 | my $self = shift( @_ ); | ||||||||||
| 1578 | local $@; | ||||||||||
| 1579 | my $rv = eval | ||||||||||
| 1580 | { | ||||||||||
| 1581 | return( $self->{dbh}->pg_savepoint( @_ ) ); | ||||||||||
| 1582 | }; | ||||||||||
| 1583 | if( $@ ) | ||||||||||
| 1584 | { | ||||||||||
| 1585 | return( $self->error( "Error calling savepoint: $@" ) ); | ||||||||||
| 1586 | } | ||||||||||
| 1587 | elsif( !$rv && $self->{dbh}->err ) | ||||||||||
| 1588 | { | ||||||||||
| 1589 | return( $self->error({ code => $self->{dbh}->err, message => $self->{dbh}->errstr }) ); | ||||||||||
| 1590 | } | ||||||||||
| 1591 | if( defined( $rv ) ) | ||||||||||
| 1592 | { | ||||||||||
| 1593 | $self->_transaction_started; | ||||||||||
| 1594 | return( $rv || 1 ); | ||||||||||
| 1595 | } | ||||||||||
| 1596 | return; | ||||||||||
| 1597 | } | ||||||||||
| 1598 | |||||||||||
| 1599 | sub schema { return( shift->_set_get_scalar( 'schema', @_ ) ); } | ||||||||||
| 1600 | |||||||||||
| 1601 | sub search_path | ||||||||||
| 1602 | { | ||||||||||
| 1603 | my $self = shift( @_ ); | ||||||||||
| 1604 | if( @_ ) | ||||||||||
| 1605 | { | ||||||||||
| 1606 | my $val = @_ == 1 | ||||||||||
| 1607 | ? $self->_is_array( $_[0] ) | ||||||||||
| 1608 | ? shift( @_ ) | ||||||||||
| 1609 | : [ @_ ] | ||||||||||
| 1610 | : [ @_ ]; | ||||||||||
| 1611 | my $arg = sprintf( '"%s"', join( '", "', @$val ) ); | ||||||||||
| 1612 | my $dbh = $self->{dbh} || return( $self->error( "Could not find database handler." ) ); | ||||||||||
| 1613 | my $sth = $dbh->prepare_cached( "SET search_path = $arg" ) || return( $self->error( "Unable to prepare sql query to set search path: ", $dbh->errstr() ) ); | ||||||||||
| 1614 | my $rc = $sth->execute() || return( $self->error( "Unable to execute sql query to set search path: ", $self->errstr() ) ); | ||||||||||
| 1615 | $sth->finish; | ||||||||||
| 1616 | return( $rc ); | ||||||||||
| 1617 | } | ||||||||||
| 1618 | else | ||||||||||
| 1619 | { | ||||||||||
| 1620 | my $dbh = $self->{dbh} || return( $self->error( "Could not find database handler." ) ); | ||||||||||
| 1621 | my $sth = $dbh->prepare_cached( "SHOW search_path" ) || return( $self->error( "Unable to prepare sql query to get search path: ", $dbh->errstr() ) ); | ||||||||||
| 1622 | my $rc = $sth->execute() || return( $self->error( "Unable to execute sql query to set search path: ", $self->errstr() ) ); | ||||||||||
| 1623 | my $val = $sth->fetchrow; | ||||||||||
| 1624 | $sth->finish; | ||||||||||
| 1625 | my $arr = $self->new_array( [ split( /\,[[:blank:]]*/, $val ) ] ); | ||||||||||
| 1626 | # Removing surrounding quotes | ||||||||||
| 1627 | for( my $i = 0; $i < scalar( @$arr ); $i++ ) | ||||||||||
| 1628 | { | ||||||||||
| 1629 | $arr->[$i] =~ s/^\"|\"$//g; | ||||||||||
| 1630 | } | ||||||||||
| 1631 | return( $arr ); | ||||||||||
| 1632 | } | ||||||||||
| 1633 | } | ||||||||||
| 1634 | |||||||||||
| 1635 | # See DB::Object; _query_component is superseded and this makes the select customised to Posgres needs | ||||||||||
| 1636 | # sub select | ||||||||||
| 1637 | |||||||||||
| 1638 | # See DB::Object | ||||||||||
| 1639 | # sub set | ||||||||||
| 1640 | |||||||||||
| 1641 | # See DB::Object | ||||||||||
| 1642 | # sub sort | ||||||||||
| 1643 | |||||||||||
| 1644 | # Was previously getfd() | ||||||||||
| 1645 | sub socket | ||||||||||
| 1646 | { | ||||||||||
| 1647 | return( shift->{dbh}->{pg_socket} ); | ||||||||||
| 1648 | } | ||||||||||
| 1649 | |||||||||||
| 1650 | # See DB::Object | ||||||||||
| 1651 | # sub stat | ||||||||||
| 1652 | |||||||||||
| 1653 | # See DB::Object | ||||||||||
| 1654 | # sub state(@) | ||||||||||
| 1655 | |||||||||||
| 1656 | # See DB::Object | ||||||||||
| 1657 | # sub table | ||||||||||
| 1658 | |||||||||||
| 1659 | # See DB::Object | ||||||||||
| 1660 | sub table_exists | ||||||||||
| 1661 | { | ||||||||||
| 1662 | my $self = shift( @_ ); | ||||||||||
| 1663 | my $table = shift( @_ ) || | ||||||||||
| 1664 | return( $self->error( "You must provide a table name to access the table methods." ) ); | ||||||||||
| 1665 | my $opts = $self->_get_args_as_hash( @_ ); | ||||||||||
| 1666 | my $schema = $opts->{schema} || ''; | ||||||||||
| 1667 | my $search_path = $self->search_path || []; | ||||||||||
| 1668 | my $cache_tables = $self->cache_tables; | ||||||||||
| 1669 | my $tables_in_cache = $cache_tables->get({ | ||||||||||
| 1670 | host => $self->host, | ||||||||||
| 1671 | driver => $self->driver, | ||||||||||
| 1672 | port => $self->port, | ||||||||||
| 1673 | database => $self->database, | ||||||||||
| 1674 | }); | ||||||||||
| 1675 | foreach my $this ( @$tables_in_cache ) | ||||||||||
| 1676 | { | ||||||||||
| 1677 | if( $this->{name} eq $table && | ||||||||||
| 1678 | ( | ||||||||||
| 1679 | ( $schema && $this->{schema} eq $schema ) || | ||||||||||
| 1680 | ( $this->{schema} && scalar( grep{ $this->{schema} eq $_ } @$search_path ) ) || | ||||||||||
| 1681 | ( !$schema || $opts->{anywhere} ) | ||||||||||
| 1682 | ) ) | ||||||||||
| 1683 | { | ||||||||||
| 1684 | return(1); | ||||||||||
| 1685 | } | ||||||||||
| 1686 | } | ||||||||||
| 1687 | # We did not find it, so let's try by checking directly the database | ||||||||||
| 1688 | my $def = $self->table_info( $table ) || return( $self->pass_error ); | ||||||||||
| 1689 | return(1) if( scalar( @$def ) ); | ||||||||||
| 1690 | return(0); | ||||||||||
| 1691 | } | ||||||||||
| 1692 | |||||||||||
| 1693 | sub table_info | ||||||||||
| 1694 | { | ||||||||||
| 1695 | my $self = shift( @_ ); | ||||||||||
| 1696 | my $table = shift( @_ ) || | ||||||||||
| 1697 | return( $self->error( "You must provide a table name to access the table methods." ) ); | ||||||||||
| 1698 | my $opts = $self->_get_args_as_hash( @_ ); | ||||||||||
| 1699 | my $schema = $self->schema || $opts->{schema} || ''; | ||||||||||
| 1700 | my $sql = <<'EOT'; | ||||||||||
| 1701 | SELECT | ||||||||||
| 1702 | n.nspname AS "schema" | ||||||||||
| 1703 | ,c.relname AS "name" | ||||||||||
| 1704 | ,CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'table' END AS "type" | ||||||||||
| 1705 | ,pg_catalog.pg_get_userbyid(c.relowner) AS "owner" | ||||||||||
| 1706 | FROM pg_catalog.pg_class c | ||||||||||
| 1707 | LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace | ||||||||||
| 1708 | WHERE n.nspname = ANY(regexp_split_to_array((SELECT REPLACE(setting,'"$user"', (SELECT CURRENT_USER)) FROM pg_catalog.pg_settings WHERE name = 'search_path'), '\,\s*')) AND c.relname = ? | ||||||||||
| 1709 | EOT | ||||||||||
| 1710 | my $dbh = $self->{dbh} || return( $self->error( "Could not find database handler." ) ); | ||||||||||
| 1711 | my $sth = $dbh->prepare_cached( $sql ) || return( $self->error( "An error occured while preparing query to check if table \"$table\" exists in our database: ", $dbh->errstr ) ); | ||||||||||
| 1712 | $sth->execute( $table ) || return( $self->error( "An error occured while executing query to check if table \"$table\" exists in our database: ", $sth->errstr ) ); | ||||||||||
| 1713 | my $all = $sth->fetchall_arrayref( {} ); | ||||||||||
| 1714 | $sth->finish; | ||||||||||
| 1715 | return( [] ) if( !scalar( @$all ) ); | ||||||||||
| 1716 | return( $all ) if( !$schema || $opts->{anywhere} ); | ||||||||||
| 1717 | foreach my $ref ( @$all ) | ||||||||||
| 1718 | { | ||||||||||
| 1719 | return( $ref ) if( $ref->{schema} eq $schema ); | ||||||||||
| 1720 | } | ||||||||||
| 1721 | return( [] ); | ||||||||||
| 1722 | } | ||||||||||
| 1723 | |||||||||||
| 1724 | # See DB::Object | ||||||||||
| 1725 | # sub table_push | ||||||||||
| 1726 | |||||||||||
| 1727 | # See DB::Object | ||||||||||
| 1728 | # sub tables | ||||||||||
| 1729 | |||||||||||
| 1730 | sub tables_info | ||||||||||
| 1731 | { | ||||||||||
| 1732 | my $self = shift( @_ ); | ||||||||||
| 1733 | my $db = shift( @_ ) || $self->{database}; | ||||||||||
| 1734 | my @tables = (); | ||||||||||
| 1735 | # Parameters are: ?, schema, table, and type | ||||||||||
| 1736 | # my $sth = $self->{ 'dbh' }->table_info( undef, undef, $table, "TABLE,VIEW" ); | ||||||||||
| 1737 | # The original query was fetched by connecting to Postgres with psql -E and executing the command \z | ||||||||||
| 1738 | # This revised query will fetch only tables, views, materialised view and foreign tables, but will avoid the mysterious view called sequence_setvals | ||||||||||
| 1739 | # my $query = <<SQL; | ||||||||||
| 1740 | # SELECT n.nspname as "schema", | ||||||||||
| 1741 | # c.relname as "name" | ||||||||||
| 1742 | # FROM pg_catalog.pg_class c | ||||||||||
| 1743 | # LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace | ||||||||||
| 1744 | # WHERE c.relkind IN ('r', 'v', 'm', 'f') | ||||||||||
| 1745 | # AND n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid) AND c.relname != 'sequence_setvals' | ||||||||||
| 1746 | # ORDER BY c.oid | ||||||||||
| 1747 | # SQL | ||||||||||
| 1748 | # AND n.nspname OPERATOR(pg_catalog.~) '^((auth|public))$' | ||||||||||
| 1749 | my $query = <<'EOT'; | ||||||||||
| 1750 | SELECT | ||||||||||
| 1751 | n.nspname as "schema" | ||||||||||
| 1752 | ,c.relname as "name" | ||||||||||
| 1753 | ,CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'table' END as "type" | ||||||||||
| 1754 | ,pg_catalog.pg_get_userbyid(c.relowner) as "owner" | ||||||||||
| 1755 | FROM pg_catalog.pg_class c | ||||||||||
| 1756 | LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace | ||||||||||
| 1757 | WHERE c.relkind IN ('r','p','s','v','m','f','') | ||||||||||
| 1758 | AND n.nspname !~ '^(pg_|information_schema$)' | ||||||||||
| 1759 | AND c.relname != 'sequence_setvals' | ||||||||||
| 1760 | ORDER BY 1,2 | ||||||||||
| 1761 | EOT | ||||||||||
| 1762 | my $sth = $self->{dbh}->prepare_cached( $query ) || return( $self->error( sprintf( "Error while preparing query $query: %s", $self->{dbh}->errstr ) ) ); | ||||||||||
| 1763 | $sth->execute() || return( $self->error( sprintf( "Error while executing query $query: %s", $sth->errstr ) ) ); | ||||||||||
| 1764 | my $all = $sth->fetchall_arrayref( {} ); | ||||||||||
| 1765 | return( $all ); | ||||||||||
| 1766 | } | ||||||||||
| 1767 | |||||||||||
| 1768 | # See DB::Object | ||||||||||
| 1769 | # sub tables_refresh | ||||||||||
| 1770 | |||||||||||
| 1771 | # See DB::Object | ||||||||||
| 1772 | # sub tie | ||||||||||
| 1773 | |||||||||||
| 1774 | sub trace($;@) | ||||||||||
| 1775 | { | ||||||||||
| 1776 | my $self = shift( @_ ); | ||||||||||
| 1777 | # Value is a numeric level; see parse_trace_flag. | ||||||||||
| 1778 | return( $self->error( "Trace can only be used on active connection. Use connect first." ) ) if( !$self->{dbh} ); | ||||||||||
| 1779 | if( @_ ) | ||||||||||
| 1780 | { | ||||||||||
| 1781 | # my( $opt, $filename ) = @_; | ||||||||||
| 1782 | $self->{dbh}->trace( @_ ); | ||||||||||
| 1783 | } | ||||||||||
| 1784 | return( $self->{dbh}->trace ); | ||||||||||
| 1785 | } | ||||||||||
| 1786 | |||||||||||
| 1787 | sub trace_msg(@) | ||||||||||
| 1788 | { | ||||||||||
| 1789 | my $self = shift( @_ ); | ||||||||||
| 1790 | return( $self->error( "Trace can only be used on active connection. Use connect first." ) ) if( !$self->{dbh} ); | ||||||||||
| 1791 | # $dbh->trace_msg( $message_text, $min_level ); | ||||||||||
| 1792 | return( $self->{dbh}->trace_msg( @_ ) ); | ||||||||||
| 1793 | } | ||||||||||
| 1794 | |||||||||||
| 1795 | # sub unix_timestamp | ||||||||||
| 1796 | |||||||||||
| 1797 | sub unlock | ||||||||||
| 1798 | { | ||||||||||
| 1799 | shift->error( "unlock() does not work with Postgres." ); | ||||||||||
| 1800 | } | ||||||||||
| 1801 | |||||||||||
| 1802 | # See DB::Object | ||||||||||
| 1803 | # sub update | ||||||||||
| 1804 | |||||||||||
| 1805 | # See DB::Object | ||||||||||
| 1806 | # sub use | ||||||||||
| 1807 | |||||||||||
| 1808 | # See DB::Object | ||||||||||
| 1809 | # sub use_cache | ||||||||||
| 1810 | |||||||||||
| 1811 | # See DB::Object | ||||||||||
| 1812 | # sub use_bind | ||||||||||
| 1813 | |||||||||||
| 1814 | sub variables | ||||||||||
| 1815 | { | ||||||||||
| 1816 | return( shift->error( "variables is currently unsupported in Postgres" ) ); | ||||||||||
| 1817 | } | ||||||||||
| 1818 | |||||||||||
| 1819 | # See DB::Object | ||||||||||
| 1820 | # sub where | ||||||||||
| 1821 | |||||||||||
| 1822 | # https://www.postgresql.org/docs/10/sql-show.html | ||||||||||
| 1823 | # show something exists since at lease from 7.1 | ||||||||||
| 1824 | sub version | ||||||||||
| 1825 | { | ||||||||||
| 1826 | my $self = shift( @_ ); | ||||||||||
| 1827 | # If we already have the information, let's use our cache instead of making a query | ||||||||||
| 1828 | return( $self->{_db_version} ) if( length( $self->{_db_version} ) ); | ||||||||||
| 1829 | # e.g. 10.4 | ||||||||||
| 1830 | my $sql = 'SHOW server_version'; | ||||||||||
| 1831 | my $sth = $self->do( $sql ) || return( $self->error( "Unable to issue the sql statement '$sql' to get the server version: ", $self->errstr ) ); | ||||||||||
| 1832 | my $ver = $sth->fetchrow; | ||||||||||
| 1833 | $sth->finish; | ||||||||||
| 1834 | # e.g.: | ||||||||||
| 1835 | # 12.1 (Ubuntu 12.1-1.pgdg16.04+1) | ||||||||||
| 1836 | # 10.4 | ||||||||||
| 1837 | $ver =~ s/^(\d+(?:\.\S+)?).*?$/$1/; | ||||||||||
| 1838 | # We cache it | ||||||||||
| 1839 | $self->{_db_version} = version->parse( $ver ); | ||||||||||
| 1840 | return( $ver ); | ||||||||||
| 1841 | } | ||||||||||
| 1842 | |||||||||||
| 1843 | sub _check_connect_param | ||||||||||
| 1844 | { | ||||||||||
| 1845 | my $self = shift( @_ ); | ||||||||||
| 1846 | my $param = $self->SUPER::_check_connect_param( @_ ) || return( $self->pass_error ); | ||||||||||
| 1847 | # This is also what the psql command line tool does | ||||||||||
| 1848 | $param->{login} = ( getpwuid( $> ) )[0] if( !$param->{login} ); | ||||||||||
| 1849 | $param->{database} = 'postgres' if( !$param->{database} ); | ||||||||||
| 1850 | # By default | ||||||||||
| 1851 | $param->{port} = 5432 if( !CORE::exists( $param->{port} ) ); | ||||||||||
| 1852 | return( $param ); | ||||||||||
| 1853 | } | ||||||||||
| 1854 | |||||||||||
| 1855 | # Called from connect once all check was done to see if there are default to set | ||||||||||
| 1856 | sub _check_default_option | ||||||||||
| 1857 | { | ||||||||||
| 1858 | my $self = shift( @_ ); | ||||||||||
| 1859 | my $opts = $self->_get_args_as_hash( @_ ); | ||||||||||
| 1860 | return( $self->error( "Provided option is not a hash reference." ) ) if( !$self->_is_hash( $opts => 'strict' ) ); | ||||||||||
| 1861 | $opts->{client_encoding} = 'utf8' if( !CORE::exists( $opts->{client_encoding} ) ); | ||||||||||
| 1862 | # Enabled but with auto-guess | ||||||||||
| 1863 | $opts->{pg_enable_utf8} = -1 if( !CORE::exists( $opts->{pg_enable_utf8} ) && ( $opts->{client_encoding} eq 'utf8' || $opts->{client_encoding} eq 'utf-8' ) ); | ||||||||||
| 1864 | return( $opts ); | ||||||||||
| 1865 | } | ||||||||||
| 1866 | |||||||||||
| 1867 | # Called by _check_connect_param | ||||||||||
| 1868 | sub _connection_options | ||||||||||
| 1869 | { | ||||||||||
| 1870 | my $self = shift( @_ ); | ||||||||||
| 1871 | my $param = shift( @_ ); | ||||||||||
| 1872 | my @pg_params = grep( /^pg_/, keys( %$param ) ); | ||||||||||
| 1873 | my $opt = $self->SUPER::_connection_options( $param ) || return( $self->pass_error ); | ||||||||||
| 1874 | @$opt{ @pg_params } = @$param{ @pg_params }; | ||||||||||
| 1875 | return( $opt ); | ||||||||||
| 1876 | } | ||||||||||
| 1877 | |||||||||||
| 1878 | # NOTE: sub _connection_params2hash_driver is not necessary here. We use our parent's one. | ||||||||||
| 1879 | |||||||||||
| 1880 | # Called by _check_connect_param | ||||||||||
| 1881 | sub _connection_parameters | ||||||||||
| 1882 | { | ||||||||||
| 1883 | my $self = shift( @_ ); | ||||||||||
| 1884 | my $param = shift( @_ ); | ||||||||||
| 1885 | my $core = $self->new_array( [qw( | ||||||||||
| 1886 | db login passwd host port driver database schema server opt uri debug | ||||||||||
| 1887 | cache_connections cache_dir cache_query cache_table connect_via unknown_field | ||||||||||
| 1888 | use_cache id | ||||||||||
| 1889 | )] ); | ||||||||||
| 1890 | my @pg_params = grep( /^pg_/, keys( %$param ) ); | ||||||||||
| 1891 | # See DBD::Pg for the list of valid parameters | ||||||||||
| 1892 | $core->push( @pg_params ); | ||||||||||
| 1893 | my $core_fields = $self->{_core_fields} || []; | ||||||||||
| 1894 | $core->push( @$core_fields ); | ||||||||||
| 1895 | return( $core->unique(1) ); | ||||||||||
| 1896 | } | ||||||||||
| 1897 | |||||||||||
| 1898 | sub _dsn | ||||||||||
| 1899 | { | ||||||||||
| 1900 | my $self = shift( @_ ); | ||||||||||
| 1901 | my @params = (); | ||||||||||
| 1902 | # See pg_service.conf | ||||||||||
| 1903 | if( $self->{service} ) | ||||||||||
| 1904 | { | ||||||||||
| 1905 | @params = ( sprintf( 'dbi:%s:%s', @$self{ qw( driver service ) } ) ); | ||||||||||
| 1906 | } | ||||||||||
| 1907 | else | ||||||||||
| 1908 | { | ||||||||||
| 1909 | # It ends with ':' | ||||||||||
| 1910 | @params = ( sprintf( 'dbi:%s:', $self->{driver} ) ); | ||||||||||
| 1911 | } | ||||||||||
| 1912 | push( @params, sprintf( 'dbname=%s', $self->{database} ) ) if( $self->{database} ); | ||||||||||
| 1913 | push( @params, sprintf( 'host=%s', $self->{host} ) ) if( $self->{host} ); | ||||||||||
| 1914 | push( @params, sprintf( 'port=%d', $self->{port} ) ) if( $self->{port} ); | ||||||||||
| 1915 | # push( @params, "options=$options" ) if( length( $options ) ); | ||||||||||
| 1916 | return( join( ';', @params ) ); | ||||||||||
| 1917 | } | ||||||||||
| 1918 | |||||||||||
| 1919 | # NOTE: _cache_this -> See DB::Object | ||||||||||
| 1920 | # sub _cache_this | ||||||||||
| 1921 | |||||||||||
| 1922 | # NOTE _clean_statement -> DB::Object | ||||||||||
| 1923 | # sub _clean_statement | ||||||||||
| 1924 | |||||||||||
| 1925 | # NOTE _cleanup -> DB::Object | ||||||||||
| 1926 | # sub _cleanup | ||||||||||
| 1927 | |||||||||||
| 1928 | # NOTE _dbi_connect -> DB::Object | ||||||||||
| 1929 | # sub _dbi_connect | ||||||||||
| 1930 | |||||||||||
| 1931 | # NOTE _make_sth -> DB::Object | ||||||||||
| 1932 | # sub _make_sth | ||||||||||
| 1933 | |||||||||||
| 1934 | sub _placeholder_regexp { return( $PLACEHOLDER_REGEXP ) } | ||||||||||
| 1935 | |||||||||||
| 1936 | # Moved to DB::Object::Postgres::Query | ||||||||||
| 1937 | # sub _query_components | ||||||||||
| 1938 | |||||||||||
| 1939 | # See DB::Object. The meat of it is really in DB::Object::Query | ||||||||||
| 1940 | # sub _reset_query | ||||||||||
| 1941 | |||||||||||
| 1942 | # See DB::Object | ||||||||||
| 1943 | # sub _save_bind | ||||||||||
| 1944 | |||||||||||
| 1945 | # See DB::Object | ||||||||||
| 1946 | # sub _value2bind | ||||||||||
| 1947 | |||||||||||
| 1948 | # AUTOLOAD is inherited | ||||||||||
| 1949 | # AUTOLOAD | ||||||||||
| 1950 | |||||||||||
| 1951 | # NOTE: DESTROY | ||||||||||
| 1952 | DESTROY | ||||||||||
| 1953 | { | ||||||||||
| 1954 | # <https://perldoc.perl.org/perlobj#Destructors> | ||||||||||
| 1955 | CORE::local( $., $@, $!, $^E, $? ); | ||||||||||
| 1956 | CORE::return if( ${^GLOBAL_PHASE} eq 'DESTRUCT' ); | ||||||||||
| 1957 | my $self = CORE::shift( @_ ); | ||||||||||
| 1958 | CORE::return if( !CORE::defined( $self ) ); | ||||||||||
| 1959 | my $class = ref( $self ) || $self; | ||||||||||
| 1960 | if( $self->{sth} ) | ||||||||||
| 1961 | { | ||||||||||
| 1962 | # print( STDERR "DESTROY(): Terminating sth '$self' for query:\n$self->{ 'query' }\n" ) if( $DEBUG ); | ||||||||||
| 1963 | $self->{sth}->finish(); | ||||||||||
| 1964 | } | ||||||||||
| 1965 | elsif( $self->{dbh} && $class =~ /^DB\:\:Object\:\:Postgres$/ ) | ||||||||||
| 1966 | { | ||||||||||
| 1967 | local( $SIG{__WARN__} ) = sub{}; | ||||||||||
| 1968 | # $self->{ 'dbh' }->disconnect(); | ||||||||||
| 1969 | $self->disconnect(); | ||||||||||
| 1970 | } | ||||||||||
| 1971 | my $locks = $self->{_locks}; | ||||||||||
| 1972 | if( $locks && $self->_is_array( $locks ) ) | ||||||||||
| 1973 | { | ||||||||||
| 1974 | foreach my $name ( @$locks ) | ||||||||||
| 1975 | { | ||||||||||
| 1976 | $self->unlock( $name ); | ||||||||||
| 1977 | } | ||||||||||
| 1978 | } | ||||||||||
| 1979 | } | ||||||||||
| 1980 | |||||||||||
| 1981 | 1; | ||||||||||
| 1982 | # NOTE: POD | ||||||||||
| 1983 | __END__ | ||||||||||