| 1 | package DBIx::Simplish; | ||
| 2 | |||
| 3 | # ABSTRACT: L<DBIx::Simple> + L<DBIx::Connector> + extras. | ||
| 4 | |||
| 5 | use Moo; | ||
| 6 | use MooX::LazyRequire; | ||
| 7 | use Types::Standard qw/Str HashRef InstanceOf Int Bool/; | ||
| 8 | use Type::Tiny::Enum; | ||
| 9 | use DBIx::Connector; | ||
| 10 | use SQL::Abstract::Limit; | ||
| 11 | use SQL::Interp qw/sql_interp/; | ||
| 12 | use Tie::Cache::LRU; | ||
| 13 | use Carp qw/croak/; | ||
| 14 | use DBIx::Simplish::Result; | ||
| 15 | use Regexp::Common; | ||
| 16 | |||
| 17 | our $VERSION = '1.000_001'; # TRIAL VERSION | ||
| 18 | |||
| 19 | my $connection_mode = Type::Tiny::Enum->new(values => [qw/ping fixup no_ping/]); | ||
| 20 | |||
| 21 | has dsn => ( | ||
| 22 | is => 'ro', | ||
| 23 | isa => Str, | ||
| 24 | lazy_required => 1, | ||
| 25 | ); | ||
| 26 | |||
| 27 | has [qw/user password/] => ( | ||
| 28 | is => 'ro', | ||
| 29 | isa => Str, | ||
| 30 | ); | ||
| 31 | |||
| 32 | has options => ( | ||
| 33 | is => 'ro', | ||
| 34 | isa => HashRef, | ||
| 35 | default => sub {{}}, | ||
| 36 | ); | ||
| 37 | |||
| 38 | has connector => ( | ||
| 39 | is => 'lazy', | ||
| 40 | isa => InstanceOf['DBIx::Connector'], | ||
| 41 | predicate => 1, | ||
| 42 | handles => { | ||
| 43 | dbh => 'dbh', | ||
| 44 | disconnect => 'disconnect', | ||
| 45 | _run => 'run', | ||
| 46 | }, | ||
| 47 | ); | ||
| 48 | |||
| 49 | has keep_statements => ( | ||
| 50 | is => 'ro', | ||
| 51 | isa => Int, | ||
| 52 | default => 16, | ||
| 53 | ); | ||
| 54 | |||
| 55 | has lc_columns => ( | ||
| 56 | is => 'rw', | ||
| 57 | isa => Bool, | ||
| 58 | default => 1, | ||
| 59 | ); | ||
| 60 | |||
| 61 | has abstract => ( | ||
| 62 | is => 'lazy', | ||
| 63 | isa => InstanceOf['SQL::Abstract'], | ||
| 64 | handles => [qw/select insert update delete/], | ||
| 65 | ); | ||
| 66 | |||
| 67 | has connection_mode => ( | ||
| 68 | is => 'ro', | ||
| 69 | isa => $connection_mode, | ||
| 70 | default => sub {'fixup'}, | ||
| 71 | ); | ||
| 72 | |||
| 73 | has _cache => ( | ||
| 74 | is => 'lazy', | ||
| 75 | init_arg => undef, | ||
| 76 | ); | ||
| 77 | |||
| 78 | has _is_sqlite => ( | ||
| 79 | is => 'lazy', | ||
| 80 | init_arg => undef, | ||
| 81 | ); | ||
| 82 | |||
| 83 | has _is_mysql => ( | ||
| 84 | is => 'lazy', | ||
| 85 | init_arg => undef, | ||
| 86 | ); | ||
| 87 | |||
| 88 | has sql_quote_char => ( | ||
| 89 | is => 'ro', | ||
| 90 | predicate => 1, | ||
| 91 | ); | ||
| 92 | |||
| 93 | has sql_name_sep => ( | ||
| 94 | is => 'ro', | ||
| 95 | predicate => 1, | ||
| 96 | ); | ||
| 97 | |||
| 98 | sub _build_connector { | ||
| 99 | my $self = shift; | ||
| 100 | my $options = $self->options; | ||
| 101 | $options->{PrintError} = 0 unless exists $options->{PrintError}; | ||
| 102 | $options->{RaiseError} = 1 unless exists $options->{RaiseError}; | ||
| 103 | if ($self->_is_mysql) { | ||
| 104 | $options->{mysql_enable_utf8} = 1 unless exists $options->{mysql_enable_utf8}; | ||
| 105 | $options->{mysql_enable_utf8mb4} = 1 unless exists $options->{mysql_enable_utf8mb4}; | ||
| 106 | } elsif ($self->_is_sqlite) { | ||
| 107 | $options->{sqlite_use_immediate_transaction} = 1 unless exists $options->{sqlite_use_immediate_transaction}; | ||
| 108 | $options->{sqlite_unicode} = 1 unless exists $options->{sqlite_unicode}; | ||
| 109 | } | ||
| 110 | my $connector = DBIx::Connector->new( | ||
| 111 | $self->dsn, | ||
| 112 | $self->user, | ||
| 113 | $self->password, | ||
| 114 | $options, | ||
| 115 | ); | ||
| 116 | $connector->mode($self->connection_mode); | ||
| 117 | return $connector; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _build_abstract { | ||
| 121 | my $self = shift; | ||
| 122 | my %args = (limit_dialect => $self->dbh); | ||
| 123 | $args{quote_char} = $self->sql_quote_char if $self->has_sql_quote_char; | ||
| 124 | $args{name_sep} = $self->sql_name_sep if $self->has_sql_name_sep; | ||
| 125 | if ($self->_is_mysql) { | ||
| 126 | $args{quote_char} = '`' unless $self->has_sql_quote_char; | ||
| 127 | $args{name_sep} = '.' unless $self->has_sql_name_sep; | ||
| 128 | } elsif ($self->_is_sqlite) { | ||
| 129 | $args{quote_char} = '"' unless $self->has_sql_quote_char; | ||
| 130 | $args{name_sep} = '.' unless $self->has_sql_name_sep; | ||
| 131 | } | ||
| 132 | return SQL::Abstract::Limit->new(%args); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub _build__cache { | ||
| 136 | my $self = shift; | ||
| 137 | my $cache; | ||
| 138 | tie %{$cache}, 'Tie::Cache::LRU', $self->keep_statements; ## no critic (ProhibitTies) | ||
| 139 | return $cache | ||
| 140 | } | ||
| 141 | |||
| 142 | sub _build__is_mysql { | ||
| 143 | my $self = shift; | ||
| 144 | if ($self->has_connector) { | ||
| 145 | return $self->dbh->{Driver}{Name} eq 'mysql'; | ||
| 146 | } else { | ||
| 147 | return $self->dsn =~ /^(?i:dbi):mysql:/; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | sub _build__is_sqlite { | ||
| 152 | my $self = shift; | ||
| 153 | if ($self->has_connector) { | ||
| 154 | return $self->dbh->{Driver}{Name} eq 'SQLite'; | ||
| 155 | } else { | ||
| 156 | return $self->dsn =~ /^(?i:dbi):SQLite:/; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | sub DEMOLISH { | ||
| 161 | my $self = shift; | ||
| 162 | $_->finish for values %{$self->_cache}; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub connect { ## no critic (ProhibitBuiltinHomonyms) | ||
| 166 | my ($self, $dsn, $user, $password, $options) = @_; | ||
| 167 | my %opts = (dsn => $dsn); | ||
| 168 | $opts{user} = $user if defined $user; | ||
| 169 | $opts{password} = $password if defined $password; | ||
| 170 | $opts{options} = $options if defined $options; | ||
| 171 | return $self->new(%opts); | ||
| 172 | } | ||
| 173 | |||
| 174 | my $omniholder_re = qr/[(][?][?][)]/; | ||
| 175 | |||
| 176 | sub _omniholder { | ||
| 177 | my ($self, $query, @binds) = @_; | ||
| 178 | my ($ordinary_binds, $omniholder_binds) = (0, 0); | ||
| 179 | (my $dummy = $query) =~ s/($RE{quoted}|$omniholder_re|[?])/ | ||
| 180 | if ($1 eq '(??)') { | ||
| 181 | $omniholder_binds++; | ||
| 182 | } elsif ($1 eq '?') { | ||
| 183 | $ordinary_binds++; | ||
| 184 | } | ||
| 185 | /ge; | ||
| 186 | return $query if $omniholder_binds == 0; | ||
| 187 | croak('There can be only one omniholder') if $omniholder_binds > 1; | ||
| 188 | croak('Not enough binds') if (@binds - $ordinary_binds) < 1; | ||
| 189 | my $replacement = '(' . join(',', ('?') x (@binds - $ordinary_binds)) . ')'; | ||
| 190 | $query =~ s/($RE{quoted}|$omniholder_re)/ | ||
| 191 | if ($1 eq '(??)') { | ||
| 192 | $replacement; | ||
| 193 | } else { | ||
| 194 | $1; | ||
| 195 | } | ||
| 196 | /ge; | ||
| 197 | return $query; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub query { | ||
| 201 | my ($self, $query, @binds) = @_; | ||
| 202 | $query = $self->_omniholder($query, @binds) if $query =~ /$omniholder_re/; | ||
| 203 | my $s = $self->_run(sub { | ||
| 204 | my $dbh = shift; | ||
| 205 | $dbh->{CachedKids} = $self->_cache; | ||
| 206 | my $sth = $dbh->prepare_cached($query, undef, 3); ## no critic (ProhibitMagicNumbers) | ||
| 207 | $sth->execute(@binds); | ||
| 208 | return $sth; | ||
| 209 | }); | ||
| 210 | return DBIx::Simplish::Result->new(sth => $s, lc_columns => $self->lc_columns); | ||
| 211 | } | ||
| 212 | |||
| 213 | before disconnect => sub { | ||
| 214 | goto &DEMOLISH; | ||
| 215 | }; | ||
| 216 | |||
| 217 | around [qw/select insert update delete iquery/] => sub { | ||
| 218 | my ($method, $self, @args) = @_; | ||
| 219 | my ($query, @binds) = $self->$method(@args); | ||
| 220 | return $self->query($query, @binds); | ||
| 221 | }; | ||
| 222 | |||
| 223 | sub iquery { | ||
| 224 | my ($self, @args) = @_; | ||
| 225 | sql_interp(@args); | ||
| 226 | } | ||
| 227 | |||
| 228 | sub func { | ||
| 229 | my ($self, @args) = @_; | ||
| 230 | return $self->_run(sub {$_->func(@args)}); | ||
| 231 | } | ||
| 232 | |||
| 233 | sub last_insert_id { | ||
| 234 | my ($self, @args) = @_; | ||
| 235 | if ($self->_is_mysql) { | ||
| 236 | return $self->_run(sub {$_->{mysql_insertid}}); | ||
| 237 | } elsif ($self->_is_sqlite) { | ||
| 238 | return $self->func('last_insert_rowid'); | ||
| 239 | } else { | ||
| 240 | return $self->_run(sub {$_->last_insert_id(@args)}); | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | sub begin_work { | ||
| 245 | my $self = shift; | ||
| 246 | return $self->_run(sub {$_->begin_work}); | ||
| 247 | } | ||
| 248 | |||
| 249 | sub begin { | ||
| 250 | goto &begin_work; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub commit { | ||
| 254 | my $self = shift; | ||
| 255 | return $self->_run(sub {$_->commit}); | ||
| 256 | } | ||
| 257 | |||
| 258 | sub rollback { | ||
| 259 | my $self = shift; | ||
| 260 | return $self->_run(sub {$_->rollback}); | ||
| 261 | } | ||
| 262 | |||
| 263 | sub error { | ||
| 264 | my $self = shift; | ||
| 265 | return $self->_run(sub {$_->errstr}); | ||
| 266 | } | ||
| 267 | |||
| 268 | 1; | ||
| 269 | |||
| 270 | __END__ |