source: branches/feature-module-update/data/class/db/dbfactory/SC_DB_DBFactory_MYSQL.php @ 16582

Revision 16582, 17.1 KB checked in by nanasess, 16 years ago (diff)

ライセンス表記変更

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