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

Revision 16938, 17.3 KB checked in by naka, 16 years ago (diff)

DBの文字コード情報を取得する関数を追加

  • 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     * SQL の中の View の存在をチェックする.
172     *
173     * @access private
174     * @param string $sql SQL 文
175     * @return bool Viewが存在しない場合 false
176     */
177    function sfInArray($sql){
178        $arrView = $this->viewToSubQuery();
179
180        foreach($arrView as $key => $val){
181            if (strcasecmp($sql, $val) == 0){
182                $changesql = eregi_replace("($key)", "$val", $sql);
183                $this->sfInArray($changesql);
184            }
185        }
186        return false;
187    }
188
189    /**
190     * View をインラインビューに変換する.
191     *
192     * @access private
193     * @param string $sql SQL 文
194     * @return string インラインビューに変換した SQL 文
195     */
196    function sfChangeView($sql){
197
198        $arrViewTmp = $this->viewToSubQuery();
199
200        // viewのwhereを変換
201        foreach($arrViewTmp as $key => $val){
202            $arrViewTmp[$key] = strtr($arrViewTmp[$key], $this->getWhereConverter());
203        }
204
205        // viewを変換
206        $changesql = strtr($sql, $arrViewTmp);
207
208        return $changesql;
209    }
210
211    /**
212     * ILIKE句 を LIKE句へ変換する.
213     *
214     * @access private
215     * @param string $sql SQL文
216     * @return string 変換後の SQL 文
217     */
218    function sfChangeILIKE($sql){
219        $changesql = eregi_replace("(ILIKE )", "LIKE BINARY ", $sql);
220        return $changesql;
221    }
222
223    /**
224     * RANDOM() を RAND() に変換する.
225     *
226     * @access private
227     * @param string $sql SQL文
228     * @return string 変換後の SQL 文
229     */
230    function sfChangeRANDOM($sql){
231        $changesql = eregi_replace("( RANDOM)", " RAND", $sql);
232        return $changesql;
233    }
234
235    /**
236     * WHERE 句置換用の配列を返す.
237     *
238     * @access private
239     * @return array WHERE 句置換用の配列
240     */
241    function getWhereConverter() {
242        return array(
243            "&&crscls_where&&" => "",
244            "&&crsprdcls_where&&" =>"",
245            "&&noncls_where&&" => "",
246            "&&allcls_where&&" => "",
247            "&&allclsdtl_where&&" => "",
248            "&&prdcls_where&&" => "",
249            "&&catcnt_where&&" => ""
250        );
251    }
252
253    /**
254     * View をサブクエリに変換するための配列を返す.
255     *
256     * @access private
257     * @return array View をサブクエリに変換するための配列
258     */
259    function viewToSubQuery() {
260        return array(
261            "vw_cross_class" => '
262                (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
263                FROM dtb_classcategory AS T1, dtb_classcategory AS T2 ) ',
264
265            "vw_cross_products_class" =>'
266                (SELECT T1.class_id1, T1.class_id2, T1.classcategory_id1, T1.classcategory_id2, T2.product_id,
267                T1.name1, T1.name2, T2.product_code, T2.stock, T2.price01, T2.price02, T1.rank1, T1.rank2
268                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
269                FROM dtb_classcategory AS T1, dtb_classcategory AS T2 ) AS T1 LEFT JOIN dtb_products_class AS T2
270                ON T1.classcategory_id1 = T2.classcategory_id1 AND T1.classcategory_id2 = T2.classcategory_id2) ',
271
272            "vw_products_nonclass" => '
273                (SELECT
274                    T1.product_id,
275                    T1.name,
276                    T1.deliv_fee,
277                    T1.sale_limit,
278                    T1.sale_unlimited,
279                    T1.category_id,
280                    T1.rank,
281                    T1.status,
282                    T1.product_flag,
283                    T1.point_rate,
284                    T1.comment1,
285                    T1.comment2,
286                    T1.comment3,
287                    T1.comment4,
288                    T1.comment5,
289                    T1.comment6,
290                    T1.file1,
291                    T1.file2,
292                    T1.file3,
293                    T1.file4,
294                    T1.file5,
295                    T1.file6,
296                    T1.main_list_comment,
297                    T1.main_list_image,
298                    T1.main_comment,
299                    T1.main_image,
300                    T1.main_large_image,
301                    T1.sub_title1,
302                    T1.sub_comment1,
303                    T1.sub_image1,
304                    T1.sub_large_image1,
305                    T1.sub_title2,
306                    T1.sub_comment2,
307                    T1.sub_image2,
308                    T1.sub_large_image2,
309                    T1.sub_title3,
310                    T1.sub_comment3,
311                    T1.sub_image3,
312                    T1.sub_large_image3,
313                    T1.sub_title4,
314                    T1.sub_comment4,
315                    T1.sub_image4,
316                    T1.sub_large_image4,
317                    T1.sub_title5,
318                    T1.sub_comment5,
319                    T1.sub_image5,
320                    T1.sub_large_image5,
321                    T1.sub_title6,
322                    T1.sub_comment6,
323                    T1.sub_image6,
324                    T1.sub_large_image6,
325                    T1.del_flg,
326                    T1.creator_id,
327                    T1.create_date,
328                    T1.update_date,
329                    T1.deliv_date_id,
330                    T2.product_id_sub,
331                    T2.product_code,
332                    T2.price01,
333                    T2.price02,
334                    T2.stock,
335                    T2.stock_unlimited,
336                    T2.classcategory_id1,
337                    T2.classcategory_id2
338                FROM (SELECT * FROM dtb_products &&noncls_where&&) AS T1 LEFT JOIN
339                (SELECT
340                product_id AS product_id_sub,
341                product_code,
342                price01,
343                price02,
344                stock,
345                stock_unlimited,
346                classcategory_id1,
347                classcategory_id2
348                FROM dtb_products_class WHERE classcategory_id1 = 0 AND classcategory_id2 = 0)
349                AS T2
350                ON T1.product_id = T2.product_id_sub) ',
351
352            "vw_products_allclass" => '
353   (SELECT T1.product_id,
354           product_code_min,
355           product_code_max,
356           price01_min,
357           price01_max,
358           price02_min,
359           price02_max,
360           stock_min,
361           stock_max,
362           stock_unlimited_min,
363           stock_unlimited_max,
364           del_flg,
365           status,
366           name,
367           comment1,
368           comment2,
369           comment3,
370           main_list_comment,
371           main_image,
372           main_list_image,
373           product_flag,
374           deliv_date_id,
375           sale_limit,
376           point_rate,
377           sale_unlimited,
378           create_date,
379           deliv_fee,
380           rank
381           ,(SELECT rank AS category_rank
382               FROM dtb_category AS T4
383              WHERE T1.category_id = T4.category_id) as category_rank
384           ,(SELECT category_id AS sub_category_id
385               FROM dtb_category T4
386              WHERE T1.category_id = T4.category_id) as category_id
387      FROM (SELECT T0.product_id,
388                   T0.del_flg,
389                   T0.status,
390                   T0.name,
391                   T0.comment1,
392                   T0.comment2,
393                   T0.comment3,
394                   T0.main_list_comment,
395                   T0.main_image,
396                   T0.main_list_image,
397                   T0.product_flag,
398                   T0.deliv_date_id,
399                   T0.sale_limit,
400                   T0.point_rate,
401                   T0.sale_unlimited,
402                   T0.create_date,
403                   T0.deliv_fee,
404                   T00.category_id,
405                   T00.rank
406              FROM dtb_products AS T0
407         LEFT JOIN dtb_product_categories AS T00
408             USING (product_id)) AS T1
409RIGHT JOIN (SELECT product_id as product_id_sub,
410                   MIN(product_code) AS product_code_min,
411                   MAX(product_code) AS product_code_max,
412                   MIN(price01) AS price01_min,
413                   MAX(price01) AS price01_max,
414                   MIN(price02) AS price02_min,
415                   MAX(price02) AS price02_max,
416                   MIN(stock) AS stock_min,
417                   MAX(stock) AS stock_max,
418                   MIN(stock_unlimited) AS stock_unlimited_min,
419                   MAX(stock_unlimited) AS stock_unlimited_max
420              FROM dtb_products_class GROUP BY product_id) AS T2
421                ON T1.product_id = T2.product_id_sub
422            ) ',
423
424            "vw_products_allclass_detail" => '
425                (SELECT product_id,price01_min,price01_max,price02_min,price02_max,stock_min,stock_max,stock_unlimited_min,stock_unlimited_max,
426                del_flg,status,name,comment1,comment2,comment3,deliv_fee,main_comment,main_image,main_large_image,
427                sub_title1,sub_comment1,sub_image1,sub_large_image1,
428                sub_title2,sub_comment2,sub_image2,sub_large_image2,
429                sub_title3,sub_comment3,sub_image3,sub_large_image3,
430                sub_title4,sub_comment4,sub_image4,sub_large_image4,
431                sub_title5,sub_comment5,sub_image5,sub_large_image5,
432                product_flag,deliv_date_id,sale_limit,point_rate,sale_unlimited,file1,file2,category_id
433                FROM ( SELECT * FROM (dtb_products AS T1 RIGHT JOIN
434                (SELECT
435                product_id AS product_id_sub,
436                MIN(price01) AS price01_min,
437                MAX(price01) AS price01_max,
438                MIN(price02) AS price02_min,
439                MAX(price02) AS price02_max,
440                MIN(stock) AS stock_min,
441                MAX(stock) AS stock_max,
442                MIN(stock_unlimited) AS stock_unlimited_min,
443                MAX(stock_unlimited) AS stock_unlimited_max
444                FROM dtb_products_class GROUP BY product_id) AS T2
445                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
446                ON T3.category_id = T4.sub_category_id) ',
447
448            "vw_product_class" => '
449                (SELECT * FROM
450                (SELECT T3.product_class_id, T3.product_id AS product_id_sub, classcategory_id1, classcategory_id2,
451                T3.rank AS rank1, T4.rank AS rank2, T3.class_id AS class_id1, T4.class_id AS class_id2,
452                stock, price01, price02, stock_unlimited, product_code
453                FROM ( SELECT
454                        T1.product_class_id,
455                        T1.product_id,
456                        classcategory_id1,
457                        classcategory_id2,
458                        T2.rank,
459                        T2.class_id,
460                        stock,
461                        price01,
462                        price02,
463                        stock_unlimited,
464                        product_code
465                 FROM (dtb_products_class AS T1 LEFT JOIN dtb_classcategory AS T2
466                ON T1.classcategory_id1 = T2.classcategory_id))
467                AS T3 LEFT JOIN dtb_classcategory AS T4
468                ON T3.classcategory_id2 = T4.classcategory_id) AS T5 LEFT JOIN dtb_products AS T6
469                ON product_id_sub = T6.product_id) ',
470
471            "vw_category_count" => '
472                (SELECT T1.category_id, T1.category_name, T1.parent_category_id, T1.level, T1.rank, T2.product_count
473                FROM dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2
474                ON T1.category_id = T2.category_id) '
475        );
476    }
477}
478?>
Note: See TracBrowser for help on using the repository browser.