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

Revision 15264, 14.9 KB checked in by nanasess, 17 years ago (diff)

svn properties 修正

  • Property svn:keywords set to Id Revision Date
  • Property svn:mime-type set to application/x-httpd-php;charset=utf-8
Line 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7
8// {{{ requires
9require_once(CLASS_PATH . "db/SC_DB_DBFactory.php");
10
11/**
12 * MySQL 固有の処理をするクラス.
13 *
14 * このクラスを直接インスタンス化しないこと.
15 * 必ず SC_DB_DBFactory クラスを経由してインスタンス化する.
16 * また, SC_DB_DBFactory クラスの関数を必ずオーバーライドしている必要がある.
17 *
18 * @package DB
19 * @author LOCKON CO.,LTD.
20 * @version $Id$
21 */
22class SC_DB_DBFactory_MYSQL extends SC_DB_DBFactory {
23
24    /**
25     * DBのバージョンを取得する.
26     *
27     * @param string $dsn データソース名
28     * @return string データベースのバージョン
29     */
30    function sfGetDBVersion($dsn = "") {
31        $objQuery = new SC_Query($this->getDSN($dsn), true, true);
32        list($db_type) = split(":", $dsn);
33        $val = $objQuery->getOne("select version()");
34        return "MySQL " . $val;
35    }
36
37    /**
38     * MySQL 用の SQL 文に変更する.
39     *
40     * @access private
41     * @param string $sql SQL 文
42     * @return string MySQL 用に置換した SQL 文
43     */
44    function sfChangeMySQL($sql){
45        // 改行、タブを1スペースに変換
46        $sql = preg_replace("/[\r\n\t]/"," ",$sql);
47        // view表をインラインビューに変換する
48        $sql = $this->sfChangeView($sql);
49        // ILIKE検索をLIKE検索に変換する
50        $sql = $this->sfChangeILIKE($sql);
51        // RANDOM()をRAND()に変換する
52        $sql = $this->sfChangeRANDOM($sql);
53        return $sql;
54    }
55
56    /**
57     * テーブルの存在チェックを行う SQL 文を返す.
58     *
59     * @return string テーブルの存在チェックを行う SQL 文
60     */
61    function getTableExistsSql() {
62        return "SHOW TABLE STATUS LIKE ?";
63    }
64
65    /**
66     * インデックスの検索結果を配列で返す.
67     *
68     * @param string $index_name インデックス名
69     * @param string $table_name テーブル名
70     * @return array インデックスの検索結果の配列
71     */
72    function getTableIndex($index_name, $table_name = "") {
73        $objQuery = new SC_Query("", true, true);
74        return $objQuery->getAll("SHOW INDEX FROM " . $table_name . " WHERE Key_name = ?",
75                                 array($index_name));
76    }
77
78    /**
79     * インデックスを作成する.
80     *
81     * @param string $index_name インデックス名
82     * @param string $table_name テーブル名
83     * @param string $col_name カラム名
84     * @param integer $length 作成するインデックスのバイト長
85     * @return void
86     */
87    function createTableIndex($index_name, $table_name, $col_name, $length = 0) {
88        $objQuery = new SC_Query($dsn, true, true);
89        $objQuery->query("CREATE INDEX ? ON ? (?(?))", array($index_name, $table_name, $col_name, $length));
90    }
91
92    /**
93     * テーブルのカラム一覧を取得する.
94     *
95     * @param string $table_name テーブル名
96     * @return array テーブルのカラム一覧の配列
97     */
98    function sfGetColumnList($table_name) {
99        $objQuery = new SC_Query();
100        $sql = "SHOW COLUMNS FROM " . $table_name;
101        $arrColList = $objQuery->getAll($sql);
102        $arrColList = SC_Utils_Ex::sfswaparray($arrColList);
103        return $arrColList["Field"];
104    }
105
106    /**
107     * SQL の中の View の存在をチェックする.
108     *
109     * @access private
110     * @param string $sql SQL 文
111     * @return bool Viewが存在しない場合 false
112     */
113    function sfInArray($sql){
114        $arrView = $this->viewToSubQuery();
115
116        foreach($arrView as $key => $val){
117            if (strcasecmp($sql, $val) == 0){
118                $changesql = eregi_replace("($key)", "$val", $sql);
119                $this->sfInArray($changesql);
120            }
121        }
122        return false;
123    }
124
125    /**
126     * View をインラインビューに変換する.
127     *
128     * @access private
129     * @param string $sql SQL 文
130     * @return string インラインビューに変換した SQL 文
131     */
132    function sfChangeView($sql){
133
134        $arrViewTmp = $this->viewToSubQuery();
135
136        // viewのwhereを変換
137        foreach($arrViewTmp as $key => $val){
138            $arrViewTmp[$key] = strtr($arrViewTmp[$key], $this->getWhereConverter());
139        }
140
141        // viewを変換
142        $changesql = strtr($sql, $arrViewTmp);
143
144        return $changesql;
145    }
146
147    /**
148     * ILIKE句 を LIKE句へ変換する.
149     *
150     * @access private
151     * @param string $sql SQL文
152     * @return string 変換後の SQL 文
153     */
154    function sfChangeILIKE($sql){
155        $changesql = eregi_replace("(ILIKE )", "LIKE BINARY ", $sql);
156        return $changesql;
157    }
158
159    /**
160     * RANDOM() を RAND() に変換する.
161     *
162     * @access private
163     * @param string $sql SQL文
164     * @return string 変換後の SQL 文
165     */
166    function sfChangeRANDOM($sql){
167        $changesql = eregi_replace("( RANDOM)", " RAND", $sql);
168        return $changesql;
169    }
170
171    /**
172     * View の WHERE 句を置換する.
173     *
174     * @access private
175     * @param string $target 置換対象の文字列
176     * @param string $where 置換する文字列
177     * @param array $arrval WHERE 句の要素の配列
178     * @param string $option SQL 文の追加文字列
179     * @return string 置換後の SQL 文
180     */
181    function sfViewWhere($target, $where = "", $arrval = array(), $option = ""){
182        global $arrViewWhere;
183        $arrWhere = split("[?]", $where);
184        $where_tmp = " WHERE " . $arrWhere[0];
185        for($i = 1; $i < count($arrWhere); $i++){
186            $where_tmp .= SC_Utils_Ex::sfQuoteSmart($arrval[$i - 1]) . $arrWhere[$i];
187        }
188        $arrViewWhere[$target] = $where_tmp . " " . $option;
189    }
190
191    /**
192     * WHERE 句置換用の配列を返す.
193     *
194     * @access private
195     * @return array WHERE 句置換用の配列
196     */
197    function getWhereConverter() {
198        return array(
199            "&&crscls_where&&" => "",
200            "&&crsprdcls_where&&" =>"",
201            "&&noncls_where&&" => "",
202            "&&allcls_where&&" => "",
203            "&&allclsdtl_where&&" => "",
204            "&&prdcls_where&&" => "",
205            "&&catcnt_where&&" => ""
206        );
207    }
208
209    /**
210     * View をサブクエリに変換するための配列を返す.
211     *
212     * @access private
213     * @return array View をサブクエリに変換するための配列
214     */
215    function viewToSubQuery() {
216        return array(
217            "vw_cross_class" => '
218                (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
219                FROM dtb_classcategory AS T1, dtb_classcategory AS T2 ) ',
220
221            "vw_cross_products_class" =>'
222                (SELECT T1.class_id1, T1.class_id2, T1.classcategory_id1, T1.classcategory_id2, T2.product_id,
223                T1.name1, T1.name2, T2.product_code, T2.stock, T2.price01, T2.price02, T1.rank1, T1.rank2
224                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
225                FROM dtb_classcategory AS T1, dtb_classcategory AS T2 ) AS T1 LEFT JOIN dtb_products_class AS T2
226                ON T1.classcategory_id1 = T2.classcategory_id1 AND T1.classcategory_id2 = T2.classcategory_id2) ',
227
228            "vw_products_nonclass" => '
229                (SELECT
230                    T1.product_id,
231                    T1.name,
232                    T1.deliv_fee,
233                    T1.sale_limit,
234                    T1.sale_unlimited,
235                    T1.category_id,
236                    T1.rank,
237                    T1.status,
238                    T1.product_flag,
239                    T1.point_rate,
240                    T1.comment1,
241                    T1.comment2,
242                    T1.comment3,
243                    T1.comment4,
244                    T1.comment5,
245                    T1.comment6,
246                    T1.file1,
247                    T1.file2,
248                    T1.file3,
249                    T1.file4,
250                    T1.file5,
251                    T1.file6,
252                    T1.main_list_comment,
253                    T1.main_list_image,
254                    T1.main_comment,
255                    T1.main_image,
256                    T1.main_large_image,
257                    T1.sub_title1,
258                    T1.sub_comment1,
259                    T1.sub_image1,
260                    T1.sub_large_image1,
261                    T1.sub_title2,
262                    T1.sub_comment2,
263                    T1.sub_image2,
264                    T1.sub_large_image2,
265                    T1.sub_title3,
266                    T1.sub_comment3,
267                    T1.sub_image3,
268                    T1.sub_large_image3,
269                    T1.sub_title4,
270                    T1.sub_comment4,
271                    T1.sub_image4,
272                    T1.sub_large_image4,
273                    T1.sub_title5,
274                    T1.sub_comment5,
275                    T1.sub_image5,
276                    T1.sub_large_image5,
277                    T1.sub_title6,
278                    T1.sub_comment6,
279                    T1.sub_image6,
280                    T1.sub_large_image6,
281                    T1.del_flg,
282                    T1.creator_id,
283                    T1.create_date,
284                    T1.update_date,
285                    T1.deliv_date_id,
286                    T2.product_id_sub,
287                    T2.product_code,
288                    T2.price01,
289                    T2.price02,
290                    T2.stock,
291                    T2.stock_unlimited,
292                    T2.classcategory_id1,
293                    T2.classcategory_id2
294                FROM (SELECT * FROM dtb_products &&noncls_where&&) AS T1 LEFT JOIN
295                (SELECT
296                product_id AS product_id_sub,
297                product_code,
298                price01,
299                price02,
300                stock,
301                stock_unlimited,
302                classcategory_id1,
303                classcategory_id2
304                FROM dtb_products_class WHERE classcategory_id1 = 0 AND classcategory_id2 = 0)
305                AS T2
306                ON T1.product_id = T2.product_id_sub) ',
307
308            "vw_products_allclass" => '
309                (SELECT
310                product_id,
311                product_code_min,
312                product_code_max,
313                price01_min,
314                price01_max,
315                price02_min,
316                price02_max,
317                stock_min,
318                stock_max,
319                stock_unlimited_min,
320                stock_unlimited_max,
321                del_flg,
322                status,
323                name,
324                comment1,
325                comment2,
326                comment3,
327                rank,
328                main_list_comment,
329                main_image,
330                main_list_image,
331                product_flag,
332                deliv_date_id,
333                sale_limit,
334                point_rate,
335                sale_unlimited,
336                create_date,
337                deliv_fee
338                ,(SELECT rank AS category_rank FROM dtb_category AS T4 WHERE T1.category_id = T4.category_id) as category_rank
339                ,(SELECT category_id AS sub_category_id FROM dtb_category T4 WHERE T1.category_id = T4.category_id) as category_id
340            FROM
341                dtb_products AS T1 RIGHT JOIN (SELECT product_id AS product_id_sub, MIN(product_code) AS product_code_min, MAX(product_code) AS product_code_max, MIN(price01) AS price01_min, MAX(price01) AS price01_max, MIN(price02) AS price02_min, MAX(price02) AS price02_max, MIN(stock) AS stock_min, MAX(stock) AS stock_max, MIN(stock_unlimited) AS stock_unlimited_min, MAX(stock_unlimited) AS stock_unlimited_max FROM dtb_products_class GROUP BY product_id) AS T2 ON T1.product_id = T2.product_id_sub
342            ) ',
343
344            "vw_products_allclass_detail" => '
345                (SELECT product_id,price01_min,price01_max,price02_min,price02_max,stock_min,stock_max,stock_unlimited_min,stock_unlimited_max,
346                del_flg,status,name,comment1,comment2,comment3,deliv_fee,main_comment,main_image,main_large_image,
347                sub_title1,sub_comment1,sub_image1,sub_large_image1,
348                sub_title2,sub_comment2,sub_image2,sub_large_image2,
349                sub_title3,sub_comment3,sub_image3,sub_large_image3,
350                sub_title4,sub_comment4,sub_image4,sub_large_image4,
351                sub_title5,sub_comment5,sub_image5,sub_large_image5,
352                product_flag,deliv_date_id,sale_limit,point_rate,sale_unlimited,file1,file2,category_id
353                FROM ( SELECT * FROM (dtb_products AS T1 RIGHT JOIN
354                (SELECT
355                product_id AS product_id_sub,
356                MIN(price01) AS price01_min,
357                MAX(price01) AS price01_max,
358                MIN(price02) AS price02_min,
359                MAX(price02) AS price02_max,
360                MIN(stock) AS stock_min,
361                MAX(stock) AS stock_max,
362                MIN(stock_unlimited) AS stock_unlimited_min,
363                MAX(stock_unlimited) AS stock_unlimited_max
364                FROM dtb_products_class GROUP BY product_id) AS T2
365                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
366                ON T3.category_id = T4.sub_category_id) ',
367
368            "vw_product_class" => '
369                (SELECT * FROM
370                (SELECT T3.product_class_id, T3.product_id AS product_id_sub, classcategory_id1, classcategory_id2,
371                T3.rank AS rank1, T4.rank AS rank2, T3.class_id AS class_id1, T4.class_id AS class_id2,
372                stock, price01, price02, stock_unlimited, product_code
373                FROM ( SELECT
374                        T1.product_class_id,
375                        T1.product_id,
376                        classcategory_id1,
377                        classcategory_id2,
378                        T2.rank,
379                        T2.class_id,
380                        stock,
381                        price01,
382                        price02,
383                        stock_unlimited,
384                        product_code
385                 FROM (dtb_products_class AS T1 LEFT JOIN dtb_classcategory AS T2
386                ON T1.classcategory_id1 = T2.classcategory_id))
387                AS T3 LEFT JOIN dtb_classcategory AS T4
388                ON T3.classcategory_id2 = T4.classcategory_id) AS T5 LEFT JOIN dtb_products AS T6
389                ON product_id_sub = T6.product_id) ',
390
391            "vw_category_count" => '
392                (SELECT T1.category_id, T1.category_name, T1.parent_category_id, T1.level, T1.rank, T2.product_count
393                FROM dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2
394                ON T1.category_id = T2.category_id) '
395        );
396    }
397}
398?>
Note: See TracBrowser for help on using the repository browser.