= Database 기본 실행 =

== db 연결 ==
{{{#!vim php
<?php 
$db = db_('mssql', 'test', true);
or 
$db = DBClient::createConnector('mssql', 'test', true); 
?>
}}}

db_ 는 [http://www.easylogic.co.kr/doc/php-db/DBClient.html#createConnector DBClient::createConnector] 의 편하게 사용해주는 함수이다.

== db 연결 확인 ==
{{{#!vim php 
<?php if ($db->isConnected()) { echo "연결 되었음"; }  ?>
}}}

== insert, delete, update 실행 ==
{{{#!vim php 
<?php 

// run sql string 
$sql = "insesrt into table values ('3', '2', '1')"; 
if ($db->query($sql)) { 
 echo "성공";
} else { 
 echo "실패";
} 

// run template
$sql = $db->sql('template.sql.insert', array(....));
if ($db->query($sql)) { 
 echo "성공";
} else { 
 echo "실패";
} 
?>
}}}

== select 실행 == 
{{{#!vim php 
<?php 

// sql 구문 실행 
$sql = "select * from db_root"; 
$data = $db->getData($sql);

while($data->next()) { 
.....
}

// 템플릿 구문 실행 
$data = $db->getData("template.sql.select", array(.....));

while($data->next()) { 
.....
}

?>
}}}

== 쿼리 실행 메소드 == 
{{{#!vim php
<?php
// sql 직접 실행 
$db->query($sql);
$db->getData($query, $isOne =false);
$db->getPageData($query, $page, $count);
$db->getOne($query);
$db->getPageCountQuery($sql, $limitPage, $limitBlock);

// template 화 된 sql 실행 
$db->sql($id, $param);
$db->sqlData($id, $param);
$db->sqlPage($id, $param);
$db->sqlOne($id, $param);
$db->sqlPageCount($id, $param);

// template 화 된 sql 체크 후 있으면 실행 , 없으면 sql 바로 실행
$db->data($id, $param);
$db->page($id, $param);
$db->one($id, $param);
$db->pageCount($id, $param);


?>
}}}

== 영향을 받은 행수 ==
{{{#!vim php 
<?php  echo $db->affectedRows(); ?>
}}}

=== 에러 출력 ===
{{{#!vim php 
<?php  echo $db->error(); ?>}}}

=== db문자열 escape 하기 ===
{{{#!vim php 
<?php  echo $db->escape($str); ?>}}}

=== 트랜잭션 ===
{{{#!vim php 
<?php  
$db->begin();
{ .... }
$db->commit();
or 
$db->rollback();
?>}}}

=== select 실행, DBData 객체 얻어오기 ===
{{{#!vim php 
<?php  
$sql = "select * from table ";
$data = $db->getData($sql);
echo $data;
?>}}}

=== record 이동 ===
{{{#!vim php 
<?php  
$sql = "select * from table";
$db->query($sql);
$db->seek(1); // record 1개 전진 
?>}}}

=== prepare 사용 insert, update, delete 실행 ===
{{{#!vim php 
<?php  
$db->prepare($sql);
$db->bind($name, $type, $output, $length, &$value); 
$db->execute();
?>}}}

=== prepare 사용 select 실행 DBData 객체 얻어오기 ===
{{{#!vim php 
<?php  
$db->prepare($sql);
$db->bind( $name, $type, $output, $length, &$value );
$db->execute();

$data = $db->getBindData();
?>}}}

=== 프로시저 실행 ===
{{{#!vim php 
<?php  
$db->spName('Proc_Name');
$db->spBind($name, $type, $output, $length, &$value);
$db->spExecute();
?>}}}

=== 프로시저 실행 DBData객체 얻어오기 ===
{{{#!vim php 
<?php  
$db->spName('Proc_Name');
$db->spBind($name, $type, $output, $length, &$value);
$db->spExecute();

$data = $db->getBindData();
?>
}}}

== insert, delete, update 바인딩 자동 생성 (미구현) ==
{{{#!vim php 
<?php  
$db->spName("table_name", "insert");
$db->spBind($name, $type, $output, $length, &$value);
$db->spExecute();

// insert into table_name values (?, ?, ?, ?); //  자동 바인딩 생성 

$db->spName("table_name", "delete");
$db->spBind($name, $type, $output, $length, &$value);
$db->spBind($name2, $type, $output, $length, &$value, array("op" => "or"));

$db->spExecute();

// delete from table_name where name = ? or name2 = ? //  자동 바인딩 생성 


$db->spName("table_name", "update");
$db->spBind($name, $type, $output, $length, &$value, array("update" => true));
$db->spBind($name2, $type, $output, $length, &$value);
$db->spBind($name3, $type, $output, $length, &$value, array("op" => "or"));
$db->spExecute();

// update table_name set name = ? where name2 = ? or name3 = ? //  자동 바인딩 생성 

?> 
}}}

=== 자동 바인딩 구문 생성 (미구현) === 

 * 옵션에 해당하는 구분을 prepare 가능한 문자열로 변환시켜준다. DB마다 다른 형태로 구현된다. 
 * mssql 2000은  [[sp_executesql]] 프로시저 이용해서 구현한다. 
 * mysql 에서는 따로 구현 할 수 있는 방법이 없기 때문에 자체적으로 구현한다. 
  * @변수 형태의 변수를 사용해서 바인딩 한다. mssql과 비슷한 방식(?)
 * mysql_i 에서는 prepare 구문을 이용한 바인딩을 한다.
 * 형식은 [[binding 방법 통일]] 참조

=== 메타데이타 얻어오기 === 

{{{#!vim php 
<?php  
// database 리스트 
$db->getMetaData("database"); 

// table 리스트 
$db->getMetaData("table"); 

// proc 리스트 
$db->getMetaData("proc"); 

// view 리스트 
$db->getMetaData("view"); 

// function 리스트 
$db->getMetaData("function"); 

// 인덱스 리스트 구하기 
$db->getMetaData("index", "test_table");

// column 리스트 
$db->getMetaData("column", "test_table"); 

// param 리스트 
$db->getMetaData("param", "test_proc"); 

// job 리스트 (미구현) 
$db->getMetaData("job"); 
?>}}}



