Skip to content

Commit

Permalink
Add Dbl::utf8_charset and convert_utf8.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Dec 10, 2021
1 parent 80a1a9b commit c92ec73
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
45 changes: 35 additions & 10 deletions lib/dbl.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,23 +835,48 @@ static function shutdown() {
self::$query_log = false;
}

/** @return string */
static function utf8(/* [$dblink,] $qstr */) {
$args = func_get_args();
$dblink = count($args) > 1 ? $args[0] : self::$default_dblink;
/** @param \mysqli|string $dblink
* @param ?string $qstr
* @return string */
static function utf8($dblink, $qstr = null) {
if (is_string($dblink)) {
$qstr = $dblink;
$dblink = self::$default_dblink;
}
$utf8 = $dblink->server_version >= 50503 ? "utf8mb4" : "utf8";
$qstr = count($args) > 1 ? $args[1] : $args[0];
return "_{$utf8}{$qstr}";
}

/** @return string */
static function utf8ci(/* [$dblink,] $qstr */) {
$args = func_get_args();
$dblink = count($args) > 1 ? $args[0] : self::$default_dblink;
/** @param \mysqli|string $dblink
* @param ?string $qstr
* @return string */
static function utf8ci($dblink, $qstr = null) {
if (is_string($dblink)) {
$qstr = $dblink;
$dblink = self::$default_dblink;
}
$utf8 = $dblink->server_version >= 50503 ? "utf8mb4" : "utf8";
$qstr = count($args) > 1 ? $args[1] : $args[0];
return "_{$utf8}{$qstr} collate {$utf8}_general_ci";
}

/** @param \mysqli|string $dblink
* @param ?string $qstr
* @return string */
static function convert_utf8($dblink, $qstr = null) {
if (is_string($dblink)) {
$qstr = $dblink;
$dblink = self::$default_dblink;
}
$utf8 = $dblink->server_version >= 50503 ? "utf8mb4" : "utf8";
return "convert($qstr using $utf8)";
}

/** @param ?\mysqli $dblink
* @return string */
static function utf8_charset($dblink = null) {
$dblink = $dblink ?? self::$default_dblink;
return $dblink->server_version >= 50503 ? "utf8mb4" : "utf8";
}
}

// quoting for SQL
Expand Down
5 changes: 3 additions & 2 deletions src/confinvariants.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ function exec_main() {
Dbl::free($result);

// whitespace is simplified
$regex = Dbl::utf8($this->conf->dblink, "'^ | \$| |[\\n\\r\\t]'");
$any = $this->invariantq("select email from ContactInfo where convert(firstName using utf8mb4) regexp $regex or convert(lastName using utf8mb4) regexp $regex or convert(affiliation using utf8mb4) regexp $regex limit 1");
$utf8cs = Dbl::utf8_charset($this->conf->dblink);
$regex = "_{$utf8cs}'^ | \$| |[\\n\\r\\t]'";
$any = $this->invariantq("select email from ContactInfo where convert(firstName using $utf8cs) regexp $regex or convert(lastName using $utf8cs) regexp $regex or convert(affiliation using $utf8cs) regexp $regex limit 1");
if ($any) {
$this->invariant_error("user_whitespace", "user whitespace is not simplified");
}
Expand Down

0 comments on commit c92ec73

Please sign in to comment.