Coder Social home page Coder Social logo

sql-twoway's Introduction

NAME

SQL::TwoWay - Run same SQL in valid SQL and DBI placeholder.

SYNOPSIS

use SQL::TwoWay;

my $name = 'STARTING OVER';
my ($sql, @binds) = two_way_sql(
    q{SELECT *
    FROM cd
    WHERE name=/* $name */"MASTERPIECE"}, {
    name => $name,
});

# $sql: SELECT * FROM cd WHERE name=?
# $binds[0] = 'STARTING OVER'

DESCRIPTION

SQL::TwoWay is a way to support 2way SQL.

I guess building complex SQL using O/R Mapper or SQL builder, like SQL::Abstract is worth. When you writing complex SQL, you should write SQL by your hand.

And then, you got a issue: "I can't run my query on MySQL console!". Yes. A query like SELECT * FROM cd WHERE name=? is not runnable on console because that contains placeholder.

So, the solution is SQL::TwoWay.

You can write a query like this.

SELECT * FROM cd WHERE name=/* $name */"MASTERPIECE";

This query is 100% valid SQL.

And you can make <$sql> and <@binds> from this query. SQL::TwoWay::two_way_sql() function convert this query.

Here is a example code:

my ($sql, @binds) = two_way_sql(
    q{SELECT * FROM cd WHERE name=/* $name */"MASTERPIECE"},
    {
        name => 'STARTING OVER'
    }
);

$sql is:

SELECT * FROM cd WHERE name=?;

And @binds is:

('STARTING OVER')

So, you can use same SQL in MySQL console and Perl code. It means 2way SQL.

SYNTAX

  • /* $var */4

  • /* $var */(1,2,3)

  • /* $var */"String"

    Replace variables.

  • /* IF $cond */n=3/* ELSE */n=5/* END */

  • /* IF $cond */n=3/* END */

PSEUDO BNF

if : /* IF $var */
else : /* ELSE */
end : /* END */
variable : /* $var */ literal
literal: TBD
sql : .

root = ( stmt )+
stmt = sql | variable | if_stmt
if_stmt = "IF" statement+ "ELSE" statement+ "END"
        | "IF" statement+ "END"

LICENSE

Copyright (C) tokuhirom

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

AUTHOR

tokuhirom [email protected]

SEE ALSO

s2dao supports 2 way SQL in Java.

sql-twoway's People

Watchers

 avatar  avatar  avatar

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.