Skip to content
This repository was archived by the owner on Jul 1, 2022. It is now read-only.

notbenh/SQL-Query-Builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SYNOPSIS

use SQL::Query::Builder;
use DBI;
my $dbh = DBI->connect(...);

# start to build out our query object
my $query = SELECT->FROM('table')->WHERE(col => [1..3]);

# because $query is an object we can still modify it 
$query->LIMIT(2);

# so whats the current state of things?
my ($query, $bind_vars) = $query->build;

# $query is a string: SELECT * FROM table WHERE (`col` = ? OR `col` = ? OR `col` = ?) LIMIT 2
# $bind_vars is an arrayref [1,2,3]

# then when we are ready we pull the trigger
my $data = $dbh->selectall_arrayref($query->dbi(Slice=>{}));

EXPORTED FUNCTIONS

All the exported functions are wrapers around object constructors.

SELECT

Builds a SQL::Query::Builder::Query::Select object for you.

my $query = SELECT; #most basic case

Due to SQL::Query::Builder::Query::Select not needing anything at new, any items passed to SELECT will be passed to it's WHAT block.

SELECT('something') same as SELECT->WHAT('something')

If nothing is passed, WHAT defaults to '*'.

exports to be use in FROM

JOIN & LJOIN

Build SQL::Query::Builder::Query::Part::JOIN setting 'type' accordingly. JOIN is only useful while defining the FROM block of the query. There are two context.

# 'USING' notation is a scalar value
SELECT->FROM(table, JOIN table2 => col) 
# SELECT * FROM table JOIN table2 USING (col)

# 'ON' notation is a hashref as value
SELECT->FROM(table, JOIN table2 -> {'table.id' => 'table2.t1_id'}) 
# SELECT * FROM table JOIN table2 ON (`table`.`id` = `table2`.`t1_id`)

exports to be use in WHERE

AND & OR

Builds a SQL::Query::Builder::Query::Part::Set, setting 'type' to 'AND' or 'OR'. There are two expected context, the following two notations resolve to the same.

AND{ col => 12, col => 13} # (`col` = ? AND `col` = ?)[12,13]
col => AND[12,13]          # (`col` = ? AND `col` = ?)[12,13]

Same for OR:

OR{ col => 12, col => 13} # (`col` = ? OR `col` = ?)[12,13]
col => OR[12,13]          # (`col` = ? OR `col` = ?)[12,13]

Context is derived based on the ref type that wraps the data.

IN

Builds a SQL::Query::Builder::Query::Part::Set::IN object, unline AND/OR, IN only takes arrayrefs:

col => IN[12,13]  # `col` IN (?,?) [12,13]

GT

Builds a SQL::Query::Builder::Query::Part::OpValuePair object, setting 'type' to the correct op based on what was called.

col => GT  12 # `col` > ? [12]

GTE

col => GTE 12 # `col` >= ? [12]

LT

col => LT  12 # `col` < ? [12]

LTE

col => LTE 12 # `col` <= ? [12]

EQ

col => EQ  12 # `col` = ? [12]

NOT

Currently NOT is not implimented, though it is exported.

About

ya I know, like the world really needs yet another SQL builder

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages