<?php
/*
 PEAR DBによる簡単DB利用クラス

・接続
$dsn = 'mysql://root@localhost/gourmet';
$this->db =& new easy_db($dsn);

・Select
$this->db->select_row('table_name',"where id=1");

・Insert
$insert_set['name'] = "name";
$insert_set['tel'] = "03-3xxx-xxxx";
$this->db->insert_row('table_name',$insert_set);

・Update
$update_set['name'] = "name";
$update_set['tel'] = "03-3xxx-xxxx";
$condition = "where id=1";
$this->db->update_row('table_name',$update_set,$condition);

*/
require_once 'DB.php';

class easy_db {

var $dsn;	// the Data Source Name
var $db;	// DB ハンドラ

// コンストラクタ
function easy_db($dsn) {
	$this->dsn = $dsn;
}

// DBに接続
function connect_db($dsn) {
	if(!$dsn) $dsn = $this->dsn;
	$db = DB::connect($dsn);
	if(DB::isError($db))
		die('DB接続エラー:'.DB::errorMessage($db)."\n");
	return $db;
}

// DBの接続を解除
function disconnect_db() {
	if($this->db) $this->db->disconnect();
}

// すべての行を連想配列で返す
function select_all($table,$condition,$fields='*') {
	if(!$this->db) $this->db = $this->connect_db($this->dsn);
	$sql = "SELECT $fields FROM $table $condition";
	$res = $this->db->query($sql);
	if (PEAR::isError($res)) die($res->getMessage());
	while($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
		$data[] = $row;
	}
	return $data;
}

// 最初の行を連想配列で返す
function select_row($table,$condition) {
	if(!$this->db) $this->db = $this->connect_db($this->dsn);
	$this->db->setFetchMode(DB_FETCHMODE_ASSOC);
	$data =& $this->db->getRow("SELECT * FROM $table $condition");
	if (PEAR::isError($data)) die($data->getMessage());
	return $data;
}

// すべての行をselectし、結果ハンドラを返す
function get_table($table,$condition,$fields='*') {
	if(!$this->db) $this->db = $this->connect_db($this->dsn);
	$sql = "SELECT $fields FROM $table $condition";
	$res = $this->db->query($sql);
	if (PEAR::isError($res)) die($res->getMessage());
	return $res;
}

//
function get_row($res) {
	$row = $res->fetchRow(DB_FETCHMODE_ASSOC);
	return $row;
}


// 最初の行の最初のデータをスカラで返す
function select_one($table,$condition,$fields='*') {
	if(!$this->db) $this->db = $this->connect_db($this->dsn);
	$data =& $this->db->getOne("SELECT $fields FROM $table $condition");
	if (PEAR::isError($data)) die(__LINE__.':'.$data->getMessage());
	return $data;
}


// INSERTを実行し、すでにあるならUPDATEを実行
function save_row($table_name,$fields_values,$condition) {
	if(!$this->db) $this->db = $this->connect_db($this->dsn);
	$res = $this->db->autoExecute($table_name, $fields_values,
											DB_AUTOQUERY_INSERT);
	if(PEAR::isError($res)) {
		if($res->getMessage()=='DB Error: already exists') {
			$res = $this->db->autoExecute($table_name, $fields_values,
								DB_AUTOQUERY_UPDATE,$condition);
			if(PEAR::isError($res)) die(__LINE__.':'.$res->getMessage());
		}
	}
	//$this->db->freePrepared($res);
	return $res;
}



// INSERTを実行して、成功したか結果を返す
function insert_row($table_name,$fields_values) {
	if(!$this->db) $this->db = $this->connect_db($this->dsn);
	$res = $this->db->autoExecute($table_name, $fields_values,
											DB_AUTOQUERY_INSERT);
	if(PEAR::isError($res)) die(__LINE__.':'.$res->getMessage());
	//$this->db->freePrepared($res);
	return $res;
}

// UPDATEを実行して、成功したか結果を返す
function update_row($table_name,$fields_values,$condition) {
	if(!$this->db) $this->db = $this->connect_db($this->dsn);
	$res = $this->db->autoExecute($table_name, $fields_values,
								DB_AUTOQUERY_UPDATE,$condition);
	if(PEAR::isError($res)) die(__LINE__.':'.$res->getMessage());
	//$this->db->freePrepared($res);
	return $res;
}


// INSERTのPreparedステートメントを生成
function get_insert_stmt($table_name,$fields_set_array) {
	if(!$this->db) $this->db = $this->connect_db($this->dsn);
	$stmt = $this->db->autoPrepare($table_name,$fields_set_array,
										DB_AUTOQUERY_INSERT);

	if(PEAR::isError($stmt)) die(__LINE__.':'.$stmt->getMessage());
	return $stmt;
}

// UPDATEのPreparedステートメントを生成
function get_update_stmt($table_name,$fields_set_array,$condition) {
	if(!$this->db) $this->db = $this->connect_db($this->dsn);
	$stmt = $this->db->autoPrepare($table_name,$fields_set_array,
									DB_AUTOQUERY_UPDATE,$condition);

	if(PEAR::isError($stmt)) die(__LINE__.':'.$stmt->getMessage());
	return $stmt;
}

// INSERT or UPDATEのPreparedステートメントを実行して、影響した行数を返す
function execute_save_row($stmt,$values_set_array) {
	if(!$this->db) $this->db = $this->connect_db($this->dsn);
	$res =& $this->db->execute($stmt,$values_set_array);
	if (PEAR::isError($res)) die($res->getMessage());
	$num = $this->db->affectedRows();
	return $num;
}

// DELETEを実行して、削除した行数を返す
function delete_rows($table_name,$condition) {
	if(!$this->db) $this->db = $this->connect_db($this->dsn);
	$sql = "DELETE FROM $table_name $condition";
	$res =& $this->db->query($sql);
	if (PEAR::isError($res)) die($res->getMessage());
	$num = $this->db->affectedRows();
	return $num;
}

// SQLを実行して、影響したした行数を返す
function exec_sql($sql) {
	if(!$this->db) $this->db = $this->connect_db($this->dsn);
	$res =& $this->db->query($sql);
	if (PEAR::isError($res)) die($res->getMessage());
	$num = $this->db->affectedRows();
	return $num;
}



} // end of easy_db class


?>