リスト8

<?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);
        }
    }
}
?>

人気記事トップ10

人気記事ランキングをもっと見る

企画広告も役立つ情報バッチリ! Sponsored

サーバやOSの保守期限、VMware環境の見直しといった節目に考える、Oracle AI Databaseの次期基盤。オンプレ継続・AWS・OCIそれぞれの特性と留意点を整理するWebセミナー 11月19日 16:00 AIエージェントの“見えないリスク”に備えてアイデンティティを「関係」で統制する 11月17日 6:25 AI生成コードをどう安全に使うか――決定論的検証とAutoFixで築く品質保証ワークフロー 11月16日 6:30 デジタル主権時代、データとモデルの持ち方は企業が主体的に定める 11月13日 6:25 ClickHouse Cloudが実現した広告運用SaaS「ATOM」のデータ基盤革新、パフォーマンスを向上しながら、運用負荷とコストを削減 11月11日 6:20 AIで開発が加速する時代、品質保証はどう変わる? Tricentisが示すAIエージェント活用の実像 11月9日 6:45 悪用までの猶予が「1分」に迫るフロンティアAI時代に開発と本番を貫く脆弱性管理が必要 10月29日 8:30 欧州CRA対応とSBOM運用。AI生成コードのOSS混入問題の対策とは ――スニペット単位で検出する「FossID」「Insignary Clarity」によるSBOM運用 9月16日 6:40 PostgreSQLからTiDBへ、稼働中SaaSで進める段階的DB移行の実践と再設計への展望 9月7日 6:30 機能安全開発におけるGoogleTestの活用を支援!「Parasoft C/C++test CT」で実現する効率化&AI連携 4月14日 6:30