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

Revision 15532, 15.0 KB checked in by nanasess, 17 years ago (diff)

svn:mime-type 修正

  • 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 * 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:SC_DB_DBFactory_MYSQL.php 15267 2007-08-09 12:31:52Z nanasess $
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     * View の WHERE 句を置換する.
108     *
109     * @param string $target 置換対象の文字列
110     * @param string $where 置換する文字列
111     * @param array $arrval WHERE 句の要素の配列
112     * @param string $option SQL 文の追加文字列
113     * @return string 置換後の SQL 文
114     */
115    function sfViewWhere($target, $where = "", $arrval = array(), $option = ""){
116
117        $arrWhere = split("[?]", $where);
118        $where_tmp = " WHERE " . $arrWhere[0];
119        for($i = 1; $i < count($arrWhere); $i++){
120            $where_tmp .= SC_Utils_Ex::sfQuoteSmart($arrval[$i - 1]) . $arrWhere[$i];
121        }
122        $arrWhere = $this->getWhereConverter();
123        $arrWhere[$target] = $where_tmp . " " . $option;
124        return $arrWhere[$target];
125    }
126
127    /**
128     * SQL の中の View の存在をチェックする.
129     *
130     * @access private
131     * @param string $sql SQL 文
132     * @return bool Viewが存在しない場合 false
133     */
134    function sfInArray($sql){
135        $arrView = $this->viewToSubQuery();
136
137        foreach($arrView as $key => $val){
138            if (strcasecmp($sql, $val) == 0){
139                $changesql = eregi_replace("($key)", "$val", $sql);
140                $this->sfInArray($changesql);
141            }
142        }
143        return false;
144    }
145
146    /**
147     * View をインラインビューに変換する.
148     *
149     * @access private
150     * @param string $sql SQL 文
151     * @return string インラインビューに変換した SQL 文
152     */
153    function sfChangeView($sql){
154
155        $arrViewTmp = $this->viewToSubQuery();
156
157        // viewのwhereを変換
158        foreach($arrViewTmp as $key => $val){
159            $arrViewTmp[$key] = strtr($arrViewTmp[$key], $this->getWhereConverter());
160        }
161
162        // viewを変換
163        $changesql = strtr($sql, $arrViewTmp);
164
165        return $changesql;
166    }
167
168    /**
169     * ILIKE句 を LIKE句へ変換する.
170     *
171     * @access private
172     * @param string $sql SQL文
173     * @return string 変換後の SQL 文
174     */
175    function sfChangeILIKE($sql){
176        $changesql = eregi_replace("(ILIKE )", "LIKE BINARY ", $sql);
177        return $changesql;
178    }
179
180    /**
181     * RANDOM() を RAND() に変換する.
182     *
183     * @access private
184     * @param string $sql SQL文
185     * @return string 変換後の SQL 文
186     */
187    function sfChangeRANDOM($sql){
188        $changesql = eregi_replace("( RANDOM)", " RAND", $sql);
189        return $changesql;
190    }
191
192    /**
193     * WHERE 句置換用の配列を返す.
194     *
195     * @access private
196     * @return array WHERE 句置換用の配列
197     */
198    function getWhereConverter() {
199        return array(
200            "&&crscls_where&&" => "",
201            "&&crsprdcls_where&&" =>"",
202            "&&noncls_where&&" => "",
203            "&&allcls_where&&" => "",
204            "&&allclsdtl_where&&" => "",
205            "&&prdcls_where&&" => "",
206            "&&catcnt_where&&" => ""
207        );
208    }
209
210    /**
211     * View をサブクエリに変換するための配列を返す.
212     *
213     * @access private
214     * @return array View をサブクエリに変換するための配列
215     */
216    function viewToSubQuery() {
217        return array(
218            "vw_cross_class" => '
219                (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
220                FROM dtb_classcategory AS T1, dtb_classcategory AS T2 ) ',
221
222            "vw_cross_products_class" =>'
223                (SELECT T1.class_id1, T1.class_id2, T1.classcategory_id1, T1.classcategory_id2, T2.product_id,
224                T1.name1, T1.name2, T2.product_code, T2.stock, T2.price01, T2.price02, T1.rank1, T1.rank2
225                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
226                FROM dtb_classcategory AS T1, dtb_classcategory AS T2 ) AS T1 LEFT JOIN dtb_products_class AS T2
227                ON T1.classcategory_id1 = T2.classcategory_id1 AND T1.classcategory_id2 = T2.classcategory_id2) ',
228
229            "vw_products_nonclass" => '
230                (SELECT
231                    T1.product_id,
232                    T1.name,
233                    T1.deliv_fee,
234                    T1.sale_limit,
235                    T1.sale_unlimited,
236                    T1.category_id,
237                    T1.rank,
238                    T1.status,
239                    T1.product_flag,
240                    T1.point_rate,
241                    T1.comment1,
242                    T1.comment2,
243                    T1.comment3,
244                    T1.comment4,
245                    T1.comment5,
246                    T1.comment6,
247                    T1.file1,
248                    T1.file2,
249                    T1.file3,
250                    T1.file4,
251                    T1.file5,
252                    T1.file6,
253                    T1.main_list_comment,
254                    T1.main_list_image,
255                    T1.main_comment,
256                    T1.main_image,
257                    T1.main_large_image,
258                    T1.sub_title1,
259                    T1.sub_comment1,
260                    T1.sub_image1,
261                    T1.sub_large_image1,
262                    T1.sub_title2,
263                    T1.sub_comment2,
264                    T1.sub_image2,
265                    T1.sub_large_image2,
266                    T1.sub_title3,
267                    T1.sub_comment3,
268                    T1.sub_image3,
269                    T1.sub_large_image3,
270                    T1.sub_title4,
271                    T1.sub_comment4,
272                    T1.sub_image4,
273                    T1.sub_large_image4,
274                    T1.sub_title5,
275                    T1.sub_comment5,
276                    T1.sub_image5,
277                    T1.sub_large_image5,
278                    T1.sub_title6,
279                    T1.sub_comment6,
280                    T1.sub_image6,
281                    T1.sub_large_image6,
282                    T1.del_flg,
283                    T1.creator_id,
284                    T1.create_date,
285                    T1.update_date,
286                    T1.deliv_date_id,
287                    T2.product_id_sub,
288                    T2.product_code,
289                    T2.price01,
290                    T2.price02,
291                    T2.stock,
292                    T2.stock_unlimited,
293                    T2.classcategory_id1,
294                    T2.classcategory_id2
295                FROM (SELECT * FROM dtb_products &&noncls_where&&) AS T1 LEFT JOIN
296                (SELECT
297                product_id AS product_id_sub,
298                product_code,
299                price01,
300                price02,
301                stock,
302                stock_unlimited,
303                classcategory_id1,
304                classcategory_id2
305                FROM dtb_products_class WHERE classcategory_id1 = 0 AND classcategory_id2 = 0)
306                AS T2
307                ON T1.product_id = T2.product_id_sub) ',
308
309            "vw_products_allclass" => '
310                (SELECT
311                product_id,
312                product_code_min,
313                product_code_max,
314                price01_min,
315                price01_max,
316                price02_min,
317                price02_max,
318                stock_min,
319                stock_max,
320                stock_unlimited_min,
321                stock_unlimited_max,
322                del_flg,
323                status,
324                name,
325                comment1,
326                comment2,
327                comment3,
328                rank,
329                main_list_comment,
330                main_image,
331                main_list_image,
332                product_flag,
333                deliv_date_id,
334                sale_limit,
335                point_rate,
336                sale_unlimited,
337                create_date,
338                deliv_fee
339                ,(SELECT rank AS category_rank FROM dtb_category AS T4 WHERE T1.category_id = T4.category_id) as category_rank
340                ,(SELECT category_id AS sub_category_id FROM dtb_category T4 WHERE T1.category_id = T4.category_id) as category_id
341            FROM
342                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
343            ) ',
344
345            "vw_products_allclass_detail" => '
346                (SELECT product_id,price01_min,price01_max,price02_min,price02_max,stock_min,stock_max,stock_unlimited_min,stock_unlimited_max,
347                del_flg,status,name,comment1,comment2,comment3,deliv_fee,main_comment,main_image,main_large_image,
348                sub_title1,sub_comment1,sub_image1,sub_large_image1,
349                sub_title2,sub_comment2,sub_image2,sub_large_image2,
350                sub_title3,sub_comment3,sub_image3,sub_large_image3,
351                sub_title4,sub_comment4,sub_image4,sub_large_image4,
352                sub_title5,sub_comment5,sub_image5,sub_large_image5,
353                product_flag,deliv_date_id,sale_limit,point_rate,sale_unlimited,file1,file2,category_id
354                FROM ( SELECT * FROM (dtb_products AS T1 RIGHT JOIN
355                (SELECT
356                product_id AS product_id_sub,
357                MIN(price01) AS price01_min,
358                MAX(price01) AS price01_max,
359                MIN(price02) AS price02_min,
360                MAX(price02) AS price02_max,
361                MIN(stock) AS stock_min,
362                MAX(stock) AS stock_max,
363                MIN(stock_unlimited) AS stock_unlimited_min,
364                MAX(stock_unlimited) AS stock_unlimited_max
365                FROM dtb_products_class GROUP BY product_id) AS T2
366                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
367                ON T3.category_id = T4.sub_category_id) ',
368
369            "vw_product_class" => '
370                (SELECT * FROM
371                (SELECT T3.product_class_id, T3.product_id AS product_id_sub, classcategory_id1, classcategory_id2,
372                T3.rank AS rank1, T4.rank AS rank2, T3.class_id AS class_id1, T4.class_id AS class_id2,
373                stock, price01, price02, stock_unlimited, product_code
374                FROM ( SELECT
375                        T1.product_class_id,
376                        T1.product_id,
377                        classcategory_id1,
378                        classcategory_id2,
379                        T2.rank,
380                        T2.class_id,
381                        stock,
382                        price01,
383                        price02,
384                        stock_unlimited,
385                        product_code
386                 FROM (dtb_products_class AS T1 LEFT JOIN dtb_classcategory AS T2
387                ON T1.classcategory_id1 = T2.classcategory_id))
388                AS T3 LEFT JOIN dtb_classcategory AS T4
389                ON T3.classcategory_id2 = T4.classcategory_id) AS T5 LEFT JOIN dtb_products AS T6
390                ON product_id_sub = T6.product_id) ',
391
392            "vw_category_count" => '
393                (SELECT T1.category_id, T1.category_name, T1.parent_category_id, T1.level, T1.rank, T2.product_count
394                FROM dtb_category AS T1 LEFT JOIN dtb_category_total_count AS T2
395                ON T1.category_id = T2.category_id) '
396        );
397    }
398}
399?>
Note: See TracBrowser for help on using the repository browser.