source: branches/version-2_4/data/class/db/dbfactory/SC_DB_DBFactory_MYSQL.php @ 17925

Revision 17925, 18.2 KB checked in by kotani, 15 years ago (diff)

MySQLでの商品表示高速化対応(商品詳細ページのみ)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id Revision Date
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
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
25require_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$
37 */
38class 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     * View をインラインビューに変換する.
174     *
175     * @access private
176     * @param string $sql SQL 文
177     * @return string インラインビューに変換した SQL 文
178     */
179    function sfChangeView($sql){
180
181        $arrViewTmp = $this->viewToSubQuery();
182
183        // viewのwhereを変換
184        foreach($arrViewTmp as $key => $val){
185            $arrViewTmp[$key] = strtr($arrViewTmp[$key], $this->getWhereConverter());
186        }
187
188        // viewを変換
189        $changesql = strtr($sql, $arrViewTmp);
190
191        return $changesql;
192    }
193
194    /**
195     * ILIKE句 を LIKE句へ変換する.
196     *
197     * @access private
198     * @param string $sql SQL文
199     * @return string 変換後の SQL 文
200     */
201    function sfChangeILIKE($sql){
202        $changesql = eregi_replace("(ILIKE )", "LIKE ", $sql);
203        return $changesql;
204    }
205
206    /**
207     * RANDOM() を RAND() に変換する.
208     *
209     * @access private
210     * @param string $sql SQL文
211     * @return string 変換後の SQL 文
212     */
213    function sfChangeRANDOM($sql){
214        $changesql = eregi_replace("( RANDOM)", " RAND", $sql);
215        return $changesql;
216    }
217
218    /**
219     * TRUNC() を TRUNCATE() に変換する.
220     *
221     * @access private
222     * @param string $sql SQL文
223     * @return string 変換後の SQL 文
224     */
225    function sfChangeTrunc($sql){
226        $changesql = eregi_replace("( TRUNC)", " TRUNCATE", $sql);
227        return $changesql;
228    }
229
230    /**
231     * WHERE 句置換用の配列を返す.
232     *
233     * @access private
234     * @return array WHERE 句置換用の配列
235     */
236    function getWhereConverter() {
237        return array(
238            "&&crscls_where&&" => "",
239            "&&crsprdcls_where&&" =>"",
240            "&&noncls_where&&" => "",
241            "&&allcls_where&&" => "",
242            "&&allclsdtl_where&&" => "",
243            "&&prdcls_where&&" => "",
244            "&&catcnt_where&&" => ""
245        );
246    }
247
248    /**
249     * View をサブクエリに変換するための配列を返す.
250     *
251     * @access private
252     * @return array View をサブクエリに変換するための配列
253     */
254    function viewToSubQuery() {
255        return array(
256            "vw_cross_class" => '
257                (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
258                FROM dtb_classcategory AS T1, dtb_classcategory AS T2 ) ',
259
260            "vw_cross_products_class" =>'
261                (SELECT T1.class_id1, T1.class_id2, T1.classcategory_id1, T1.classcategory_id2, T2.product_id,
262                T1.name1, T1.name2, T2.product_code, T2.stock, T2.price01, T2.price02, T1.rank1, T1.rank2
263                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
264                FROM dtb_classcategory AS T1, dtb_classcategory AS T2 ) AS T1 LEFT JOIN dtb_products_class AS T2
265                ON T1.classcategory_id1 = T2.classcategory_id1 AND T1.classcategory_id2 = T2.classcategory_id2) ',
266
267            "vw_products_nonclass" => '
268                (SELECT
269                    T1.product_id,
270                    T1.name,
271                    T1.deliv_fee,
272                    T1.sale_limit,
273                    T1.sale_unlimited,
274                    T1.category_id,
275                    T1.rank,
276                    T1.status,
277                    T1.product_flag,
278                    T1.point_rate,
279                    T1.comment1,
280                    T1.comment2,
281                    T1.comment3,
282                    T1.comment4,
283                    T1.comment5,
284                    T1.comment6,
285                    T1.file1,
286                    T1.file2,
287                    T1.file3,
288                    T1.file4,
289                    T1.file5,
290                    T1.file6,
291                    T1.main_list_comment,
292                    T1.main_list_image,
293                    T1.main_comment,
294                    T1.main_image,
295                    T1.main_large_image,
296                    T1.sub_title1,
297                    T1.sub_comment1,
298                    T1.sub_image1,
299                    T1.sub_large_image1,
300                    T1.sub_title2,
301                    T1.sub_comment2,
302                    T1.sub_image2,
303                    T1.sub_large_image2,
304                    T1.sub_title3,
305                    T1.sub_comment3,
306                    T1.sub_image3,
307                    T1.sub_large_image3,
308                    T1.sub_title4,
309                    T1.sub_comment4,
310                    T1.sub_image4,
311                    T1.sub_large_image4,
312                    T1.sub_title5,
313                    T1.sub_comment5,
314                    T1.sub_image5,
315                    T1.sub_large_image5,
316                    T1.sub_title6,
317                    T1.sub_comment6,
318                    T1.sub_image6,
319                    T1.sub_large_image6,
320                    T1.del_flg,
321                    T1.creator_id,
322                    T1.create_date,
323                    T1.update_date,
324                    T1.note,
325                    T1.deliv_date_id,
326                    T2.product_id_sub,
327                    T2.product_code,
328                    T2.price01,
329                    T2.price02,
330                    T2.stock,
331                    T2.stock_unlimited,
332                    T2.classcategory_id1,
333                    T2.classcategory_id2
334                FROM (SELECT * FROM dtb_products &&noncls_where&&) AS T1 LEFT JOIN
335                (SELECT
336                product_id AS product_id_sub,
337                product_code,
338                price01,
339                price02,
340                stock,
341                stock_unlimited,
342                classcategory_id1,
343                classcategory_id2
344                FROM dtb_products_class WHERE classcategory_id1 = 0 AND classcategory_id2 = 0)
345                AS T2
346                ON T1.product_id = T2.product_id_sub) ',
347
348            "vw_products_allclass" => '
349   (SELECT T1.product_id,
350           product_code_min,
351           product_code_max,
352           price01_min,
353           price01_max,
354           price02_min,
355           price02_max,
356           stock_min,
357           stock_max,
358           stock_unlimited_min,
359           stock_unlimited_max,
360           del_flg,
361           status,
362           name,
363           comment1,
364           comment2,
365           comment3,
366           main_list_comment,
367           main_image,
368           main_list_image,
369           product_flag,
370           deliv_date_id,
371           sale_limit,
372           point_rate,
373           sale_unlimited,
374           create_date,
375           deliv_fee,
376           rank
377           ,(SELECT rank AS category_rank
378               FROM dtb_category AS T4
379              WHERE T1.category_id = T4.category_id) as category_rank
380           ,(SELECT category_id AS sub_category_id
381               FROM dtb_category T4
382              WHERE T1.category_id = T4.category_id) as category_id
383      FROM (SELECT T0.product_id,
384                   T0.del_flg,
385                   T0.status,
386                   T0.name,
387                   T0.comment1,
388                   T0.comment2,
389                   T0.comment3,
390                   T0.main_list_comment,
391                   T0.main_image,
392                   T0.main_list_image,
393                   T0.product_flag,
394                   T0.deliv_date_id,
395                   T0.sale_limit,
396                   T0.point_rate,
397                   T0.sale_unlimited,
398                   T0.create_date,
399                   T0.deliv_fee,
400                   T00.category_id,
401                   T00.rank
402              FROM dtb_products AS T0
403         LEFT JOIN dtb_product_categories AS T00
404             USING (product_id)) AS T1
405RIGHT JOIN (SELECT product_id as product_id_sub,
406                   MIN(product_code) AS product_code_min,
407                   MAX(product_code) AS product_code_max,
408                   MIN(price01) AS price01_min,
409                   MAX(price01) AS price01_max,
410                   MIN(price02) AS price02_min,
411                   MAX(price02) AS price02_max,
412                   MIN(stock) AS stock_min,
413                   MAX(stock) AS stock_max,
414                   MIN(stock_unlimited) AS stock_unlimited_min,
415                   MAX(stock_unlimited) AS stock_unlimited_max
416              FROM dtb_products_class GROUP BY product_id) AS T2
417                ON T1.product_id = T2.product_id_sub
418            ) ',
419
420            "vw_products_allclass_detail" => '
421        (SELECT
422            dtb_products.product_id,
423            T4.price01_min,
424            T4.price01_max,
425            T4.price02_min,
426            T4.price02_max,
427            T4.stock_min,
428            T4.stock_max,
429            T4.stock_unlimited_min,
430            T4.stock_unlimited_max,
431            dtb_products.del_flg,
432            dtb_products.status,
433            dtb_products.name,
434            dtb_products.comment1,
435            dtb_products.comment2,
436            dtb_products.comment3,
437            dtb_products.deliv_fee,
438            dtb_products.main_comment,
439            dtb_products.main_image,
440            dtb_products.main_large_image,
441            dtb_products.sub_title1,
442            dtb_products.sub_comment1,
443            dtb_products.sub_image1,
444            dtb_products.sub_large_image1,
445            dtb_products.sub_title2,
446            dtb_products.sub_comment2,
447            dtb_products.sub_image2,
448            dtb_products.sub_large_image2,
449            dtb_products.sub_title3,
450            dtb_products.sub_comment3,
451            dtb_products.sub_image3,
452            dtb_products.sub_large_image3,
453            dtb_products.sub_title4,
454            dtb_products.sub_comment4,
455            dtb_products.sub_image4,
456            dtb_products.sub_large_image4,
457            dtb_products.sub_title5,
458            dtb_products.sub_comment5,
459            dtb_products.sub_image5,
460            dtb_products.sub_large_image5,
461            dtb_products.product_flag,
462            dtb_products.deliv_date_id,
463            dtb_products.sale_limit,
464            dtb_products.point_rate,
465            dtb_products.sale_unlimited,
466            dtb_products.file1,
467            dtb_products.file2
468        FROM
469            dtb_products
470            LEFT JOIN
471                (
472                    SELECT
473                        product_id,
474                        MIN(product_code) AS product_code_min,
475                        MAX(product_code) AS product_code_max,
476                        MIN(price01) AS price01_min,
477                        MAX(price01) AS price01_max,
478                        MIN(price02) AS price02_min,
479                        MAX(price02) AS price02_max,
480                        MIN(stock) AS stock_min,
481                        MAX(stock) AS stock_max,
482                        MIN(stock_unlimited) AS stock_unlimited_min,
483                        MAX(stock_unlimited) AS stock_unlimited_max,
484                        COUNT(*) as class_count
485                    FROM dtb_products_class
486                    GROUP BY product_id
487                ) AS T4
488                ON dtb_products.product_id = T4.product_id
489        ) ',
490
491            "vw_product_class" => '
492                (SELECT * FROM
493                (SELECT T3.product_class_id, T3.product_id AS product_id_sub, classcategory_id1, classcategory_id2,
494                T3.rank AS rank1, T4.rank AS rank2, T3.class_id AS class_id1, T4.class_id AS class_id2,
495                stock, price01, price02, stock_unlimited, product_code
496                FROM ( SELECT
497                        T1.product_class_id,
498                        T1.product_id,
499                        classcategory_id1,
500                        classcategory_id2,
501                        T2.rank,
502                        T2.class_id,
503                        stock,
504                        price01,
505                        price02,
506                        stock_unlimited,
507                        product_code
508                 FROM (dtb_products_class AS T1 LEFT JOIN dtb_classcategory AS T2
509                ON T1.classcategory_id1 = T2.classcategory_id))
510                AS T3 LEFT JOIN dtb_classcategory AS T4
511                ON T3.classcategory_id2 = T4.classcategory_id) AS T5 LEFT JOIN dtb_products AS T6
512                ON product_id_sub = T6.product_id) ',
513
514            "vw_category_count" => '
515                (SELECT T1.category_id, T1.category_name, T1.parent_category_id, T1.level, T1.rank, T2.product_count
516                FROM dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2
517                ON T1.category_id = T2.category_id) '
518        );
519    }
520}
521?>
Note: See TracBrowser for help on using the repository browser.