| 1 | <?php |
---|
| 2 | /* |
---|
| 3 | * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. |
---|
| 4 | * |
---|
| 5 | * http://www.lockon.co.jp/ |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | $current_dir = realpath(dirname(__FILE__)); |
---|
| 9 | require_once($current_dir . "/../module/DB.php"); |
---|
| 10 | |
---|
| 11 | $objDbConn = ""; |
---|
| 12 | |
---|
| 13 | class SC_DbConn{ |
---|
| 14 | |
---|
| 15 | var $conn; |
---|
| 16 | var $result; |
---|
| 17 | var $includePath; |
---|
| 18 | var $error_mail_to; |
---|
| 19 | var $error_mail_title; |
---|
| 20 | var $dsn; |
---|
| 21 | var $err_disp = true; |
---|
| 22 | var $dbFactory; |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | // コンストラクタ |
---|
| 26 | function SC_DbConn($dsn = "", $err_disp = true, $new = false){ |
---|
| 27 | global $objDbConn; |
---|
| 28 | |
---|
| 29 | // Debugモード指定 |
---|
| 30 | $options['debug'] = PEAR_DB_DEBUG; |
---|
| 31 | // 既に接続されていないか、新規接続要望の場合は接続する。 |
---|
| 32 | if(!isset($objDbConn->connection) || $new) { |
---|
| 33 | if($dsn != "") { |
---|
| 34 | $objDbConn = DB::connect($dsn, $options); |
---|
| 35 | $this->dsn = $dsn; |
---|
| 36 | } else { |
---|
| 37 | if(defined('DEFAULT_DSN')) { |
---|
| 38 | $objDbConn = DB::connect(DEFAULT_DSN, $options); |
---|
| 39 | $this->dsn = DEFAULT_DSN; |
---|
| 40 | } else { |
---|
| 41 | return; |
---|
| 42 | } |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | $this->conn = $objDbConn; |
---|
| 46 | $this->error_mail_to = DB_ERROR_MAIL_TO; |
---|
| 47 | $this->error_mail_title = DB_ERROR_MAIL_SUBJECT; |
---|
| 48 | $this->err_disp = $err_disp; |
---|
| 49 | $this->dbFactory = SC_DB_DBFactory::getInstance(); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | // クエリの実行 |
---|
| 53 | function query($n ,$arr = "", $ignore_err = false){ |
---|
| 54 | // mysqlの場合にはビュー表を変換する |
---|
| 55 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
---|
| 56 | |
---|
| 57 | if ( $arr ) { |
---|
| 58 | $result = $this->conn->query($n, $arr); |
---|
| 59 | } else { |
---|
| 60 | $result = $this->conn->query($n); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | if ($this->conn->isError($result) && !$ignore_err){ |
---|
| 64 | $this->send_err_mail ($result, $n); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | $this->result = $result; |
---|
| 68 | return $this->result; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | // 一件のみ取得 |
---|
| 72 | function getOne($n, $arr = ""){ |
---|
| 73 | |
---|
| 74 | // mysqlの場合にはビュー表を変換する |
---|
| 75 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
---|
| 76 | |
---|
| 77 | if ( $arr ) { |
---|
| 78 | $result = $this->conn->getOne($n, $arr); |
---|
| 79 | } else { |
---|
| 80 | $result = $this->conn->getOne($n); |
---|
| 81 | } |
---|
| 82 | if ($this->conn->isError($result)){ |
---|
| 83 | $this->send_err_mail ($result ,$n); |
---|
| 84 | } |
---|
| 85 | $this->result = $result; |
---|
| 86 | |
---|
| 87 | return $this->result; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | function getRow($n, $arr = ""){ |
---|
| 91 | |
---|
| 92 | // mysqlの場合にはビュー表を変換する |
---|
| 93 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
---|
| 94 | |
---|
| 95 | if ( $arr ) { |
---|
| 96 | $result = $this->conn->getRow($n, $arr); |
---|
| 97 | } else { |
---|
| 98 | $result = $this->conn->getRow($n); |
---|
| 99 | } |
---|
| 100 | if ($this->conn->isError($result)){ |
---|
| 101 | $this->send_err_mail ($result ,$n); |
---|
| 102 | } |
---|
| 103 | $this->result = $result; |
---|
| 104 | return $this->result; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // SELECT文の実行結果を全て取得 |
---|
| 108 | function getAll($n, $arr = ""){ |
---|
| 109 | |
---|
| 110 | // mysqlの場合にはビュー表を変換する |
---|
| 111 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
---|
| 112 | |
---|
| 113 | if(PEAR::isError($this->conn)) { |
---|
| 114 | if(ADMIN_MODE){ |
---|
| 115 | SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:" . $this->dsn); |
---|
| 116 | }else{ |
---|
| 117 | SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:"); |
---|
| 118 | } |
---|
| 119 | return 0; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | if ( $arr ){ |
---|
| 123 | $result = $this->conn->getAll($n, $arr, DB_FETCHMODE_ASSOC); |
---|
| 124 | } else { |
---|
| 125 | $result = $this->conn->getAll($n, DB_FETCHMODE_ASSOC); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | if ($this->conn->isError($result)){ |
---|
| 129 | $this->send_err_mail ($result, $n); |
---|
| 130 | } |
---|
| 131 | $this->result = $result; |
---|
| 132 | |
---|
| 133 | return $this->result; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | function autoExecute($table_name, $fields_values, $sql_where = null){ |
---|
| 137 | |
---|
| 138 | if ( $sql_where ) { |
---|
| 139 | $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_UPDATE, $sql_where); |
---|
| 140 | } else { |
---|
| 141 | $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_INSERT); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | if ($this->conn->isError($result)){ |
---|
| 145 | $this->send_err_mail ($result, $n); |
---|
| 146 | } |
---|
| 147 | $this->result = $result; |
---|
| 148 | return $this->result; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | function prepare($n){ |
---|
| 153 | global $sql; |
---|
| 154 | $sql = $n; |
---|
| 155 | $result = $this->conn->prepare($n); |
---|
| 156 | $this->result = $result; |
---|
| 157 | return $this->result; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | function execute($n, $obj){ |
---|
| 161 | global $sql; |
---|
| 162 | $sql = $n; |
---|
| 163 | $result = $this->conn->execute($n, $obj); |
---|
| 164 | $this->result = $result; |
---|
| 165 | return $this->result; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | function reset(){ |
---|
| 169 | $this->conn->disconnect(); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | function send_err_mail($result, $sql){ |
---|
| 173 | $url = ''; |
---|
| 174 | $errmsg = ''; |
---|
| 175 | |
---|
| 176 | if ($_SERVER['HTTPS'] == "on") { |
---|
| 177 | $url = "https://"; |
---|
| 178 | } else { |
---|
| 179 | $url = "http://"; |
---|
| 180 | } |
---|
| 181 | $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
---|
| 182 | |
---|
| 183 | $errmsg = $url."\n\n"; |
---|
| 184 | $errmsg .= $sql . "\n"; |
---|
| 185 | $errmsg .= $result->message . "\n\n"; |
---|
| 186 | $errmsg .= $result->userinfo . "\n\n"; |
---|
| 187 | |
---|
| 188 | if ($this->err_disp && DEBUG_MODE === true) { |
---|
| 189 | print('<pre>'); |
---|
| 190 | print_r(htmlspecialchars($errmsg, ENT_QUOTES, CHAR_CODE)); |
---|
| 191 | print('</pre>'); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | GC_Utils_Ex::gfDebugLog($errmsg); |
---|
| 195 | |
---|
| 196 | exit(); |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | ?> |
---|