Changeset 15224 for branches/feature-module-update/data
- Timestamp:
- 2007/08/06 22:05:53 (17 years ago)
- Location:
- branches/feature-module-update/data/class
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/feature-module-update/data/class/helper/SC_Helper_DB.php
r15176 r15224 17 17 // }}} 18 18 // {{{ functions 19 20 /** 21 * データベースのバージョンを所得する. 22 * 23 * @param string $dsn データソース名 24 * @return string データベースのバージョン 25 */ 26 function sfGetDBVersion($dsn = "") { 27 if($dsn == "") { 28 if(defined('DEFAULT_DSN')) { 29 $dsn = DEFAULT_DSN; 30 } else { 31 return; 32 } 33 } 34 35 $objQuery = new SC_Query($dsn, true, true); 36 list($db_type) = split(":", $dsn); 37 if($db_type == 'mysql') { 38 $val = $objQuery->getOne("select version()"); 39 $version = "MySQL " . $val; 40 } 41 if($db_type == 'pgsql') { 42 $val = $objQuery->getOne("select version()"); 43 $arrLine = split(" " , $val); 44 $version = $arrLine[0] . " " . $arrLine[1]; 45 } 46 return $version; 47 } 48 49 /** 50 * テーブルの存在をチェックする. 51 * 52 * @param string $table_name チェック対象のテーブル名 53 * @param string $dsn データソース名 54 * @return テーブルが存在する場合 true 55 */ 56 function sfTabaleExists($table_name, $dsn = "") { 57 if($dsn == "") { 58 if(defined('DEFAULT_DSN')) { 59 $dsn = DEFAULT_DSN; 60 } else { 61 return; 62 } 63 } 64 65 $objQuery = new SC_Query($dsn, true, true); 66 // 正常に接続されている場合 67 if(!$objQuery->isError()) { 68 list($db_type) = split(":", $dsn); 69 // postgresqlとmysqlとで処理を分ける 70 if ($db_type == "pgsql") { 71 $sql = "SELECT 72 relname 73 FROM 74 pg_class 75 WHERE 76 (relkind = 'r' OR relkind = 'v') AND 77 relname = ? 78 GROUP BY 79 relname"; 80 $arrRet = $objQuery->getAll($sql, array($table_name)); 81 if(count($arrRet) > 0) { 82 return true; 83 } 84 }else if ($db_type == "mysql") { 85 $sql = "SHOW TABLE STATUS LIKE ?"; 86 $arrRet = $objQuery->getAll($sql, array($table_name)); 87 if(count($arrRet) > 0) { 88 return true; 89 } 90 } 91 } 92 return false; 93 } 94 95 /** 96 * カラムの存在チェックと作成を行う. 97 * 98 * チェック対象のテーブルに, 該当のカラムが存在するかチェックする. 99 * 引数 $add が true の場合, 該当のカラムが存在しない場合は, カラムの生成を行う. 100 * カラムの生成も行う場合は, $col_type も必須となる. 101 * 102 * @param string $table_name テーブル名 103 * @param string $column_name カラム名 104 * @param string $col_type カラムのデータ型 105 * @param string $dsn データソース名 106 * @param bool $add カラムの作成も行う場合 true 107 * @return bool カラムが存在する場合とカラムの生成に成功した場合 true, 108 * テーブルが存在しない場合 false, 109 * 引数 $add == false でカラムが存在しない場合 false 110 */ 111 function sfColumnExists($table_name, $col_name, $col_type = "", $dsn = "", $add = false) { 112 if($dsn == "") { 113 if(defined('DEFAULT_DSN')) { 114 $dsn = DEFAULT_DSN; 115 } else { 116 return; 117 } 118 } 119 120 // テーブルが無ければエラー 121 if(!sfTabaleExists($table_name, $dsn)) return false; 122 123 $objQuery = new SC_Query($dsn, true, true); 124 // 正常に接続されている場合 125 if(!$objQuery->isError()) { 126 list($db_type) = split(":", $dsn); 127 128 // カラムリストを取得 129 $arrRet = sfGetColumnList($table_name, $objQuery, $db_type); 130 if(count($arrRet) > 0) { 131 if(in_array($col_name, $arrRet)){ 132 return true; 133 } 134 } 135 } 136 137 // カラムを追加する 138 if($add){ 139 $objQuery->query("ALTER TABLE $table_name ADD $col_name $col_type "); 140 return true; 141 } 142 143 return false; 144 } 145 146 /** 147 * インデックスの存在チェックと作成を行う. 148 * 149 * チェック対象のテーブルに, 該当のインデックスが存在するかチェックする. 150 * 引数 $add が true の場合, 該当のインデックスが存在しない場合は, インデックスの生成を行う. 151 * インデックスの生成も行う場合で, DB_TYPE が mysql の場合は, $length も必須となる. 152 * 153 * @param string $table_name テーブル名 154 * @param string $column_name カラム名 155 * @param string $index_name インデックス名 156 * @param integer|string $length インデックスを作成するデータ長 157 * @param string $dsn データソース名 158 * @param bool $add インデックスの生成もする場合 true 159 * @return bool インデックスが存在する場合とインデックスの生成に成功した場合 true, 160 * テーブルが存在しない場合 false, 161 * 引数 $add == false でインデックスが存在しない場合 false 162 */ 163 function sfIndexExists($table_name, $col_name, $index_name, $length = "", $dsn = "", $add = false) { 164 if($dsn == "") { 165 if(defined('DEFAULT_DSN')) { 166 $dsn = DEFAULT_DSN; 167 } else { 168 return; 169 } 170 } 171 172 // テーブルが無ければエラー 173 if(!sfTabaleExists($table_name, $dsn)) return false; 174 175 $objQuery = new SC_Query($dsn, true, true); 176 // 正常に接続されている場合 177 if(!$objQuery->isError()) { 178 list($db_type) = split(":", $dsn); 179 switch($db_type) { 180 case 'pgsql': 181 // インデックスの存在確認 182 $arrRet = $objQuery->getAll("SELECT relname FROM pg_class WHERE relname = ?", array($index_name)); 183 break; 184 case 'mysql': 185 // インデックスの存在確認 186 $arrRet = $objQuery->getAll("SHOW INDEX FROM ? WHERE Key_name = ?", array($table_name, $index_name)); 187 break; 188 default: 189 return false; 190 } 191 // すでにインデックスが存在する場合 192 if(count($arrRet) > 0) { 193 return true; 194 } 195 } 196 197 // インデックスを作成する 198 if($add){ 199 switch($db_type) { 200 case 'pgsql': 201 $objQuery->query("CREATE INDEX ? ON ? (?)", array($index_name, $table_name, $col_name)); 202 break; 203 case 'mysql': 204 $objQuery->query("CREATE INDEX ? ON ? (?(?))", array($index_name, $table_name, $col_name, $length)); 205 break; 206 default: 207 return false; 208 } 209 return true; 210 } 211 return false; 212 } 213 214 /** 215 * データの存在チェックを行う. 216 * 217 * @param string $table_name テーブル名 218 * @param string $where データを検索する WHERE 句 219 * @param string $dsn データソース名 220 * @param string $sql データの追加を行う場合の SQL文 221 * @param bool $add データの追加も行う場合 true 222 * @return bool データが存在する場合 true, データの追加に成功した場合 true, 223 * $add == false で, データが存在しない場合 false 224 */ 225 function sfDataExists($table_name, $where, $arrval, $dsn = "", $sql = "", $add = false) { 226 if($dsn == "") { 227 if(defined('DEFAULT_DSN')) { 228 $dsn = DEFAULT_DSN; 229 } else { 230 return; 231 } 232 } 233 $objQuery = new SC_Query($dsn, true, true); 234 $count = $objQuery->count($table_name, $where, $arrval); 235 236 if($count > 0) { 237 $ret = true; 238 } else { 239 $ret = false; 240 } 241 // データを追加する 242 if(!$ret && $add) { 243 $objQuery->exec($sql); 244 } 245 return $ret; 246 } 19 247 20 248 /** … … 142 370 return $objPage; 143 371 } 372 373 /** 374 * 受注一時テーブルへの書き込み処理を行う. 375 * 376 * @param string $uniqid ユニークID 377 * @param array $sqlval SQLの値の配列 378 * @return void 379 */ 380 function sfRegistTempOrder($uniqid, $sqlval) { 381 if($uniqid != "") { 382 // 既存データのチェック 383 $objQuery = new SC_Query(); 384 $where = "order_temp_id = ?"; 385 $cnt = $objQuery->count("dtb_order_temp", $where, array($uniqid)); 386 // 既存データがない場合 387 if ($cnt == 0) { 388 // 初回書き込み時に会員の登録済み情報を取り込む 389 $sqlval = $this->sfGetCustomerSqlVal($uniqid, $sqlval); 390 $sqlval['create_date'] = "now()"; 391 $objQuery->insert("dtb_order_temp", $sqlval); 392 } else { 393 $objQuery->update("dtb_order_temp", $sqlval, $where, array($uniqid)); 394 } 395 } 396 } 397 398 /** 399 * 会員情報から SQL文の値を生成する. 400 * 401 * @param string $uniqid ユニークID 402 * @param array $sqlval SQL の値の配列 403 * @return array 会員情報を含んだ SQL の値の配列 404 */ 405 function sfGetCustomerSqlVal($uniqid, $sqlval) { 406 $objCustomer = new SC_Customer(); 407 // 会員情報登録処理 408 if ($objCustomer->isLoginSuccess()) { 409 // 登録データの作成 410 $sqlval['order_temp_id'] = $uniqid; 411 $sqlval['update_date'] = 'Now()'; 412 $sqlval['customer_id'] = $objCustomer->getValue('customer_id'); 413 $sqlval['order_name01'] = $objCustomer->getValue('name01'); 414 $sqlval['order_name02'] = $objCustomer->getValue('name02'); 415 $sqlval['order_kana01'] = $objCustomer->getValue('kana01'); 416 $sqlval['order_kana02'] = $objCustomer->getValue('kana02'); 417 $sqlval['order_sex'] = $objCustomer->getValue('sex'); 418 $sqlval['order_zip01'] = $objCustomer->getValue('zip01'); 419 $sqlval['order_zip02'] = $objCustomer->getValue('zip02'); 420 $sqlval['order_pref'] = $objCustomer->getValue('pref'); 421 $sqlval['order_addr01'] = $objCustomer->getValue('addr01'); 422 $sqlval['order_addr02'] = $objCustomer->getValue('addr02'); 423 $sqlval['order_tel01'] = $objCustomer->getValue('tel01'); 424 $sqlval['order_tel02'] = $objCustomer->getValue('tel02'); 425 $sqlval['order_tel03'] = $objCustomer->getValue('tel03'); 426 if (defined('MOBILE_SITE')) { 427 $sqlval['order_email'] = $objCustomer->getValue('email_mobile'); 428 } else { 429 $sqlval['order_email'] = $objCustomer->getValue('email'); 430 } 431 $sqlval['order_job'] = $objCustomer->getValue('job'); 432 $sqlval['order_birth'] = $objCustomer->getValue('birth'); 433 } 434 return $sqlval; 435 } 436 437 /** 438 * 受注一時テーブルから情報を取得する. 439 * 440 * @param integer $order_temp_id 受注一時ID 441 * @return array 受注一時情報の配列 442 */ 443 function sfGetOrderTemp($order_temp_id) { 444 $objQuery = new SC_Query(); 445 $where = "order_temp_id = ?"; 446 $arrRet = $objQuery->select("*", "dtb_order_temp", $where, array($order_temp_id)); 447 return $arrRet[0]; 448 } 449 450 /** 451 * SELECTボックス用リストを作成する. 452 * 453 * @param string $table テーブル名 454 * @param string $keyname プライマリーキーのカラム名 455 * @param string $valname データ内容のカラム名 456 * @return array SELECT ボックス用リストの配列 457 */ 458 function sfGetIDValueList($table, $keyname, $valname) { 459 $objQuery = new SC_Query(); 460 $col = "$keyname, $valname"; 461 $objQuery->setwhere("del_flg = 0"); 462 $objQuery->setorder("rank DESC"); 463 $arrList = $objQuery->select($col, $table); 464 $count = count($arrList); 465 for($cnt = 0; $cnt < $count; $cnt++) { 466 $key = $arrList[$cnt][$keyname]; 467 $val = $arrList[$cnt][$valname]; 468 $arrRet[$key] = $val; 469 } 470 return $arrRet; 471 } 144 472 } 145 473 ?> -
branches/feature-module-update/data/class/util/SC_Utils.php
r15214 r15224 1179 1179 } 1180 1180 1181 // SELECTボックス用リストの作成1182 function sfGetIDValueList($table, $keyname, $valname) {1183 $objQuery = new SC_Query();1184 $col = "$keyname, $valname";1185 $objQuery->setwhere("del_flg = 0");1186 $objQuery->setorder("rank DESC");1187 $arrList = $objQuery->select($col, $table);1188 $count = count($arrList);1189 for($cnt = 0; $cnt < $count; $cnt++) {1190 $key = $arrList[$cnt][$keyname];1191 $val = $arrList[$cnt][$valname];1192 $arrRet[$key] = $val;1193 }1194 return $arrRet;1195 }1196 1197 1181 function sfTrim($str) { 1198 1182 $ret = ereg_replace("^[ \n\r]*", "", $str); … … 1771 1755 } 1772 1756 1773 /* 会員情報を一時受注テーブルへ */1774 function sfGetCustomerSqlVal($uniqid, $sqlval) {1775 $objCustomer = new SC_Customer();1776 // 会員情報登録処理1777 if ($objCustomer->isLoginSuccess()) {1778 // 登録データの作成1779 $sqlval['order_temp_id'] = $uniqid;1780 $sqlval['update_date'] = 'Now()';1781 $sqlval['customer_id'] = $objCustomer->getValue('customer_id');1782 $sqlval['order_name01'] = $objCustomer->getValue('name01');1783 $sqlval['order_name02'] = $objCustomer->getValue('name02');1784 $sqlval['order_kana01'] = $objCustomer->getValue('kana01');1785 $sqlval['order_kana02'] = $objCustomer->getValue('kana02');1786 $sqlval['order_sex'] = $objCustomer->getValue('sex');1787 $sqlval['order_zip01'] = $objCustomer->getValue('zip01');1788 $sqlval['order_zip02'] = $objCustomer->getValue('zip02');1789 $sqlval['order_pref'] = $objCustomer->getValue('pref');1790 $sqlval['order_addr01'] = $objCustomer->getValue('addr01');1791 $sqlval['order_addr02'] = $objCustomer->getValue('addr02');1792 $sqlval['order_tel01'] = $objCustomer->getValue('tel01');1793 $sqlval['order_tel02'] = $objCustomer->getValue('tel02');1794 $sqlval['order_tel03'] = $objCustomer->getValue('tel03');1795 if (defined('MOBILE_SITE')) {1796 $sqlval['order_email'] = $objCustomer->getValue('email_mobile');1797 } else {1798 $sqlval['order_email'] = $objCustomer->getValue('email');1799 }1800 $sqlval['order_job'] = $objCustomer->getValue('job');1801 $sqlval['order_birth'] = $objCustomer->getValue('birth');1802 }1803 return $sqlval;1804 }1805 1806 // 受注一時テーブルへの書き込み処理1807 function sfRegistTempOrder($uniqid, $sqlval) {1808 if($uniqid != "") {1809 // 既存データのチェック1810 $objQuery = new SC_Query();1811 $where = "order_temp_id = ?";1812 $cnt = $objQuery->count("dtb_order_temp", $where, array($uniqid));1813 // 既存データがない場合1814 if ($cnt == 0) {1815 // 初回書き込み時に会員の登録済み情報を取り込む1816 $sqlval = sfGetCustomerSqlVal($uniqid, $sqlval);1817 $sqlval['create_date'] = "now()";1818 $objQuery->insert("dtb_order_temp", $sqlval);1819 } else {1820 $objQuery->update("dtb_order_temp", $sqlval, $where, array($uniqid));1821 }1822 }1823 }1824 1825 1757 /* 会員のメルマガ登録があるかどうかのチェック(仮会員を含まない) */ 1826 1758 function sfCheckCustomerMailMaga($email) { … … 1864 1796 1865 1797 return $return; 1866 }1867 1868 // 受注一時テーブルから情報を取得する1869 function sfGetOrderTemp($order_temp_id) {1870 $objQuery = new SC_Query();1871 $where = "order_temp_id = ?";1872 $arrRet = $objQuery->select("*", "dtb_order_temp", $where, array($order_temp_id));1873 return $arrRet[0];1874 1798 } 1875 1799
Note: See TracChangeset
for help on using the changeset viewer.