Skip to content
Snippets Groups Projects
class.orm.php 68.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jared Hancock's avatar
    Jared Hancock committed
        function next() {
            $status = $this->stmt->fetch();
            if ($status === false)
    
                throw new OrmException($this->stmt->error);
    
    Jared Hancock's avatar
    Jared Hancock committed
            elseif ($status === null) {
                $this->close();
                return false;
            }
            return true;
        }
    
        function getArray() {
            $output = array();
            $variables = array();
    
            if (!isset($this->stmt))
                $this->_prepare();
    
            foreach ($this->fields as $f)
                $variables[] = &$output[$f->name]; // pass by reference
    
    
            if (!call_user_func_array(array($this->stmt, 'bind_result'), $variables))
                throw new OrmException('Unable to bind result: ' . $this->stmt->error);
    
    
    Jared Hancock's avatar
    Jared Hancock committed
            if (!$this->next())
                return false;
            return $output;
        }
    
    
        function getRow() {
            $output = array();
            $variables = array();
    
            if (!isset($this->stmt))
                $this->_prepare();
    
            foreach ($this->fields as $f)
                $variables[] = &$output[]; // pass by reference
    
    
            if (!call_user_func_array(array($this->stmt, 'bind_result'), $variables))
                throw new OrmException('Unable to bind result: ' . $this->stmt->error);
    
    
            if (!$this->next())
                return false;
            return $output;
        }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
        function close() {
            if (!$this->stmt)
                return;
    
            $this->stmt->close();
            $this->stmt = null;
        }
    
    
        function affected_rows() {
            return $this->stmt->affected_rows;
        }
    
        function insert_id() {
            return $this->stmt->insert_id;
        }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
        function __toString() {
            return $this->sql;
        }
    }
    
    class Q implements Serializable {
    
        const NEGATED = 0x0001;
        const ANY =     0x0002;
    
        var $constraints;
        var $flags;
        var $negated = false;
        var $ored = false;
    
    
        function __construct($filter=array(), $flags=0) {
    
            if (!is_array($filter))
                $filter = array($filter);
    
            $this->constraints = $filter;
            $this->negated = $flags & self::NEGATED;
            $this->ored = $flags & self::ANY;
        }
    
        function isNegated() {
            return $this->negated;
        }
    
        function isOred() {
            return $this->ored;
        }
    
        function negate() {
            $this->negated = !$this->negated;
            return $this;
        }
    
    
        function union() {
            $this->ored = true;
        }
    
        function add($constraints) {
            if (is_array($constraints))
                $this->constraints = array_merge($this->constraints, $constraints);
            elseif ($constraints instanceof static)
                $this->constraints[] = $constraints;
            else
                throw new InvalidArgumentException('Expected an instance of Q or an array thereof');
            return $this;
        }
    
    
        static function not(array $constraints) {
            return new static($constraints, self::NEGATED);
        }
    
        static function any(array $constraints) {
    
            return new static($constraints, self::ANY);
    
    
        function serialize() {
            return serialize(array(
                'f' =>
                    ($this->negated ? self::NEGATED : 0)
                  | ($this->ored ? self::ANY : 0),
                'c' => $this->constraints
            ));
        }
    
        function unserialize($data) {
            $data = unserialize($data);
            $this->constraints = $data['c'];
            $this->ored = $data['f'] & self::ANY;
            $this->negated = $data['f'] & self::NEGATED;
        }