<?php
/**
* @(#)dbutil.php
* PHP version 5.2.3
*
*
* Copyright 2007 SoftRoad. All Rights Reserved.
*
*====================================================================
* 変更履歴
*
* 新規作成 2007 年5月17日 蒋彪
*/
require ("hashtable.php");
class DBUtil {
/*データベースのタイプ*/
var $dbType;
/*接続のタイプ*/
var $connectType;
/*接続号*/
var $idCon;
var $curRow; // current row number of data from the result
// associated with the specified result identifier array
var $seek; // current row number of data from DataSeek function array
/*
* データベースConnectionオブジェクトをゲットする。
*
* @return Connectionオブジェクト
*/
function connectDb($dbType, $connectType, $connect, $username = "", $password = "") {
$this->dbType = $dbType;
switch ($dbType) {
case "mssql" :
if ($connectType == "c") {
$idCon = mssql_connect($connect, $username, $password);
} else {
$idCon = mssql_pconnect($connect, $username, $password);
}
break;
case "mysql" :
if ($connectType == "c") {
$idCon = mysql_connect($connect, $username, $password);
} else {
$idCon = mysql_pconnect($connect, $username, $password);
}
break;
case "pg" :
if ($connectType == "c") {
$idCon = pg_connect($connect . " user=" . $username . " password=" . $password);
pg_set_client_encoding("SJIS");
} else {
$idCon = pg_pconnect($connect . " user=" . $username . " password=" . $password);
pg_set_client_encoding("SJIS");
}
break;
default :
$idCon = 0;
break;
}
$this->idCon = $idCon;
return $idCon;
}
/*
* データベースConnectionをクローズする。
*
* return TRUE,FALSE
*/
function closeDb() {
switch ($this->dbType) {
case "mssql" :
$r = mssql_close($this->idCon);
break;
case "mysql" :
$r = mysql_close($this->idCon);
break;
case "pg" :
$r = pg_close($this->idCon);
break;
default :
$r = False;
break;
}
return $r;
}
/*
* データベースを選びます
*
* return TRUE,FALSE
*/
function selectDb($dbName) {
switch ($this->dbType) {
case "mssql" :
$r = mssql_select_db($dbName);
break;
case "mysql" :
$r = mysql_select_db($dbName);
break;
case "pg" :
$r = False;
break;
default :
$r = False;
break;
}
return $r;
}
/*
* sql文を実行します
*
*/
function executeQuery($query) {
switch ($this->dbType) {
case "mssql" :
$r = mssql_query($query, $this->idCon);
break;
case "mysql" :
$r = mysql_query($query, $this->idCon);
break;
case "pg" :
$r = pg_query($this->idCon, $query);
break;
default :
$r = False;
break;
}
$this->curRow[$r] = 0;
$this->seek[$r] = 0;
return $r;
}
/*
* 記録のポインターを移動する
*
*/
function dataSeek($result, $RowNumber) {
switch ($this->dbType) {
case "mssql" :
$r = mssql_data_seek($result, $RowNumber);
break;
case "mysql" :
$r = mysql_data_seek($result, $RowNumber);
break;
case "pg" :
$r = False;
break;
default :
$r = False;
break;
}
$this->seek[$result] = (int) $RowNumber;
return $r;
}
/*
* カラム名を得ます
*
*/
function fieldName($result, $offset) {
switch ($this->dbType) {
case "mssql" :
$r = mssql_field_name($result, $offset);
break;
case "mysql" :
$r = mysql_field_name($result, $offset);
break;
case "pg" :
$r = pg_fieldname($result, $offset);
break;
default :
$r = False;
break;
}
return $r;
}
/*
* カラムのタイプを得ます
*
*/
function fieldType($result, $offset) {
switch ($this->dbType) {
case "mssql" :
$r = mssql_field_type($result, $offset);
break;
case "mysql" :
$r = mysql_field_type($result, $offset);
break;
case "pg" :
$r = pg_fieldtype($result, $offset);
break;
default :
$r = False;
break;
}
return $r;
}
/*
* カラムの長さを得ます
*
*/
function fieldLength($result, $offset) {
switch ($this->dbType) {
case "mssql" :
$r = mssql_field_length($result, $offset);
break;
case "mysql" :
$r = mysql_field_len($result, $offset);
break;
case "pg" :
$r = pg_fieldsize($result, $offset);
break;
default :
$r = False;
break;
}
return $r;
}
/*
* データを得する、配列を保存します。
*
*/
function fetchRow($result, $RowNumber = 0) {
switch ($this->dbType) {
case "mssql" :
$r = mssql_fetch_row($result);
break;
case "mysql" :
$r = mysql_fetch_row($result);
break;
case "pg" :
$r = pg_fetch_row($result, $RowNumber);
if ($r) {
$this->curRow[$result] = $RowNumber;
$this->seek[$result] = $RowNumber;
}
break;
default :
$r = False;
break;
}
return $r;
}
/*
* データを得する、配列を保存します。
*
*/
function fetchArray($result, $RowNumber = 0, $ResultType = 2) {
switch ($this->dbType) {
case "mssql" :
$r = mssql_fetch_array($result);
break;
case "mysql" :
$r = mysql_fetch_array($result);
break;
case "pg" :
$r = pg_fetch_array($result, $RowNumber, $ResultType);
if ($r) {
$this->curRow[$result] = $RowNumber;
$this->seek[$result] = $RowNumber;
}
break;
default :
$r = False;
break;
}
return $r;
}
/*
* データを得する、配列を保存します。
*
*/
function fetchObject($result, $RowNumber = 0, $ResultType = 2) {
switch ($this->dbType) {
case "mssql" :
$r = mssql_fetch_object($result);
break;
case "mysql" :
$r = mysql_fetch_object($result);
break;
case "pg" :
$r = pg_fetch_object($result, $RowNumber, $ResultType);
if ($r) {
$this->curRow[$result] = $RowNumber;
$this->seek[$result] = $RowNumber;
}
break;
default :
$r = False;
break;
}
return $r;
}
/*
* 当面行に指定カラムの値を得ます
*/
function getString($result, $RowNumber, $FieldName) {
switch ($this->dbType) {
case "mssql" :
$r = mssql_result($result, $RowNumber, $FieldName);
break;
case "mysql" :
$r = mysql_result($result, $RowNumber, $FieldName);
break;
case "pg" :
$r = pg_result($result, $RowNumber, $FieldName);
break;
default :
$r = False;
break;
}
return $r;
}
/*
*
* 記録集を釈放する
*/
function freeResult($result) {
switch ($this->dbType) {
case "mssql" :
$r = mssql_free_result($result);
break;
case "mysql" :
$r = mysql_free_result($result);
break;
case "pg" :
$r = pg_freeresult($result);
break;
default :
$r = False;
break;
}
return $r;
}
/*
*
* 記録数量を得ます
*/
function rowsNumber($result) {
switch ($this->dbType) {
case "mssql" :
$r = mssql_num_rows($result);
break;
case "mysql" :
$r = mysql_num_rows($result);
break;
case "pg" :
$r = pg_numrows($result);
break;
default :
$r = False;
break;
}
return $r;
}
/*
*
* カラムの数量を得ます
*/
function fieldsNumber($result) {
switch ($this->dbType) {
case "mssql" :
$r = mssql_num_fields($result);
break;
case "mysql" :
$r = mysql_num_fields($result);
break;
case "pg" :
$r = pg_numfields($result);
break;
default :
$r = False;
break;
}
return $r;
}
/*
*
* 当面記録号を得ます
*/
function curRecNumber($result) {
$r = $this->curRow[$result];
return $r;
}
/*
*
* 当面の号を得ます
*/
function recordNumber($result) {
$cr = $this->curRecNumber($result) + 1;
return $cr;
}
/*
*
* 第一条の記録を 移動する
*/
function moveFirstRec($result) {
switch ($this->dbType) {
case "pg" :
$r = $this->fetchRow($result, 0);
break;
default :
$rn = $this->dataSeek($result, 0);
if ($rn) {
$r = $this->fetchRow($result);
if ($r)
$this->curRow[$result] = $this->seek[$result];
} else {
$r = False;
}
break;
}
return $r;
}
/*
*
* 最後の記録を 移動する
*/
function moveLastRec($result) {
$rs = $this->rowsNumber($result);
if ($rs) {
$rs--;
switch ($this->dbType) {
case "pg" :
$r = $this->fetchRow($result, $rs);
break;
default :
$rn = $this->dataSeek($result, $rs);
if ($rn) {
$r = $this->fetchRow($result);
if ($r)
$this->curRow[$result] = $this->seek[$result];
} else {
$r = False;
}
break;
}
}
return $r;
}
/*
*
* 前への記録を 移動する
*/
function movePreviousRec($result) {
$rs = $this->curRecNumber($result);
if ($rs) {
$rs--;
switch ($this->dbType) {
case "pg" :
$r = $this->fetchRow($result, $rs);
break;
default :
$rn = $this->dataSeek($result, $rs);
if ($rn) {
$r = $this->fetchRow($result);
if ($r)
$this->curRow[$result] = $this->seek[$result];
} else {
$r = False;
}
break;
}
}
return $r;
}
/*
* 下への記録を 移動する
*/
function moveNextRec($result) {
$rs = $this->curRecNumber($result);
$rn = $this->rowsNumber($result);
$rs++;
if ($rs != $rn) {
switch ($this->dbType) {
case "pg" :
$r = $this->fetchRow($result, $rs);
break;
default :
$re = $this->fetchRow($result);
if ($re) {
$r = $re;
$this->curRow[$result]++;
$this->seek[$result] = $this->curRow[$result];
} else {
$r = False;
}
break;
}
}
return $r;
}
/*
* 特定の記録を 移動する
*
*/
function moveToRec($result, $RowNumber) {
$rn = $this->rowsNumber($result);
if ($RowNumber > 0 And $RowNumber < $rn) {
$RowNumber--;
switch ($this->dbType) {
case "pg" :
$r = $this->fetchRow($result, $RowNumber);
break;
default :
$rn = $this->dataSeek($result, $RowNumber);
if ($rn) {
$r = $this->fetchRow($result);
if ($r)
$this->curRow[$result] = $this->seek[$result];
} else {
$r = False;
}
break;
}
}
return $r;
}
function select($sql) {
$ret = array ();
$rs = $this->executeQuery($sql);
$filedsnum = $this->fieldsNumber($rs);
$recodeNum = $this->rowsNumber($rs);
for ($rownum = 0; $rownum < $recodeNum; $rownum++) {
$ht = new HashTable();
$row = $this->fetchRow($rs, $rownum);
for ($colnum = 0; $colnum < $filedsnum; $colnum++) {
$filedname = $this->fieldName($rs, $colnum);
$filedvalue = $this->getString($rs, $rownum, $filedname);
$ht->put($filedname, $filedvalue);
}
$ret[$rownum] = $ht;
}
$this->freeResult($rs);
return $ret;
}
}
?>