All pastes #839497 Raw Edit

Anonymous

public php v1 · immutable
#839497 ·published 2007-12-31 01:32 UTC
rendered paste body
<?phpclass WpSqlBuilder{        const WHERE     = ' WHERE ';    const AND_WHERE = ' AND ';    const OR_WHERE  = ' OR ';        public function select($fields)    {        return new WpSqlSelectBuilder($fields);    }        public function insertInto($table)    {        return new WpSqlInsertBuilder($table);    }        public function update($table)    {        return new WpSqlUpdateBuilder($table);    }        public function deleteFrom($table)    {        return new WpSqlDeleteBuilder($table);    }}abstract class WpStatementBuilder{    protected $sql;    public function getSql()    {        echo $this->sql;    }}class WpSqlDeleteBuilder extends WpStatementBuilder {    public function __construct($table)    {        $this->sql = 'DELETE FROM ' . $table;    }        public function where($variable, $value)    {        $this->sql .= " WHERE $variable = '$value'";        return $this;    }}class WpSqlUpdateBuilder extends WpStatementBuilder {        public function __construct($table)    {        $this->sql = 'UPDATE ' . $table;    }        public function set($fields)    {        $lastValue = end($fields);        $lastCol   = key($fields);                     array_pop($fields);                             $set = '';                             foreach ($fields as $column => $value)        {            $set .= "$column = '$value', ";        }                $this->sql .= " SET $set $lastCol = '$lastValue'";                return $this;    }        public function where($variable, $value)    {        $this->sql .= " WHERE $variable = '$value'";        return $this;    }}class WpSqlInsertBuilder extends WpStatementBuilder {        public function __construct($table)    {        $this->sql = 'INSERT INTO ' . $table;    }        public function values($fields)    {        $cols   = '';        $values = '';        $lastValue = end($fields);        $lastCol   = key($fields);                     array_pop($fields);                foreach ($fields as $column => $value)        {            $cols   .= "$column, ";            $values .= "'$value', ";        }                $this->sql .= " ($cols $lastCol) VALUES($values '$lastValue')";                return $this;    }}class WpSqlSelectBuilder extends WpStatementBuilder {      public function __construct($fields)    {        $this->sql = 'SELECT ' . $fields;    }        public function from($table, $as = null)    {        $this->sql .= ' FROM ' .$table . (isset($as) ? ' AS ' . $as : '');        return $this;    }        public function andFrom($table, $as)    {        $this->sql .= ', ' .$table . (isset($as) ? ' AS ' . $as : '');        return $this;    }        public function where($variable, $value, $type = null, $isString = true)    {        $this->sql .= (isset($type) ? $type : WpSqlBuilder::WHERE) . $variable . ' = ' . (($isString) ? "'$value'" : $value);        return $this;    }    }$fields = array('id' => 1, 'col' => 'etc.');$table  = 'myTable';$SqlBuilder = new WpSqlBuilder();$SqlBuilder->deleteFrom($table)->where('id', 5)->getSql();$SqlBuilder->update($table)->set($fields)->where('id', 5)->getSql();$SqlBuilder->insertInto($table)->values($fields)->getSql();$SqlBuilder->select('*')->from($table, 'a')                        ->andFrom($table, 'b')                        ->where('id', 5, WpSqlBuilder::WHERE, false)                        ->where('a.id', 'b.id', WpSqlBuilder::AND_WHERE, false)                        ->getSql();                        /*DELETE FROM myTable WHERE id = '5'UPDATE myTable SET id = '1',  col = 'etc.' WHERE id = '5'INSERT INTO myTable (id,  col) VALUES('1',  'etc.')SELECT * FROM myTable AS a, myTable AS b WHERE id = 5 AND a.id = b.id*/?>