<?php
/**
 * YANA library
 *
 * @package yana
 * @licence http://creativecommons.org/licenses/by-nc-sa/2.5/
 */

/**
 * simulate a database
 *
 * this class simulates a sql-database on a flat-file
 *
 * @access      public
 * @package     yana
 * @subpackage  io
 */
class FileDb extends BufferedDbStream
{

    /**
     * constructor
     *
     * @param string $filename (optional)
     */
    function FileDb($filename = "")
    {
        settype($filename, "string");

        $this->database = new filedb_connection($filename);
        $this->database->autoCommit(false);

        if (preg_match('/^([\w\d_]+)$/', $filename)) {
            global $YANA;
            if (isset($YANA)) {
                $filename = $YANA->getVar('DBDIR').$filename.".config";
            } else {
                $filename = "config/db/".$filename.".config";
            }
        }

        if (empty($this->database)) {
            /* connection failed - abort */
            $this->exists = YANA_DATABASE_FAILURE;
        } else if (empty($filename)) {
            /* no structure file available, estimate structure */
            $this->exists = YANA_DATABASE_NO_FILE;
        } else if (!file_exists($filename) || !is_readable($filename)){
            /* given file does not exist */
            unset($this->structure);
            $this->exists = YANA_DATABASE_NOT_AVAILABLE;
            $this->handleError("FILE-DB ERROR:  Structure file {$filename} not found.");
        } else {
            /* all fine, proceed */
            $this->attributes['FILESIZE'] = filesize($filename);
            $this->attributes['MTIME']    = date("d.m.Y",filemtime($filename));
            $this->attributes['ATIME']    = date("d.m.Y H:i:s",fileatime($filename));
            $this->attributes['CTIME']    = date("d.m.Y",filectime($filename));
            clearstatcache();
            $this->exists = YANA_DATABASE_OK;
        }
        $this->attributes['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'];
        $this->filename = $filename;

    }

    /**
     * read
     *
     * @access private
     * @return boolean
     * @ignore
     */
    function read()
    {
        $test = parent::read();
        $this->database->setStructure($this->structure);
        return $test;
    }

    /**
     * «not implemented» query
     *
     * @access public
     * @param string $query
     * @return mixed
     * @ignore
     */ 
    function query($query)
    {
        /* intentionally left blank */
        return null;
    }

    /**
     * «not implemented» importSQL
     *
     * file database is created automatically on first use - nothing to do here!
     *
     * @access  public
     * @name    filedb::importsql()
     * @param   string  $sqlFile
     * @return  bool
     *
     * @ignore
     */ 
    function importSQL($sqlFile)
    {
        /* intentionally left blank */
        return true;
    }

    /**
     * «not implemented» buildStructure
     *
     * @access public
     * @ignore
     */ 
    function buildStructure()
    {
        /* intentionally left blank */
    }

    /**
     * isError
     *
     * @access private
     * @param mixed $result
     * @return boolean
     * @ignore
     */
    function isError($result)
    {
        if (method_exists($result, 'isError') && $result->isError()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * quote
     *
     * @access private
     * @param mixed $value
     * @return string
     * @ignore
     */
    function quote($value)
    {
        return "\"$value\"";
    }

    /**
     * quoteId
     *
     * @access private
     * @param mixed $value
     * @return string
     * @ignore
     */
    function quoteId($value)
    {
        return "\"$value\"";
    }

}

?>
