{"id":17635952,"url":"https://github.com/mohawk2/sql-abstract-prefetch","last_synced_at":"2025-03-30T03:43:08.880Z","repository":{"id":56832755,"uuid":"180375806","full_name":"mohawk2/SQL-Abstract-Prefetch","owner":"mohawk2","description":"SQL::Abstract-like, implement \"prefetch\" with \"collapse\"","archived":false,"fork":false,"pushed_at":"2019-04-11T04:11:37.000Z","size":19,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-05T06:14:23.134Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://metacpan.org/pod/SQL::Abstract::Prefetch","language":"Perl","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mohawk2.png","metadata":{"files":{"readme":"README.md","changelog":"Changes","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-09T13:41:42.000Z","updated_at":"2019-04-11T04:11:34.000Z","dependencies_parsed_at":"2022-09-08T05:11:37.006Z","dependency_job_id":null,"html_url":"https://github.com/mohawk2/SQL-Abstract-Prefetch","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohawk2%2FSQL-Abstract-Prefetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohawk2%2FSQL-Abstract-Prefetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohawk2%2FSQL-Abstract-Prefetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohawk2%2FSQL-Abstract-Prefetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mohawk2","download_url":"https://codeload.github.com/mohawk2/SQL-Abstract-Prefetch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246273515,"owners_count":20750904,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-23T02:24:47.127Z","updated_at":"2025-03-30T03:43:08.849Z","avatar_url":"https://github.com/mohawk2.png","language":"Perl","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NAME\n\nSQL::Abstract::Prefetch - implement \"prefetch\" for DBI RDBMS\n\n# PROJECT STATUS\n\n| OS      |  Build status |\n|:-------:|--------------:|\n| Linux   | [![Build Status](https://travis-ci.com/mohawk2/SQL-Abstract-Prefetch.svg?branch=master)](https://travis-ci.com/mohawk2/SQL-Abstract-Prefetch) |\n\n[![CPAN version](https://badge.fury.io/pl/SQL-Abstract-Prefetch.svg)](https://metacpan.org/pod/SQL::Abstract::Prefetch) [![Coverage Status](https://coveralls.io/repos/github/mohawk2/SQL-Abstract-Prefetch/badge.svg?branch=master)](https://coveralls.io/github/mohawk2/SQL-Abstract-Prefetch?branch=master)\n\n# SYNOPSIS\n\n    my $queryspec = {\n      table =\u003e 'blog',\n      fields =\u003e [\n        'html',\n        'id',\n        'is_published',\n        'markdown',\n        'slug',\n        'title',\n        'user_id',\n      ],\n      keys =\u003e [ 'id' ],\n      multi =\u003e {\n        comments =\u003e {\n          table =\u003e 'comment',\n          fields =\u003e [ 'blog_id', 'html', 'id', 'markdown', 'user_id' ],\n          keys =\u003e [ 'id' ],\n        },\n      },\n      single =\u003e {\n        user =\u003e {\n          table =\u003e 'user',\n          fields =\u003e [ 'access', 'age', 'email', 'id', 'password', 'username' ],\n          keys =\u003e [ 'id' ],\n        },\n      },\n    };\n    my $abstract = SQL::Abstract::Pg-\u003enew( name_sep =\u003e '.', quote_char =\u003e '\"' );\n    my $dbh = DBI-\u003econnect( \"dbi:SQLite:dbname=filename.db\", '', '' );\n    my $prefetch = SQL::Abstract::Prefetch-\u003enew(\n      abstract =\u003e $abstract,\n      dbhgetter =\u003e sub { $dbh },\n      dbcatalog =\u003e undef, # for SQLite\n      dbschema =\u003e undef,\n      filter_table =\u003e sub { $_[0] !~ /^sqlite_/ },\n    );\n    my ( $sql, @bind ) = $prefetch-\u003eselect_from_queryspec(\n      $queryspec,\n      { id =\u003e $items{blog}[0]{id} },\n    );\n    my ( $extractspec ) = $prefetch-\u003eextractspec_from_queryspec( $queryspec );\n    my $sth = $dbh-\u003eprepare( $sql );\n    $sth-\u003eexecute( @bind );\n    my ( $got ) = $prefetch-\u003eextract_from_query( $extractspec, $sth );\n\n# DESCRIPTION\n\nThis class implements \"prefetch\" in the style of [DBIx::Class](https://metacpan.org/pod/DBIx::Class). Stages\nof operation:\n\n- Generate a \"query spec\" that describes what you want back from the\ndatabase - which fields from which tables, and what relations to join.\n- Generate SQL (and bind parameters) from that \"query spec\".\n- Pass the SQL and parameters to a [DBI](https://metacpan.org/pod/DBI) `$dbh` to prepare and execute.\n- Pass the `$sth` when ready (this allows for asynchronous operation)\nto the extractor method to turn the returned rows into the hash-refs\nrepresented, including array-ref values for any \"has many\" relationships.\n\n# ATTRIBUTES\n\n## abstract\n\nCurrently, must be a [SQL::Abstract::Pg](https://metacpan.org/pod/SQL::Abstract::Pg) object.\n\n## dbhgetter\n\nA code-ref that returns a [DBI](https://metacpan.org/pod/DBI) `$dbh`.\n\n## dbcatalog\n\nThe [DBI](https://metacpan.org/pod/DBI) \"catalog\" argument for e.g. [\"column\\_info\" in DBI](https://metacpan.org/pod/DBI#column_info).\n\n## dbschema\n\nThe [DBI](https://metacpan.org/pod/DBI) \"schema\" argument for e.g. [\"column\\_info\" in DBI](https://metacpan.org/pod/DBI#column_info).\n\n## filter\\_table\n\nCoderef called with a table name, returns a boolean of true to keep, false\nto discard - typically for a system table.\n\n## multi\\_namer\n\nCoderef called with a table name, returns a suitable name for the relation\nto that table. Defaults to [\"to\\_PL\" in Lingua::EN::Inflect::Number](https://metacpan.org/pod/Lingua::EN::Inflect::Number#to_PL).\n\n## dbspec\n\nBy default, will be calculated from the supplied `$dbh`, using the\nsupplied `dbhgetter`, `dbcatalog`, `dbschema`, `filter_table`,\nand `multi_namer`. May however be supplied, in which case those other\nattributes are not needed.\n\nA \"database spec\"; a hash-ref mapping tables to maps of the\nrelation-name (a string) to a further hash-ref with keys:\n\n- type\n\n    either `single` or `multi`\n\n- fromkey\n\n    the column name in the \"from\" table\n\n- fromtable\n\n    the name of the \"from\" table\n\n- tokey\n\n    the column name in the \"to\" table\n\n- totable\n\n    the name of the \"to\" table\n\nThe relation-name for \"multi\" will be calculated using\nthe `multi_namer` on the remote table name.\n\n# METHODS\n\n## select\\_from\\_queryspec\n\nParameters:\n\n- a \"query spec\"; a hash-ref with these keys:\n    - table\n    - keys\n\n        array-ref of fields that are primary keys on this table\n\n    - fields\n\n        array-ref of fields that are primitive types to show in result,\n        including PKs if wanted. If not wanted, the joins still function.\n\n    - single\n\n        hash-ref mapping relation-names to \"query specs\" - a recursive data\n        structure; the relation is \"has one\"\n\n    - multi\n\n        hash-ref mapping relation-names to \"relate specs\" as above; the relation is\n        \"has many\"\n- an [SQL::Abstract](https://metacpan.org/pod/SQL::Abstract) \"where\" specification\n- an [SQL::Abstract](https://metacpan.org/pod/SQL::Abstract) \"options\" specification, including `order_by`,\n`limit`, and `offset`\n\nReturns the generated SQL, then a list of parameters to bind.\n\n## extractspec\\_from\\_queryspec\n\nParameters: a \"query spec\" as above.\n\nReturns an opaque \"extract spec\": data to be used by\n[\"extract\\_from\\_query\"](#extract_from_query) to interpret results generated from the\n[\"select\\_from\\_queryspec\"](#select_from_queryspec) query.\n\n## extract\\_from\\_query\n\nParameters: an opaque \"extract spec\" created by\n[\"extractspec\\_from\\_queryspec\"](#extractspec_from_queryspec), and a [DBI](https://metacpan.org/pod/DBI) `$sth`.\n\nReturns a list of hash-refs of items as reconstructed according to the spec.\n\n# SEE ALSO\n\n[Yancy::Backend](https://metacpan.org/pod/Yancy::Backend), [DBI](https://metacpan.org/pod/DBI), [DBIx::Class](https://metacpan.org/pod/DBIx::Class)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohawk2%2Fsql-abstract-prefetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmohawk2%2Fsql-abstract-prefetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohawk2%2Fsql-abstract-prefetch/lists"}