<?php
⁄**
*
*
*
* @author Darryl Patterson
* @copyright Euro RSCG 4D
*
*⁄
require_once('common/util/class-Config.php');
require_once(
'common/dataobject/class-ReadOnlyResultSet.php');
class DataObject {
var $dop_config;
var $dop_tableName;
var $dop_pk;
var $dop_className;
var $dop_DEBUG;
var $dop_databaseName;
function DataObject($tableName, $pk)
{
$className = $tableName;
$databaseName = '';
$this->dop_DEBUG = false;
if (strpos($tableName, '.') !== false){
list($databaseName, $className) = explode('.', $tableName);
}
$this->dop_config = Config::instance();
$this->dop_databaseName = $databaseName;
$this->dop_tableName = $tableName;
$this->dop_pk = $pk;
$this->dop_className = 'DO_' . $className;
}
function setField($name, $value)
{
if ($this->isValidProperty($name)){
$this->$name = $value;
}
}
function getField($name)
{
return $this->$name;
}
function setDebug($debug)
{
$this->dop_DEBUG = $debug;
}
function getDebug()
{
return $this->dop_DEBUG;
}
function getAll($orderBy = false)
{
$orderByString = '';
if ($orderBy !== false){
$orderByString = ' ORDER BY ' .mysql_real_escape_string($orderBy);
}
$sql = "SELECT
*
FROM " .
$this->dop_tableName . " " .
$orderByString;
if($this->dop_DEBUG){
echo '<!-- SQL from table ' .
$this->dop_tableName . ', function getAll(): ' . $sql . "\n-->";
}
$rs = mysql_query($sql, $this->getConnection());
if($this->dop_DEBUG){
echo '<!-- SQL error: ' . mysql_error() . "\n-->";
}
return new ReadOnlyResultSet($rs);
}
// この関数は指定されたプライマリキーで検索するselect文として働く。
function get($id)
{
$sql = "SELECT
*
FROM " .
$this->dop_tableName . " ";
if (is_array($this->dop_pk)){
$whereClause = array();
for ($i=0; $i < count($this->dop_pk); $i++){
$whereClause[] = $this->dop_pk[$i] .
' = "' . mysql_real_escape_string( $id[$i]) . '" ';
}
$sql .= " WHERE " . implode(' AND ', $whereClause);
} else {
$sql .= " WHERE " . $this->dop_pk .
' = "' . mysql_real_escape_string($id) . '" ';
}
if($this->dop_DEBUG){
echo '<!-- SQL from table ' .
$this->dop_tableName . ', function get(): ' . $sql . "\n-->";
}
$rs = mysql_query($sql, $this->getConnection());
if($this->dop_DEBUG){
echo '<!-- SQL error: ' . mysql_error() . "\n-->";
}
if (mysql_num_rows($rs) > 0) {
$row = mysql_fetch_array($rs);
$this->populate($row);
return true;
} else {
return false;
}
}
function insert()
{
$setClause = array();
$properties=get_class_vars($this->dop_className);
foreach($properties as $propertyName => $propertyValue){
if ($this->isValidProperty($propertyName)
&& $propertyName != $this->dop_pk){
$nameKey = '`' . $propertyName . '`';
$setClause[$nameKey] = '"' . mysql_real_escape_string(
$this->getField($propertyName)) . '"';
}
}
if (count($setClause) > 0){
$sql = "INSERT INTO " . $this->dop_tableName .
" (" . implode(',', array_keys($setClause)) . ") " .
" VALUES(" . implode(',', array_values($setClause)) . ")";
$sql = str_replace("\\'", "'", $sql);
if($this->dop_DEBUG){
echo '<!-- SQL from table ' .
$this->dop_tableName .
', function insert(): ' . $sql . "\n-->";
}
if (mysql_query($sql,$this->getConnection())){
if (!is_array($this->dop_pk)) {
$pk = $this->dop_pk;
$this->$pk = mysql_insert_id(
$this->getConnection());
}
return true;
}
}
if($this->dop_DEBUG){
echo '<!-- SQL error: ' . mysql_error() . "\n-->";
}
return false;
}
function replace()
{
$setClause = array();
$properties=get_class_vars($this->dop_className);
foreach($properties as $propertyName => $propertyValue){
if ($this->isValidProperty($propertyName)
&& $propertyName != $this->dop_pk){
$nameKey = '`' . $propertyName . '`';
$setClause[$nameKey] = '"' . mysql_real_escape_string(
$this->getField($propertyName)) . '"';
}
}
if (count($setClause) > 0){
$sql = "REPLACE INTO ". $this->dop_tableName .
" (" . implode(',', array_keys($setClause)) . ") " .
" VALUES(" . implode(',', array_values($setClause)) . ")";
$sql = str_replace("\\'", "'", $sql);
if($this->dop_DEBUG){
echo '<!-- SQL from table ' .
$this->dop_tableName .
', function replace(): ' . $sql . "\n-->";
}
if (mysql_query($sql,$this->getConnection())){
if (!is_array($this->dop_pk)) {
$pk = $this->dop_pk;
$this->$pk = mysql_insert_id(
$this->getConnection());
}
return true;
}
}
if($this->dop_DEBUG){
echo '<!-- SQL error: ' . mysql_error() . "\n-->";
}
return false;
}
function update()
{
$setClause = array();
$sql = 'UPDATE ' . $this->dop_tableName . ' ';
$properties=get_class_vars($this->dop_className);
foreach($properties as $propertyName =>
$propertyValue){
if ($this->isValidProperty($propertyName) &&
$propertyName != $this->dop_pk){
$setClause[] = '`' . $propertyName . '`'
. '="' . mysql_real_escape_string(
$this->getField($propertyName)) . '"';
}
}
if (count($setClause) > 0){
$sql .= ' SET ' . implode(',', $setClause);
if (is_array($this->dop_pk)){
$whereClause = array();
for ($i=0;$i < count($this->dop_pk);$i++){
$whereClause[] = $this->dop_pk[$i] .
' = "' . mysql_real_escape_string(
$this->{$this->dop_pk[$i]}) . '" ';
}
$sql .= " WHERE " . implode(' AND ', $whereClause);
} else {
$sql .= " WHERE " . $this->dop_pk . ' = "'
. mysql_real_escape_string(
$this->{$this->dop_pk}) . '" ';
}
$sql = str_replace("\\'", "'", $sql);
if($this->dop_DEBUG){
echo '<!-- SQL from table ' .
$this->dop_tableName . ',
function update(): ' . $sql . "\n-->";
}
mysql_query($sql, $this->getConnection());
if($this->dop_DEBUG){
echo '<!-- SQL error: ' . mysql_error() . "\n-->";
}
}
}
function delete()
{
$sql = "DELETE FROM " . $this->dop_tableName.' ';
if (is_array($this->dop_pk)){
$whereClause = array();
for ($i=0; $i < count($this->dop_pk); $i++){
$whereClause[] = $this->dop_pk[$i] .
' = "' . mysql_real_escape_string(
$this->{$this->dop_pk[$i]}) . '" ';
}
$sql .= " WHERE " . implode(' AND ', $whereClause);
} else {
$sql .= " WHERE " . $this->dop_pk . ' = "' .
mysql_real_escape_string(
$this->{$this->dop_pk}) . '" ';
}
if($this->dop_DEBUG){
echo '<!-- SQL from table ' .
$this->dop_tableName .
', function delete(): ' . $sql . "\n-->";
}
if (mysql_query($sql, $this->getConnection())) {
return true;
}
if($this->dop_DEBUG){
echo '<!-- SQL error: ' . mysql_error() . "\n-->";
}
return false;
}
⁄**
* このクラスの現在のプロパティにマッチするレコードを全て見つける。
* ReadOnlyResultSetを返す。
*
* AND条件でしか使えないことに注意。
* (ie userId=1 AND userName='joe').
*
* @example $myDO->userId = 1; $rs = $myDO->find(); // userId=1となるすべてのレコードに結果セットを返す。
* @param string $sort What DB field to sort by (inserted into ORDER BY clause
* @return ReadOnlyResultSet
*⁄
function find($sort='')
{
if ($sort != '') {
$orderBy = ' ORDER BY ' . $sort; } else {
$orderBy = '';
}
$sql = "SELECT * FROM ".$this->dop_tableName." ";
$whereClause = array();
$properties=get_class_vars($this->dop_className);
foreach($properties as $propertyName =>
$propertyValue){
if ($this->isValidProperty($propertyName) &&
$this->getField($propertyName) != ""){
$whereClause[] = '`' . $propertyName .
'`' . '="' . mysql_real_escape_string(
$this->getField($propertyName)) . '"';
}
}
if (count($whereClause) > 0){
$sql .= " WHERE " . implode(' AND ', $whereClause);
}
$sql .= $orderBy;
if($this->dop_DEBUG){
echo '<!-- SQL from table ' .
$this->dop_tableName .
', function find(): ' . $sql . "\n-->";
}
$rs = mysql_query($sql, $this->getConnection());
if($this->dop_DEBUG){
echo '<!-- SQL error: ' . mysql_error() . "\n-->";
}
return new ReadOnlyResultSet($rs);
}
function isValidProperty($name)v
{
$name = str_replace('`', '', $name);
if ($name == 'dop_config' || $name == 'dop_pk' ||
$name == 'dop_tableName' ||
$name == 'dop_className' ||
$name == 'dop_databaseName' ||
$name == 'dop_DEBUG'){
return false;
} else {
return true;
}
}
function getConnection()
{
return $this->dop_config->getDBConnection();
}
function populate($row)
{
while (list($fieldName, $fieldVal) = each($row)){
$this->setField($fieldName, $fieldVal);
}
}
}
?>
|
|