Repository navigation
Expand file tree
/
Copy pathBaseTable.php
More file actions
239 lines (214 loc) · 6.91 KB
/
Copy pathBaseTable.php
File metadata and controls
239 lines (214 loc) · 6.91 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
* Created by PhpStorm.
* User: zhangjun
* Date: 01/08/2018
* Time: 4:46 PM
*/
class BaseTable
{
/**
* @var BaseCtx
*/
public $ctx;
/**
* @var \PDO
*/
public $db;
/**
* @var \PDO
*/
public $dbSlave;
public function __construct(BaseCtx $context)
{
$this->ctx = $context;
$this->db = $this->ctx->db;
$this->dbSlave = $this->ctx->db;
if ($context->isMysqlDB()) {
$this->initSlaveDb();
}
$this->init();
}
public function initSlaveDb()
{
$mysqlSlaveArr = ZalyConfig::getConfig("mysqlSlave");
if (!empty($mysqlSlaveArr)) {
$slaveDbName = array_rand($mysqlSlaveArr, 1);
$slaveConfig = $mysqlSlaveArr[$slaveDbName];
$dbName = $slaveConfig['dbName'];
$dbHost = $slaveConfig['dbHost'];
$dbPort = $slaveConfig['dbPort'];
$dbUserName = $slaveConfig['dbUserName'];
$dbPwssword = $slaveConfig['dbPassword'];
$dbDsn = "mysql:host=$dbHost;port=$dbPort;dbname=$dbName;";
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
// PDO::ATTR_PERSISTENT => true,
);
$this->dbSlave = new PDO($dbDsn, $dbUserName, $dbPwssword, $options);//创建一个pdo对象
}
}
public function init()
{
}
/**
* 公用的插入方法,基本满足所有的插入状况
* @param $tableName
* @param $data
* @param $defaultColumns
* @param bool $tag
* @return bool
* @throws Exception
*/
public function insertData($tableName, $data, $defaultColumns, $tag = false)
{
if (!$tag) {
$tag = __CLASS__ . "-" . __FUNCTION__;
}
$startTime = microtime(true);
$insertKeys = array_keys($data);
$insertKeyStr = implode(",", $insertKeys);
$placeholderStr = "";
foreach ($insertKeys as $key => $val) {
if (!in_array($val, $defaultColumns)) {
continue;
}
$placeholderStr .= ",:" . $val . "";
}
$placeholderStr = trim($placeholderStr, ",");
if (!$placeholderStr) {
throw new Exception($tag . " update is fail");
}
$sql = " insert into $tableName({$insertKeyStr}) values ({$placeholderStr});";
$prepare = $this->db->prepare($sql);
$this->handlePrepareError($tag, $prepare);
foreach ($data as $key => $val) {
if (!in_array($key, $defaultColumns)) {
continue;
}
$prepare->bindValue(":" . $key, $val);
}
$flag = $prepare->execute();
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, $data, $startTime);
$count = $prepare->rowCount();
if (!$flag || !$count) {
throw new Exception($tag . "insert data error=" . var_export($prepare->errorInfo(), true));
}
return true;
}
/**
* 公用的更新方法,仅仅适用于and更新
* @param $tableName
* @param $where
* @param $data
* @param $defaultColumns
* @param $tag
* @return bool
* @throws Exception
*/
public function updateInfo($tableName, $where, $data, $defaultColumns, $tag = false)
{
if (!$tag) {
$tag = __CLASS__ . "-" . __FUNCTION__;
}
$startTime = microtime(true);
$updateStr = "";
$updateKeys = array_keys($data);
foreach ($updateKeys as $updateField) {
if (!in_array($updateField, $defaultColumns)) {
continue;
}
$updateStr .= "$updateField=:$updateField,";
}
$updateStr = trim($updateStr, ",");
if (!is_array($where)) {
throw new Exception("update fail");
}
$whereKeys = array_keys($where);
$whereKeyStr = "";
foreach ($whereKeys as $k => $val) {
if (!in_array($val, $defaultColumns)) {
continue;
}
$whereKeyStr .= " $val=:$val and";
}
$whereKeyStr = trim($whereKeyStr, "and");
if (!$whereKeyStr) {
throw new Exception($tag . " update is fail");
}
$sql = "update $tableName set $updateStr where $whereKeyStr";
$prepare = $this->db->prepare($sql);
$this->handlePrepareError($tag, $prepare);
foreach ($data as $key => $val) {
if (!in_array($key, $defaultColumns)) {
continue;
}
$prepare->bindValue(":" . $key, $val);
}
foreach ($where as $key => $val) {
$prepare->bindValue(":$key", $val);
}
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, ["data" => $data, "where" => $where], $startTime);
$flag = $prepare->execute();
// $count = $prepare->rowCount();
// if (!$flag || !$count) {
// throw new Exception($tag . " update is fail=" . var_export($prepare->errorInfo(), true));
// }
return $this->handlerUpdateResult($flag, $prepare, $tag);
}
public function handlePrepareError($tag, $prepare)
{
if (!$prepare) {
$error = [
"error_code" => $this->db->errorCode(),
"error_info" => $this->db->errorInfo(),
];
if ($this->db->errorCode() == 'HY000') {
$this->ctx->Wpf_Logger->error("db.error", var_export($this->db->errorInfo(), true));
}
$this->ctx->Wpf_Logger->error($tag, json_encode($error));
throw new Exception("execute prepare fail" . json_encode($error));
}
}
protected function getCurrentTimeMills()
{
return $this->ctx->ZalyHelper->getMsectime();
}
/**
* 处理 增,删 情况
* @param $tag
* @param $prepare
* @param $result
* @return bool
* @throws Exception
*/
protected function handlerResult($result, $prepare, $tag)
{
if ($prepare) {
if ($result && $prepare->errorCode() == '00000') {
return true;
} elseif ($prepare->errorCode() == 'HY000') {
$this->ctx->Wpf_Logger->error("table not exists =" . var_export($prepare->errorInfo(), true));
}
throw new Exception($tag . " execute prepare error="
. var_export($prepare->errorInfo(), true));
}
throw new Exception($tag . " execute prepare fail as prepare false");
}
/**
* 单独处理 update 情况,必须rowCount>0 才算成功
* @param $result
* @param $prepare
* @param $tag
* @return bool
* @throws Exception
*/
protected function handlerUpdateResult($result, $prepare, $tag)
{
$result = $this->handlerResult($result, $prepare, $tag);
if ($result) {
return $prepare->rowCount() > 0;
}
return false;
}
}