Only encode url if it is the front end in unit tests
[civicrm-core.git] / CRM / Utils / SQL.php
index 4e42f6eab0864074e7de9ba896d8fbc65a70189f..70e1c5a26ed30a9c71bfd8ee43047114e45d3f14 100644 (file)
  */
 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
    *