Created
April 14, 2012 08:52
-
-
Save kor-trickster/2382966 to your computer and use it in GitHub Desktop.
[PHP] postgreSQL wrapperClass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<?php | |
const PGSQL_NOT_QUOTES_NOT_EQUAL = 0; | |
const PGSQL_QUOTES_NOT_EQUAL = 1; | |
const PGSQL_QUOTES_EQUAL = 2; | |
class PgSqlControler{ | |
private $conn; | |
private $host=""; | |
private $db=""; | |
private $user=""; | |
private $pw=""; | |
private $port=5432; | |
function PgSqlControler(){ | |
} | |
/** | |
* PostgreSQL CONNECT by C'est-bon. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $host : | |
* @param string $db : | |
* @param string $user : | |
* @param string $pw : | |
* @param string $port : default 5432 | |
* | |
* @return bool $result | |
*/ | |
function Connect($host, $db, $user, $pw, $port=5432){ | |
$this->host = $host; | |
$this->db = $db; | |
$this->pw = $pw; | |
$this->user = $user; | |
$this->port = $port; | |
$this->conn = pg_connect("host=".$host." port=".$port." dbname=".$db." user=".$user." password=".$pw); | |
return $this->conn; | |
} | |
/** | |
* PostgreSQL SELECT by C'est-bon. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $tableName : 테이블 명 | |
* @param array $columns (또는 string "*") : 테이블에서 검색할 컬럼값 배열. 또는 *로 전체 컬럼 검색(default). | |
* @param string $condition : 조건( WHERE절 ). 그냥 문자열로 때려박으셈. 이건 내 능력부족. 수정해주시면 감사. | |
* | |
* @return array $array : 성공 or 실패. 쿼리 실패면 Error 출력. | |
*/ | |
function Select($tableName, $columns = "*", $condition = null){ | |
if(strcmp($columns, "*") == 0) | |
$query = "SELECT * FROM ".$tableName; | |
else{ | |
$strValue = $this->ValueCreator($columns, PGSQL_NOT_QUOTES_NOT_EQUAL); | |
$query = "SELECT \"".$value."\" FROM ".$tableName; | |
} | |
if($condition != null) | |
$query = $query." WHERE ".$condition; | |
$result = $this->ExecQuery($query); | |
if(!$result){ | |
echo "Error in SQL query: ".pg_last_error(); | |
return $result; | |
} | |
$array; | |
$i = 0; | |
while($val = pg_fetch_array($result)){ | |
$array[$i++] = $val; | |
} | |
return $array; | |
} | |
/** | |
* PostgreSQL INSERT by C'est-bon. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $tableName : 테이블 명 | |
* @param array $values : 삽입할 값 배열(COLUMN NAME => VALUE). | |
* | |
* @return bool $result : 성공 or 실패. 쿼리 실패면 Error출력. | |
*/ | |
function Insert($tableName, $values){ | |
$columns = $this->ValueCreator(array_keys($values), PGSQL_NOT_QUOTES_NOT_EQUAL); | |
$insertValues = $this->ValueCreator(array_values($values), PGSQL_QUOTES_NOT_EQUAL); | |
$query = "INSERT INTO ".$tableName." ("; | |
$query = $query.$columns.") VALUES ("; | |
$query = $query.$insertValues.")"; | |
return $this->ExecQuery($query); | |
} | |
/** | |
* PostgreSQL UPDATE by C'est-bon. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $tableName : 테이블 명 | |
* @param array $values : 삽입할 값 배열(COLUMN NAME => VALUE). | |
* @param array $condition : 조건( WHERE절 ). 문자열로 박으셈. 내 능력 부족. (default : null) | |
* | |
* @return bool $result : 성공 or 실패. 쿼리 실패면 Error출력. | |
*/ | |
function Update($tableName, $values, $condition = null){ | |
$updateValues = $this->ValueCreator($values, PGSQL_QUOTES_EQUAL); | |
$query = "UPDATE ".$tableName." SET "; | |
$query = $query.$updateValues; | |
if($condition != null) | |
$query = $query." WHERE ".$condition; | |
echo "<br/>".$query."<br/>"; | |
return $this->ExecQuery($query); | |
} | |
/** | |
* PostgreSQL DELETE by C'est-bon. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $tableName : 테이블 명 | |
* @param array $condition : 조건( WHERE절 ). 문자열로 박으셈. 내 능력 부족. | |
* | |
* @return bool $result : 성공 or 실패. 쿼리 실패면 Error출력. | |
*/ | |
function Delete($tableName, $condition = null){ | |
$query = "DELETE FROM ".$tableName; | |
if($condition != null) | |
$query = $query." WHERE ".$condition; | |
return $this->ExecQuery($query); | |
} | |
private function ValueCreator($values, $mode){ | |
if($values == null) | |
return; | |
$i = 0; | |
foreach($values as $key => $value){ | |
if(($mode == PGSQL_NOT_QUOTES_NOT_EQUAL) || is_integer($value)){ | |
$query = $query."$value"; | |
} | |
else{ | |
if($mode == PGSQL_QUOTES_NOT_EQUAL) | |
$addStr = ""; | |
else if($mode == PGSQL_QUOTES_EQUAL) | |
$addStr = "$key"."="; | |
if(is_string($value)) | |
$addStr = $addStr."'$value'"; | |
else | |
$addStr = $addStr.$value; | |
$query = $query.$addStr; | |
} | |
if(++$i < count($values)) | |
$query = $query.", "; | |
} | |
return $query; | |
} | |
function ExecQuery($query){ | |
$result = pg_exec($this->conn, $query); | |
if(!$result) | |
echo "Error in SQL query: ".pg_last_error(); | |
return $result; | |
} | |
function Close(){ | |
pg_close($this->conn); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment