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

Revision 17939, 17.6 KB checked in by kajiwara, 15 years ago (diff)

#436 MySQLの高速化対応。MySQL4.1 MySQL5 で検証済。

  • 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:SC_DB_DBFactory_MYSQL.php 15267 2007-08-09 12:31:52Z nanasess $
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     * 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.note,
344                    T1.deliv_date_id,
345                    T2.product_id_sub,
346                    T2.product_code,
347                    T2.price01,
348                    T2.price02,
349                    T2.stock,
350                    T2.stock_unlimited,
351                    T2.classcategory_id1,
352                    T2.classcategory_id2
353                FROM (SELECT * FROM dtb_products &&noncls_where&&) AS T1 LEFT JOIN
354                (SELECT
355                product_id AS product_id_sub,
356                product_code,
357                price01,
358                price02,
359                stock,
360                stock_unlimited,
361                classcategory_id1,
362                classcategory_id2
363                FROM dtb_products_class WHERE classcategory_id1 = 0 AND classcategory_id2 = 0)
364                AS T2
365                ON T1.product_id = T2.product_id_sub) ',
366
367        "vw_products_allclass" => '
368               (SELECT
369                    T2.product_id
370                    ,T1.product_code_min
371                    ,T1.product_code_max
372                    ,T1.price01_min
373                    ,T1.price01_max
374                    ,T1.price02_min
375                    ,T1.price02_max
376                    ,T1.stock_min
377                    ,T1.stock_max
378                    ,T1.stock_unlimited_min
379                    ,T1.stock_unlimited_max
380                    ,T2.del_flg
381                    ,T2.status
382                    ,T2.name
383                    ,T2.comment1
384                    ,T2.comment2
385                    ,T2.comment3
386                    ,T2.main_list_comment
387                    ,T2.main_image
388                    ,T2.main_list_image
389                    ,T2.product_flag
390                    ,T2.deliv_date_id
391                    ,T2.sale_limit
392                    ,T2.point_rate
393                    ,T2.sale_unlimited
394                    ,T2.create_date
395                    ,T2.deliv_fee
396                    ,T3.rank
397                    ,T4.rank AS category_rank
398                    ,T4.category_id
399                FROM
400                    (
401                        (dtb_products AS T2 RIGHT JOIN
402                            (SELECT
403                                product_id AS product_id_sub
404                                ,MIN(product_code) AS product_code_min
405                                ,MAX(product_code) AS product_code_max
406                                ,MIN(price01) AS price01_min
407                                ,MAX(price01) AS price01_max
408                                ,MIN(price02) AS price02_min
409                                ,MAX(price02) AS price02_max
410                                ,MIN(stock) AS stock_min
411                                ,MAX(stock) AS stock_max
412                                ,MIN(stock_unlimited) AS stock_unlimited_min
413                                ,MAX(stock_unlimited) AS stock_unlimited_max
414                            FROM dtb_products_class GROUP BY product_id
415                            ) AS T1 ON T1.product_id_sub = T2.product_id
416                        ) LEFT JOIN dtb_product_categories AS T3 ON T2.product_id = T3.product_id
417                    ) LEFT JOIN dtb_category AS T4 ON T3.category_id = T4.category_id
418                ) ',
419
420            "vw_products_allclass_detail" => '
421                (SELECT product_id,price01_min,price01_max,price02_min,price02_max,stock_min,stock_max,stock_unlimited_min,stock_unlimited_max,
422                del_flg,status,name,comment1,comment2,comment3,deliv_fee,main_comment,main_image,main_large_image,
423                sub_title1,sub_comment1,sub_image1,sub_large_image1,
424                sub_title2,sub_comment2,sub_image2,sub_large_image2,
425                sub_title3,sub_comment3,sub_image3,sub_large_image3,
426                sub_title4,sub_comment4,sub_image4,sub_large_image4,
427                sub_title5,sub_comment5,sub_image5,sub_large_image5,
428                product_flag,deliv_date_id,sale_limit,point_rate,sale_unlimited,file1,file2,category_id
429                FROM ( SELECT * FROM (dtb_products AS T1 RIGHT JOIN
430                (SELECT
431                product_id AS product_id_sub,
432                MIN(price01) AS price01_min,
433                MAX(price01) AS price01_max,
434                MIN(price02) AS price02_min,
435                MAX(price02) AS price02_max,
436                MIN(stock) AS stock_min,
437                MAX(stock) AS stock_max,
438                MIN(stock_unlimited) AS stock_unlimited_min,
439                MAX(stock_unlimited) AS stock_unlimited_max
440                FROM dtb_products_class GROUP BY product_id) AS T2
441                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
442                ON T3.category_id = T4.sub_category_id) ',
443
444            "vw_product_class" => '
445                (SELECT * FROM
446                (SELECT T3.product_class_id, T3.product_id AS product_id_sub, classcategory_id1, classcategory_id2,
447                T3.rank AS rank1, T4.rank AS rank2, T3.class_id AS class_id1, T4.class_id AS class_id2,
448                stock, price01, price02, stock_unlimited, product_code
449                FROM ( SELECT
450                        T1.product_class_id,
451                        T1.product_id,
452                        classcategory_id1,
453                        classcategory_id2,
454                        T2.rank,
455                        T2.class_id,
456                        stock,
457                        price01,
458                        price02,
459                        stock_unlimited,
460                        product_code
461                 FROM (dtb_products_class AS T1 LEFT JOIN dtb_classcategory AS T2
462                ON T1.classcategory_id1 = T2.classcategory_id))
463                AS T3 LEFT JOIN dtb_classcategory AS T4
464                ON T3.classcategory_id2 = T4.classcategory_id) AS T5 LEFT JOIN dtb_products AS T6
465                ON product_id_sub = T6.product_id) ',
466
467            "vw_category_count" => '
468                (SELECT T1.category_id, T1.category_name, T1.parent_category_id, T1.level, T1.rank, T2.product_count
469                FROM dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2
470                ON T1.category_id = T2.category_id) '
471        );
472    }
473}
474?>
Note: See TracBrowser for help on using the repository browser.