X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FSQL.php;h=70e1c5a26ed30a9c71bfd8ee43047114e45d3f14;hb=100b381f5d21df3a546e2b5929916c4e68577326;hp=4e42f6eab0864074e7de9ba896d8fbc65a70189f;hpb=af028bfa606d5d8db62150656fe8241722521bb5;p=civicrm-core.git diff --git a/CRM/Utils/SQL.php b/CRM/Utils/SQL.php index 4e42f6eab0..70e1c5a26e 100644 --- a/CRM/Utils/SQL.php +++ b/CRM/Utils/SQL.php @@ -17,6 +17,38 @@ */ class CRM_Utils_SQL { + /** + * Given a string like "UPDATE some_table SET !field = @value", replace "!field" and "@value". + * + * This is syntactic sugar for using CRM_Utils_SQL_*::interpolate() without an OOP representation of the query. + * + * @param string $expr SQL expression + * @param null|array $args a list of values to insert into the SQL expression; keys are prefix-coded: + * prefix '@' => escape SQL + * prefix '#' => literal number, skip escaping but do validation + * prefix '!' => literal, skip escaping and validation + * if a value is an array, then it will be imploded + * + * PHP NULL's will be treated as SQL NULL's. The PHP string "null" will be treated as a string. + * + * @return string + */ + public static function interpolate($expr, $args) { + if (!isset(Civi::$statics[__CLASS__][__FUNCTION__])) { + Civi::$statics[__CLASS__][__FUNCTION__] = new class extends CRM_Utils_SQL_BaseParamQuery { + + public function __construct() { + $this->mode = CRM_Utils_SQL_BaseParamQuery::INTERPOLATE_INPUT; + $this->strict(); + } + + }; + } + /** @var \CRM_Utils_SQL_BaseParamQuery $qb */ + $qb = Civi::$statics[__CLASS__][__FUNCTION__]; + return $qb->strict()->interpolate($expr, $args); + } + /** * Helper function for adding the permissioned subquery from one entity onto another *