clear_querys(); $this -> clear_counts(); } //clear querys function clear_querys() { $this -> query_str = ""; $this -> query_where = ""; $this -> query_orderby = ""; $this -> query_groupby = ""; $this -> query_table = ""; $this -> query_field = ""; $this -> query_value = ""; } //clear counts function clear_counts() { $this -> currentFieldnum = 0; $this -> currentRownum = 0; } //make query function make( $_mode = "select"){ $ret_code = false; $this -> errorMessage = ""; switch ($_mode){ case "select"; if ($this -> query_field != "" ) { if ($this -> query_table != "" ) { $ret_code = true; $this -> query_str = "select ".$this ->query_field; $this -> query_str = $this -> query_str." from ".$this -> query_table; if ($this -> query_where != "" ) { $this -> query_str = $this -> query_str." where ".$this -> query_where; }; if ($this -> query_groupby != "" ) { $this -> query_str = $this -> query_str." group by ".$this -> query_groupby; }; if ($this -> query_orderby != "" ) { $this -> query_str = $this -> query_str." order by ".$this -> query_orderby; }; }; }; break; case "insert"; if ($this -> query_table != "" ) { if ($this -> query_value != "" ) { $ret_code = true; $this -> query_str = "insert into ".$this -> query_table; if ($this -> query_field != "" ) { $this -> query_str = $this -> query_str."(".$this -> query_field.")";}; $this -> query_str = $this -> query_str." values (".$this -> query_value.")"; }; }; break; case "update"; if ($this -> query_table != "" ) { if ($this -> query_value != "" ) { if ($this -> query_where != "" ) { $ret_code = true; $this -> query_str = "update ".$this -> query_table; $this -> query_str = $this -> query_str." set ".$this -> query_value; $this -> query_str = $this -> query_str." where ".$this -> query_where; }; }; }; break; case "delete"; if ($this -> query_table != "" ) { if ($this -> query_where != "" ) { $ret_code = true; $this -> query_str = "delete from ".$this -> query_table; $this -> query_str = $this -> query_str." where ".$this -> query_where; }; }; break; default; $this -> errorMessage = "対応していないmodeです.(select : insert : update : delete)"; } return $ret_code; } // execute query function exec( $_mode = "select"){ $ret_code = $this -> make($_mode); if ($ret_code) $ret_code = $this -> execute($this -> query_str); return $ret_code; } //(sub function) add querystr function _add(&$_pstr,$_note,$_mode){ if ($_mode == "new") $_pstr = ""; elseif($_pstr != "" ) $_pstr = $_pstr." ".$_mode." "; $_pstr = $_pstr.$_note; return true; } //add querystr function add( $_note = "",$_type = "select" , $_mode = "and"){ $ret_code = false; $this -> errorMessage = ""; // check mode switch($_mode){ case "and"; break; case "or"; break; case ","; break; case "new"; break; default; $this -> errorMessage = "指定できないmodeです.(and : or : , : new)"; return $ret_code; } if ($_note == ""){ $this -> errorMessage = "追加するクエリーがありません."; return $ret_code; } switch($_type){ case "select"; case "field"; $ret_code = $this->_add($this->query_field,$_note,$_mode); break; case "from"; case "into"; case "table"; $ret_code = $this->_add($this->query_table,$_note,$_mode); break; case "where"; $ret_code = $this->_add($this->query_where,$_note,$_mode); break; case "orderby"; $ret_code = $this->_add($this->query_orderby,$_note,$_mode); break; case "groupby"; $ret_code = $this->_add($this->query_groupby,$_note,$_mode); break; case "values"; case "set"; case "value"; // 旧クラスとの互換性のため $ret_code = $this->_add($this->query_value,$_note,$_mode); break; default; $this -> errorMessage = "指定できないtypeです.(select : from : where : orderby : groupby : table : values : set : field : into)"; } return $ret_code; } //set parameter function set($_field = "" , $_value = "", $_sign = "=", $_mode = "auto"){ $ret_code = false; if ($_field <> ""){ switch($_mode){ case "auto"; if(!is_numeric($_value)) $_value = "'".$_value."'"; break; case "str"; $_value = "'".$_value."'"; break; } $ret_code = $_field." ".$_sign." ".$_value; } return $ret_code; } //get fieldnames function getfieldname() { if ($this -> currentFieldnum >= $this -> colcount) { $this -> currentFieldnum = 0; return false; } else { //$s = pg_fieldname($this -> queryresult,$this -> currentFieldnum); // - PHP4.2.0 $s = pg_field_name($this -> queryresult,$this -> currentFieldnum); // PHP4.2.0 - ++$this -> currentFieldnum; return $s; }; } //get rows function getrow() { if ($this -> currentRownum >= $this -> rowcount) { $this -> currentRownum = 0; return false; } else { $s = pg_fetch_row($this -> queryresult, $this -> currentRownum); ++$this -> currentRownum; return $s; }; } } // pgsql base operation class class pgsql_base { // init var $dbhost = "localhost"; //Database Information var $dbname; var $dbuser; var $dbport = ""; var $dbpassword = ""; //dont change var $connection; //connection ID var $queryresult; //result ID var $errormessage; //ErrorMessage //useful property var $rowcount; var $colcount; //init function init() { $this -> connection = false; $this -> queryresult = false; $this -> errormessage = ""; $this -> rowcount = 0; $this -> colcount = 0; } //constructor function pgsql_base( $_dbname = "",$_dbuser = "") { $this -> dbname = $_dbname; $this -> dbuser = $_dbuser; $this -> init(); } //connect Database function connect() { $ret_code = false; $s = ""; if ($this -> dbhost != "") { $s .= "host=".$this -> dbhost; }; if ($this -> dbport != "") { $s .= " port=".$this -> dbport; }; if ($this -> dbname != "") { $s .= " dbname=".$this -> dbname; }; if ($this -> dbuser != "") { $s .= " user=".$this -> dbuser; }; if ($this -> dbpassword != "") { $s .= " password=".$this -> dbpassword; }; // connecting database if ($this -> connection) $this->close(); $this -> connection = pg_connect($s); if (!$this -> connection){ $this -> errormessage = "DB接続に失敗しました."; } else { $ret_code = $this -> connection; $this -> errormessage = "DB接続に成功しました."; } return $ret_code; } //close Database function close() { if ($this -> connection) pg_close($this -> connection); $this -> init(); return true; } //execute query function execute($str_query) { $ret_code = false; $this -> errormessage = ""; if (!$this -> connection) { $this -> errormessage = "DBに接続していません."; } else { //free result memorys if ($this -> queryresult) { //pg_freeresult($this -> queryresult); // - php4.2.0 pg_free_result($this -> queryresult); // php4.2.0- $this -> queryresult = false; }; //execute query //$this -> queryresult = pg_exec($this -> connection, $str_query); // - php4.2.0 $this -> queryresult = pg_query($this -> connection, $str_query); // php4.2.0 - if (!$this -> queryresult) { $this -> errormessage = "クエリ実行に失敗しました.\n原因: ".pg_result_error($this->connection); } else { $ret_code = $this -> queryresult; //get result information //$this -> rowcount = pg_numrows($this -> queryresult); // - php4.2.0 $this -> rowcount = pg_num_rows($this -> queryresult); // php4.2.0 - //$this -> colcount = pg_numfields($this -> queryresult); // - php4.2.0 $this -> colcount = pg_num_fields($this -> queryresult); // php4.2.0 - }; } return $ret_code; } } ?>