Giter Club home page Giter Club logo

dbix-sunny's Introduction

Build Status MetaCPAN Release

NAME

DBIx::Sunny - Simple DBI wrapper

SYNOPSIS

use DBIx::Sunny;

my $dbh = DBIx::Sunny->connect(...);

# or

use DBI;

my $dbh = DBI->connect(.., {
    RootClass => 'DBIx::Sunny',
    PrintError => 0,
    RaiseError => 1,
});

DESCRIPTION

DBIx::Sunny is a simple DBI wrapper. It provides better usability for you. This module based on Amon2::DBI. DBIx::Sunny supports only SQLite and MySQL.

FEATURES

  • Set AutoInactiveDestroy to true.

    DBIx::Sunny sets AutoInactiveDestroy as true.

  • [SQLite/MySQL/Pg] Auto encode/decode UTF-8

    DBIx::Sunny sets sqlite_unicode, mysql_enable_utf8 and pg_enable_utf8 automatically.

  • [SQLite] Performance tuning

    DBIx::Sunny sets sqlite_use_immediate_transaction to true, and executes these PRAGMA statements

      PRAGMA journal_mode = WAL
      PRAGMA synchronous = NORMAL
    
  • Nested transaction management.

    DBIx::Sunny supports nested transaction management based on RAII like DBIx::Class or DBIx::Skinny. It uses DBIx::TransactionManager internally.

  • Error Handling

    DBIx::Sunny sets RaiseError and ShowErrorStatement as true. DBIx::Sunny raises exception and shows current statement if your $dbh occurred exception.

  • SQL comment

    DBIx::Sunny adds file name and line number as SQL comment that invokes SQL statement.

  • Easy access to last_insert_id

    DBIx::Sunny's last_insert_id needs no arguments. It's shortcut for mysql_insertid or last_insert_rowid.

  • Auto expanding arrayref bind parameters

    select_(one|row|all) and query methods support auto-expanding arrayref bind parameters.

      $dbh->select_all('SELECT * FROM id IN (?)', [1 2 3])
      #SQL: 'SELECT * FROM id IN (?,?,?)'
      #@BIND: (1, 2, 3)
    
  • Named placeholder

    select_(one|row|all) and query methods support named placeholder.

      $dbh->select_all('SELECT * FROM users WHERE id IN (:ids) AND status = :status', {
          ids    => [1,2,3],
          status => 'active',
      });
      #SQL: 'SELECT * FROM users WHERE id IN (?,?,?) AND status = ?'
      #@BIND: (1, 2, 3, 'active')
    
  • Typed bind parameters

    DBIx::Sunny allows you to specify data types of bind parameters. If a bind parameter is SQL::Maker::SQLType object, its value is passed as its type, otherwise it is passed as default type (VARCHAR).

      use SQL::Maker::SQLType qw/sql_type/;
      use DBI qw/:sql_types/
    
      $dbh->query(
          'INSERT INTO bin_table (bin_col) VALUES (?)',
          sql_type(\"\xDE\xAD\xBE\xEF", SQL_BINARY)),
      );
    

ADDITIONAL METHODS

  • $col = $dbh->select_one($query, @bind);

    Shortcut for prepare, execute and fetchrow_arrayref->[0]

  • $row = $dbh->select_row($query, @bind);

    Shortcut for prepare, execute and fetchrow_hashref

  • $rows = $dbh->select_all($query, @bind);

    Shortcut for prepare, execute and selectall_arrayref(.., { Slice => {} }, ..)

  • $model = $dbh->select_row_as($model_class, $query, @bind);

    Shortcut for $model_class->new(%{ $dbh->select_row($query, @bind) });

  • $models = $dbh->select_all_as($model_class, $query, @bind);

    Shortcut for [ map { $model_class->new(%$_) } @{ $dbh->select_all($query, @bind) } ];

  • $dbh->query($query, @bind);

    Shortcut for prepare, execute.

AUTHOR

Masahiro Nagano <kazeburo KZBRKZBR@ gmail.com>

SEE ALSO

DBI, Amon2::DBI

LICENSE

Copyright (C) Masahiro Nagano

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

dbix-sunny's People

Contributors

egawata avatar fujiwara avatar kazeburo avatar kfly8 avatar motemen avatar nanto avatar songmu avatar tokuhirom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

dbix-sunny's Issues

Fix database support in docs

The Description section says that only SQLite and MySQL are supported but the Features section mentions Postgres (Pg).
Might be a good time to add info if MariaDB is supported or not.
Thanks!

Support SQL::Maker::SQLType

This is a feature request.

Since DBD::mysql 4.042 distinguish between binary data and strings (cf. perl5-dbi/DBD-mysql#107), we have to use $sth->bind_param($p_num, $bind_value, $bind_type) to specify binary data under mysql_enable_utf8.

Teng can receive SQL::Maker::SQLType objects and call bind_param with type for one. It's helpful if DBIx::Sunny can handle SQL::Maker::SQLType objects too.

could not find postmaster

t/09_pg.t may fail, probably if Test::PostgreSQL is installed but postgres itself is not:

could not find postmaster, please set appropriate PATH or POSTGRES_HOME at /opt/perl-5.24.4t/lib/site_perl/5.24.4/Test/PostgreSQL.pm line 241.
t/09_pg.t .............. 
Dubious, test returned 2 (wstat 512, 0x200)
No subtests run 

Test failures (with DBI >= 1.635)

My smokers started to generate test fails for DBIx-Sunny-0.22. The fails look like this:

...
#   Failed test at t/01_basic.t line 41.
#                   'DBD::SQLite::db prepare failed: table bar has no column named e at /tmpfs/.cpan-build-cpansand/2016042421/DBIx-Sunny-0.22-SJrDGu/blib/lib/DBIx/Sunny.pm line 94.
# '
#     doesn't match '(?^:/\* .+ line \d+ \*/)'

#   Failed test at t/01_basic.t line 49.
#                   'DBD::SQLite::db selectall_arrayref failed: no such column: e at t/01_basic.t line 46.
# '
#     doesn't match '(?^:for Statement)'
...

Statistical analysis of fail reports on my machine suggest that the problem is caused by DBI 1.635 and later (@timbunce: FYI).

****************************************************************
Regression 'mod:DBI'
****************************************************************
Name                   Theta          StdErr     T-stat
[0='const']           1.0000          0.0000    157247560194740480.00
[1='eq_1.632']        0.0000          0.0000       3.00
[2='eq_1.633']        0.0000          0.0000       5.89
[3='eq_1.634']        0.0000          0.0000       0.00
[4='eq_1.635']       -1.0000          0.0000    -53935468181018976.00
[5='eq_1.636']       -1.0000          0.0000    -72150128842863440.00

R^2= 1.000, N= 113, K= 6
****************************************************************

Customizable ignoring rules for comment setting.

DBIx::Sunny's comment setting system puts common internal classes as a comment.
For example, DBIx::Class uses Try::Tiny in query generation. As a result, when a user uses DBIx::Sunny with DBIx::Class, the user will use queries including /* local/lib/perl5/Try/Tiny.pm line 3 */.

In this line, DBIx::Sunny skips DBIx::* and DBD::* packages. In addition, DBIx::Sunny should support customizable package ignoring.
https://github.com/kazeburo/DBIx-Sunny/blob/master/lib/DBIx/Sunny.pm#L84

Carp.pm has a similar function. I suggest that you imitate the name from there and define a package variable named our %INTERNAL.

value too long for type ...

On some of my smokers the t/09_pg.t test fails:

DBD::Pg::st execute failed: ERROR:  value too long for type character varying(10) [for Statement "INSERT /* t/09_pg.t line 37 */ INTO bar (x) VALUES (?)" with ParamValues: 1='ã..ã..ã.«ã.¡ã.¯'] at /home/cpansand/.cpan/build/2018071910/DBIx-Sunny-0.9991-2/blib/lib/DBIx/Sunny/Util.pm line 23.
# Tests were run but no plan was declared and done_testing() was not seen.
# Looks like your test exited with 255 just after 8.
t/09_pg.t .............. 
Dubious, test returned 255 (wstat 65280, 0xff00)
All 8 subtests passed 

The above happened on a freebsd 11 system with postgres 9.5.13 installed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.