Skip to content

Commit

Permalink
Merge pull request #336 from perftools/ansi_sql
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc authored Sep 30, 2020
2 parents 85a9247 + 6debe28 commit b077dc0
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 89 deletions.
60 changes: 30 additions & 30 deletions src/Xhgui/Saver/PdoSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,43 @@ class PdoSaver implements SaverInterface
const TABLE_DDL = <<<SQL
CREATE TABLE IF NOT EXISTS "%s" (
id CHAR(24) PRIMARY KEY,
profile TEXT NOT NULL,
url TEXT NULL,
SERVER TEXT NULL,
GET TEXT NULL,
ENV TEXT NULL,
simple_url TEXT NULL,
request_ts INTEGER NOT NULL,
request_ts_micro NUMERIC(15, 4) NOT NULL,
request_date DATE NOT NULL,
main_wt INTEGER NOT NULL,
main_ct INTEGER NOT NULL,
main_cpu INTEGER NOT NULL,
main_mu INTEGER NOT NULL,
main_pmu INTEGER NOT NULL
"id" CHAR(24) PRIMARY KEY,
"profile" TEXT NOT NULL,
"url" TEXT NULL,
"SERVER" TEXT NULL,
"GET" TEXT NULL,
"ENV" TEXT NULL,
"simple_url" TEXT NULL,
"request_ts" INTEGER NOT NULL,
"request_ts_micro" NUMERIC(15, 4) NOT NULL,
"request_date" DATE NOT NULL,
"main_wt" INTEGER NOT NULL,
"main_ct" INTEGER NOT NULL,
"main_cpu" INTEGER NOT NULL,
"main_mu" INTEGER NOT NULL,
"main_pmu" INTEGER NOT NULL
);
SQL;

const INSERT_DML = <<<SQL
INSERT INTO "%s" (
id,
profile,
url,
SERVER,
GET,
ENV,
simple_url,
request_ts,
request_ts_micro,
request_date,
main_wt,
main_ct,
main_cpu,
main_mu,
main_pmu
"id",
"profile",
"url",
"SERVER",
"GET",
"ENV",
"simple_url",
"request_ts",
"request_ts_micro",
"request_date",
"main_wt",
"main_ct",
"main_cpu",
"main_mu",
"main_pmu"
) VALUES (
:id,
:profile,
Expand Down
117 changes: 58 additions & 59 deletions src/Xhgui/Searcher/PdoSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ public function __construct(PDO $pdo, $table)
*/
public function latest()
{
$stmt = $this->pdo->query("
$stmt = $this->pdo->query('
SELECT
id,
profile,
url,
SERVER,
GET,
ENV,
simple_url,
request_ts,
request_ts_micro,
request_date
FROM {$this->table}
ORDER BY request_date ASC
"id",
"profile",
"url",
"SERVER",
"GET",
"ENV",
"simple_url",
"request_ts",
"request_ts_micro,"
"request_date"
FROM "' . $this->table . '"
ORDER BY "request_date" ASC
LIMIT 1
");
');

$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (false === $row) {
Expand Down Expand Up @@ -84,20 +84,20 @@ public function query($conditions, $limit, $fields = [])
*/
public function get($id)
{
$stmt = $this->pdo->prepare("
$stmt = $this->pdo->prepare('
SELECT
profile,
url,
SERVER,
GET,
ENV,
simple_url,
request_ts,
request_ts_micro,
request_date
FROM {$this->table}
"profile",
"url",
"SERVER",
"GET",
"ENV",
"simple_url",
"request_ts",
"request_ts_micro",
"request_date"
FROM "' . $this->table . '"
WHERE id = :id
");
');

$stmt->execute(['id' => $id]);

Expand Down Expand Up @@ -159,11 +159,11 @@ public function getAll($options = [])
$perPage = (int)$options['perPage'];
$url = $options['conditions']['url'] ?? "";

$stmt = $this->pdo->prepare("
$stmt = $this->pdo->prepare('
SELECT COUNT(*) AS count
FROM {$this->table}
WHERE simple_url LIKE :url
");
FROM "' . $this->table . '"
WHERE "simple_url" LIKE :url
');
$stmt->execute(['url' => '%'.$url.'%']);
$totalRows = (int)$stmt->fetchColumn();

Expand All @@ -173,27 +173,26 @@ public function getAll($options = [])
}
$skip = ($page-1) * $perPage;

$stmt = $this->pdo->prepare("
$stmt = $this->pdo->prepare('
SELECT
id,
url,
SERVER,
GET,
ENV,
simple_url,
request_ts,
request_ts_micro,
request_date,
main_wt,
main_ct,
main_cpu,
main_mu,
main_pmu
FROM {$this->table}
WHERE simple_url LIKE :url
ORDER BY request_ts DESC
LIMIT $skip OFFSET $perPage
");
"id",
"url",
"SERVER",
"GET",
"ENV",
"simple_url",
"request_ts",
"request_ts_micro",
"request_date",
"main_wt",
"main_ct",
"main_cpu",
"main_mu",
"main_pmu"
FROM "' . $this->table . '"
WHERE "simple_url" LIKE :url
ORDER BY "request_ts" DESC
LIMIT ' . $skip . ' OFFSET ' . $perPage);
$stmt->execute(['url' => '%'.$url.'%']);

$results = [];
Expand Down Expand Up @@ -237,10 +236,10 @@ public function getAll($options = [])
*/
public function delete($id)
{
$stmt = $this->pdo->prepare("
DELETE FROM {$this->table}
$stmt = $this->pdo->prepare('
DELETE FROM "' . $this->table . '"
WHERE id = :id
");
');

$stmt->execute(['id' => $id]);
}
Expand All @@ -251,7 +250,7 @@ public function delete($id)
public function truncate()
{
return is_int(
$this->pdo->exec("DELETE FROM {$this->table}")
$this->pdo->exec('DELETE FROM "' . $this->table . '"')
);
}

Expand Down Expand Up @@ -283,13 +282,13 @@ public function truncateWatches()
*/
public function stats()
{
$stmt = $this->pdo->query("
$stmt = $this->pdo->query('
SELECT
COUNT(*) AS profiles,
MAX(request_ts) AS latest,
SUM(LENGTH(profile)) AS bytes
FROM {$this->table}
", PDO::FETCH_ASSOC);
MAX("request_ts") AS latest,
SUM(LENGTH("profile")) AS bytes
FROM "' . $this->table . '"
', PDO::FETCH_ASSOC);

$row = $stmt->fetch(PDO::FETCH_ASSOC);

Expand Down
5 changes: 5 additions & 0 deletions src/Xhgui/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,15 @@ protected function _services()
throw new RuntimeException("Required extension ext-pdo is missing");
}

$adapter = explode(':', $c['config']['pdo']['dsn'], 2)[0];
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
];

if ($adapter === 'mysql') {
$options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET SQL_MODE=ANSI_QUOTES;';
}

return new PDO(
$c['config']['pdo']['dsn'],
$c['config']['pdo']['user'],
Expand Down

0 comments on commit b077dc0

Please sign in to comment.