source: branches/version-2_5-dev/data/class/db/dbfactory/SC_DB_DBFactory_MYSQL.php @ 18815

Revision 18815, 14.8 KB checked in by nanasess, 14 years ago (diff)

規格まわりの内部構成変更に伴う修正(#781)

  • 規格のデータ構造を木構造へ変更
  • 商品検索ロジックを SC_Product クラスへできるだけ集約
  • 以下の VIEW を削除
    • vw_category_count;
    • vw_product_class;
    • vw_products_nonclass;
    • vw_cross_products_class;
    • vw_cross_class;
    • vw_download_class;
  • 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-2010 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    /** SC_Query インスタンス */
41    var $objQuery;
42
43    /**
44     * DBのバージョンを取得する.
45     *
46     * @param string $dsn データソース名
47     * @return string データベースのバージョン
48     */
49    function sfGetDBVersion($dsn = "") {
50        $objQuery =& SC_Query::getSingletonInstance();
51        $val = $objQuery->getOne("select version()");
52        return "MySQL " . $val;
53    }
54
55    /**
56     * MySQL 用の SQL 文に変更する.
57     *
58     * @access private
59     * @param string $sql SQL 文
60     * @return string MySQL 用に置換した SQL 文
61     */
62    function sfChangeMySQL($sql){
63        // 改行、タブを1スペースに変換
64        $sql = preg_replace("/[\r\n\t]/"," ",$sql);
65        // view表をインラインビューに変換する
66        $sql = $this->sfChangeView($sql);
67        // ILIKE検索をLIKE検索に変換する
68        $sql = $this->sfChangeILIKE($sql);
69        // RANDOM()をRAND()に変換する
70        $sql = $this->sfChangeRANDOM($sql);
71        // TRUNCをTRUNCATEに変換する
72        $sql = $this->sfChangeTrunc($sql);
73        return $sql;
74    }
75
76    /**
77     * 文字コード情報を取得する
78     *
79     * @return array 文字コード情報
80     */
81    function getCharSet() {
82        $objQuery =& SC_Query::getSingletonInstance();
83        $arrRet = $objQuery->getAll("SHOW VARIABLES LIKE 'char%'");
84        return $arrRet;
85    }
86
87    /**
88     * 昨日の売上高・売上件数を算出する SQL を返す.
89     *
90     * @param string $method SUM または COUNT
91     * @return string 昨日の売上高・売上件数を算出する SQL
92     */
93    function getOrderYesterdaySql($method) {
94        return "SELECT ".$method."(total) FROM dtb_order "
95              . "WHERE del_flg = 0 "
96                . "AND cast(create_date as date) = DATE_ADD(current_date, interval -1 day) "
97                . "AND status <> " . ORDER_CANCEL;
98    }
99
100    /**
101     * 当月の売上高・売上件数を算出する SQL を返す.
102     *
103     * @param string $method SUM または COUNT
104     * @return string 当月の売上高・売上件数を算出する SQL
105     */
106    function getOrderMonthSql($method) {
107        return "SELECT ".$method."(total) FROM dtb_order "
108              . "WHERE del_flg = 0 "
109                . "AND date_format(create_date, '%Y/%m') = ? "
110                . "AND date_format(create_date, '%Y/%m/%d') <> date_format(now(), '%Y/%m/%d') "
111                . "AND status <> " . ORDER_CANCEL;
112    }
113
114    /**
115     * 昨日のレビュー書き込み件数を算出する SQL を返す.
116     *
117     * @return string 昨日のレビュー書き込み件数を算出する SQL
118     */
119    function getReviewYesterdaySql() {
120        return "SELECT COUNT(*) FROM dtb_review AS A "
121          . "LEFT JOIN dtb_products AS B "
122                 . "ON A.product_id = B.product_id "
123              . "WHERE A.del_flg = 0 "
124                . "AND B.del_flg = 0 "
125                . "AND cast(A.create_date as date) = DATE_ADD(current_date, interval -1 day) "
126                . "AND cast(A.create_date as date) != current_date";
127    }
128
129    /**
130     * メール送信履歴の start_date の検索条件の SQL を返す.
131     *
132     * @return string 検索条件の SQL
133     */
134    function getSendHistoryWhereStartdateSql() {
135        return "start_date BETWEEN date_add(now(),INTERVAL -5 minute) AND date_add(now(),INTERVAL 5 minute)";
136    }
137
138    /**
139     * ダウンロード販売の検索条件の SQL を返す.
140     *
141     * @param string $dtb_order_alias
142     * @return string 検索条件の SQL
143     */
144    function getDownloadableDaysWhereSql($dtb_order_alias) {
145        return "(SELECT IF((SELECT d1.downloadable_days_unlimited FROM dtb_baseinfo d1)=1, 1, DATE(NOW()) <= DATE(DATE_ADD(" . $dtb_order_alias . ".commit_date, INTERVAL (SELECT downloadable_days FROM dtb_baseinfo) DAY))))";
146    }
147
148    /**
149     * 文字列連結を行う.
150     *
151     * @param array $columns 連結を行うカラム名
152     * @return string 連結後の SQL 文
153     */
154    function concatColumn($columns) {
155        $sql = "concat(";
156        $i = 0;
157        $total = count($columns);
158        foreach ($columns as $column) {
159            $sql .= $column;
160            if ($i < $total -1) {
161                $sql .= ", ";
162            }
163            $i++;
164        }
165        $sql .= ")";
166        return $sql;
167    }
168
169    /**
170     * テーブルのカラム一覧を取得する.
171     *
172     * @deprecated SC_Query::listTableFields() を使用してください
173     * @param string $table_name テーブル名
174     * @return array テーブルのカラム一覧の配列
175     */
176    function sfGetColumnList($table_name) {
177        $objQuery =& SC_Query::getSingletonInstance();
178        $sql = "SHOW COLUMNS FROM " . $table_name;
179        $arrColList = $objQuery->getAll($sql);
180        $arrColList = SC_Utils_Ex::sfswaparray($arrColList);
181        return $arrColList["Field"];
182    }
183
184    /**
185     * テーブルを検索する.
186     *
187     * 引数に部分一致するテーブル名を配列で返す.
188     *
189     * @param string $expression 検索文字列
190     * @return array テーブル名の配列
191     */
192    function findTableNames($expression = "") {
193        $objQuery =& SC_Query::getSingletonInstance();
194        $sql = "SHOW TABLES LIKE ". $objQuery->quote("%" . $expression . "%");
195        $arrColList = $objQuery->getAll($sql);
196        $arrColList = SC_Utils_Ex::sfswaparray($arrColList, false);
197        return $arrColList[0];
198    }
199
200    /**
201     * View の WHERE 句を置換する.
202     *
203     * @param string $target 置換対象の文字列
204     * @param string $where 置換する文字列
205     * @param array $arrval WHERE 句の要素の配列
206     * @param string $option SQL 文の追加文字列
207     * @return string 置換後の SQL 文
208     */
209    function sfViewWhere($target, $where = "", $arrval = array(), $option = ""){
210
211        $arrWhere = split("[?]", $where);
212        $where_tmp = " WHERE " . $arrWhere[0];
213        for($i = 1; $i < count($arrWhere); $i++){
214            $where_tmp .= SC_Utils_Ex::sfQuoteSmart($arrval[$i - 1]) . $arrWhere[$i];
215        }
216        $arrWhere = $this->getWhereConverter();
217        $arrWhere[$target] = $where_tmp . " " . $option;
218        return $arrWhere[$target];
219    }
220
221    /**
222     * View をインラインビューに変換する.
223     *
224     * @access private
225     * @param string $sql SQL 文
226     * @return string インラインビューに変換した SQL 文
227     */
228    function sfChangeView($sql){
229
230        $arrViewTmp = $this->viewToSubQuery();
231
232            // viewのwhereを変換
233        foreach($arrViewTmp as $key => $val){
234            $arrViewTmp[$key] = strtr($arrViewTmp[$key], $this->getWhereConverter());
235        }
236
237            // viewを変換
238        $changesql = strtr($sql, $arrViewTmp);
239
240        return $changesql;
241    }
242
243    /**
244     * ILIKE句 を LIKE句へ変換する.
245     *
246     * @access private
247     * @param string $sql SQL文
248     * @return string 変換後の SQL 文
249     */
250    function sfChangeILIKE($sql){
251        $changesql = eregi_replace("(ILIKE )", "LIKE ", $sql);
252        return $changesql;
253    }
254
255    /**
256     * RANDOM() を RAND() に変換する.
257     *
258     * @access private
259     * @param string $sql SQL文
260     * @return string 変換後の SQL 文
261     */
262    function sfChangeRANDOM($sql){
263        $changesql = eregi_replace("( RANDOM)", " RAND", $sql);
264        return $changesql;
265    }
266
267    /**
268     * TRUNC() を TRUNCATE() に変換する.
269     *
270     * @access private
271     * @param string $sql SQL文
272     * @return string 変換後の SQL 文
273     */
274    function sfChangeTrunc($sql){
275        $changesql = eregi_replace("( TRUNC)", " TRUNCATE", $sql);
276        return $changesql;
277    }
278
279    /**
280     * WHERE 句置換用の配列を返す.
281     *
282     * @access private
283     * @return array WHERE 句置換用の配列
284     */
285    function getWhereConverter() {
286        return array(
287            "&&crscls_where&&" => "",
288            "&&crsprdcls_where&&" =>"",
289            "&&noncls_where&&" => "",
290            "&&allcls_where&&" => "",
291            "&&allclsdtl_where&&" => "",
292            "&&prdcls_where&&" => "",
293            "&&catcnt_where&&" => ""
294        );
295    }
296
297    /**
298     * View をサブクエリに変換するための配列を返す.
299     *
300     * @access private
301     * @return array View をサブクエリに変換するための配列
302     */
303    function viewToSubQuery() {
304
305        static $sql = array();
306
307        if (empty($sql)) {
308
309
310            $sql['vw_products_allclass_detail'] = <<< __EOS__
311                (
312                    SELECT
313                        dtb_products.product_id,
314                        dtb_products.name,
315                        dtb_products.maker_id,
316                        dtb_products.rank,
317                        dtb_products.status,
318                        dtb_products.comment1,
319                        dtb_products.comment2,
320                        dtb_products.comment3,
321                        dtb_products.comment4,
322                        dtb_products.comment5,
323                        dtb_products.comment6,
324                        dtb_products.note,
325                        dtb_products.file1,
326                        dtb_products.file2,
327                        dtb_products.file3,
328                        dtb_products.file4,
329                        dtb_products.file5,
330                        dtb_products.file6,
331                        dtb_products.main_list_comment,
332                        dtb_products.main_list_image,
333                        dtb_products.main_comment,
334                        dtb_products.main_image,
335                        dtb_products.main_large_image,
336                        dtb_products.sub_title1,
337                        dtb_products.sub_comment1,
338                        dtb_products.sub_image1,
339                        dtb_products.sub_large_image1,
340                        dtb_products.sub_title2,
341                        dtb_products.sub_comment2,
342                        dtb_products.sub_image2,
343                        dtb_products.sub_large_image2,
344                        dtb_products.sub_title3,
345                        dtb_products.sub_comment3,
346                        dtb_products.sub_image3,
347                        dtb_products.sub_large_image3,
348                        dtb_products.sub_title4,
349                        dtb_products.sub_comment4,
350                        dtb_products.sub_image4,
351                        dtb_products.sub_large_image4,
352                        dtb_products.sub_title5,
353                        dtb_products.sub_comment5,
354                        dtb_products.sub_image5,
355                        dtb_products.sub_large_image5,
356                        dtb_products.sub_title6,
357                        dtb_products.sub_comment6,
358                        dtb_products.sub_image6,
359                        dtb_products.sub_large_image6,
360                        dtb_products.del_flg,
361                        dtb_products.creator_id,
362                        dtb_products.create_date,
363                        dtb_products.update_date,
364                        dtb_products.deliv_date_id,
365                        dtb_products.down,
366                        dtb_products.down_filename,
367                        dtb_products.down_realfilename,
368                        T4.product_code_min,
369                        T4.product_code_max,
370                        T4.price01_min,
371                        T4.price01_max,
372                        T4.price02_min,
373                        T4.price02_max,
374                        T4.stock_min,
375                        T4.stock_max,
376                        T4.stock_unlimited_min,
377                        T4.stock_unlimited_max,
378                        T4.class_count
379                    FROM
380                        dtb_products
381                    JOIN
382                            (
383                                SELECT
384                                    product_id,
385                                    MIN(product_code) AS product_code_min,
386                                    MAX(product_code) AS product_code_max,
387                                    MIN(price01) AS price01_min,
388                                    MAX(price01) AS price01_max,
389                                    MIN(price02) AS price02_min,
390                                    MAX(price02) AS price02_max,
391                                    MIN(stock) AS stock_min,
392                                    MAX(stock) AS stock_max,
393                                    MIN(stock_unlimited) AS stock_unlimited_min,
394                                    MAX(stock_unlimited) AS stock_unlimited_max,
395                                    COUNT(*) as class_count
396                                FROM dtb_products_class
397                                GROUP BY product_id
398                            ) AS T4
399                            ON dtb_products.product_id = T4.product_id
400                )
401__EOS__;
402
403            $sql['vw_products_allclass'] = <<< __EOS__
404                (
405                    SELECT
406                        alldtl.*,
407                        dtb_category.rank AS category_rank,
408                        T2.category_id,
409                        T2.rank AS product_rank
410                    FROM
411                        {$sql['vw_products_allclass_detail']} AS alldtl
412                        LEFT JOIN
413                            dtb_product_categories AS T2
414                            ON alldtl.product_id = T2.product_id
415                        LEFT JOIN
416                            dtb_category
417                            ON T2.category_id = dtb_category.category_id
418                )
419__EOS__;
420
421        }
422
423        return $sql;
424
425    }
426}
427?>
Note: See TracBrowser for help on using the repository browser.