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 | return array( |
---|
275 | "vw_cross_class" => ' |
---|
276 | (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 |
---|
277 | FROM dtb_classcategory AS T1, dtb_classcategory AS T2 ) ', |
---|
278 | |
---|
279 | "vw_cross_products_class" =>' |
---|
280 | (SELECT T1.class_id1, T1.class_id2, T1.classcategory_id1, T1.classcategory_id2, T2.product_id, |
---|
281 | T1.name1, T1.name2, T2.product_code, T2.stock, T2.price01, T2.price02, T1.rank1, T1.rank2 |
---|
282 | 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 |
---|
283 | FROM dtb_classcategory AS T1, dtb_classcategory AS T2 ) AS T1 LEFT JOIN dtb_products_class AS T2 |
---|
284 | ON T1.classcategory_id1 = T2.classcategory_id1 AND T1.classcategory_id2 = T2.classcategory_id2) ', |
---|
285 | |
---|
286 | "vw_products_nonclass" => ' |
---|
287 | (SELECT |
---|
288 | T1.product_id, |
---|
289 | T1.name, |
---|
290 | T1.deliv_fee, |
---|
291 | T1.sale_limit, |
---|
292 | T1.sale_unlimited, |
---|
293 | T1.category_id, |
---|
294 | T1.rank, |
---|
295 | T1.status, |
---|
296 | T1.product_flag, |
---|
297 | T1.point_rate, |
---|
298 | T1.comment1, |
---|
299 | T1.comment2, |
---|
300 | T1.comment3, |
---|
301 | T1.comment4, |
---|
302 | T1.comment5, |
---|
303 | T1.comment6, |
---|
304 | T1.file1, |
---|
305 | T1.file2, |
---|
306 | T1.file3, |
---|
307 | T1.file4, |
---|
308 | T1.file5, |
---|
309 | T1.file6, |
---|
310 | T1.main_list_comment, |
---|
311 | T1.main_list_image, |
---|
312 | T1.main_comment, |
---|
313 | T1.main_image, |
---|
314 | T1.main_large_image, |
---|
315 | T1.sub_title1, |
---|
316 | T1.sub_comment1, |
---|
317 | T1.sub_image1, |
---|
318 | T1.sub_large_image1, |
---|
319 | T1.sub_title2, |
---|
320 | T1.sub_comment2, |
---|
321 | T1.sub_image2, |
---|
322 | T1.sub_large_image2, |
---|
323 | T1.sub_title3, |
---|
324 | T1.sub_comment3, |
---|
325 | T1.sub_image3, |
---|
326 | T1.sub_large_image3, |
---|
327 | T1.sub_title4, |
---|
328 | T1.sub_comment4, |
---|
329 | T1.sub_image4, |
---|
330 | T1.sub_large_image4, |
---|
331 | T1.sub_title5, |
---|
332 | T1.sub_comment5, |
---|
333 | T1.sub_image5, |
---|
334 | T1.sub_large_image5, |
---|
335 | T1.sub_title6, |
---|
336 | T1.sub_comment6, |
---|
337 | T1.sub_image6, |
---|
338 | T1.sub_large_image6, |
---|
339 | T1.del_flg, |
---|
340 | T1.creator_id, |
---|
341 | T1.create_date, |
---|
342 | T1.update_date, |
---|
343 | T1.deliv_date_id, |
---|
344 | T2.product_id_sub, |
---|
345 | T2.product_code, |
---|
346 | T2.price01, |
---|
347 | T2.price02, |
---|
348 | T2.stock, |
---|
349 | T2.stock_unlimited, |
---|
350 | T2.classcategory_id1, |
---|
351 | T2.classcategory_id2 |
---|
352 | FROM (SELECT * FROM dtb_products &&noncls_where&&) AS T1 LEFT JOIN |
---|
353 | (SELECT |
---|
354 | product_id AS product_id_sub, |
---|
355 | product_code, |
---|
356 | price01, |
---|
357 | price02, |
---|
358 | stock, |
---|
359 | stock_unlimited, |
---|
360 | classcategory_id1, |
---|
361 | classcategory_id2 |
---|
362 | FROM dtb_products_class WHERE classcategory_id1 = 0 AND classcategory_id2 = 0) |
---|
363 | AS T2 |
---|
364 | ON T1.product_id = T2.product_id_sub) ', |
---|
365 | |
---|
366 | "vw_products_allclass" => ' |
---|
367 | (SELECT T1.product_id, |
---|
368 | product_code_min, |
---|
369 | product_code_max, |
---|
370 | price01_min, |
---|
371 | price01_max, |
---|
372 | price02_min, |
---|
373 | price02_max, |
---|
374 | stock_min, |
---|
375 | stock_max, |
---|
376 | stock_unlimited_min, |
---|
377 | stock_unlimited_max, |
---|
378 | del_flg, |
---|
379 | status, |
---|
380 | name, |
---|
381 | comment1, |
---|
382 | comment2, |
---|
383 | comment3, |
---|
384 | main_list_comment, |
---|
385 | main_image, |
---|
386 | main_list_image, |
---|
387 | product_flag, |
---|
388 | deliv_date_id, |
---|
389 | sale_limit, |
---|
390 | point_rate, |
---|
391 | sale_unlimited, |
---|
392 | create_date, |
---|
393 | deliv_fee, |
---|
394 | rank |
---|
395 | ,(SELECT rank AS category_rank |
---|
396 | FROM dtb_category AS T4 |
---|
397 | WHERE T1.category_id = T4.category_id) as category_rank |
---|
398 | ,(SELECT category_id AS sub_category_id |
---|
399 | FROM dtb_category T4 |
---|
400 | WHERE T1.category_id = T4.category_id) as category_id |
---|
401 | FROM (SELECT T0.product_id, |
---|
402 | T0.del_flg, |
---|
403 | T0.status, |
---|
404 | T0.name, |
---|
405 | T0.comment1, |
---|
406 | T0.comment2, |
---|
407 | T0.comment3, |
---|
408 | T0.main_list_comment, |
---|
409 | T0.main_image, |
---|
410 | T0.main_list_image, |
---|
411 | T0.product_flag, |
---|
412 | T0.deliv_date_id, |
---|
413 | T0.sale_limit, |
---|
414 | T0.point_rate, |
---|
415 | T0.sale_unlimited, |
---|
416 | T0.create_date, |
---|
417 | T0.deliv_fee, |
---|
418 | T00.category_id, |
---|
419 | T00.rank |
---|
420 | FROM dtb_products AS T0 |
---|
421 | LEFT JOIN dtb_product_categories AS T00 |
---|
422 | USING (product_id)) AS T1 |
---|
423 | RIGHT JOIN (SELECT product_id as product_id_sub, |
---|
424 | MIN(product_code) AS product_code_min, |
---|
425 | MAX(product_code) AS product_code_max, |
---|
426 | MIN(price01) AS price01_min, |
---|
427 | MAX(price01) AS price01_max, |
---|
428 | MIN(price02) AS price02_min, |
---|
429 | MAX(price02) AS price02_max, |
---|
430 | MIN(stock) AS stock_min, |
---|
431 | MAX(stock) AS stock_max, |
---|
432 | MIN(stock_unlimited) AS stock_unlimited_min, |
---|
433 | MAX(stock_unlimited) AS stock_unlimited_max |
---|
434 | FROM dtb_products_class GROUP BY product_id) AS T2 |
---|
435 | ON T1.product_id = T2.product_id_sub |
---|
436 | ) ', |
---|
437 | |
---|
438 | "vw_products_allclass_detail" => ' |
---|
439 | (SELECT product_id,price01_min,price01_max,price02_min,price02_max,stock_min,stock_max,stock_unlimited_min,stock_unlimited_max, |
---|
440 | del_flg,status,name,comment1,comment2,comment3,deliv_fee,main_comment,main_image,main_large_image, |
---|
441 | sub_title1,sub_comment1,sub_image1,sub_large_image1, |
---|
442 | sub_title2,sub_comment2,sub_image2,sub_large_image2, |
---|
443 | sub_title3,sub_comment3,sub_image3,sub_large_image3, |
---|
444 | sub_title4,sub_comment4,sub_image4,sub_large_image4, |
---|
445 | sub_title5,sub_comment5,sub_image5,sub_large_image5, |
---|
446 | product_flag,deliv_date_id,sale_limit,point_rate,sale_unlimited,file1,file2,category_id |
---|
447 | FROM ( SELECT * FROM (dtb_products AS T1 RIGHT JOIN |
---|
448 | (SELECT |
---|
449 | product_id AS product_id_sub, |
---|
450 | MIN(price01) AS price01_min, |
---|
451 | MAX(price01) AS price01_max, |
---|
452 | MIN(price02) AS price02_min, |
---|
453 | MAX(price02) AS price02_max, |
---|
454 | MIN(stock) AS stock_min, |
---|
455 | MAX(stock) AS stock_max, |
---|
456 | MIN(stock_unlimited) AS stock_unlimited_min, |
---|
457 | MAX(stock_unlimited) AS stock_unlimited_max |
---|
458 | FROM dtb_products_class GROUP BY product_id) AS T2 |
---|
459 | ON T1.product_id = T2.product_id_sub ) ) AS T3 LEFT JOIN (SELECT rank AS category_rank, category_id AS sub_category_id FROM dtb_category) AS T4 |
---|
460 | ON T3.category_id = T4.sub_category_id) ', |
---|
461 | |
---|
462 | "vw_product_class" => ' |
---|
463 | (SELECT * FROM |
---|
464 | (SELECT T3.product_class_id, T3.product_id AS product_id_sub, classcategory_id1, classcategory_id2, |
---|
465 | T3.rank AS rank1, T4.rank AS rank2, T3.class_id AS class_id1, T4.class_id AS class_id2, |
---|
466 | stock, price01, price02, stock_unlimited, product_code |
---|
467 | FROM ( SELECT |
---|
468 | T1.product_class_id, |
---|
469 | T1.product_id, |
---|
470 | classcategory_id1, |
---|
471 | classcategory_id2, |
---|
472 | T2.rank, |
---|
473 | T2.class_id, |
---|
474 | stock, |
---|
475 | price01, |
---|
476 | price02, |
---|
477 | stock_unlimited, |
---|
478 | product_code |
---|
479 | FROM (dtb_products_class AS T1 LEFT JOIN dtb_classcategory AS T2 |
---|
480 | ON T1.classcategory_id1 = T2.classcategory_id)) |
---|
481 | AS T3 LEFT JOIN dtb_classcategory AS T4 |
---|
482 | ON T3.classcategory_id2 = T4.classcategory_id) AS T5 LEFT JOIN dtb_products AS T6 |
---|
483 | ON product_id_sub = T6.product_id) ', |
---|
484 | |
---|
485 | "vw_category_count" => ' |
---|
486 | (SELECT T1.category_id, T1.category_name, T1.parent_category_id, T1.level, T1.rank, T2.product_count |
---|
487 | FROM dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2 |
---|
488 | ON T1.category_id = T2.category_id) ' |
---|
489 | ); |
---|
490 | } |
---|
491 | } |
---|
492 | ?> |
---|