source: branches/comu-ver2/data/class/db/dbfactory/SC_DB_DBFactory_MYSQL.php @ 17549

Revision 17549, 17.7 KB checked in by Seasoft, 16 years ago (diff)

・VIEWを改訂。簡素化と高速化を目論む。(従来互換を考慮していますが、影響範囲が大きいので不具合もあるかと思います。また、MySQL v4系は、環境が無いため未テストです。バグレポートをお願いします。)
r17509 の不具合対応。
r17548 の応急処置を正式に対応。
・[検索結果をすべて削除]でエラーが発生する不具合を修正。

  • 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        return $sql;
70    }
71   
72    /**
73     * 文字コード情報を取得する
74     *
75     * @return array 文字コード情報
76     */
77     function getCharSet() {
78        $objQuery = new SC_Query();
79        $arrRet = $objQuery->getAll("SHOW VARIABLES LIKE 'char%'");
80        return $arrRet;
81     }
82   
83    /**
84     * テーブルの存在チェックを行う SQL 文を返す.
85     *
86     * @return string テーブルの存在チェックを行う SQL 文
87     */
88    function getTableExistsSql() {
89        return "SHOW TABLE STATUS LIKE ?";
90    }
91
92    /**
93     * インデックスの検索結果を配列で返す.
94     *
95     * @param string $index_name インデックス名
96     * @param string $table_name テーブル名
97     * @return array インデックスの検索結果の配列
98     */
99    function getTableIndex($index_name, $table_name = "") {
100        $objQuery = new SC_Query("", true, true);
101        return $objQuery->getAll("SHOW INDEX FROM " . $table_name . " WHERE Key_name = ?",
102                                 array($index_name));
103    }
104
105    /**
106     * インデックスを作成する.
107     *
108     * @param string $index_name インデックス名
109     * @param string $table_name テーブル名
110     * @param string $col_name カラム名
111     * @param integer $length 作成するインデックスのバイト長
112     * @return void
113     */
114    function createTableIndex($index_name, $table_name, $col_name, $length = 0) {
115        $objQuery = new SC_Query($dsn, true, true);
116        $objQuery->query("CREATE INDEX ? ON ? (?(?))", array($index_name, $table_name, $col_name, $length));
117    }
118
119    /**
120     * テーブルのカラム一覧を取得する.
121     *
122     * @param string $table_name テーブル名
123     * @return array テーブルのカラム一覧の配列
124     */
125    function sfGetColumnList($table_name) {
126        $objQuery = new SC_Query();
127        $sql = "SHOW COLUMNS FROM " . $table_name;
128        $arrColList = $objQuery->getAll($sql);
129        $arrColList = SC_Utils_Ex::sfswaparray($arrColList);
130        return $arrColList["Field"];
131    }
132
133    /**
134     * テーブルを検索する.
135     *
136     * 引数に部分一致するテーブル名を配列で返す.
137     *
138     * @param string $expression 検索文字列
139     * @return array テーブル名の配列
140     */
141    function findTableNames($expression = "") {
142        $objQuery = new SC_Query();
143        $sql = "SHOW TABLES LIKE ?";
144        $arrColList = $objQuery->getAll($sql, array("%" . $expression . "%"));
145        $arrColList = SC_Utils_Ex::sfswaparray($arrColList, false);
146        return $arrColList[0];
147    }
148
149    /**
150     * View の WHERE 句を置換する.
151     *
152     * @param string $target 置換対象の文字列
153     * @param string $where 置換する文字列
154     * @param array $arrval WHERE 句の要素の配列
155     * @param string $option SQL 文の追加文字列
156     * @return string 置換後の SQL 文
157     */
158    function sfViewWhere($target, $where = "", $arrval = array(), $option = ""){
159
160        $arrWhere = split("[?]", $where);
161        $where_tmp = " WHERE " . $arrWhere[0];
162        for($i = 1; $i < count($arrWhere); $i++){
163            $where_tmp .= SC_Utils_Ex::sfQuoteSmart($arrval[$i - 1]) . $arrWhere[$i];
164        }
165        $arrWhere = $this->getWhereConverter();
166        $arrWhere[$target] = $where_tmp . " " . $option;
167        return $arrWhere[$target];
168    }
169
170    /**
171     * View をインラインビューに変換する.
172     *
173     * @access private
174     * @param string $sql SQL 文
175     * @return string インラインビューに変換した SQL 文
176     */
177    function sfChangeView($sql){
178       
179        $arrViewTmp = $this->viewToSubQuery();
180       
181            // viewのwhereを変換
182        foreach($arrViewTmp as $key => $val){
183            $arrViewTmp[$key] = strtr($arrViewTmp[$key], $this->getWhereConverter());
184        }
185           
186            // viewを変換
187        $changesql = strtr($sql, $arrViewTmp);
188       
189        return $changesql;
190    }
191
192    /**
193     * ILIKE句 を LIKE句へ変換する.
194     *
195     * @access private
196     * @param string $sql SQL文
197     * @return string 変換後の SQL 文
198     */
199    function sfChangeILIKE($sql){
200        $changesql = eregi_replace("(ILIKE )", "LIKE BINARY ", $sql);
201        return $changesql;
202    }
203
204    /**
205     * RANDOM() を RAND() に変換する.
206     *
207     * @access private
208     * @param string $sql SQL文
209     * @return string 変換後の SQL 文
210     */
211    function sfChangeRANDOM($sql){
212        $changesql = eregi_replace("( RANDOM)", " RAND", $sql);
213        return $changesql;
214    }
215
216    /**
217     * WHERE 句置換用の配列を返す.
218     *
219     * @access private
220     * @return array WHERE 句置換用の配列
221     */
222    function getWhereConverter() {
223        return array(
224            "&&crscls_where&&" => "",
225            "&&crsprdcls_where&&" =>"",
226            "&&noncls_where&&" => "",
227            "&&allcls_where&&" => "",
228            "&&allclsdtl_where&&" => "",
229            "&&prdcls_where&&" => "",
230            "&&catcnt_where&&" => ""
231        );
232    }
233
234    /**
235     * View をサブクエリに変換するための配列を返す.
236     *
237     * @access private
238     * @return array View をサブクエリに変換するための配列
239     */
240    function viewToSubQuery() {
241        $sql['vw_products_allclass_detail'] =<<< __EOS__
242            (
243                SELECT
244                    dtb_products.product_id,
245                    dtb_products.name,
246                    dtb_products.deliv_fee,
247                    dtb_products.sale_limit,
248                    dtb_products.sale_unlimited,
249                    dtb_products.maker_id,
250                    dtb_products.rank,
251                    dtb_products.status,
252                    dtb_products.product_flag,
253                    dtb_products.point_rate,
254                    dtb_products.comment1,
255                    dtb_products.comment2,
256                    dtb_products.comment3,
257                    dtb_products.comment4,
258                    dtb_products.comment5,
259                    dtb_products.comment6,
260                    dtb_products.note,
261                    dtb_products.file1,
262                    dtb_products.file2,
263                    dtb_products.file3,
264                    dtb_products.file4,
265                    dtb_products.file5,
266                    dtb_products.file6,
267                    dtb_products.main_list_comment,
268                    dtb_products.main_list_image,
269                    dtb_products.main_comment,
270                    dtb_products.main_image,
271                    dtb_products.main_large_image,
272                    dtb_products.sub_title1,
273                    dtb_products.sub_comment1,
274                    dtb_products.sub_image1,
275                    dtb_products.sub_large_image1,
276                    dtb_products.sub_title2,
277                    dtb_products.sub_comment2,
278                    dtb_products.sub_image2,
279                    dtb_products.sub_large_image2,
280                    dtb_products.sub_title3,
281                    dtb_products.sub_comment3,
282                    dtb_products.sub_image3,
283                    dtb_products.sub_large_image3,
284                    dtb_products.sub_title4,
285                    dtb_products.sub_comment4,
286                    dtb_products.sub_image4,
287                    dtb_products.sub_large_image4,
288                    dtb_products.sub_title5,
289                    dtb_products.sub_comment5,
290                    dtb_products.sub_image5,
291                    dtb_products.sub_large_image5,
292                    dtb_products.sub_title6,
293                    dtb_products.sub_comment6,
294                    dtb_products.sub_image6,
295                    dtb_products.sub_large_image6,
296                    dtb_products.del_flg,
297                    dtb_products.creator_id,
298                    dtb_products.create_date,
299                    dtb_products.update_date,
300                    dtb_products.deliv_date_id,
301                    T4.product_code_min,
302                    T4.product_code_max,
303                    T4.price01_min,
304                    T4.price01_max,
305                    T4.price02_min,
306                    T4.price02_max,
307                    T4.stock_min,
308                    T4.stock_max,
309                    T4.stock_unlimited_min,
310                    T4.stock_unlimited_max,
311                    T4.class_count
312                FROM
313                    dtb_products
314                    LEFT JOIN
315                        (
316                            SELECT
317                                product_id,
318                                MIN(product_code) AS product_code_min,
319                                MAX(product_code) AS product_code_max,
320                                MIN(price01) AS price01_min,
321                                MAX(price01) AS price01_max,
322                                MIN(price02) AS price02_min,
323                                MAX(price02) AS price02_max,
324                                MIN(stock) AS stock_min,
325                                MAX(stock) AS stock_max,
326                                MIN(stock_unlimited) AS stock_unlimited_min,
327                                MAX(stock_unlimited) AS stock_unlimited_max,
328                                COUNT(*) as class_count
329                            FROM dtb_products_class
330                            GROUP BY product_id
331                        ) AS T4
332                        ON dtb_products.product_id = T4.product_id
333            )
334__EOS__;
335
336        return array(
337            "vw_cross_class" => '
338                (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
339                FROM dtb_classcategory AS T1, dtb_classcategory AS T2 ) ',
340
341            "vw_cross_products_class" =>'
342                (SELECT T1.class_id1, T1.class_id2, T1.classcategory_id1, T1.classcategory_id2, T2.product_id,
343                T1.name1, T1.name2, T2.product_code, T2.stock, T2.price01, T2.price02, T1.rank1, T1.rank2
344                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
345                FROM dtb_classcategory AS T1, dtb_classcategory AS T2 ) AS T1 LEFT JOIN dtb_products_class AS T2
346                ON T1.classcategory_id1 = T2.classcategory_id1 AND T1.classcategory_id2 = T2.classcategory_id2) ',
347
348            "vw_products_nonclass" => '
349                (SELECT
350                    T1.product_id,
351                    T1.name,
352                    T1.deliv_fee,
353                    T1.sale_limit,
354                    T1.sale_unlimited,
355                    T1.category_id,
356                    T1.rank,
357                    T1.status,
358                    T1.product_flag,
359                    T1.point_rate,
360                    T1.comment1,
361                    T1.comment2,
362                    T1.comment3,
363                    T1.comment4,
364                    T1.comment5,
365                    T1.comment6,
366                    T1.file1,
367                    T1.file2,
368                    T1.file3,
369                    T1.file4,
370                    T1.file5,
371                    T1.file6,
372                    T1.main_list_comment,
373                    T1.main_list_image,
374                    T1.main_comment,
375                    T1.main_image,
376                    T1.main_large_image,
377                    T1.sub_title1,
378                    T1.sub_comment1,
379                    T1.sub_image1,
380                    T1.sub_large_image1,
381                    T1.sub_title2,
382                    T1.sub_comment2,
383                    T1.sub_image2,
384                    T1.sub_large_image2,
385                    T1.sub_title3,
386                    T1.sub_comment3,
387                    T1.sub_image3,
388                    T1.sub_large_image3,
389                    T1.sub_title4,
390                    T1.sub_comment4,
391                    T1.sub_image4,
392                    T1.sub_large_image4,
393                    T1.sub_title5,
394                    T1.sub_comment5,
395                    T1.sub_image5,
396                    T1.sub_large_image5,
397                    T1.sub_title6,
398                    T1.sub_comment6,
399                    T1.sub_image6,
400                    T1.sub_large_image6,
401                    T1.del_flg,
402                    T1.creator_id,
403                    T1.create_date,
404                    T1.update_date,
405                    T1.deliv_date_id,
406                    T2.product_id_sub,
407                    T2.product_code,
408                    T2.price01,
409                    T2.price02,
410                    T2.stock,
411                    T2.stock_unlimited,
412                    T2.classcategory_id1,
413                    T2.classcategory_id2
414                FROM (SELECT * FROM dtb_products &&noncls_where&&) AS T1 LEFT JOIN
415                (SELECT
416                product_id AS product_id_sub,
417                product_code,
418                price01,
419                price02,
420                stock,
421                stock_unlimited,
422                classcategory_id1,
423                classcategory_id2
424                FROM dtb_products_class WHERE classcategory_id1 = 0 AND classcategory_id2 = 0)
425                AS T2
426                ON T1.product_id = T2.product_id_sub) ',
427
428            "vw_products_allclass" => "
429            (
430                SELECT
431                    alldtl.*,
432                    dtb_category.rank AS category_rank,
433                    T2.category_id
434                FROM
435                    {$sql['vw_products_allclass_detail']} AS alldtl
436                    LEFT JOIN
437                        dtb_product_categories AS T2
438                        ON alldtl.product_id = T2.product_id
439                    LEFT JOIN
440                        dtb_category
441                        ON T2.category_id = dtb_category.category_id
442            ) ",
443
444            "vw_products_allclass_detail" => $sql['vw_products_allclass_detail'],
445
446            "vw_product_class" => '
447                (SELECT * FROM
448                (SELECT T3.product_class_id, T3.product_id AS product_id_sub, classcategory_id1, classcategory_id2,
449                T3.rank AS rank1, T4.rank AS rank2, T3.class_id AS class_id1, T4.class_id AS class_id2,
450                stock, price01, price02, stock_unlimited, product_code
451                FROM ( SELECT
452                        T1.product_class_id,
453                        T1.product_id,
454                        classcategory_id1,
455                        classcategory_id2,
456                        T2.rank,
457                        T2.class_id,
458                        stock,
459                        price01,
460                        price02,
461                        stock_unlimited,
462                        product_code
463                 FROM (dtb_products_class AS T1 LEFT JOIN dtb_classcategory AS T2
464                ON T1.classcategory_id1 = T2.classcategory_id))
465                AS T3 LEFT JOIN dtb_classcategory AS T4
466                ON T3.classcategory_id2 = T4.classcategory_id) AS T5 LEFT JOIN dtb_products AS T6
467                ON product_id_sub = T6.product_id) ',
468
469            "vw_category_count" => '
470                (SELECT T1.category_id, T1.category_name, T1.parent_category_id, T1.level, T1.rank, T2.product_count
471                FROM dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2
472                ON T1.category_id = T2.category_id) '
473        );
474    }
475}
476?>
Note: See TracBrowser for help on using the repository browser.