| 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | * Copyright(c) 2000-2006 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 |
|
|---|
| 23 | // ¥³¥ó¥¹¥È¥é¥¯¥¿
|
|---|
| 24 | function SC_DbConn($dsn = "", $err_disp = true, $new = false){
|
|---|
| 25 | global $objDbConn;
|
|---|
| 26 |
|
|---|
| 27 | // Debug¥â¡¼¥É»ØÄê
|
|---|
| 28 | $options['debug'] = PEAR_DB_DEBUG;
|
|---|
| 29 | // ´û¤ËÀܳ¤µ¤ì¤Æ¤¤¤Ê¤¤¤«¡¢¿·µ¬ÀܳÍ×˾¤Î¾ì¹ç¤ÏÀܳ¤¹¤ë¡£
|
|---|
| 30 | if(!isset($objDbConn->connection) || $new) {
|
|---|
| 31 | if($dsn != "") {
|
|---|
| 32 | $objDbConn = DB::connect($dsn, $options);
|
|---|
| 33 | $this->dsn = $dsn;
|
|---|
| 34 | } else {
|
|---|
| 35 | if(defined('DEFAULT_DSN')) {
|
|---|
| 36 | $objDbConn = DB::connect(DEFAULT_DSN, $options);
|
|---|
| 37 | $this->dsn = DEFAULT_DSN;
|
|---|
| 38 | } else {
|
|---|
| 39 | return;
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 | $this->conn = $objDbConn;
|
|---|
| 44 | $this->error_mail_to = DB_ERROR_MAIL_TO;
|
|---|
| 45 | $this->error_mail_title = DB_ERROR_MAIL_SUBJECT;
|
|---|
| 46 | $this->err_disp = $err_disp;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | // ¥¯¥¨¥ê¤Î¼Â¹Ô
|
|---|
| 50 | function query($n ,$arr = "", $ignore_err = false){
|
|---|
| 51 | if ( $arr ) {
|
|---|
| 52 | $result = $this->conn->query($n, $arr);
|
|---|
| 53 | } else {
|
|---|
| 54 | $result = $this->conn->query($n);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | if ($this->conn->isError($result) && !$ignore_err){
|
|---|
| 58 | $this->send_err_mail ($result, $n);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | $this->result = $result;
|
|---|
| 62 | return $this->result;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | // °ì·ï¤Î¤ß¼èÆÀ
|
|---|
| 66 | function getOne($n, $arr = ""){
|
|---|
| 67 |
|
|---|
| 68 | // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
|
|---|
| 69 | if (DB_TYPE == "mysql") $n = sfChangeMySQL($n);
|
|---|
| 70 |
|
|---|
| 71 | if ( $arr ) {
|
|---|
| 72 | $result = $this->conn->getOne($n, $arr);
|
|---|
| 73 | } else {
|
|---|
| 74 | $result = $this->conn->getOne($n);
|
|---|
| 75 | }
|
|---|
| 76 | if ($this->conn->isError($result)){
|
|---|
| 77 | $this->send_err_mail ($result ,$n);
|
|---|
| 78 | }
|
|---|
| 79 | $this->result = $result;
|
|---|
| 80 |
|
|---|
| 81 | //sfPrintR($this->conn->last_query);
|
|---|
| 82 |
|
|---|
| 83 | return $this->result;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | function getRow($n, $arr = ""){
|
|---|
| 87 |
|
|---|
| 88 | // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
|
|---|
| 89 | if (DB_TYPE == "mysql") $n = sfChangeMySQL($n);
|
|---|
| 90 |
|
|---|
| 91 | if ( $arr ) {
|
|---|
| 92 | $result = $this->conn->getRow($n, $arr);
|
|---|
| 93 | } else {
|
|---|
| 94 | $result = $this->conn->getRow($n);
|
|---|
| 95 | }
|
|---|
| 96 | if ($this->conn->isError($result)){
|
|---|
| 97 | $this->send_err_mail ($result ,$n);
|
|---|
| 98 | }
|
|---|
| 99 | $this->result = $result;
|
|---|
| 100 | return $this->result;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | // SELECTʸ¤Î¼Â¹Ô·ë²Ì¤òÁ´¤Æ¼èÆÀ
|
|---|
| 104 | function getAll($n, $arr = ""){
|
|---|
| 105 |
|
|---|
| 106 | // mysql¤Î¾ì¹ç¤Ë¤Ï¥Ó¥å¡¼É½¤òÊÑ´¹¤¹¤ë
|
|---|
| 107 | if (DB_TYPE == "mysql") $n = sfChangeMySQL($n);
|
|---|
| 108 |
|
|---|
| 109 | if(PEAR::isError($this->conn)) {
|
|---|
| 110 | sfErrorHeader("DB¤Ø¤ÎÀܳ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£:" . $this->dsn);
|
|---|
| 111 | return 0;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | if ( $arr ){
|
|---|
| 115 | $result = $this->conn->getAll($n, $arr, DB_FETCHMODE_ASSOC);
|
|---|
| 116 | } else {
|
|---|
| 117 | $result = $this->conn->getAll($n, DB_FETCHMODE_ASSOC);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | if ($this->conn->isError($result)){
|
|---|
| 121 | $this->send_err_mail ($result, $n);
|
|---|
| 122 | }
|
|---|
| 123 | $this->result = $result;
|
|---|
| 124 |
|
|---|
| 125 | //sfPrintR($this->conn->last_query);
|
|---|
| 126 |
|
|---|
| 127 | return $this->result;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | function autoExecute($table_name, $fields_values, $sql_where = null){
|
|---|
| 131 |
|
|---|
| 132 | if ( $sql_where ) {
|
|---|
| 133 | $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_UPDATE, $sql_where);
|
|---|
| 134 | } else {
|
|---|
| 135 | $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_INSERT);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | if ($this->conn->isError($result)){
|
|---|
| 139 | $this->send_err_mail ($result, $n);
|
|---|
| 140 | }
|
|---|
| 141 | $this->result = $result;
|
|---|
| 142 | return $this->result;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 | function prepare($n){
|
|---|
| 147 | global $sql;
|
|---|
| 148 | $sql = $n;
|
|---|
| 149 | $result = $this->conn->prepare($n);
|
|---|
| 150 | $this->result = $result;
|
|---|
| 151 | return $this->result;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | function execute($n, $obj){
|
|---|
| 155 | global $sql;
|
|---|
| 156 | $sql = $n;
|
|---|
| 157 | $result = $this->conn->execute($n, $obj);
|
|---|
| 158 | $this->result = $result;
|
|---|
| 159 | return $this->result;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | function reset(){
|
|---|
| 163 | $this->conn->disconnect();
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | function send_err_mail( $result, $sql ){
|
|---|
| 167 |
|
|---|
| 168 | if ($this->err_disp) {
|
|---|
| 169 | if ($_SERVER['HTTPS'] == "on") {
|
|---|
| 170 | $url = "https://";
|
|---|
| 171 | } else {
|
|---|
| 172 | $url = "http://";
|
|---|
| 173 | }
|
|---|
| 174 | $url.= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
|
|---|
| 175 |
|
|---|
| 176 | $errmsg = $url."\n\n";
|
|---|
| 177 | $errmsg.= "SERVER_ADDR:" . $_SERVER['SERVER_ADDR'] . "\n";
|
|---|
| 178 | $errmsg.= "REMOTE_ADDR:" . $_SERVER['REMOTE_ADDR'] . "\n";
|
|---|
| 179 | $errmsg.= "USER_AGENT:" . $_SERVER['HTTP_USER_AGENT'] . "\n\n";
|
|---|
| 180 | $errmsg.= $sql . "\n";
|
|---|
| 181 | $errmsg.= $result->message . "\n\n";
|
|---|
| 182 | $errmsg.= $result->userinfo . "\n\n";
|
|---|
| 183 |
|
|---|
| 184 | $arrRbacktrace = array_reverse($result->backtrace);
|
|---|
| 185 |
|
|---|
| 186 | foreach($arrRbacktrace as $backtrace) {
|
|---|
| 187 | if($backtrace['class'] != "") {
|
|---|
| 188 | $func = $backtrace['class'] . "->" . $backtrace['function'];
|
|---|
| 189 | } else {
|
|---|
| 190 | $func = $backtrace['function'];
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | $errmsg.= $backtrace['file'] . " " . $backtrace['line'] . ":" . $func . "\n";
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | mb_send_mail(DB_ERROR_MAIL_TO, DB_ERROR_MAIL_SUBJECT, $errmsg);
|
|---|
| 197 |
|
|---|
| 198 | exit;
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | ?> |
|---|