-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.php
139 lines (123 loc) · 4.02 KB
/
db.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
class Db
{
/**
* @var PDO $pdo
*/
private $pdo;
public function __construct()
{
try {
$this->pdo = new PDO("sqlite:site.db");
$this->pdo->exec("PRAGMA journal_mode=WAL;");
$this->pdo->exec("PRAGMA busy_timeout=5000;");
$this->pdo->exec("PRAGMA cache=shared");
$this->createTable();
} catch (Throwable $throwable) {
var_dump($throwable->getTraceAsString(), $throwable->getMessage());
echo "数据库连接错误";
exit;
}
}
private function createTable()
{
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `article` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT ,
`title` varchar(255) NOT NULL,
`summary` varchar(255) NOT NULL DEFAULT '',
`pic` varchar(255) NOT NULL DEFAULT '',
`content` text NOT NULL,
`author` varchar(255) NOT NULL DEFAULT '',
`type_id` int NOT NULL DEFAULT '0',
`type_name` varchar(255) NOT NULL DEFAULT '',
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS title_idx on article(title);
CREATE INDEX IF NOT EXISTS created_at_idx on article(created_at);
CREATE TABLE IF NOT EXISTS `site_config` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`domain` varchar(100) NOT NULL,
`index_title` varchar(100) NOT NULL DEFAULT '',
`index_keywords` varchar(255) NOT NULL DEFAULT '',
`index_description` varchar(255) NOT NULL DEFAULT '',
`template_name` varchar(100) NOT NULL DEFAULT '',
`routes` text NOT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE UNIQUE INDEX IF NOT EXISTS domain_uni on site_config(domain);
SQL;
$this->pdo->exec($sql);
}
public function getSiteConfig($host): array
{
$stmt = $this->pdo->prepare("select * from site_config where domain=?");
$stmt->bindParam(1, $host);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_ASSOC);
}
public function getArticleCount()
{
$stmt = $this->pdo->prepare("select count(*) from article");
$stmt->execute();
$count = $stmt->fetchColumn();
return $count;
}
public function getArticleList($limit = 20): array
{
$order = "order by id ";
switch (mt_rand(1, 2)) {
case 1:
$order = "order by title ";
break;
case 2:
$order = "order by created_at ";
break;
}
$count = $this->getArticleCount();
$offset = mt_rand(0, $count);
if ($count < $limit) {
$offset = 0;
}
if ($offset > 0 && $offset > $count - $limit) {
$offset = $count - $limit;
}
$stmt = $this->pdo->prepare("select * from article {$order} limit ?,?");
$stmt->bindParam(1, $offset);
$stmt->bindParam(2, $limit);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function getRandomArticleId()
{
$count = $this->getArticleCount();
$order = "order by id ";
switch (mt_rand(1, 2)) {
case 1:
$order = "order by title ";
break;
case 2:
$order = "order by created_at ";
break;
}
$offset = mt_rand(0, $count - 1);
$s = sprintf("select id from article %s limit %d,1", $order, $offset);
$stmt = $this->pdo->prepare($s);
$stmt->execute();
return $stmt->fetchColumn();
}
public function getArticle($articleId): array
{
$stmt = $this->pdo->prepare("select * from article where id=?");
$stmt->bindParam(1, $articleId);
$stmt->execute();
$articel = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$articel) {
$id = $this->getRandomArticleId();
return $this->getArticle(articleId: $id);
}
return $articel;
}
}