 |
|
|  |
ÃÊ°£´Ü php - oracle ¿¬µ¿ Ŭ·¡½º |
|
|
 |
14³â Àü |
PHP¿¡¼ ¿À¶óŬ ¿¬µ¿ÇÏ·Á¸é Á» °íÅ뽺·´´Ù.
¹º°¡ mySQLÀ̶û Á» ´Ù¸¥ ºÎºÐµµ ÀÖ´Â°Í °°°í..
ÇÏ¿© Ŭ·¡½º·Î ¸¸µé¾îº¸¾Ò´Ù.
class Oracle
{
var $DBID = "id";
var $DBPW = "pw";
var $DBSID = "sid";
var $conn;
var $stmt;
var $error = false; // ¿¡·¯ ¹ß»ýÇϸé true ·Î ¼öÁ¤µÊ. commit,rollback °áÁ¤¿¡ »ç¿ë
var $transaction = false; // true ¸é auto commit ¾ÊÇÔ
var $bind = array();
var $data_size = array();
// php4 ÀÇ »ý¼ºÀÚ
function Oracle(){
$this->connect();
}
// php5 ÀÇ »ý¼ºÀÚ
function __construct(){
$this->connect();
}
// php5 ÀÇ ¼Ò¸êÀÚ
function __destruct(){
$this->disconnect();
}
function connect(){
if(!$this->conn)
$this->conn = OCILogon($this->DBID,$this->DBPW,$this->DBSID);
}
function disConnect(){
if($this->stmt)
@OCIFreeStatement($this->stmt);
if($this->conn)
@OCILogoff($this->conn);
}
// ¹ÙÀε庯¼ö °ª ÁöÁ¤
// °°Àº °ªÀÌ¶óµµ executeDML() È£ÃâÀü¿¡ ¹Ýµå½Ã ¸Å¹ø È£ÃâÇØ¾ß ÇÔ(executeDML() ÇÔ¼öÈ£ÃâÈÄ ÃʱâȵǹǷÎ)
function setBind($bind){
if(is_array($bind))
$this->bind = $bind;
else if($bind)
$this->bind = array($bind);
}
// ¹ÙÀε庯¼ö »çÀÌÁî ÁöÁ¤. ÁöÁ¤¾ÈÇϸé Çش纯¼öÀÇ ÃÖ´ë»çÀÌÁî°¡ ±âº»°ªÀÓ
function setDataSize($data_size){
if(is_array($data_size))
$this->data_size = $data_size;
else if($data_size)
$this->data_size = array($data_size);
}
// Äõ¸®¹® °á°ú¸¦ '´ÙÁß¹è¿($rs[Çʵå¸í][À妽º])'·Î ¸®ÅÏÇÑ´Ù
// $preferch_size´Â °¡Á®¿Ã ·¹ÄÚµå°Ç¼ö¸¦ ÁöÁ¤ÇÔ(¿É¼Ç)
function selectList($query,$preferch_size=1){
$this->connect();
$this->stmt = OCIparse($this->conn,$query);
if($this->stmt){
$this->bindByName();
$this->prefetch($preferch_size);
if($this->transaction){
@OCIexecute($this->stmt,OCI_DEFAULT);
$this->error();
}else{
@OCIexecute($this->stmt);
}
$rows = @OCIFetchStatement($this->stmt,$rs);
if($rows){
if(!$this->transaction)
@OCIFreeStatement($this->stmt);
return $rs;
}
}
return array();
}
// Äõ¸®¹® °á°ú 1°ÇÀ» '¹è¿($rs[Çʵå¸í or À妽º])'·Î ¸®ÅÏÇÑ´Ù
// $option Àº OCI_ASSOC(Çʵå¸í) or OCI_NUM(À妽º) À» ÁöÁ¤(¿É¼Ç)
function selectRow($query,$option=OCI_ASSOC){
$this->connect();
$this->stmt = OCIparse($this->conn,$query);
if($this->stmt){
$this->bindByName();
if($this->transaction){
@OCIexecute($this->stmt,OCI_DEFAULT);
$this->error();
}else{
@OCIexecute($this->stmt);
}
if(@OCIFetchInto($this->stmt,$rs,$option+OCI_RETURN_NULLS)){
if(!$this->transaction)
@OCIFreeStatement($this->stmt);
return $rs;
}
}
return array();
}
// ÇÑ°³ °ª¸¸ ¸®ÅÏÇÏ´Â Äõ¸®¹®À» ó¸®ÇÑ´Ù
// ROWID,LOB,FILE µî ¿Ü¿¡´Â ½ºÆ®¸µÀ¸·Î ¹ÝȯÇÑ´Ù
function selectOne($query){
$this->connect();
$this->stmt = OCIparse($this->conn,$query);
if($this->stmt){
$this->bindByName();
if($this->transaction){
@OCIexecute($this->stmt,OCI_DEFAULT);
$this->error();
}else{
@OCIexecute($this->stmt);
}
if(@OCIFetch($this->stmt)){
// Àμö 1Àº Ä÷³¼ø¼ À妽º(1ºÎÅÍ ½ÃÀÛÇÔ¿¡ ÁÖÀÇ!,Ä÷³¸í ÁöÁ¤µµ °¡´É)
$value = @OCIResult($this->stmt,1);
if(!$this->transaction)
@OCIFreeStatement($this->stmt);
return $value;
}
}
}
// insert,update,deleteµîÀ» ½ÇÇàÈÄ ¿µÇâ¹ÞÀº ·Î¿ìÀÇ °¹¼ö¸¦ ¸®ÅÏÇÑ´Ù(auto commitÀÏ °æ¿ì¸¸ °¹¼ö¸¦ ¸®ÅÏÇÔ)
// $transaction=true ÀÏ °æ¿ì ¸¶Áö¸·¿¡ ¹Ýµå½Ã commit() ÇÔ¼ö¸¦ È£ÃâÇÑ´Ù
function executeDML($sql){
$this->connect();
$this->stmt = OCIparse($this->conn,$sql);
if($this->stmt){
$this->bindByName();
if($this->transaction){ // auto commit ÀÌ ¾Æ´Ò °æ¿ì
@OCIexecute($this->stmt,OCI_DEFAULT);
$this->error();
}else{
@OCIexecute($this->stmt);
$num = @OCIRowCount($this->stmt);
@OCIFreeStatement($this->stmt);
return $num;
}
}
}
// auto commit ¾ÊÇÏ°í ¸í½ÃÀûÀ¸·Î transaction ½ÃÀÛ
// executeDML() È£ÃâÈÄ ¸¶Áö¸·¿¡ ¹Ýµå½Ã commit() ÇÔ¼ö¸¦ È£ÃâÇÑ´Ù
function transaction(){
$this->transaction = true;
}
// transaction ¿Ï·áÈÄ commit À̸é true¸¦ ¸®ÅÏÇÔ
// executeDML() ¿¡¼ ¿¡·¯°¡ Çϳª¶óµµ ¹ß»ýÇϸé ÀÚµ¿ rollback µÊ
function commit(){
if(!$this->error){
@OCICommit($this->conn);
$commit = true;
}else{
@OCIRollback($this->conn);
$commit = false;
}
if($this->stmt)
@OCIFreeStatement($this->stmt);
$this->transaction = false;
$this->error = false;
return $commit;
}
// $transaction=true ÀÏ °æ¿ì ¸Å¹ø executeDML ¿¡¼ ÀÚµ¿È£ÃâµÈ´Ù
// ¿¡·¯ ¹ß»ýÇÒ °æ¿ì commit,rollbackÀÇ ÆǴܱâÁØÀÎ $error °ªÀ» º¯°æ
function error(){
if($error = @OCIError($this->stmt)){
//echo "<p> Error is : " . $error["code"] . " - " . $error["message"] . "<p>";
$this->error = true;
}
}
// :b1,:b2,:b3...¿¡ ¹ÙÀÎµå º¯¼ö ÁöÁ¤
// ¹ÙÀε庯¼ö¸íÀº ¹Ýµå½Ã :b1,:b2,:b3... À¸·Î ÁöÁ¤ÇÑ´Ù
function bindByName(){
$size = sizeof($this->bind);
for($i=0 ; $i < $size ; $i++){
$ds = $this->data_size[$i];
if(!$ds) $ds = -1;
@OCIBindByName($this->stmt,":b".($i+1),$this->bind[$i],$ds);
}
$this->bind = array();
$this->data_size = array();
}
// ¿À¶óŬ Ŭ¶óÀ̾ðÆ® ¹öÆÛ¿¡ ÀúÀåµÇ´Â ·¹ÄÚµåÀÇ ¼ö¸¦ ÁöÁ¤
// ¿©·¯ ·¹Äڵ带 selectÇÒ °æ¿ì µðÆúÆ® °ªÀÌ À۾Ƽ ºñÈ¿À²ÀûÀÌ¸é °¡Á®¿Ã °Ç¼ö¸¸Å ÁöÁ¤
function prefetch($preferch_size){
if($preferch_size > 1)
@OCISetPrefetch($this->stmt,$preferch_size);
}
}
################# ¿©±â¼ºÎÅÍ »ç¿ë¿¹ ÀÔ´Ï´Ù #################
1. ¿©·¯°ÇÀÇ ·¹Äڵ带 select ÇÒ °æ¿ì
$db = new Oracle();
$query = "select title,name,date from tb where ... »ý·«";
$rs = $db->selectList($query);
for( $i=0 ; $i < sizeof($rs["TITLE"]) ; $i++ ){
echo $rs["TITLE"][$i] . $rs["NAME"][$i] . $rs["DATE"][$i] . "<br>";
}
* ÁÖÀÇÇÏ½Ç°Ç ²À ¹è¿¿¡ ´ë¹®ÀÚ·Î ÀûÀ¸¼Å¾ß ÇÑ´Ù´Â °Ì´Ï´Ù
2. ÇÑ°ÇÀÇ ·¹Äڵ带 select ÇÒ °æ¿ì
$query = "select title,name,date from tb where rownum=1";
$rs = $db->selectRow($query);
echo $rs["TITLE"] . $rs["NAME"] . $rs["DATE"];
3. ÇÑ°³°ªÀ» select ÇÒ °æ¿ì
$query = "select count(*) from tb";
$value = $db->selectOne($query);
echo "ÃÑ " . $value . " °Ç ÀÔ´Ï´Ù";
4. insert,update,delete ÇÒ °æ¿ì
$sql = "insert into tb (title,name) values ('$title','$name')";
$rs = $db->executeDML($sql);
if($rs){
echo $rs . " °ÇÀÇ µ¥ÀÌÅ͸¦ ó¸®ÇÏ¿´½À´Ï´Ù";
}
5. Æ®·£Á§¼Ç ó¸®
$sql1 = "insert into tb1 ... »ý·«";
$sql2 = "insert into tb2 ... »ý·«";
$sql3 = "insert into tb3 ... »ý·«";
$db->transaction();
$db->executeDML($sql1);
$db->executeDML($sql2);
$db->executeDML($sql3);
$rs = $db->commit();
if($rs){
echo "ó¸® ¼º°ø!";
}else{
echo "¿¡·¯ ¹ß»ý!";
}
transaction() ÇÔ¼ö¸¦ È£ÃâÇϸé auto commit ÀÌ false °¡ µÇ¸ç ¸¶Áö¸·¿¡ commit() ÇÔ¼ö¸¦ È£ÃâÇÏ¸é ¿¡·¯°¡ Çϳª¶óµµ ¹ß»ýÇϸé ÀÚµ¿À¸·Î ·Ñ¹éµË´Ï´Ù ¹°·Ð ¿¡·¯°¡ ¾øÀ¸¸é Ä¿¹ÔµÇ±¸¿ä ÇÔ¼ö¸íÀÌ commit À̶ó°í ¹«Á¶°Ç Ä¿¹ÔµÇ´Â°Å ¾Æ´Õ´Ï´Ù ^^
transaction() ÇÔ¼ö¸¦ È£Ãâ¾ÊÇÏ°í executeDML()À» »ç¿ëÇϸé Ç×»ó auto commit ÀÔ´Ï´Ù
6. ¹ÙÀε庯¼ö »ç¿ë
$query = "select name from tb where id=:b1 and pw=:b2";
$bind = array($id,$pw);
$db->setBind($bind);
$name = $db->selectOne($query);
echo $name;
¹ÙÀÎµå º¯¼ö¸íÀº ²À :b1,:b2,:b3... ÀÌ·±½ÄÀ¸·Î ÁöÁ¤Çϸç setBind() ÇÔ¼ö¿¡ ¹è¿·Î °ªÀ» ³Ñ°ÜÁÝ´Ï´Ù
¹ÙÀε庯¼ö°¡ :b1 ÇÑ°³¸¸ »ç¿ëµÈ °æ¿ì´Â ¹è¿ÀÌ ¾Æ´Ï¶ó °ªÇϳª¸¸ ³Ñ°Üµµ µË´Ï´Ù(¾Æ·¡¿¹ ó·³)
³Ñ°ÜÁø °ªÀº selectXxx(),executeDML()µîÀÇ ÇÔ¼ö»ç¿ëÈÄ¿¡ ÃʱâȵǹǷΠÇÔ¼ö ½ÇÇàÀü¿¡ ¸Å¹ø setBind() ÇÔ¼ö¸¦ È£ÃâÇØ¾ß ÇÕ´Ï´Ù
¾Æ·¡¿¹´Â Ʋ¸° »ç¿ë¿¹ÀÔ´Ï´Ù $cnt2 ¿¡´Â ¾Æ¹«°ªµµ ¾ø½À´Ï´Ù
$query1 = "select count(*) from tb1 where id=:b1";
$query2 = "select count(*) from tb2 where id=:b1";
$db->setBind($id);
$cnt1 = $db->selectOne($query1);
$cnt2 = $db->selectOne($query2);
¾Æ·¡¿¹°¡ ¿ÇÀº »ç¿ë¿¹ÀÔ´Ï´Ù
$db->setBind($id);
$cnt1 = $db->selectOne($query1);
$db->setBind($id);
$cnt2 = $db->selectOne($query2);
7. Æ®·£Á§¼Ç ó¸®¿Í ¹ÙÀÎµå º¯¼ö »ç¿ë
$sql1 = "insert into tb(title,name) values(:b1,:b2)";
$sql2 = "update tb2 set addr=:b1 where id=:b2";
$bind1 = array($title,$name);
$bind2 = array($addr,$id);
$db->transaction();
$db->setBind($bind1);
$db->executeDML($sql1);
$db->setBind($bind2);
$db->executeDML($sql2);
$rs = $db->commit();
if($rs){
echo "ó¸® ¼º°ø!";
}else{
echo "¿¡·¯ ¹ß»ý!";
}
|
|
̵̧ : 330 |
̵̧
¸ñ·Ï
|
|
|  |
|