1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. |
---|
6 | * |
---|
7 | * http://www.lockon.co.jp/ |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License |
---|
11 | * as published by the Free Software Foundation; either version 2 |
---|
12 | * of the License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | // {{{ requires |
---|
25 | require_once(CLASS_PATH . "db/SC_DB_DBFactory.php"); |
---|
26 | |
---|
27 | /** |
---|
28 | * MySQL 固有の処理をするクラス. |
---|
29 | * |
---|
30 | * このクラスを直接インスタンス化しないこと. |
---|
31 | * 必ず SC_DB_DBFactory クラスを経由してインスタンス化する. |
---|
32 | * また, SC_DB_DBFactory クラスの関数を必ずオーバーライドしている必要がある. |
---|
33 | * |
---|
34 | * @package DB |
---|
35 | * @author LOCKON CO.,LTD. |
---|
36 | * @version $Id:SC_DB_DBFactory_MYSQL.php 15267 2007-08-09 12:31:52Z nanasess $ |
---|
37 | */ |
---|
38 | class SC_DB_DBFactory_MYSQL extends SC_DB_DBFactory { |
---|
39 | |
---|
40 | /** |
---|
41 | * DBのバージョンを取得する. |
---|
42 | * |
---|
43 | * @param string $dsn データソース名 |
---|
44 | * @return string データベースのバージョン |
---|
45 | */ |
---|
46 | function sfGetDBVersion($dsn = "") { |
---|
47 | $objQuery = new SC_Query($this->getDSN($dsn), true, true); |
---|
48 | list($db_type) = split(":", $dsn); |
---|
49 | $val = $objQuery->getOne("select version()"); |
---|
50 | return "MySQL " . $val; |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * MySQL 用の SQL 文に変更する. |
---|
55 | * |
---|
56 | * @access private |
---|
57 | * @param string $sql SQL 文 |
---|
58 | * @return string MySQL 用に置換した SQL 文 |
---|
59 | */ |
---|
60 | function sfChangeMySQL($sql){ |
---|
61 | // 改行、タブを1スペースに変換 |
---|
62 | $sql = preg_replace("/[\r\n\t]/"," ",$sql); |
---|
63 | // view表をインラインビューに変換する |
---|
64 | $sql = $this->sfChangeView($sql); |
---|
65 | // ILIKE検索をLIKE検索に変換する |
---|
66 | $sql = $this->sfChangeILIKE($sql); |
---|
67 | // RANDOM()をRAND()に変換する |
---|
68 | $sql = $this->sfChangeRANDOM($sql); |
---|
69 | // TRUNCをTRUNCATEに変換する |
---|
70 | $sql = $this->sfChangeTrunc($sql); |
---|
71 | return $sql; |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * 文字コード情報を取得する |
---|
76 | * |
---|
77 | * @return array 文字コード情報 |
---|
78 | */ |
---|
79 | function getCharSet() { |
---|
80 | $objQuery = new SC_Query(); |
---|
81 | $arrRet = $objQuery->getAll("SHOW VARIABLES LIKE 'char%'"); |
---|
82 | return $arrRet; |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * テーブルの存在チェックを行う SQL 文を返す. |
---|
87 | * |
---|
88 | * @return string テーブルの存在チェックを行う SQL 文 |
---|
89 | */ |
---|
90 | function getTableExistsSql() { |
---|
91 | return "SHOW TABLE STATUS LIKE ?"; |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * インデックスの検索結果を配列で返す. |
---|
96 | * |
---|
97 | * @param string $index_name インデックス名 |
---|
98 | * @param string $table_name テーブル名 |
---|
99 | * @return array インデックスの検索結果の配列 |
---|
100 | */ |
---|
101 | function getTableIndex($index_name, $table_name = "") { |
---|
102 | $objQuery = new SC_Query("", true, true); |
---|
103 | return $objQuery->getAll("SHOW INDEX FROM " . $table_name . " WHERE Key_name = ?", |
---|
104 | array($index_name)); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * インデックスを作成する. |
---|
109 | * |
---|
110 | * @param string $index_name インデックス名 |
---|
111 | * @param string $table_name テーブル名 |
---|
112 | * @param string $col_name カラム名 |
---|
113 | * @param integer $length 作成するインデックスのバイト長 |
---|
114 | * @return void |
---|
115 | */ |
---|
116 | function createTableIndex($index_name, $table_name, $col_name, $length = 0) { |
---|
117 | $objQuery = new SC_Query($dsn, true, true); |
---|
118 | $objQuery->query("CREATE INDEX ? ON ? (?(?))", array($index_name, $table_name, $col_name, $length)); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * テーブルのカラム一覧を取得する. |
---|
123 | * |
---|
124 | * @param string $table_name テーブル名 |
---|
125 | * @return array テーブルのカラム一覧の配列 |
---|
126 | */ |
---|
127 | function sfGetColumnList($table_name) { |
---|
128 | $objQuery = new SC_Query(); |
---|
129 | $sql = "SHOW COLUMNS FROM " . $table_name; |
---|
130 | $arrColList = $objQuery->getAll($sql); |
---|
131 | $arrColList = SC_Utils_Ex::sfswaparray($arrColList); |
---|
132 | return $arrColList["Field"]; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * テーブルを検索する. |
---|
137 | * |
---|
138 | * 引数に部分一致するテーブル名を配列で返す. |
---|
139 | * |
---|
140 | * @param string $expression 検索文字列 |
---|
141 | * @return array テーブル名の配列 |
---|
142 | */ |
---|
143 | function findTableNames($expression = "") { |
---|
144 | $objQuery = new SC_Query(); |
---|
145 | $sql = "SHOW TABLES LIKE ?"; |
---|
146 | $arrColList = $objQuery->getAll($sql, array("%" . $expression . "%")); |
---|
147 | $arrColList = SC_Utils_Ex::sfswaparray($arrColList, false); |
---|
148 | return $arrColList[0]; |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * View の WHERE 句を置換する. |
---|
153 | * |
---|
154 | * @param string $target 置換対象の文字列 |
---|
155 | * @param string $where 置換する文字列 |
---|
156 | * @param array $arrval WHERE 句の要素の配列 |
---|
157 | * @param string $option SQL 文の追加文字列 |
---|
158 | * @return string 置換後の SQL 文 |
---|
159 | */ |
---|
160 | function sfViewWhere($target, $where = "", $arrval = array(), $option = ""){ |
---|
161 | |
---|
162 | $arrWhere = split("[?]", $where); |
---|
163 | $where_tmp = " WHERE " . $arrWhere[0]; |
---|
164 | for($i = 1; $i < count($arrWhere); $i++){ |
---|
165 | $where_tmp .= SC_Utils_Ex::sfQuoteSmart($arrval[$i - 1]) . $arrWhere[$i]; |
---|
166 | } |
---|
167 | $arrWhere = $this->getWhereConverter(); |
---|
168 | $arrWhere[$target] = $where_tmp . " " . $option; |
---|
169 | return $arrWhere[$target]; |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * SQL の中の View の存在をチェックする. |
---|
174 | * |
---|
175 | * @access private |
---|
176 | * @param string $sql SQL 文 |
---|
177 | * @return bool Viewが存在しない場合 false |
---|
178 | */ |
---|
179 | function sfInArray($sql){ |
---|
180 | $arrView = $this->viewToSubQuery(); |
---|
181 | |
---|
182 | foreach($arrView as $key => $val){ |
---|
183 | if (strcasecmp($sql, $val) == 0){ |
---|
184 | $changesql = eregi_replace("($key)", "$val", $sql); |
---|
185 | $this->sfInArray($changesql); |
---|
186 | } |
---|
187 | } |
---|
188 | return false; |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * View をインラインビューに変換する. |
---|
193 | * |
---|
194 | * @access private |
---|
195 | * @param string $sql SQL 文 |
---|
196 | * @return string インラインビューに変換した SQL 文 |
---|
197 | */ |
---|
198 | function sfChangeView($sql){ |
---|
199 | |
---|
200 | $arrViewTmp = $this->viewToSubQuery(); |
---|
201 | |
---|
202 | // viewのwhereを変換 |
---|
203 | foreach($arrViewTmp as $key => $val){ |
---|
204 | $arrViewTmp[$key] = strtr($arrViewTmp[$key], $this->getWhereConverter()); |
---|
205 | } |
---|
206 | |
---|
207 | // viewを変換 |
---|
208 | $changesql = strtr($sql, $arrViewTmp); |
---|
209 | |
---|
210 | return $changesql; |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | * ILIKE句 を LIKE句へ変換する. |
---|
215 | * |
---|
216 | * @access private |
---|
217 | * @param string $sql SQL文 |
---|
218 | * @return string 変換後の SQL 文 |
---|
219 | */ |
---|
220 | function sfChangeILIKE($sql){ |
---|
221 | $changesql = eregi_replace("(ILIKE )", "LIKE ", $sql); |
---|
222 | return $changesql; |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * RANDOM() を RAND() に変換する. |
---|
227 | * |
---|
228 | * @access private |
---|
229 | * @param string $sql SQL文 |
---|
230 | * @return string 変換後の SQL 文 |
---|
231 | */ |
---|
232 | function sfChangeRANDOM($sql){ |
---|
233 | $changesql = eregi_replace("( RANDOM)", " RAND", $sql); |
---|
234 | return $changesql; |
---|
235 | } |
---|
236 | |
---|
237 | /** |
---|
238 | * TRUNC() を TRUNCATE() に変換する. |
---|
239 | * |
---|
240 | * @access private |
---|
241 | * @param string $sql SQL文 |
---|
242 | * @return string 変換後の SQL 文 |
---|
243 | */ |
---|
244 | function sfChangeTrunc($sql){ |
---|
245 | $changesql = eregi_replace("( TRUNC)", " TRUNCATE", $sql); |
---|
246 | return $changesql; |
---|
247 | } |
---|
248 | |
---|
249 | /** |
---|
250 | * WHERE 句置換用の配列を返す. |
---|
251 | * |
---|
252 | * @access private |
---|
253 | * @return array WHERE 句置換用の配列 |
---|
254 | */ |
---|
255 | function getWhereConverter() { |
---|
256 | return array( |
---|
257 | "&&crscls_where&&" => "", |
---|
258 | "&&crsprdcls_where&&" =>"", |
---|
259 | "&&noncls_where&&" => "", |
---|
260 | "&&allcls_where&&" => "", |
---|
261 | "&&allclsdtl_where&&" => "", |
---|
262 | "&&prdcls_where&&" => "", |
---|
263 | "&&catcnt_where&&" => "" |
---|
264 | ); |
---|
265 | } |
---|
266 | |
---|
267 | /** |
---|
268 | * View をサブクエリに変換するための配列を返す. |
---|
269 | * |
---|
270 | * @access private |
---|
271 | * @return array View をサブクエリに変換するための配列 |
---|
272 | */ |
---|
273 | function viewToSubQuery() { |
---|
274 | $sql['vw_products_allclass_detail'] =<<< __EOS__ |
---|
275 | ( |
---|
276 | SELECT |
---|
277 | dtb_products.product_id, |
---|
278 | dtb_products.name, |
---|
279 | dtb_products.deliv_fee, |
---|
280 | dtb_products.sale_limit, |
---|
281 | dtb_products.sale_unlimited, |
---|
282 | dtb_products.rank, |
---|
283 | dtb_products.status, |
---|
284 | dtb_products.product_flag, |
---|
285 | dtb_products.point_rate, |
---|
286 | dtb_products.comment1, |
---|
287 | dtb_products.comment2, |
---|
288 | dtb_products.comment3, |
---|
289 | dtb_products.comment4, |
---|
290 | dtb_products.comment5, |
---|
291 | dtb_products.comment6, |
---|
292 | dtb_products.note, |
---|
293 | dtb_products.file1, |
---|
294 | dtb_products.file2, |
---|
295 | dtb_products.file3, |
---|
296 | dtb_products.file4, |
---|
297 | dtb_products.file5, |
---|
298 | dtb_products.file6, |
---|
299 | dtb_products.main_list_comment, |
---|
300 | dtb_products.main_list_image, |
---|
301 | dtb_products.main_comment, |
---|
302 | dtb_products.main_image, |
---|
303 | dtb_products.main_large_image, |
---|
304 | dtb_products.sub_title1, |
---|
305 | dtb_products.sub_comment1, |
---|
306 | dtb_products.sub_image1, |
---|
307 | dtb_products.sub_large_image1, |
---|
308 | dtb_products.sub_title2, |
---|
309 | dtb_products.sub_comment2, |
---|
310 | dtb_products.sub_image2, |
---|
311 | dtb_products.sub_large_image2, |
---|
312 | dtb_products.sub_title3, |
---|
313 | dtb_products.sub_comment3, |
---|
314 | dtb_products.sub_image3, |
---|
315 | dtb_products.sub_large_image3, |
---|
316 | dtb_products.sub_title4, |
---|
317 | dtb_products.sub_comment4, |
---|
318 | dtb_products.sub_image4, |
---|
319 | dtb_products.sub_large_image4, |
---|
320 | dtb_products.sub_title5, |
---|
321 | dtb_products.sub_comment5, |
---|
322 | dtb_products.sub_image5, |
---|
323 | dtb_products.sub_large_image5, |
---|
324 | dtb_products.sub_title6, |
---|
325 | dtb_products.sub_comment6, |
---|
326 | dtb_products.sub_image6, |
---|
327 | dtb_products.sub_large_image6, |
---|
328 | dtb_products.del_flg, |
---|
329 | dtb_products.creator_id, |
---|
330 | dtb_products.create_date, |
---|
331 | dtb_products.update_date, |
---|
332 | dtb_products.deliv_date_id, |
---|
333 | T4.product_code_min, |
---|
334 | T4.product_code_max, |
---|
335 | T4.price01_min, |
---|
336 | T4.price01_max, |
---|
337 | T4.price02_min, |
---|
338 | T4.price02_max, |
---|
339 | T4.stock_min, |
---|
340 | T4.stock_max, |
---|
341 | T4.stock_unlimited_min, |
---|
342 | T4.stock_unlimited_max, |
---|
343 | T4.class_count |
---|
344 | FROM |
---|
345 | dtb_products |
---|
346 | LEFT JOIN |
---|
347 | ( |
---|
348 | SELECT |
---|
349 | product_id, |
---|
350 | MIN(product_code) AS product_code_min, |
---|
351 | MAX(product_code) AS product_code_max, |
---|
352 | MIN(price01) AS price01_min, |
---|
353 | MAX(price01) AS price01_max, |
---|
354 | MIN(price02) AS price02_min, |
---|
355 | MAX(price02) AS price02_max, |
---|
356 | MIN(stock) AS stock_min, |
---|
357 | MAX(stock) AS stock_max, |
---|
358 | MIN(stock_unlimited) AS stock_unlimited_min, |
---|
359 | MAX(stock_unlimited) AS stock_unlimited_max, |
---|
360 | COUNT(*) as class_count |
---|
361 | FROM dtb_products_class |
---|
362 | GROUP BY product_id |
---|
363 | ) AS T4 |
---|
364 | ON dtb_products.product_id = T4.product_id |
---|
365 | ) |
---|
366 | __EOS__; |
---|
367 | |
---|
368 | return array( |
---|
369 | "vw_cross_class" => ' |
---|
370 | (SELECT T1.class_id AS class_id1, T2.class_id AS class_id2, T1.classcategory_id AS classcategory_id1, T2.classcategory_id AS classcategory_id2, T1.name AS name1, T2.name AS name2, T1.rank AS rank1, T2.rank AS rank2 |
---|
371 | FROM dtb_classcategory AS T1, dtb_classcategory AS T2 ) ', |
---|
372 | |
---|
373 | "vw_cross_products_class" =>' |
---|
374 | (SELECT T1.class_id1, T1.class_id2, T1.classcategory_id1, T1.classcategory_id2, T2.product_id, |
---|
375 | T1.name1, T1.name2, T2.product_code, T2.stock, T2.price01, T2.price02, T1.rank1, T1.rank2 |
---|
376 | FROM (SELECT T1.class_id AS class_id1, T2.class_id AS class_id2, T1.classcategory_id AS classcategory_id1, T2.classcategory_id AS classcategory_id2, T1.name AS name1, T2.name AS name2, T1.rank AS rank1, T2.rank AS rank2 |
---|
377 | FROM dtb_classcategory AS T1, dtb_classcategory AS T2 ) AS T1 LEFT JOIN dtb_products_class AS T2 |
---|
378 | ON T1.classcategory_id1 = T2.classcategory_id1 AND T1.classcategory_id2 = T2.classcategory_id2) ', |
---|
379 | |
---|
380 | "vw_products_nonclass" => ' |
---|
381 | (SELECT |
---|
382 | T1.product_id, |
---|
383 | T1.name, |
---|
384 | T1.deliv_fee, |
---|
385 | T1.sale_limit, |
---|
386 | T1.sale_unlimited, |
---|
387 | T1.category_id, |
---|
388 | T1.rank, |
---|
389 | T1.status, |
---|
390 | T1.product_flag, |
---|
391 | T1.point_rate, |
---|
392 | T1.comment1, |
---|
393 | T1.comment2, |
---|
394 | T1.comment3, |
---|
395 | T1.comment4, |
---|
396 | T1.comment5, |
---|
397 | T1.comment6, |
---|
398 | T1.file1, |
---|
399 | T1.file2, |
---|
400 | T1.file3, |
---|
401 | T1.file4, |
---|
402 | T1.file5, |
---|
403 | T1.file6, |
---|
404 | T1.main_list_comment, |
---|
405 | T1.main_list_image, |
---|
406 | T1.main_comment, |
---|
407 | T1.main_image, |
---|
408 | T1.main_large_image, |
---|
409 | T1.sub_title1, |
---|
410 | T1.sub_comment1, |
---|
411 | T1.sub_image1, |
---|
412 | T1.sub_large_image1, |
---|
413 | T1.sub_title2, |
---|
414 | T1.sub_comment2, |
---|
415 | T1.sub_image2, |
---|
416 | T1.sub_large_image2, |
---|
417 | T1.sub_title3, |
---|
418 | T1.sub_comment3, |
---|
419 | T1.sub_image3, |
---|
420 | T1.sub_large_image3, |
---|
421 | T1.sub_title4, |
---|
422 | T1.sub_comment4, |
---|
423 | T1.sub_image4, |
---|
424 | T1.sub_large_image4, |
---|
425 | T1.sub_title5, |
---|
426 | T1.sub_comment5, |
---|
427 | T1.sub_image5, |
---|
428 | T1.sub_large_image5, |
---|
429 | T1.sub_title6, |
---|
430 | T1.sub_comment6, |
---|
431 | T1.sub_image6, |
---|
432 | T1.sub_large_image6, |
---|
433 | T1.del_flg, |
---|
434 | T1.creator_id, |
---|
435 | T1.create_date, |
---|
436 | T1.update_date, |
---|
437 | T1.note, |
---|
438 | T1.deliv_date_id, |
---|
439 | T2.product_id_sub, |
---|
440 | T2.product_code, |
---|
441 | T2.price01, |
---|
442 | T2.price02, |
---|
443 | T2.stock, |
---|
444 | T2.stock_unlimited, |
---|
445 | T2.classcategory_id1, |
---|
446 | T2.classcategory_id2 |
---|
447 | FROM (SELECT * FROM dtb_products &&noncls_where&&) AS T1 LEFT JOIN |
---|
448 | (SELECT |
---|
449 | product_id AS product_id_sub, |
---|
450 | product_code, |
---|
451 | price01, |
---|
452 | price02, |
---|
453 | stock, |
---|
454 | stock_unlimited, |
---|
455 | classcategory_id1, |
---|
456 | classcategory_id2 |
---|
457 | FROM dtb_products_class WHERE classcategory_id1 = 0 AND classcategory_id2 = 0) |
---|
458 | AS T2 |
---|
459 | ON T1.product_id = T2.product_id_sub) ', |
---|
460 | |
---|
461 | "vw_products_allclass" => " |
---|
462 | ( |
---|
463 | SELECT |
---|
464 | alldtl.*, |
---|
465 | dtb_category.rank AS category_rank, |
---|
466 | T2.category_id, |
---|
467 | T2.rank AS product_rank |
---|
468 | FROM |
---|
469 | {$sql['vw_products_allclass_detail']} AS alldtl |
---|
470 | LEFT JOIN |
---|
471 | dtb_product_categories AS T2 |
---|
472 | ON alldtl.product_id = T2.product_id |
---|
473 | LEFT JOIN |
---|
474 | dtb_category |
---|
475 | ON T2.category_id = dtb_category.category_id |
---|
476 | ) ", |
---|
477 | |
---|
478 | "vw_products_allclass_detail" => $sql['vw_products_allclass_detail'], |
---|
479 | |
---|
480 | "vw_product_class" => ' |
---|
481 | (SELECT * FROM |
---|
482 | (SELECT T3.product_class_id, T3.product_id AS product_id_sub, classcategory_id1, classcategory_id2, |
---|
483 | T3.rank AS rank1, T4.rank AS rank2, T3.class_id AS class_id1, T4.class_id AS class_id2, |
---|
484 | stock, price01, price02, stock_unlimited, product_code |
---|
485 | FROM ( SELECT |
---|
486 | T1.product_class_id, |
---|
487 | T1.product_id, |
---|
488 | classcategory_id1, |
---|
489 | classcategory_id2, |
---|
490 | T2.rank, |
---|
491 | T2.class_id, |
---|
492 | stock, |
---|
493 | price01, |
---|
494 | price02, |
---|
495 | stock_unlimited, |
---|
496 | product_code |
---|
497 | FROM (dtb_products_class AS T1 LEFT JOIN dtb_classcategory AS T2 |
---|
498 | ON T1.classcategory_id1 = T2.classcategory_id)) |
---|
499 | AS T3 LEFT JOIN dtb_classcategory AS T4 |
---|
500 | ON T3.classcategory_id2 = T4.classcategory_id) AS T5 LEFT JOIN dtb_products AS T6 |
---|
501 | ON product_id_sub = T6.product_id) ', |
---|
502 | |
---|
503 | "vw_category_count" => ' |
---|
504 | (SELECT T1.category_id, T1.category_name, T1.parent_category_id, T1.level, T1.rank, T2.product_count |
---|
505 | FROM dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2 |
---|
506 | ON T1.category_id = T2.category_id) ' |
---|
507 | ); |
---|
508 | } |
---|
509 | } |
---|
510 | ?> |
---|