| 1 | |||
| 2 | package DBIx::Romani::PreparedStatement; | ||
| 3 | |||
| 4 | use DBIx::Romani::ResultSet; | ||
| 5 | use DBI qw(:sql_types); | ||
| 6 | use strict; | ||
| 7 | |||
| 8 | sub new | ||
| 9 | { | ||
| 10 | my $class = shift; | ||
| 11 | my $args = shift; | ||
| 12 | |||
| 13 | my $conn; | ||
| 14 | my $sql; | ||
| 15 | |||
| 16 | if ( ref($args) eq 'HASH' ) | ||
| 17 | { | ||
| 18 | $conn = $args->{conn}; | ||
| 19 | $sql = $args->{sql}; | ||
| 20 | } | ||
| 21 | else | ||
| 22 | { | ||
| 23 | $conn = $args; | ||
| 24 | $sql = shift; | ||
| 25 | } | ||
| 26 | |||
| 27 | my $sth = $conn->get_dbh()->prepare( $sql ); | ||
| 28 | |||
| 29 | my $self = { | ||
| 30 | conn => $conn, | ||
| 31 | sql => $sql, | ||
| 32 | sth => $sth, | ||
| 33 | }; | ||
| 34 | |||
| 35 | bless $self, $class; | ||
| 36 | return $self; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub get_conn { return shift->{conn}; } | ||
| 40 | sub get_sth { return shift->{sth}; } | ||
| 41 | |||
| 42 | sub execute_query | ||
| 43 | { | ||
| 44 | my $self = shift; | ||
| 45 | |||
| 46 | my $params; | ||
| 47 | my $fetchmode; | ||
| 48 | |||
| 49 | my $args = $_[0]; | ||
| 50 | if ( ref($args) eq 'HASH' ) | ||
| 51 | { | ||
| 52 | $fetchmode = $args->{fetchmode}; | ||
| 53 | $params = $args->{params}; | ||
| 54 | } | ||
| 55 | else | ||
| 56 | { | ||
| 57 | $params = \@_; | ||
| 58 | } | ||
| 59 | |||
| 60 | $self->{sth}->execute( @$params ); | ||
| 61 | |||
| 62 | return $self->{conn}->get_driver()->create_result_set( $self->{conn}, $self->{sth}, $fetchmode ); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub execute_update | ||
| 66 | { | ||
| 67 | my $self = shift; | ||
| 68 | return $self->{sth}->execute( @_ ); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub set | ||
| 72 | { | ||
| 73 | my ($self, $index, $value) = @_; | ||
| 74 | $self->{sth}->bind_param( $index, $value ); | ||
| 75 | } | ||
| 76 | |||
| 77 | # | ||
| 78 | # TODO: Implement these: | ||
| 79 | # | ||
| 80 | # * sub set_clob | ||
| 81 | # * sub set_blob | ||
| 82 | # | ||
| 83 | |||
| 84 | sub set_date | ||
| 85 | { | ||
| 86 | my ($self, $index, $value) = @_; | ||
| 87 | |||
| 88 | # TODO: convert date into ODBC format! | ||
| 89 | |||
| 90 | $self->{sth}->bind_param( $index, $value, SQL_DATE ); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub set_time | ||
| 94 | { | ||
| 95 | my ($self, $index, $value) = @_; | ||
| 96 | |||
| 97 | # TODO: convert date into ODBC format! | ||
| 98 | |||
| 99 | $self->{sth}->bind_param( $index, $value, SQL_TIME ); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub set_datetime | ||
| 103 | { | ||
| 104 | my ($self, $index, $value) = @_; | ||
| 105 | |||
| 106 | # TODO: convert date into ODBC format! | ||
| 107 | |||
| 108 | $self->{sth}->bind_param( $index, $value, SQL_DATETIME ); | ||
| 109 | } | ||
| 110 | |||
| 111 | 1; | ||
| 112 |