@@ -73,12 +73,38 @@ class DB
73
73
*/
74
74
protected $ id = '' ;
75
75
76
+ /**
77
+ * 走主库的查寻语句
78
+ *
79
+ * @var array
80
+ */
81
+ private $ master = ['insert ' , 'update ' , 'delete ' ];
82
+
83
+ /**
84
+ * 当前查询主从
85
+ *
86
+ * @var string
87
+ */
88
+ private $ masterSlave = '' ;
89
+
90
+ /**
91
+ * 数据库配置
92
+ *
93
+ * @var array
94
+ */
95
+ private $ dbConfig = [
96
+ 'dbhost ' => '' ,
97
+ 'dbname ' => '' ,
98
+ 'username ' => '' ,
99
+ 'password ' => ''
100
+ ];
101
+
76
102
/**
77
103
* 构造函数
78
104
*/
79
105
public function __construct ()
80
106
{
81
- $ this ->init ();
107
+ // $this->init();
82
108
}
83
109
84
110
/**
@@ -96,20 +122,25 @@ public static function table($tableName = '')
96
122
if (! empty ($ prefix )) {
97
123
$ db ->tableName = $ prefix . '_ ' . $ db ->tableName ;
98
124
}
99
- $ db ->init ();
125
+ // $db->init();
100
126
101
127
return $ db ;
102
128
}
103
129
104
130
/**
105
131
* 初始化策略
106
132
*
133
+ * @param $masterOrSlave 初始化主库还是从库
107
134
* @return void
108
135
*/
109
- public function init ()
136
+ public function init ($ masterOrSlave = '' )
110
137
{
111
138
$ config = APP ::$ container ->getSingle ('config ' );
112
139
$ this ->dbtype = $ config ->config ['database ' ]['dbtype ' ];
140
+ if (! empty ($ masterOrSlave )) {
141
+ $ this ->masterSlave = $ masterOrSlave ;
142
+ }
143
+ $ this ->isMasterOrSlave ();
113
144
$ this ->decide ();
114
145
}
115
146
@@ -121,14 +152,77 @@ public function init()
121
152
public function decide ()
122
153
{
123
154
$ dbStrategyName = $ this ->dbStrategyMap [$ this ->dbtype ];
124
- $ this ->dbInstance = APP ::$ container ->setSingle (
125
- $ this ->dbtype ,
126
- function () use ($ dbStrategyName ) {
127
- return new $ dbStrategyName ();
155
+ $ dbConfig = $ this ->dbConfig ;
156
+ $ this ->dbInstance = APP ::$ container ->getSingle (
157
+ "{$ this ->dbtype }- {$ this ->masterSlave }" ,
158
+ function () use ($ dbStrategyName , $ dbConfig ) {
159
+ return new $ dbStrategyName (
160
+ $ dbConfig ['dbhost ' ],
161
+ $ dbConfig ['dbname ' ],
162
+ $ dbConfig ['username ' ],
163
+ $ dbConfig ['password ' ]
164
+ );
128
165
}
129
166
);
130
167
}
131
168
169
+ /**
170
+ * 判断走主库还是从库
171
+ *
172
+ * @return void
173
+ */
174
+ public function isMasterOrSlave ()
175
+ {
176
+ if (! empty ($ this ->masterSlave )) {
177
+ $ this ->initMaster ();
178
+ return ;
179
+ }
180
+ foreach ($ this ->master as $ v ) {
181
+ $ res = stripos ($ this ->sql , $ v );
182
+ if ($ res === 0 || $ res ) {
183
+ $ this ->initMaster ();
184
+ return ;
185
+ }
186
+ }
187
+ $ this ->initSlave ();
188
+ }
189
+
190
+ /**
191
+ * 初始化主库
192
+ */
193
+ public function initMaster ()
194
+ {
195
+ $ config = APP ::$ container ->getSingle ('config ' );
196
+ $ dbConfig = $ config ->config ['database ' ];
197
+ $ this ->dbConfig ['dbhost ' ] = $ dbConfig ['dbhost ' ];
198
+ $ this ->dbConfig ['dbname ' ] = $ dbConfig ['dbname ' ];
199
+ $ this ->dbConfig ['username ' ] = $ dbConfig ['username ' ];
200
+ $ this ->dbConfig ['password ' ] = $ dbConfig ['password ' ];
201
+
202
+ $ this ->masterSlave = 'master ' ;
203
+ }
204
+
205
+ /**
206
+ * 初始化从库
207
+ */
208
+ public function initSlave ()
209
+ {
210
+ $ config = APP ::$ container ->getSingle ('config ' );
211
+ if (! isset ($ config ->config ['database ' ]['slave ' ])) {
212
+ $ this ->initMaster ();
213
+ return ;
214
+ }
215
+ $ slave = $ config ->config ['database ' ]['slave ' ];
216
+ $ randSlave = $ slave [array_rand ($ slave )];
217
+ $ dbConfig = $ config ->config ["database-slave- {$ randSlave }" ];
218
+ $ this ->dbConfig ['dbhost ' ] = $ dbConfig ['dbhost ' ];
219
+ $ this ->dbConfig ['dbname ' ] = $ dbConfig ['dbname ' ];
220
+ $ this ->dbConfig ['username ' ] = $ dbConfig ['username ' ];
221
+ $ this ->dbConfig ['password ' ] = $ dbConfig ['password ' ];
222
+
223
+ $ this ->masterSlave = "slave- {$ randSlave }" ;
224
+ }
225
+
132
226
/**
133
227
* 查找一条数据
134
228
*
@@ -165,6 +259,7 @@ public function findAll($data = [])
165
259
public function save ($ data = [])
166
260
{
167
261
$ this ->insert ($ data );
262
+ $ this ->init ();
168
263
$ functionName = __FUNCTION__ ;
169
264
return $ this ->dbInstance ->$ functionName ($ this );
170
265
}
@@ -231,6 +326,7 @@ public function sum($data = '')
231
326
public function query ($ sql = '' )
232
327
{
233
328
$ this ->querySql ($ sql );
329
+ $ this ->init ();
234
330
return $ this ->dbInstance ->query ($ this );
235
331
}
236
332
@@ -250,6 +346,8 @@ public function buildSql()
250
346
if (! empty ($ this ->limit )) {
251
347
$ this ->sql .= $ this ->limit ;
252
348
}
349
+
350
+ $ this ->init ();
253
351
}
254
352
255
353
/**
@@ -259,10 +357,11 @@ public function buildSql()
259
357
*/
260
358
public static function beginTransaction ()
261
359
{
262
- $ instance = APP ::$ container ->setSingle ('DB ' , function () {
360
+ $ instance = APP ::$ container ->getSingle ('DB ' , function () {
263
361
return new DB ();
264
362
}
265
363
);
364
+ $ instance ->init ('master ' );
266
365
$ instance ->dbInstance ->beginTransaction ();
267
366
}
268
367
@@ -273,10 +372,11 @@ public static function beginTransaction()
273
372
*/
274
373
public static function commit ()
275
374
{
276
- $ instance = APP ::$ container ->setSingle ('DB ' , function () {
375
+ $ instance = APP ::$ container ->getSingle ('DB ' , function () {
277
376
return new DB ();
278
377
}
279
378
);
379
+ $ instance ->init ('master ' );
280
380
$ instance ->dbInstance ->commit ();
281
381
}
282
382
@@ -291,6 +391,7 @@ public static function rollBack()
291
391
return new DB ();
292
392
}
293
393
);
394
+ $ instance ->init ('master ' );
294
395
$ instance ->dbInstance ->rollBack ();
295
396
}
296
397
0 commit comments