1 package DBIx::Class::Storage::DBI::Sybase;
7 use base qw/DBIx::Class::Storage::DBI/;
9 use Carp::Clan qw/^DBIx::Class/;
13 DBIx::Class::Storage::DBI::Sybase - Storage::DBI subclass for Sybase
17 This subclass supports L<DBD::Sybase> for real Sybase databases. If you are
18 using an MSSQL database via L<DBD::Sybase>, your storage will be reblessed to
19 L<DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server>.
23 If your version of Sybase does not support placeholders, then your storage
24 will be reblessed to L<DBIx::Class::Storage::DBI::Sybase::NoBindVars>. You can
25 also enable that driver explicitly, see the documentation for more details.
27 With this driver there is unfortunately no way to get the C<last_insert_id>
28 without doing a C<select max(col)>.
30 But your queries will be cached.
32 A recommended L<DBIx::Class::Storage::DBI/connect_info> settings:
34 on_connect_call => [['datetime_setup'], [blob_setup => log_on_update => 0]]
40 __PACKAGE__->mk_group_accessors('simple' =>
41 qw/_blob_log_on_update/
47 if (ref($self) eq 'DBIx::Class::Storage::DBI::Sybase') {
49 @{$self->dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2]
54 my $subclass = "DBIx::Class::Storage::DBI::Sybase::${dbtype}";
56 if (!$exception && $dbtype && $self->load_optional_class($subclass)) {
57 bless $self, $subclass;
61 if (not $self->dbh->{syb_dynamic_supported}) {
62 bless $self, 'DBIx::Class::Storage:DBI::Sybase::NoBindVars';
65 $self->connect_call_datetime_setup;
66 $self->connect_call_blob_setup;
73 $self->next::method(@_);
74 $self->connect_call_datetime_setup;
75 $self->connect_call_blob_setup;
79 =head2 connect_call_blob_setup
83 on_connect_call => [ [ blob_setup => log_on_update => 0 ] ]
85 Does C<< $dbh->{syb_binary_images} = 1; >> to return C<IMAGE> data as raw binary
86 instead of as a hex string.
90 Also sets the C<log_on_update> value for blob write operations. The default is
91 C<1>, but C<0> is better if your database is configured for it.
94 L<DBD::Sybase/Handling_IMAGE/TEXT_data_with_syb_ct_get_data()/syb_ct_send_data()>.
98 sub connect_call_blob_setup {
101 my $dbh = $self->_dbh;
102 $dbh->{syb_binary_images} = 1;
104 $self->_blob_log_on_update($args{log_on_update})
105 if exists $args{log_on_update};
110 shift =~ /(?:text|image|lob|bytea|binary)/i;
115 my ($source, $to_insert) = @_;
119 for my $col (keys %$to_insert) {
120 $blob_cols{$col} = delete $to_insert->{$col}
121 if $self->_is_lob_type($source->column_info($col)->{data_type});
124 my $updated_cols = $self->next::method(@_);
126 $self->_update_blobs($source, \%blob_cols, $to_insert) if %blob_cols;
128 return $updated_cols;
132 my ($self, $source, $blob_cols, $inserted) = @_;
133 my $dbh = $self->dbh;
135 my $table = $source->from;
137 my (@primary_cols) = $source->primary_columns;
139 croak "Cannot update TEXT/IMAGE without a primary key!"
140 unless @primary_cols;
142 my $search_cond = join ',' => map "$_ = ?", @primary_cols;
144 for my $col (keys %$blob_cols) {
145 my $blob = $blob_cols->{$col};
147 # First update to empty string in case it's NULL, can't update a NULL blob using
149 my $sth = $dbh->prepare_cached(
150 qq{update $table set $col = '' where $search_cond}
152 $sth->execute(map $inserted->{$_}, @primary_cols) or die $sth->errstr;
155 $sth = $dbh->prepare_cached(
156 "select $col from $table where $search_cond"
158 $sth->execute(map $inserted->{$_}, @primary_cols);
161 while ($sth->fetch) {
162 $sth->func('CS_GET', 1, 'ct_data_info') or die $sth->errstr;
164 $sth->func('ct_prepare_send') or die $sth->errstr;
166 my $log_on_update = $self->_blob_log_on_update;
167 $log_on_update = 1 if not defined $log_on_update;
169 $sth->func('CS_SET', 1, {
170 total_txtlen => length($blob),
171 log_on_update => $log_on_update
172 }, 'ct_data_info') or die $sth->errstr;
174 $sth->func($blob, length($blob), 'ct_send_data') or die $sth->errstr;
176 $sth->func('ct_finish_send') or die $sth->errstr;
180 croak $exception if $exception;
184 =head2 connect_call_datetime_setup
188 on_connect_call => 'datetime_setup'
190 In L<DBIx::Class::Storage::DBI/connect_info> to set:
192 $dbh->syb_date_fmt('ISO_strict'); # output fmt: 2004-08-21T14:36:48.080Z
193 $dbh->do('set dateformat mdy'); # input fmt: 08/13/1979 18:08:55.080
195 On connection for use with L<DBIx::Class::InflateColumn::DateTime>, using
196 L<DateTime::Format::Sybase>, which you will need to install.
198 This works for both C<DATETIME> and C<SMALLDATETIME> columns, although
199 C<SMALLDATETIME> columns only have minute precision.
204 my $old_dbd_warned = 0;
206 sub connect_call_datetime_setup {
208 my $dbh = $self->_dbh;
210 if ($dbh->can('syb_date_fmt')) {
211 $dbh->syb_date_fmt('ISO_strict');
212 } elsif (not $old_dbd_warned) {
213 carp "Your DBD::Sybase is too old to support ".
214 "DBIx::Class::InflateColumn::DateTime, please upgrade!";
218 $dbh->do('set dateformat mdy');
224 sub datetime_parser_type { "DateTime::Format::Sybase" }
226 sub _dbh_last_insert_id {
227 my ($self, $dbh, $source, $col) = @_;
229 # sorry, there's no other way!
230 my $sth = $dbh->prepare_cached("select max($col) from ".$source->from);
231 return ($dbh->selectrow_array($sth))[0];
234 # previous implementation of limited count for Sybase, does not include
237 #sub _copy_attributes_for_count {
238 # my ($self, $source, $attrs) = @_;
239 # my %attrs = %$attrs;
241 # # take off any column specs, any pagers, record_filter is cdbi, and no point of ordering a count
242 # delete @attrs{qw/select as rows offset page order_by record_filter/};
249 #Counts for limited queries are emulated by executing select queries and
250 #returning the number of successful executions minus the offset.
252 #This is necessary due to the limitations of Sybase.
258 # my ($source, $attrs) = @_;
260 # my $new_attrs = $self->_copy_attributes_for_count($source, $attrs);
262 # if (exists $attrs->{rows}) {
263 # my $offset = $attrs->{offset} || 0;
264 # my $total = $attrs->{rows} + $offset;
266 # my $first_pk = ($source->primary_columns)[0];
268 # $new_attrs->{select} = $first_pk ? "me.$first_pk" : 1;
270 # my $tmp_rs = $source->resultset_class->new($source, $new_attrs);
272 # $self->dbh->{syb_rowcount} = $total;
275 # $count++ while $tmp_rs->cursor->next;
277 # $self->dbh->{syb_rowcount} = 0;
279 # return $count - $offset;
281 # # overwrite the selector
282 # $new_attrs->{select} = { count => '*' };
284 # my $tmp_rs = $source->resultset_class->new($source, $new_attrs);
285 # my ($count) = $tmp_rs->cursor->next;
287 # # if the offset/rows attributes are still present, we did not use
288 # # a subquery, so we need to make the calculations in software
289 # $count -= $attrs->{offset} if $attrs->{offset};
290 # $count = $attrs->{rows} if $attrs->{rows} and $attrs->{rows} < $count;
291 # $count = 0 if ($count < 0);
301 See L</connect_call_datetime_setup> to setup date formats
302 for L<DBIx::Class::InflateColumn::DateTime>.
304 =head1 IMAGE AND TEXT COLUMNS
306 See L</connect_call_blob_setup> for a L<DBIx::Class::Storage::DBI/connect_info>
307 setting you need to work with C<IMAGE> columns.
309 Due to limitations in L<DBD::Sybase> and this driver, it is only possible to
310 select one C<TEXT> or C<IMAGE> column at a time.
314 See L<DBIx::Class/CONTRIBUTORS>.
318 You may distribute this code under the same terms as Perl itself.