discourse-legacysite-perl/site/glist/templates/help/GT/SQL/Condition.html
2024-06-17 22:24:05 +10:00

391 lines
8.8 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>GT::SQL::Condition - Creates complex where clauses</title>
<link rev="made" href="mailto:root@penguin.office.gossamer-threads.com" />
<style type="text/css">
/* $MVD$:fontset("Untitled Font Set 1","ARIEL","HELVETICA","HELV","SANSERIF") */
/* $MVD$:fontset("Arial","Arial") */
/* $MVD$:fontset("Arial Black","Arial Black") */
/* $MVD$:fontset("Algerian","Algerian") */
body {
background-color: white;
font-family: Verdana, Arial, sans-serif;
font-size: small;
color: black;
}
p {
background-color : white;
font-family : Verdana, Arial, sans-serif;
font-size : small;
color : black;
}
h1 {
font-family : Verdana, Arial, sans-serif;
font-weight : bold;
font-size : medium;
background-color : white;
color : maroon;
}
h2 {
font-family : Verdana, Arial, sans-serif;
font-size : medium;
font-weight : bold;
color : blue;
background-color : white;
}
h3 {
font-family : Verdana, Arial, sans-serif;
font-weight : bold;
font-size : medium;
color : black;
background-color : white;
}
h4 {
font-family : Verdana, Arial, sans-serif;
font-weight : bold;
font-size : small;
color : maroon;
background-color : white;
}
h5 {
font-family : Verdana, Arial, sans-serif;
font-weight : bold;
font-size : small;
color : blue;
background-color : white;
}
h6 {
font-family : Verdana, Arial, sans-serif;
font-weight : bold;
font-size : small;
color : black;
background-color : white;
}
ul {
font-family : Verdana, Arial, sans-serif;
font-size : small;
color : black;
}
ol {
font-family : Verdana, Arial, sans-serif;
font-size : small;
color : black;
}
dl {
font-family : Verdana, Arial, sans-serif;
font-size : small;
color : black;
}
li {
font-family : Verdana, Arial, sans-serif;
font-size : small;
color : black;
}
th {
font-family : Verdana, Arial, sans-serif;
font-size : small;
color : black;
}
td {
font-family : Verdana, Arial, sans-serif;
font-size : small;
color : black;
}
dl {
font-family : Verdana, Arial, sans-serif;
font-size : small;
color : black;
}
dd {
font-family : Verdana, Arial, sans-serif;
font-size : small;
color : black;
}
dt {
font-family : Verdana, Arial, sans-serif;
font-size : small;
color : black;
}
code {
font-family : Courier;
font-size : small;
color : black;
}
pre {
font-family : Courier;
font-size : small;
color : black;
}
.mvd-H1 {
font-family : Verdana, Arial, sans-serif;
font-weight : bold;
font-size : 14.0pt;
background-color : transparent;
background-image : none;
color : maroon;
}
.mvd-H2 {
font-family : Verdana, Arial, sans-serif;
font-size : 12.0pt;
color : blue;
}
p.indent {
font-family : "Verdana, Arial, sans-serif";
list-style-type : circle;
list-style-position : inside;
color : black;
margin-left : 16.0pt;
}
.mvd-P-indent {
font-family : Verdana, Arial, sans-serif;
list-style-type : circle;
list-style-position : inside;
color : black;
margin-left : 16.0pt;
}
pre.programlisting {
font-size : 9.0pt;
list-style-type : disc;
margin-left : 16.0pt;
margin-top : -14.0pt;
}
.mvd-PRE-programlisting {
font-size : 9.0pt;
list-style-type : disc;
margin-left : 16.0pt;
margin-top : -14.0pt;
}
.mvd-PRE {
font-size : 9.0pt;
}
p.note {
margin-left : 28.0pt;
}
.mvd-P-note {
margin-left : 28.0pt;
}
.mvd-H4 {
font-family : Verdana, Arial, sans-serif;
font-weight : normal;
font-size : 9.0pt;
color : black;
margin-left : 6.0pt;
margin-top : -14.0pt;
}
.mvd-P {
font-family : Verdana, Arial, sans-serif;
font-size : 10.0pt;
color : black;
}
.mvd-BODY {
font-family : Verdana, Arial, sans-serif;
background-color : white;
}
p.indentnobullet {
font-family : Verdana, Arial, sans-serif;
list-style-type : none;
}
.mvd-P-indentnobullet {
font-family : Verdana, Arial, sans-serif;
list-style-type : none;
}
</style>
</head>
<body style="background-color: white">
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsys">SYNOPSYS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#copyright">COPYRIGHT</a></li>
<li><a href="#version">VERSION</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>GT::SQL::Condition - Creates complex where clauses</p>
<p>
</p>
<hr />
<h1><a name="synopsys">SYNOPSYS</a></h1>
<pre>
my $cond = GT::SQL::Condition-&gt;new(Column =&gt; LIKE =&gt; 'foo%');
print $cond-&gt;sql;</pre>
<pre>
my $cond = GT::SQL::Condition-&gt;new(
Column =&gt; LIKE =&gt; 'foo%',
Column2 =&gt; '&lt;' =&gt; 'abc'
);
$cond-&gt;bool('OR');
print $cond-&gt;sql;</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>The condition module is useful for generating complex SQL WHERE clauses. At
it's simplest, a condition is composed of three parts: column, condition and
value.</p>
<p>Here are some examples.</p>
<p>To find all users with a first name that starts with Alex use:</p>
<pre>
my $cond = GT::SQL::Condition-&gt;new(FirstName =&gt; LIKE =&gt; 'Alex%');</pre>
<p>To find users with first name like alex, <strong>and</strong> last name like krohn use:</p>
<pre>
my $cond = GT::SQL::Condition-&gt;new(
FirstName =&gt; LIKE =&gt; 'Alex%',
LastName =&gt; LIKE =&gt; 'Krohn%'
);</pre>
<p>To find users with first name like alex <strong>or</strong> last name like krohn use:</p>
<pre>
my $cond = GT::SQL::Condition-&gt;new(
FirstName =&gt; LIKE =&gt; 'Alex%',
LastName =&gt; LIKE =&gt; 'Krohn%'
);
$cond-&gt;bool('OR');</pre>
<p>You may also specify this as:</p>
<pre>
my $cond = GT::SQL::Condition-&gt;new(
FirstName =&gt; LIKE =&gt; 'Alex%',
LastName =&gt; LIKE =&gt; 'Krohn%',
'OR'
);</pre>
<p>Now say we wanted something a bit more complex that would normally involve
setting parentheses. We want to find users who have either first name like alex
or last name like krohn, and whose employer is Gossamer Threads. We could use:</p>
<pre>
my $cond1 = GT::SQL::Condition-&gt;new(
'FirstName', 'LIKE', 'Alex%',
'LastName', 'LIKE', 'Krohn%'
);
$cond1-&gt;bool('or');
my $cond2 = GT::SQL::Condition-&gt;new(
$cond1,
Employer =&gt; '=' =&gt; 'Gossamer Threads'
);</pre>
<p>By default, all values are quoted, so you don't need to bother using any quote
function. If you don't want something quoted (say you want to use a function
for example), then you pass in a reference.</p>
<p>For example, to find users who have a last name that sounds like 'krohn', you
could use your SQL engines SOUNDEX function:</p>
<pre>
my $cond = GT::SQL::Condition-&gt;new(LastName =&gt; '=' =&gt; \&quot;SOUNDEX('krohn')&quot;);</pre>
<p>and the right side wouldn't be quoted.</p>
<p>You can also use a condition object to specify a list of multiple values, which
will become the SQL 'IN' operator. For example, to match anyone with a first
name of Alex, Scott or Jason, you can do:</p>
<pre>
my $cond = GT::SQL::Condition-&gt;new(FirstName =&gt; IN =&gt; ['Alex', 'Scott', 'Jason']);</pre>
<p>which will turn into:</p>
<pre>
FirstName IN ('Alex', 'Scott', 'Jason')</pre>
<p>Note that when using multiple values, you can use '=' instead of 'IN'. Empty
lists will be treated as an impossible condition (1 = 0). This is primarily
useful for list handling list of id numbers.</p>
<p>To match NULL values, you can use <code>undef</code> for the value passed to the <code>add()</code>
method. If specifying '=' as the operator, it will automatically be changed to
'IS':</p>
<pre>
$cond-&gt;add(MiddleName =&gt; '=' =&gt; undef);</pre>
<p>becomes:</p>
<pre>
MiddleName IS NULL</pre>
<p>To negate your queries you can use the <code>not</code> function.</p>
<pre>
my $cond = GT::SQL::Condition-&gt;new(a =&gt; '=' =&gt; 5);
$cond-&gt;not;</pre>
<p>would translate into NOT (a = '5'). You can also do this all on one line like:</p>
<pre>
print GT::SQL::Condition-&gt;new(a =&gt; '=' =&gt; '5')-&gt;not-&gt;sql;</pre>
<p>This returns the sql right away.</p>
<p>
</p>
<hr />
<h1><a name="copyright">COPYRIGHT</a></h1>
<p>Copyright (c) 2004 Gossamer Threads Inc. All Rights Reserved.
<a href="http://www.gossamer-threads.com/">http://www.gossamer-threads.com/</a></p>
<p>
</p>
<hr />
<h1><a name="version">VERSION</a></h1>
<p>Revision: $Id: Condition.pm,v 1.44 2004/10/12 17:54:30 jagerman Exp $</p>
</body>
</html>