Root/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | <?php /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* # ***** BEGIN LICENSE BLOCK ***** # This file is part of Plume Framework, a simple PHP Application Framework. # Copyright (C) 2001-2007 Loic d'Anterroches and contributors. # # Plume Framework is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # Plume Framework is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # ***** END LICENSE BLOCK ***** */ /** * Generate the WHERE SQL clause in an easy and SQL proof way. */ class Pluf_SQL { protected $db ; protected $where = '' ; public $ands = array (); /** * Construct the constructor with a default condition. */ function __construct( $base = '' , $args = array ()) { $this ->db = Pluf::db(); if ( strlen ( $base ) > 0) { $this ->Q( $base , $args ); } } /** * Returns the where clause. * * @return string Where clause without the WHERE */ function gen() { return implode( ' AND ' , $this ->ands); } /** * Add a condition. * * @param string String to 'interpolate' * @param mixed String or array of parameters (array()) */ function Q( $base , $args = array ()) { $escaped = array (); if (! is_array ( $args )) { $args = array ( $args ); } foreach ( $args as $arg ) { $escaped [] = $this ->db->esc( $arg ); } $this ->ands[] = vsprintf( $base , $escaped ); return $this ; } /** * Add another SQL as a AND. * * @param Pluf_SQL Other object to add to the current. */ function SAnd( $sql ) { return $this ->SDef( $sql ); } /** * Add another SQL as a OR * * @param Pluf_SQL Other object to add to the current. */ function SOr( $sql ) { return $this ->SDef( $sql , 'OR' ); } /** * Add another SQL to the current * * @param Pluf_SQL Other object to add to the current. * @param string Type of addition */ function SDef( $sql , $k = 'AND' ) { if ( empty ( $this ->ands)) { $this ->ands = $sql ->ands; } else { $othersql = $sql ->gen(); $current = $this ->gen(); if ( strlen ( $othersql )) { $this ->ands = array (); $this ->ands[] = '(' . $current . ') ' . $k . ' (' . $othersql . ')' ; } } return $this ; } /** * Get keywords. * * Considering a query string, explode the query string in * keywords given a defined delimiter. * * @param string Query string * @param string delimiter (' ') * @return array Array of keywords */ function keywords( $string , $del = ' ' ) { $keys = array (); $args = explode ( $del , $string ); foreach ( $args as $arg ) { $arg = trim( $arg ); if ( strlen ( $arg ) > 0) { $keys [] = $arg ; } } return $keys ; } } |