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

Revision 17580, 18.0 KB checked in by Seasoft, 16 years ago (diff)

merge r17506,r17512,r17513,r17515,r17517,r17520,r17524,r17526,r17530

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