DB
[ class tree: DB ] [ index: DB ] [ all elements ]

Source for file SC_DB_DBFactory_MYSQL.php

Documentation is available at SC_DB_DBFactory_MYSQL.php

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

Documentation generated on Fri, 24 Feb 2012 14:00:05 +0900 by Seasoft