PHP设计模式-解释器

适用性

  • 理解:就是一个上下文的连接器
  • 使用场景:构建一个编译器,SQL解析器
  • 下面我们来实现一个简单增删改查的sql解析器

    代码示例

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

class SqlInterpreter
{
/**
* 表名
* @var string
*/
private $_tableName = '';

/**
* 当前类的实例
* @var object
*/
private static $_instance;

/**
* 设置表名
*
* @param string $table 表名
*/
public static function db($tableName='')
{
if (empty($tableName)) {
throw new Exception("argument tableName is null", 400);
}
// 单例
if(!self::$_instance instanceof self){// instanceof运算符的优先级高于!
self::$_instance = new self();
}
// 更新实例表名
self::$_instance->_setTableName($tableName);
// 返回实例
return self::$_instance;
}

/**
* 设置表名
*
* @param string $tableName 表名
*/
private function _setTableName($tableName='')
{
$this->_tableName = $tableName;
}

/**
* 插入一条数据
*
* @param array $data 数据
* @return mixed
*/
public function insert($data=[])
{
if (empty($data)) {
throw new Exception("argument data is null", 400);
}
$count = count($data);
//拼接字段
$field = array_keys($data);
$fieldString = '';
foreach ($field as $k => $v) {
if ($k === (int)($count - 1)) {
$fieldString .= "`{$v}`";
continue;
}
$fieldString .= "`{$v}`".',';
}
unset($k);
unset($v);

//拼接值
$value = array_values($data);
$valueString = '';
foreach ($value as $k => $v) {
if ($k === (int)($count - 1)) {
$valueString .= "'{$v}'";
continue;
}
$valueString .= "'{$v}'".',';
}
unset($k);
unset($v);

$sql = "INSERT INTO `{$this->_tableName}` ({$fieldString}) VALUES ({$valueString})";

echo $sql . "\n";
}

/**
* 删除数据
*
* @param array $data 数据
* @return mixed
*/
public function delete($data=[])
{
if (empty($data)) {
throw new Exception("argument data is null", 400);
}
// 拼接where语句
$count = (int)count($data);
$where = '';
$dataCopy = $data;
$pop = array_pop($dataCopy);
if ($count === 1) {
$field = array_keys($data)[0];
$value = array_values($data)[0];
$where = "`{$field}` = '{$value}'";
}else{
foreach ($data as $k => $v) {
if ($v === $pop) {
$where .= "`{$k}` = '{$v}'";
continue;
}
$where .= "`{$k}` = '{$v}' AND ";
}
}

$sql = "DELETE FROM `{$this->_tableName}` WHERE {$where}";

echo $sql . "\n";
}

/**
* 更新一条数据
*
* @param array $data 数据
* @return mixed
*/
public function update($data=[])
{
if (empty($data)) {
throw new Exception("argument data is null", 400);
}
if (empty($data['id'])) {
throw new Exception("argument data['id'] is null", 400);
}
$set = '';
$dataCopy = $data;
$pop = array_pop($dataCopy);
foreach ($data as $k => $v) {
if ($v === $pop) {
$set .= "`{$k}` = '$v'";
continue;
}
$set .= "`{$k}` = '$v',";
}

$sql = "UPDATE `{$this->_tableName}` SET {$set}";

echo $sql . "\n";
}

/**
* 查找一条数据
*
* @param array $data 数据
* @return mixed
*/
public function find($data=[])
{
if (empty($data)) {
throw new Exception("argument data is null", 400);
}

// 拼接where语句
$count = (int)count($data);
$where = '';
$dataCopy = $data;
$pop = array_pop($dataCopy);
if ($count === 1) {
$field = array_keys($data)[0];
$value = array_values($data)[0];
$where = "`{$field}` = '{$value}'";
}else{
foreach ($data as $k => $v) {
if ($v === $pop) {
$where .= "`{$k}` = '{$v}'";
continue;
}
$where .= "`{$k}` = '{$v}' AND ";
}
}

$sql = "SELECT * FROM `{$this->_tableName}` WHERE {$where}";

echo $sql . "\n";
}
}


try {
//增加数据
SqlInterpreter::db('user')->insert([
'nickname' => 'tigerb',
'mobile' => '1366666666',
'password' => '123456'
]);
//删除数据
SqlInterpreter::db('user')->delete([
'nickname' => 'tigerb',
'mobile' => '1366666666',
]);
//修改数据
SqlInterpreter::db('member')->update([
'id' => '1',
'nickname' => 'tigerbcode'
]);
//查询数据
SqlInterpreter::db('member')->find([
'mobile' => '1366666666',
]);
} catch (\Exception $e) {
echo 'error:' . $e->getMessage();
}