项目作者: vti

项目描述 :
SQL flexible placeholders
高级语言: Perl
项目地址: git://github.com/vti/perl-SQL-Bind.git
创建时间: 2020-04-28T10:08:22Z
项目社区:https://github.com/vti/perl-SQL-Bind

开源协议:Other

下载


NAME

SQL::Bind - SQL flexible placeholders

SYNOPSIS

  1. use SQL::Bind qw(sql);
  2. # Scalars
  3. my ($sql, @bind) =
  4. sql 'SELECT foo FROM bar WHERE id=:id AND status=:status',
  5. id => 1,
  6. status => 'active';
  7. # Arrays
  8. my ($sql, @bind) = sql 'SELECT foo FROM bar WHERE id IN (:id)', id => [1, 2, 3];
  9. # Hashes
  10. my ($sql, @bind) = sql 'UPDATE bar SET :columns', columns => {foo => 'bar'};
  11. # Raw values (!)
  12. my ($sql, @bind) = sql 'INSERT INTO bar (:keys!) VALUES (:values)',
  13. keys => [qw/foo/],
  14. values => [qw/bar/];
  15. # Recursive binding (*)
  16. my ($sql, @bind) =
  17. sql 'SELECT foo FROM bar WHERE :recursive_query*',
  18. recursive_query => 'name = :name',
  19. name => 'hello';

DESCRIPTION

SQL::Bind simplifies SQL queries maintenance by introducing placeholders. The behavior of the replacement depends on
the type of the value. Scalars, Arrays and Hashes are supported.

Configuration

$PlaceholderPrefix

Placeholder prefix (: by default) can be changed by setting the $PlaceholderPrefix global variable:

  1. local $SQL::Bind::PlaceholderPrefix = '@';
  2. my ($sql, @bind) =
  3. sql 'SELECT foo FROM bar WHERE id=@id',
  4. id => 1;

Placeholders

A placeholders is an alphanumeric sequence that is prefixed with : (by default) and can end with ! for raw values
or * for recursive binding. Some examples:

  1. :name
  2. :status
  3. :CamelCase
  4. :Value_123
  5. :ThisWillBeInsertedAsIs!
  6. :recursive*

Scalar values

Every value is replaced with a ?.

  1. my ($sql, @bind) =
  2. sql 'SELECT foo FROM bar WHERE id=:id AND status=:status',
  3. id => 1,
  4. status => 'active';
  5. # SELECT foo FROM bar WHERE id=? AND status=?
  6. # [1, 'active']

Array values

Arrays are replaced with a sequence of ?, ?, ....

  1. my ($sql, @bind) = sql 'SELECT foo FROM bar WHERE id IN (:id)', id => [1, 2, 3];
  2. # SELECT foo FROM bar WHERE id IN (?, ?, ?)
  3. # [1, 2, 3]

Hash values

Hahes are replaced with a sequence of key1=?, key2=?, ....

  1. my ($sql, @bind) = sql 'UPDATE bar SET :columns', columns => {foo => 'bar'};
  2. # UPDATE bar SET foo=?
  3. # ['bar']

Raw values

Sometimes raw values are needed be it another identifier, or a list of columns (e.g. INSERT, UPDATE). For this case
a placeholder should be suffixed with a !.

  1. my ($sql, @bind) = sql 'INSERT INTO bar (:keys!) VALUES (:values)',
  2. keys => [qw/foo/],
  3. values => [qw/bar/];
  4. # INSERT INTO bar (foo) VALUES (?)
  5. # ['bar']

Recursive binding

Recursive binding allows you to recursively parse already replaced values. This helps building complex subqueries.

  1. my ($sql, @bind) =
  2. sql 'SELECT foo FROM bar WHERE :recursive_query*',
  3. recursive_query => 'name = :name',
  4. name => 'hello';
  5. # 'SELECT foo FROM bar WHERE name = ?
  6. # ['hello']

DEVELOPMENT

Repository

  1. http://github.com/vti/sql-bind

CREDITS

AUTHOR

Viacheslav Tykhanovskyi, vti@cpan.org.

COPYRIGHT AND LICENSE

Copyright (C) 2020, Viacheslav Tykhanovskyi

This program is free software, you can redistribute it and/or modify it under
the terms of the Artistic License version 2.0.