| 1 | use 5.014; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package # hide from PAUSE | ||
| 5 | DBIx::DBO::DBD::Pg; | ||
| 6 | use Carp 'croak'; | ||
| 7 | |||
| 8 | sub _get_table_schema { | ||
| 9 | my($class, $me, $table) = @_; | ||
| 10 | |||
| 11 | my $q_table = $table =~ s/([\\_%])/\\$1/gr; | ||
| 12 | |||
| 13 | # First try just these types | ||
| 14 | my $info = $me->rdbh->table_info(undef, undef, $q_table, | ||
| 15 | 'TABLE,VIEW,GLOBAL TEMPORARY,LOCAL TEMPORARY,SYSTEM TABLE')->fetchall_arrayref({}); | ||
| 16 | # Then if we found nothing, try any type | ||
| 17 | $info = $me->rdbh->table_info(undef, undef, $q_table)->fetchall_arrayref({}) if $info and @$info == 0; | ||
| 18 | croak 'Invalid table: '.$class->_qi($me, $table) unless $info and @$info == 1 and $info->[0]{pg_table} eq $table; | ||
| 19 | return $info->[0]{pg_schema}; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub _get_column_info { | ||
| 23 | my($class, $me, $schema, $table) = @_; | ||
| 24 | |||
| 25 | my $q_schema = $schema; | ||
| 26 | my $q_table = $table; | ||
| 27 | $q_schema =~ s/([\\_%])/\\$1/g if defined $q_schema; | ||
| 28 | $q_table =~ s/([\\_%])/\\$1/g; | ||
| 29 | |||
| 30 | my $cols = $me->rdbh->column_info(undef, $q_schema, $q_table, '%')->fetchall_arrayref({}); | ||
| 31 | croak 'Invalid table: '.$class->_qi($me, $schema, $table) unless @$cols; | ||
| 32 | |||
| 33 | map { $_->{pg_column} => $_->{ORDINAL_POSITION} } @$cols; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _set_table_key_info { | ||
| 37 | my($class, $me, $schema, $table, $h) = @_; | ||
| 38 | |||
| 39 | if (my $keys = $me->rdbh->primary_key_info(undef, $schema, $table)) { | ||
| 40 | # In Pg the KEY_SEQ is actually the column index! Rows returned are in key seq order | ||
| 41 | # And the column names are quoted so we use the pg_column names instead | ||
| 42 | $h->{PrimaryKeys} = [ map $h->{Columns}[$_->{KEY_SEQ} - 1], @{$keys->fetchall_arrayref({})} ]; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _save_last_insert_id { | ||
| 47 | my($class, $me, $sth) = @_; | ||
| 48 | |||
| 49 | return $sth->{Database}->last_insert_id(undef, @$me{qw(Schema Name)}, undef); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub _build_limit { | ||
| 53 | my($class, $me) = @_; | ||
| 54 | my $h = $me->_build_data; | ||
| 55 | return '' unless defined $h->{limit}; | ||
| 56 | my $sql = 'LIMIT '; | ||
| 57 | $sql .= $h->{limit}[0] >= 0 ? $h->{limit}[0] : 'ALL'; | ||
| 58 | $sql .= ' OFFSET '.$h->{limit}[1] if $h->{limit}[1]; | ||
| 59 | return $sql; | ||
| 60 | } | ||
| 61 | |||
| 62 | # Pg doesn't allow the use of aliases in the WHERE, GROUP BY or HAVING clause | ||
| 63 | sub _alias_preference { | ||
| 64 | my($class, $me, $method) = @_; | ||
| 65 | $method ||= ((caller(2))[3] =~ /\b(\w+)$/); | ||
| 66 | return 0 if $method eq 'join_on' or $method eq 'where' or $method eq 'group_by' or $method eq 'having'; | ||
| 67 | return 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub _bulk_insert { | ||
| 71 | shift->_fast_bulk_insert(@_); | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; |