Changeset 18609 for tmp/version-2_5-test/data/class/SC_DbConn.php
- Timestamp:
- 2010/03/11 10:35:11 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tmp/version-2_5-test/data/class/SC_DbConn.php
r18562 r18609 24 24 $current_dir = realpath(dirname(__FILE__)); 25 25 require_once($current_dir . "/../module/DB.php"); 26 require_once($current_dir . "/util/SC_Utils.php"); 27 require_once($current_dir . "/util/GC_Utils.php"); 28 $objDbConn = ""; 29 30 class SC_DbConn{ 26 27 $g_arr_objDbConn = array(); 28 29 class SC_DbConn { 31 30 32 31 var $conn; 33 32 var $result; 34 33 var $includePath; 35 var $error_mail_to;36 var $error_mail_title;37 34 var $dsn; 38 35 var $err_disp = true; … … 42 39 // コンストラクタ 43 40 function SC_DbConn($dsn = "", $err_disp = true, $new = false){ 44 global $ objDbConn;41 global $g_arr_objDbConn; 45 42 46 43 // Debugモード指定 … … 49 46 $options['persistent'] = PEAR_DB_PERSISTENT; 50 47 48 if (strlen($dsn) >= 1) { 49 $this->dsn = $dsn; 50 } elseif (defined('DEFAULT_DSN')) { 51 $this->dsn = DEFAULT_DSN; 52 } else { 53 // XXX 以前の仕様を継承しているが、意図が良く分からない。(2010/03/03 Seasoft 塚田) 54 return; 55 } 56 51 57 // 既に接続されていないか、新規接続要望の場合は接続する。 52 if (!isset($objDbConn->connection) || $new) {53 if($dsn != "") {54 $objDbConn = DB::connect($dsn, $options);55 $this->dsn = $dsn; 56 } else{57 if(defined('DEFAULT_DSN')) {58 $objDbConn = DB::connect(DEFAULT_DSN, $options);59 $this->dsn = DEFAULT_DSN; 60 } else{61 return;62 }58 if (!isset($g_arr_objDbConn[$this->dsn]) || !isset($g_arr_objDbConn[$this->dsn]->connection)) { 59 $new = true; 60 } 61 62 if ($new) { 63 $this->conn = DB::connect($this->dsn, $options); 64 $g_arr_objDbConn[$this->dsn] = $this->conn; 65 66 if (DB_TYPE == 'mysql') { 67 $g_arr_objDbConn->query('SET NAMES utf8'); // FIXME mysql_set_charset を使える環境では、その方が良さそう (2010/03/03 Seasoft 塚田) 68 $g_arr_objDbConn->query("SET SESSION sql_mode = 'ANSI'"); 63 69 } 64 } 65 //MySQL文字化け対策(MySQLで文字化けする場合は以下のコメントアウトをはずして動作確認してみてください。) 66 //if (DB_TYPE == 'mysql') { 67 // $objDbConn->query('SET NAMES utf8'); 68 //} 69 $this->conn = $objDbConn; 70 $this->error_mail_to = DB_ERROR_MAIL_TO; 71 $this->error_mail_title = DB_ERROR_MAIL_SUBJECT; 70 } else { 71 $this->conn = $g_arr_objDbConn[$this->dsn]; 72 } 73 72 74 $this->err_disp = $err_disp; 73 75 $this->dbFactory = SC_DB_DBFactory_Ex::getInstance(); … … 86 88 87 89 if ($this->conn->isError($result) && !$ignore_err){ 88 $this->send_err_mail 90 $this->send_err_mail($result, $n); 89 91 } 90 92 … … 105 107 } 106 108 if ($this->conn->isError($result)){ 107 $this->send_err_mail ($result ,$n); 108 } 109 $this->result = $result; 110 111 return $this->result; 112 } 113 114 function getRow($n, $arr = ""){ 115 116 // mysqlの場合にはビュー表を変換する 117 if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); 118 119 if ( $arr ) { 120 $result = $this->conn->getRow($n, $arr); 121 } else { 122 $result = $this->conn->getRow($n); 123 } 109 $this->send_err_mail($result ,$n); 110 } 111 $this->result = $result; 112 113 return $this->result; 114 } 115 116 /** 117 * クエリを実行し、最初の行を返す 118 * 119 * @param string $sql SQL クエリ 120 * @param array $arrVal プリペアドステートメントの実行時に使用される配列。配列の要素数は、クエリ内のプレースホルダの数と同じでなければなりません。 121 * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。 122 * @return array データを含む1次元配列。失敗した場合に DB_Error オブジェクトを返します。 123 */ 124 function getRow($sql, $arrVal = array(), $fetchmode = DB_FETCHMODE_ASSOC) { 125 126 // mysqlの場合にはビュー表を変換する 127 if (DB_TYPE == "mysql") $sql = $this->dbFactory->sfChangeMySQL($sql); 128 129 $result = $this->conn->getRow($sql, $arrVal ,$fetchmode); 130 124 131 if ($this->conn->isError($result)){ 125 $this->send_err_mail ($result ,$n);132 $this->send_err_mail($result ,$sql); 126 133 } 127 134 $this->result = $result; … … 146 153 } 147 154 148 // SELECT文の実行結果を全て取得 149 function getAll($n, $arr = ""){ 150 151 // mysqlの場合にはビュー表を変換する 152 if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); 153 154 if(PEAR::isError($this->conn)) { 155 if(ADMIN_MODE){ 155 /** 156 * クエリを実行し、全ての行を返す 157 * 158 * @param string $sql SQL クエリ 159 * @param array $arrVal プリペアドステートメントの実行時に使用される配列。配列の要素数は、クエリ内のプレースホルダの数と同じでなければなりません。 160 * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。 161 * @return array データを含む2次元配列。失敗した場合に 0 または DB_Error オブジェクトを返します。 162 */ 163 function getAll($sql, $arrVal = "", $fetchmode = DB_FETCHMODE_ASSOC) { 164 165 // mysqlの場合にはビュー表を変換する 166 if (DB_TYPE == "mysql") $sql = $this->dbFactory->sfChangeMySQL($sql); 167 168 // XXX このエラー処理はここで行なうべきなのか疑問。また、戻り値も疑問(なお、変更時はドキュメントも変更を)。 169 if (PEAR::isError($this->conn)) { 170 if (ADMIN_MODE) { 156 171 SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:" . $this->dsn); 157 } else{172 } else { 158 173 SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:"); 159 174 } … … 161 176 } 162 177 163 if ( $arr ){164 $result = $this->conn->getAll($ n, $arr, DB_FETCHMODE_ASSOC);165 } else { 166 $result = $this->conn->getAll($ n, DB_FETCHMODE_ASSOC);167 } 168 169 if ($this->conn->isError($result)) {170 $this->send_err_mail ($result, $n);178 if ($arrVal) { // FIXME 判定が曖昧 179 $result = $this->conn->getAll($sql, $arrVal, $fetchmode); 180 } else { 181 $result = $this->conn->getAll($sql, $fetchmode); 182 } 183 184 if ($this->conn->isError($result)) { 185 $this->send_err_mail($result, $sql); 171 186 } 172 187 $this->result = $result; … … 184 199 185 200 if ($this->conn->isError($result)){ 186 $this->send_err_mail 201 $this->send_err_mail($result, $n); 187 202 } 188 203 $this->result = $result; … … 211 226 } 212 227 213 function send_err_mail($result, $sql){ 214 $url = ''; 215 $errmsg = ''; 216 217 if (SC_Utils_Ex::sfIsHTTPS()) { 218 $url = "https://"; 219 } else { 220 $url = "http://"; 221 } 222 $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 223 224 $errmsg = $url."\n\n"; 225 $errmsg.= "SERVER_ADDR:" . $_SERVER['SERVER_ADDR'] . "\n"; 226 $errmsg.= "REMOTE_ADDR:" . $_SERVER['REMOTE_ADDR'] . "\n"; 227 $errmsg.= "USER_AGENT:" . $_SERVER['HTTP_USER_AGENT'] . "\n\n"; 228 $errmsg.= $sql . "\n"; 229 $errmsg.= $result->message . "\n\n"; 230 $errmsg.= $result->userinfo . "\n\n"; 231 232 $arrRbacktrace = array_reverse($result->backtrace); 233 234 foreach($arrRbacktrace as $backtrace) { 235 if($backtrace['class'] != "") { 236 $func = $backtrace['class'] . "->" . $backtrace['function']; 237 } else { 238 $func = $backtrace['function']; 239 } 240 241 $errmsg.= $backtrace['file'] . " " . $backtrace['line'] . ":" . $func . "\n"; 242 } 243 244 require_once(CLASS_EX_PATH . "page_extends/error/LC_Page_Error_SystemError_Ex.php"); 245 246 $objPage = new LC_Page_Error_SystemError_Ex(); 247 register_shutdown_function(array($objPage, "destroy")); 248 $objPage->init(); 249 $objPage->process(); 250 251 if (DEBUG_MODE == true) { 252 print('<pre>'); 253 print_r(htmlspecialchars($errmsg, ENT_QUOTES, CHAR_CODE)); 254 print('</pre>'); 228 function send_err_mail($pearResult, $sql){ 229 230 $errmsg = $sql . "\n\n"; 231 232 // PEAR エラーを伴う場合 233 if (!is_null($pearResult)) { 234 $errmsg .= $pearResult->message . "\n\n"; 235 $errmsg .= $pearResult->userinfo . "\n\n"; 236 $errmsg .= SC_Utils_Ex::sfBacktraceToString($pearResult->backtrace); 237 } 238 // (上に該当せず)バックトレースを生成できる環境(一般的には PHP 4 >= 4.3.0, PHP 5)の場合 239 else if (function_exists("debug_backtrace")) { 240 $errmsg .= SC_Utils_Ex::sfBacktraceToString(array_slice(debug_backtrace(), 2)); 255 241 } 256 242 257 243 GC_Utils_Ex::gfPrintLog($errmsg); 258 244 trigger_error($errmsg, E_USER_ERROR); 259 245 exit(); 260 246 } 247 248 /** 249 * 直前に実行されたSQL文を取得する. 250 * 251 * @param boolean $disp trueの場合、画面出力を行う. 252 * @return string SQL文 253 */ 254 function getLastQuery($disp = true) { 255 $sql = $this->conn->last_query; 256 if($disp) { 257 print($sql.";<br />\n"); 258 } 259 return $sql; 260 } 261 261 } 262 263 262 ?>
Note: See TracChangeset
for help on using the changeset viewer.