| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||
| 3 | # File: GX/SQL/Builder/Pg.pm | ||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 5 | |||
| 6 | package GX::SQL::Builder::Pg; | ||
| 7 | |||
| 8 | use GX::Exception; | ||
| 9 | |||
| 10 | use DBD::Pg (); | ||
| 11 | |||
| 12 | |||
| 13 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 14 | # Class setup | ||
| 15 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 16 | |||
| 17 | use GX::Class ( extends => 'GX::SQL::Builder' ); | ||
| 18 | |||
| 19 | build; | ||
| 20 | |||
| 21 | |||
| 22 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 23 | # Private methods | ||
| 24 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 25 | |||
| 26 | sub _build_limit_clause { | ||
| 27 | |||
| 28 | my ( $self, $limit, $offset, $sql, @bind ) = @_; | ||
| 29 | |||
| 30 | if ( defined $limit ) { | ||
| 31 | $limit =~ /^\d+$/ or throw "Invalid argument (\"limit\" must be a number)"; | ||
| 32 | } | ||
| 33 | else { | ||
| 34 | return; | ||
| 35 | } | ||
| 36 | |||
| 37 | if ( defined $offset ) { | ||
| 38 | $offset =~ /^\d+$/ or throw "Invalid argument (\"offset\" must be a number)"; | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | $offset = 0; | ||
| 42 | } | ||
| 43 | |||
| 44 | # Postgres limit syntax: | ||
| 45 | # "ORDER BY $order LIMIT $limit" | ||
| 46 | # "ORDER BY $order LIMIT $limit OFFSET $offset" | ||
| 47 | |||
| 48 | $$sql .= ' LIMIT ' . $limit; | ||
| 49 | $$sql .= ' OFFSET ' . $offset if $offset; | ||
| 50 | |||
| 51 | return; | ||
| 52 | |||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 57 | # Attribute initializers | ||
| 58 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 59 | |||
| 60 | sub _initialize_bind_type_map { | ||
| 61 | |||
| 62 | return { | ||
| 63 | |||
| 64 | %{ $_[0]->SUPER::_initialize_bind_type_map }, | ||
| 65 | |||
| 66 | GX::SQL::Types::BINARY => { pg_type => DBD::Pg::PG_BYTEA } | ||
| 67 | |||
| 68 | }; | ||
| 69 | |||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | 1; | ||
| 74 | |||
| 75 | __END__ |