| 1 | =head1 NAME | ||
| 2 | |||
| 3 | DBIx::SQLEngine::Driver::Trait::PerlDBLib - For use with SQL::Statement | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | # Classes can import this behavior if they're based on SQL::Statement | ||
| 8 | use DBIx::SQLEngine::Driver::Trait::PerlDBLib ':all'; | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | This package works with DBD drivers which are implemented in Perl using SQL::Statement. It combines several other traits and methods which can be shared by most such drivers. | ||
| 13 | |||
| 14 | =head2 About Driver Traits | ||
| 15 | |||
| 16 | You do not need to use this package directly; it is used internally by those driver subclasses which need it. | ||
| 17 | |||
| 18 | For more information about Driver Traits, see L<DBIx::SQLEngine::Driver/"About Driver Traits">. | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | ######################################################################## | ||
| 23 | |||
| 24 | package DBIx::SQLEngine::Driver::Trait::PerlDBLib; | ||
| 25 | |||
| 26 | use strict; | ||
| 27 | use Carp; | ||
| 28 | use vars qw( @EXPORT_OK %EXPORT_TAGS ); | ||
| 29 | |||
| 30 | ######################################################################## | ||
| 31 | |||
| 32 | use DBIx::SQLEngine::Driver::Trait::NoUnions ':all'; | ||
| 33 | use DBIx::SQLEngine::Driver::Trait::NoSequences ':all'; | ||
| 34 | use DBIx::SQLEngine::Driver::Trait::NoColumnTypes ':all'; | ||
| 35 | use DBIx::SQLEngine::Driver::Trait::NoAdvancedFeatures qw( :all ); | ||
| 36 | |||
| 37 | use Exporter; | ||
| 38 | sub import { goto &Exporter::import } | ||
| 39 | @EXPORT_OK = ( | ||
| 40 | qw( | ||
| 41 | fetch_one_value sql_limit | ||
| 42 | detect_any sql_detect_any | ||
| 43 | dbms_create_column_types dbms_create_column_text_long_type | ||
| 44 | sql_create_columns | ||
| 45 | dbms_select_table_as_unsupported | ||
| 46 | ), | ||
| 47 | @DBIx::SQLEngine::Driver::Trait::NoUnions::EXPORT_OK, | ||
| 48 | @DBIx::SQLEngine::Driver::Trait::NoSequences::EXPORT_OK, | ||
| 49 | @DBIx::SQLEngine::Driver::Trait::NoColumnTypes::EXPORT_OK, | ||
| 50 | @DBIx::SQLEngine::Driver::Trait::NoAdvancedFeatures::EXPORT_OK, | ||
| 51 | ); | ||
| 52 | %EXPORT_TAGS = ( all => \@EXPORT_OK ); | ||
| 53 | |||
| 54 | ######################################################################## | ||
| 55 | |||
| 56 | =head1 REFERENCE | ||
| 57 | |||
| 58 | The following methods are provided: | ||
| 59 | |||
| 60 | =cut | ||
| 61 | ######################################################################## | ||
| 62 | |||
| 63 | =head2 fetch_one_value | ||
| 64 | |||
| 65 | Special handling for simple functions. Allows select count(), max(), or min(), but only if that is the only value being returned. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub fetch_one_value { | ||
| 70 | my $self = shift; | ||
| 71 | my %args = @_; | ||
| 72 | if ( my $column_clause = $args{columns} ) { | ||
| 73 | if ( $column_clause =~ /\A\s*count\((.*?)\)\s*\Z/ ) { | ||
| 74 | $args{columns} = $1; | ||
| 75 | my $rows = $self->fetch_select( %args ); | ||
| 76 | return( $rows ? scalar( @$rows ) : 0 ) | ||
| 77 | } elsif ( $column_clause =~ /\A\s*max\((.*?)\)\s*\Z/ ) { | ||
| 78 | $args{columns} = $1; | ||
| 79 | $args{order} = "$1 desc"; | ||
| 80 | } elsif ( $column_clause =~ /\A\s*min\((.*?)\)\s*\Z/ ) { | ||
| 81 | $args{columns} = $1; | ||
| 82 | $args{order} = "$1"; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | $self->NEXT('fetch_one_value', %args ); | ||
| 86 | } | ||
| 87 | |||
| 88 | ######################################################################## | ||
| 89 | |||
| 90 | =head2 sql_limit | ||
| 91 | |||
| 92 | Adds support for SQL select limit clause. | ||
| 93 | |||
| 94 | TODO: Needs workaround to support offset. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub sql_limit { | ||
| 99 | my $self = shift; | ||
| 100 | my ( $limit, $offset, $sql, @params ) = @_; | ||
| 101 | |||
| 102 | # You can't apply "limit" to non-table fetches | ||
| 103 | $sql .= " limit $limit" if ( $sql =~ / from / ); | ||
| 104 | |||
| 105 | return ($sql, @params); | ||
| 106 | } | ||
| 107 | |||
| 108 | ######################################################################## | ||
| 109 | |||
| 110 | =head2 do_insert_with_sequence | ||
| 111 | |||
| 112 | $sqldb->do_insert_with_sequence( $sequence_name, %sql_clauses ) : $row_count | ||
| 113 | |||
| 114 | Implemented using DBIx::SQLEngine::Driver::Trait::NoSequences. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | ######################################################################## | ||
| 119 | |||
| 120 | =head2 detect_any | ||
| 121 | |||
| 122 | $sqldb->detect_any ( ) : $boolean | ||
| 123 | |||
| 124 | Returns 1, as we presume that the requisite driver modules are | ||
| 125 | available or we wouldn't have reached this point. | ||
| 126 | |||
| 127 | =head2 sql_detect_any | ||
| 128 | |||
| 129 | This should not be called. Throws fatal exception. | ||
| 130 | |||
| 131 | =cut | ||
| 132 | |||
| 133 | sub detect_any { | ||
| 134 | return 1; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub sql_detect_any { | ||
| 138 | croak "Unsupported"; | ||
| 139 | } | ||
| 140 | |||
| 141 | ######################################################################## | ||
| 142 | |||
| 143 | =head2 sql_create_columns | ||
| 144 | |||
| 145 | $sqldb->sql_create_columns( $column, $fragment_array_ref ) : $sql_fragment | ||
| 146 | |||
| 147 | Generates the SQL fragment to define a column in a create table statement. | ||
| 148 | |||
| 149 | Overridden to B<not> produce "PRIMARY KEY ( foo )" clauses for the primary key. | ||
| 150 | |||
| 151 | =head2 dbms_create_column_types | ||
| 152 | |||
| 153 | $sqldb->dbms_create_column_types () : %column_type_codes | ||
| 154 | |||
| 155 | Implemented using the standard int and varchar types. | ||
| 156 | |||
| 157 | =head2 dbms_create_column_text_long_type | ||
| 158 | |||
| 159 | $sqldb->dbms_create_column_text_long_type () : $col_type_str | ||
| 160 | |||
| 161 | Implemented using the standard varchar type. | ||
| 162 | |||
| 163 | =cut | ||
| 164 | |||
| 165 | # Filter out primary key clauses in SQL create statements | ||
| 166 | sub sql_create_columns { | ||
| 167 | my($self, $table, $column, $columns) = @_; | ||
| 168 | return if ( $column->{type} eq 'primary' ); | ||
| 169 | $self->NEXT('sql_create_columns', $table, $column, $columns ); | ||
| 170 | } | ||
| 171 | |||
| 172 | sub dbms_create_column_types { | ||
| 173 | 'sequential' => 'int', | ||
| 174 | } | ||
| 175 | |||
| 176 | sub dbms_create_column_text_long_type { | ||
| 177 | 'varchar(16384)' | ||
| 178 | } | ||
| 179 | |||
| 180 | ######################################################################## | ||
| 181 | |||
| 182 | =head2 dbms_select_table_as_unsupported | ||
| 183 | |||
| 184 | $sqldb->dbms_select_table_as_unsupported () : 1 | ||
| 185 | |||
| 186 | Capability Limitation: This driver does not support table aliases such as "select * from foo as bar". | ||
| 187 | |||
| 188 | =head2 dbms_column_types_unsupported | ||
| 189 | |||
| 190 | $sqldb->dbms_column_types_unsupported () : 1 | ||
| 191 | |||
| 192 | Capability Limitation: This driver does not store column type information. | ||
| 193 | |||
| 194 | =head2 dbms_indexes_unsupported | ||
| 195 | |||
| 196 | $sqldb-> dbms_indexes_unsupported () : 1 | ||
| 197 | |||
| 198 | Capability Limitation: This driver does not support indexes. | ||
| 199 | |||
| 200 | =head2 dbms_storedprocs_unsupported | ||
| 201 | |||
| 202 | $sqldb-> dbms_storedprocs_unsupported () : 1 | ||
| 203 | |||
| 204 | Capability Limitation: This driver does not support stored procedures. | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | sub dbms_select_table_as_unsupported { 1 } | ||
| 209 | |||
| 210 | ######################################################################## | ||
| 211 | |||
| 212 | =head1 SEE ALSO | ||
| 213 | |||
| 214 | See L<DBIx::SQLEngine> for the overall interface and developer documentation. | ||
| 215 | |||
| 216 | See L<DBIx::SQLEngine::Docs::ReadMe> for general information about | ||
| 217 | this distribution, including installation and license information. | ||
| 218 | |||
| 219 | See L<DBIx::Sequence> for another version of the sequence-table functionality, which greatly inspired this module. | ||
| 220 | |||
| 221 | =cut | ||
| 222 | |||
| 223 | ######################################################################## | ||
| 224 | |||
| 225 | 1; | ||
| 226 |