Giter VIP home page Giter VIP logo

sql-abstract-prefetch's Introduction

NAME

SQL::Abstract::Prefetch - implement "prefetch" for DBI RDBMS

PROJECT STATUS

OS Build status
Linux Build Status

CPAN version Coverage Status

SYNOPSIS

my $queryspec = {
  table => 'blog',
  fields => [
    'html',
    'id',
    'is_published',
    'markdown',
    'slug',
    'title',
    'user_id',
  ],
  keys => [ 'id' ],
  multi => {
    comments => {
      table => 'comment',
      fields => [ 'blog_id', 'html', 'id', 'markdown', 'user_id' ],
      keys => [ 'id' ],
    },
  },
  single => {
    user => {
      table => 'user',
      fields => [ 'access', 'age', 'email', 'id', 'password', 'username' ],
      keys => [ 'id' ],
    },
  },
};
my $abstract = SQL::Abstract::Pg->new( name_sep => '.', quote_char => '"' );
my $dbh = DBI->connect( "dbi:SQLite:dbname=filename.db", '', '' );
my $prefetch = SQL::Abstract::Prefetch->new(
  abstract => $abstract,
  dbhgetter => sub { $dbh },
  dbcatalog => undef, # for SQLite
  dbschema => undef,
  filter_table => sub { $_[0] !~ /^sqlite_/ },
);
my ( $sql, @bind ) = $prefetch->select_from_queryspec(
  $queryspec,
  { id => $items{blog}[0]{id} },
);
my ( $extractspec ) = $prefetch->extractspec_from_queryspec( $queryspec );
my $sth = $dbh->prepare( $sql );
$sth->execute( @bind );
my ( $got ) = $prefetch->extract_from_query( $extractspec, $sth );

DESCRIPTION

This class implements "prefetch" in the style of DBIx::Class. Stages of operation:

  • Generate a "query spec" that describes what you want back from the database - which fields from which tables, and what relations to join.
  • Generate SQL (and bind parameters) from that "query spec".
  • Pass the SQL and parameters to a DBI $dbh to prepare and execute.
  • Pass the $sth when ready (this allows for asynchronous operation) to the extractor method to turn the returned rows into the hash-refs represented, including array-ref values for any "has many" relationships.

ATTRIBUTES

abstract

Currently, must be a SQL::Abstract::Pg object.

dbhgetter

A code-ref that returns a DBI $dbh.

dbcatalog

The DBI "catalog" argument for e.g. "column_info" in DBI.

dbschema

The DBI "schema" argument for e.g. "column_info" in DBI.

filter_table

Coderef called with a table name, returns a boolean of true to keep, false to discard - typically for a system table.

multi_namer

Coderef called with a table name, returns a suitable name for the relation to that table. Defaults to "to_PL" in Lingua::EN::Inflect::Number.

dbspec

By default, will be calculated from the supplied $dbh, using the supplied dbhgetter, dbcatalog, dbschema, filter_table, and multi_namer. May however be supplied, in which case those other attributes are not needed.

A "database spec"; a hash-ref mapping tables to maps of the relation-name (a string) to a further hash-ref with keys:

  • type

    either single or multi

  • fromkey

    the column name in the "from" table

  • fromtable

    the name of the "from" table

  • tokey

    the column name in the "to" table

  • totable

    the name of the "to" table

The relation-name for "multi" will be calculated using the multi_namer on the remote table name.

METHODS

select_from_queryspec

Parameters:

  • a "query spec"; a hash-ref with these keys:
    • table

    • keys

      array-ref of fields that are primary keys on this table

    • fields

      array-ref of fields that are primitive types to show in result, including PKs if wanted. If not wanted, the joins still function.

    • single

      hash-ref mapping relation-names to "query specs" - a recursive data structure; the relation is "has one"

    • multi

      hash-ref mapping relation-names to "relate specs" as above; the relation is "has many"

  • an SQL::Abstract "where" specification
  • an SQL::Abstract "options" specification, including order_by, limit, and offset

Returns the generated SQL, then a list of parameters to bind.

extractspec_from_queryspec

Parameters: a "query spec" as above.

Returns an opaque "extract spec": data to be used by "extract_from_query" to interpret results generated from the "select_from_queryspec" query.

extract_from_query

Parameters: an opaque "extract spec" created by "extractspec_from_queryspec", and a DBI $sth.

Returns a list of hash-refs of items as reconstructed according to the spec.

SEE ALSO

Yancy::Backend, DBI, DBIx::Class

sql-abstract-prefetch's People

Contributors

mohawk2 avatar

Watchers

 avatar  avatar

sql-abstract-prefetch's Issues

FALSE vs. 0 (with older DBD::SQLite?)

On some of my smoker systems the test suite fails:

...
    #   Failed test at t/basic.t line 134.
    #     Structures begin differing at:
    #          $got->{is_published} = 'FALSE'
    #     $expected->{is_published} = '0'
    # {
    #   'html' => '<h1>Super</h1>',
    #   'id' => 1,
    #   'is_published' => 'FALSE',
    #   'markdown' => '# Super',
    #   'slug' => 't-1',
    #   'title' => 'T 1',
    #   'user_id' => 1
    # }
    # Looks like you failed 1 test of 1.

#   Failed test 'get nojoin'
#   at t/basic.t line 137.
... (etc) ...

This seems to happen with older DBD::SQLite (theta = 0 means "bad"):

****************************************************************
Regression 'mod:DBD::SQLite'
****************************************************************
Name           	       Theta	      StdErr	 T-stat
[0='const']    	     -0.0000	      0.0000	  -0.92
[1='eq_1.50']  	     -0.0000	      0.0000	  -0.53
[2='eq_1.54']  	     -0.0000	      0.0000	  -0.29
[3='eq_1.56']  	      0.0000	      0.0000	   0.00
[4='eq_1.58']  	      0.0000	      0.0000	   1.81
[5='eq_1.60']  	      1.0000	      0.0000	5236218131169351.00
[6='eq_1.62']  	      1.0000	      0.0000	5407942831767525.00

R^2= 1.000, N= 92, K= 7
****************************************************************

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.