From: Raphaël Barrois Date: Wed, 10 Feb 2010 08:41:11 +0000 (+0100) Subject: Add XDB::formatWildcard X-Git-Tag: core/1.1.0~86 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=adf947ffd2957bc98ab2b0e86e8843489b2b4106;p=platal.git Add XDB::formatWildcard Signed-off-by: Raphaël Barrois --- diff --git a/classes/xdb.php b/classes/xdb.php index a652ed5..34485e1 100644 --- a/classes/xdb.php +++ b/classes/xdb.php @@ -159,6 +159,28 @@ class XDB return '(' . implode(', ', array_map(array('XDB', 'escape'), $array)) . ')'; } + const WILDCARD_EXACT = 0x00; + const WILDCARD_PREFIX = 0x01; + const WILDCARD_SUFFIX = 0x02; + const WILDCARD_CONTAINS = 0x03; // WILDCARD_PREFIX | WILDCARD_SUFFIX + + // Returns the SQL statement for a wildcard search. + public static function formatWildcards($mode, $text) + { + if ($mode == self::WILDCARD_EXACT) { + return XDB::format(' = {?}', $text); + } else { + $text = str_replace(array('%', '_'), array('\%', '\_'), $text); + if ($mode & self::WILDCARD_PREFIX) { + $text = $text . '%'; + } + if ($mode & self::WILDCARD_SUFFIX) { + $text = '%' . $text; + } + return XDB::format(" LIKE {?}", $text); + } + } + public static function execute() { global $globals;