diff options
Diffstat (limited to 'includes/database/database.inc')
-rw-r--r-- | includes/database/database.inc | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/includes/database/database.inc b/includes/database/database.inc index 4b1cff0f6..fb324eded 100644 --- a/includes/database/database.inc +++ b/includes/database/database.inc @@ -820,6 +820,34 @@ abstract class DatabaseConnection extends PDO { } /** + * Escape characters that work as wildcard characters in a LIKE pattern. + * + * The wildcard characters "%" and "_" as well as backslash are prefixed with + * a backslash. Use this to do a seach for a verbatim string without any + * wildcard behavior. + * + * For example, the following does a case-insensitive query for all rows whose + * name starts with $prefix: + * @code + * $result = db_query( + * 'SELECT * FROM person WHERE name LIKE :pattern', + * array(':pattern' => db_like($prefix) . '%') + * ); + * @endcode + * + * Backslash is defined as escape character for LIKE patterns in + * DatabaseCondition::mapConditionOperator(). + * + * @param $string + * The string to escape. + * @return + * The escaped string. + */ + public function escapeLike($string) { + return addcslashes($string, '\%_'); + } + + /** * Determine if there is an active transaction open. * * @return @@ -2235,6 +2263,34 @@ function db_escape_table($table) { } /** + * Escape characters that work as wildcard characters in a LIKE pattern. + * + * The wildcard characters "%" and "_" as well as backslash are prefixed with + * a backslash. Use this to do a seach for a verbatim string without any + * wildcard behavior. + * + * For example, the following does a case-insensitive query for all rows whose + * name starts with $prefix: + * @code + * $result = db_query( + * 'SELECT * FROM person WHERE name LIKE :pattern', + * array(':pattern' => db_like($prefix) . '%') + * ); + * @endcode + * + * Backslash is defined as escape character for LIKE patterns in + * DatabaseCondition::mapConditionOperator(). + * + * @param $string + * The string to escape. + * @return + * The escaped string. + */ +function db_like($string) { + return Database::getConnection()->escapeLike($string); +} + +/** * Retrieve the name of the currently active database driver, such as * "mysql" or "pgsql". * |