Commit 63815bac authored by Jonathan.Wage's avatar Jonathan.Wage

Removed. Will be replaced by native Resource connection driver.

parent 2bd36677
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Resource
*
* @package Doctrine
* @subpackage Resource
* @author Jonathan H. Wage <jwage@mac.com>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource
{
protected $_config = null;
const FORMAT = 'json';
public function __construct($config)
{
foreach ($config as $key => $value) {
$this->getConfig()->set($key, $value);
}
$loadDoctrine = false;
foreach ($this->getConfig()->getAll() as $key => $value) {
if ($key == 'url') {
$this->loadDoctrine = true;
}
}
}
public function getConfig($key = null)
{
if ($this->_config === null) {
$this->_config = new Doctrine_Resource_Config();
}
if ($key === null) {
return $this->_config;
} else {
return $this->_config->get($key);
}
}
}
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Resource_Access
*
* @package Doctrine
* @subpackage Resource
* @author Jonathan H. Wage <jwage@mac.com>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource_Access extends Doctrine_Access
{ }
\ No newline at end of file
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Resource_Client
*
* @package Doctrine
* @subpackage Resource
* @author Jonathan H. Wage <jwage@mac.com>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource_Client extends Doctrine_Resource
{
public $loadDoctrine = false;
static public function getInstance($url = null, $config = null)
{
static $instance;
if ( ! $instance) {
$instance = new Doctrine_Resource_Client($url, $config);
if ($instance->loadDoctrine === true) {
$instance->loadDoctrine();
}
}
return $instance;
}
public function __construct($url, $config)
{
if ($url) {
$config['url'] = $url;
}
parent::__construct($config);
}
public function loadDoctrine()
{
$path = '/tmp/' . $this->getClientKey();
$classesPath = $path.'.classes.php';
if (file_exists($path)) {
$schema = file_get_contents($path);
} else {
$request = new Doctrine_Resource_Request();
$request->set('action', 'load');
$schema = $request->execute();
if ($schema) {
file_put_contents($path, Doctrine_Parser::dump($schema, Doctrine_Resource::FORMAT));
}
}
if (file_exists($path) && $schema) {
$import = new Doctrine_Import_Schema();
$schema = $import->buildSchema($path, Doctrine_Resource::FORMAT);
if ( ! file_exists($classesPath)) {
$build = "<?php\n";
foreach ($schema['schema'] as $className => $details) {
$build .= "class " . $className . " extends Doctrine_Resource_Record { protected \$_model = '".$className."'; public function __construct() { parent::__construct(\$this->_model); } }\n";
$schema['schema'][$className]['relations'] = isset($schema['relations'][$className]) ? $schema['relations'][$className]:array();
}
file_put_contents($classesPath, $build);
}
require_once($classesPath);
$this->getConfig()->set('schema', $schema);
}
}
public function getClientKey()
{
return md5(Doctrine_Resource::FORMAT.serialize($this->getConfig()));
}
public function getTable($table)
{
static $instance;
if( ! isset($instance[$table])) {
$instance[$table] = new Doctrine_Resource_Table($table);
}
return $instance[$table];
}
public function printSchema()
{
$schema = $this->getConfig('schema');
echo '<h2>Schema</h2>';
echo '<ul>';
foreach ($schema['schema'] as $className => $info) {
echo '<a name="'.$className.'"></a>';
echo '<li><h3>'.$className.'</h3></li>';
echo '<ul>';
echo '<li>Columns';
echo '<ul>';
foreach ($info['columns'] as $columnName => $column)
{
echo '<li>' . $columnName;
echo '<ul>';
foreach ($column as $key => $value) {
if ($value) {
echo '<li>'.$key.': '.$value.'</li>';
}
}
echo '</ul>';
echo '</li>';
}
echo '</ul>';
echo '</li>';
echo '</ul>';
if (isset($info['relations']) && !empty($info['relations'])) {
echo '<ul>';
echo '<li>Relations';
echo '<ul>';
foreach ($info['relations'] as $relationName => $relation)
{
echo '<li><a href="#'.$relation['class'].'">' . $relationName . '</a></li>';
}
echo '</ul>';
echo '</li>';
echo '</ul>';
}
}
echo '</ul>';
exit;
}
}
\ No newline at end of file
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Resource_Collection
*
* @package Doctrine
* @subpackage Resource
* @author Jonathan H. Wage <jwage@mac.com>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements Countable, IteratorAggregate
{
protected $_data = array();
protected $_config = array();
protected $_model = null;
public function __construct($model)
{
$this->_model = $model;
}
public function getConfig($key = null)
{
return Doctrine_Resource_Client::getInstance()->getConfig($key);
}
public function count()
{
return count($this->_data);
}
public function get($key)
{
if ( ! isset($key) || !isset($this->_data[$key])) {
return $this->add();
} else {
return $this->_data[$key];
}
}
public function set($key, $value)
{
if ( ! isset($key) || !isset($this->_data[$key])) {
$this->_data[$key] = $value;
} else {
$val = $this->add();
$val->_data[$key] = $value;
}
}
public function add(Doctrine_Resource_Record $value = null)
{
if ( ! $value) {
$value = new $this->_model;
}
if ($value) {
$this->_data[] = $value;
return $value;
}
}
public function getIterator()
{
return new ArrayIterator($this->_data);
}
public function getFirst()
{
return isset($this->_data[0]) ? $this->_data[0]:null;
}
public function toArray($deep = false)
{
$data = array();
foreach ($this->_data as $key => $record) {
$data[$key] = $record->toArray($deep);
}
return $data;
}
public function fromArray(array $array)
{
foreach ($array as $key => $record) {
$this->add()->fromArray($record);
}
}
public function save()
{
foreach ($this as $record) {
$record->save();
}
}
public function delete()
{
foreach ($this as $record) {
$record->delete();
}
}
}
\ No newline at end of file
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Resource_Config
*
* @package Doctrine
* @subpackage Resource
* @author Jonathan H. Wage <jwage@mac.com>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource_Config extends Doctrine_Resource_Params
{ }
\ No newline at end of file
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Resource_Exception
*
* @package Doctrine
* @subpackage Resource
* @author Jonathan H. Wage <jwage@mac.com>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource_Exception extends Doctrine_Exception
{ }
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Resource_Params
*
* @package Doctrine
* @subpackage Resource
* @author Jonathan H. Wage <jwage@mac.com>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource_Params
{
protected $_data = array();
public function __construct($array = null)
{
if ($array !== null) {
foreach ($array as $key => $value) {
$this->set($key, $value);
}
}
}
public function set($key, $value)
{
$this->_data[$key] = $value;
}
public function get($key)
{
return isset($this->_data[$key]) ? $this->_data[$key]:null;
}
public function has($key)
{
return isset($this->_data[$key]) ? true:false;
}
public function remove($key)
{
unset($this->_data[$key]);
}
public function getAll()
{
return $this->_data;
}
}
\ No newline at end of file
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Resource_Query
*
* @package Doctrine
* @subpackage Resource
* @author Jonathan H. Wage <jwage@mac.com>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource_Query
{
protected $_parts = array();
protected $_dql = null;
public function getConfig($key = null)
{
return Doctrine_Resource_Client::getInstance()->getConfig($key);
}
public function query($dql, $params = array())
{
$this->_dql = $dql;
return $this->execute($params);
}
public function execute($params = array())
{
$params = is_array($params) ? $params:array($params);
$request = new Doctrine_Resource_Request();
$request->set('dql', $this->getDql());
$request->set('params', $params);
$request->set('action', 'query');
$request->set('model', $this->getModel());
$response = $request->execute();
if ( ! empty($response)) {
$collection = new Doctrine_Resource_Collection($this->getModel());
$collection->fromArray($response);
} else {
$model = $this->getModel();
$collection = new Doctrine_Resource_Collection($model);
$collection[] = new $model();
}
return $collection;
}
public function getDql()
{
if ( ! $this->_dql && !empty($this->_parts)) {
$q = '';
$q .= ( ! empty($this->_parts['select']))? 'SELECT ' . implode(', ', $this->_parts['select']) : '';
$q .= ( ! empty($this->_parts['from']))? ' FROM ' . implode(' ', $this->_parts['from']) : '';
$q .= ( ! empty($this->_parts['where']))? ' WHERE ' . implode(' AND ', $this->_parts['where']) : '';
$q .= ( ! empty($this->_parts['groupby']))? ' GROUP BY ' . implode(', ', $this->_parts['groupby']) : '';
$q .= ( ! empty($this->_parts['having']))? ' HAVING ' . implode(' AND ', $this->_parts['having']) : '';
$q .= ( ! empty($this->_parts['orderby']))? ' ORDER BY ' . implode(', ', $this->_parts['orderby']) : '';
$q .= ( ! empty($this->_parts['limit']))? ' LIMIT ' . implode(' ', $this->_parts['limit']) : '';
$q .= ( ! empty($this->_parts['offset']))? ' OFFSET ' . implode(' ', $this->_parts['offset']) : '';
return $q;
} else {
return $this->_dql;
}
}
public function buildUrl($array)
{
$url = '';
foreach ($array as $key => $value) {
if (is_array($value)) {
$url .= $this->buildUrl($value);
} else {
$url .= $key.'='.$value.'&';
}
}
return $url;
}
public function getModel()
{
$dql = $this->getDql();
$e = explode('FROM ', $dql);
$e = explode(' ', $e[1]);
return $e[0];
}
/**
* addSelect
* adds fields to the SELECT part of the query
*
* @param string $select Query SELECT part
* @return Doctrine_Query
*/
public function addSelect($select)
{
return $this->parseQueryPart('select', $select, true);
}
/**
* addFrom
* adds fields to the FROM part of the query
*
* @param string $from Query FROM part
* @return Doctrine_Query
*/
public function addFrom($from)
{
return $this->parseQueryPart('from', $from, true);
}
/**
* addWhere
* adds conditions to the WHERE part of the query
*
* @param string $where Query WHERE part
* @param mixed $params an array of parameters or a simple scalar
* @return Doctrine_Query
*/
public function addWhere($where)
{
return $this->parseQueryPart('where', $where, true);
}
/**
* addGroupBy
* adds fields to the GROUP BY part of the query
*
* @param string $groupby Query GROUP BY part
* @return Doctrine_Query
*/
public function addGroupBy($groupby)
{
return $this->parseQueryPart('groupby', $groupby, true);
}
/**
* addHaving
* adds conditions to the HAVING part of the query
*
* @param string $having Query HAVING part
* @param mixed $params an array of parameters or a simple scalar
* @return Doctrine_Query
*/
public function addHaving($having)
{
return $this->parseQueryPart('having', $having, true);
}
/**
* addOrderBy
* adds fields to the ORDER BY part of the query
*
* @param string $orderby Query ORDER BY part
* @return Doctrine_Query
*/
public function addOrderBy($orderby)
{
return $this->parseQueryPart('orderby', $orderby, true);
}
/**
* select
* sets the SELECT part of the query
*
* @param string $select Query SELECT part
* @return Doctrine_Query
*/
public function select($select)
{
return $this->parseQueryPart('select', $select);
}
/**
* distinct
* Makes the query SELECT DISTINCT.
*
* @param bool $flag Whether or not the SELECT is DISTINCT (default true).
* @return Doctrine_Query
*/
public function distinct($flag = true)
{
$this->parseQueryPart('distinct', (bool) $flag);
}
/**
* from
* sets the FROM part of the query
*
* @param string $from Query FROM part
* @return Doctrine_Query
*/
public function from($from)
{
return $this->parseQueryPart('from', $from);
}
/**
* innerJoin
* appends an INNER JOIN to the FROM part of the query
*
* @param string $join Query INNER JOIN
* @return Doctrine_Query
*/
public function innerJoin($join)
{
return $this->parseQueryPart('from', 'INNER JOIN ' . $join, true);
}
/**
* leftJoin
* appends a LEFT JOIN to the FROM part of the query
*
* @param string $join Query LEFT JOIN
* @return Doctrine_Query
*/
public function leftJoin($join)
{
return $this->parseQueryPart('from', 'LEFT JOIN ' . $join, true);
}
/**
* groupBy
* sets the GROUP BY part of the query
*
* @param string $groupby Query GROUP BY part
* @return Doctrine_Query
*/
public function groupBy($groupby)
{
return $this->parseQueryPart('groupby', $groupby);
}
/**
* where
* sets the WHERE part of the query
*
* @param string $join Query WHERE part
* @param mixed $params an array of parameters or a simple scalar
* @return Doctrine_Query
*/
public function where($where)
{
return $this->parseQueryPart('where', $where);
}
/**
* having
* sets the HAVING part of the query
*
* @param string $having Query HAVING part
* @param mixed $params an array of parameters or a simple scalar
* @return Doctrine_Query
*/
public function having($having)
{
return $this->parseQueryPart('having', $having);
}
/**
* orderBy
* sets the ORDER BY part of the query
*
* @param string $orderby Query ORDER BY part
* @return Doctrine_Query
*/
public function orderBy($orderby)
{
return $this->parseQueryPart('orderby', $orderby);
}
/**
* limit
* sets the Query query limit
*
* @param integer $limit limit to be used for limiting the query results
* @return Doctrine_Query
*/
public function limit($limit)
{
return $this->parseQueryPart('limit', $limit);
}
/**
* offset
* sets the Query query offset
*
* @param integer $offset offset to be used for paginating the query
* @return Doctrine_Query
*/
public function offset($offset)
{
return $this->parseQueryPart('offset', $offset);
}
/**
* parseQueryPart
* parses given DQL query part
*
* @param string $queryPartName the name of the query part
* @param string $queryPart query part to be parsed
* @return Doctrine_Query this object
*/
public function parseQueryPart($queryPartName, $queryPart)
{
$this->_parts[$queryPartName][] = $queryPart;
return $this;
}
}
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Resource_Record
*
* @package Doctrine
* @subpackage Resource
* @author Jonathan H. Wage <jwage@mac.com>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Countable, IteratorAggregate
{
protected $_data = array();
protected $_model = null;
protected $_changes = array();
public function __construct($model)
{
$this->_model = $model;
$this->initialize();
}
public function clearChanges()
{
$this->_changes = array();
}
public function initialize($loadRelations = true)
{
$schema = $this->getTable()->getSchema();
$relations = $schema['relations'];
if (isset($schema['columns'])) {
$columns = $schema['columns'];
foreach ($columns as $column) {
if ( ! isset($this->_data[$column['name']]) || $this->_data[$column['name']]) {
$this->_data[$column['name']] = null;
}
}
}
}
public function getConfig($key = null)
{
return Doctrine_Resource_Client::getInstance()->getConfig($key);
}
public function get($key)
{
if ( ! isset($key)) {
return;
}
if ( ! isset($this->_data[$key]) && $this->getTable()->hasRelation($key)) {
$this->_data[$key] = $this->createRelation($key);
}
if ( ! array_key_exists($key, $this->_data)) {
throw new Doctrine_Resource_Exception('Unknown property / related component: '.$key);
}
return $this->_data[$key];
}
public function set($key, $value)
{
if ( ! isset($key)) {
return;
}
if ( ! isset($this->_data[$key]) && $this->getTable()->hasRelation($key)) {
$this->_data[$key] = $this->createRelation($key);
}
if ( ! array_key_exists($key, $this->_data)) {
throw new Doctrine_Resource_Exception('Unknown property / related component: '.$key);
}
if ($this->_data[$key] != $value && !$value instanceof Doctrine_Resource_Record && !$value instanceof Doctrine_Resource_Collection) {
$this->_changes[$key] = $value;
}
$this->_data[$key] = $value;
}
public function createRelation($key)
{
$relation = $this->getTable()->getRelation($key);
$class = $relation['class'];
if ($relation['type'] === Doctrine_Relation::ONE) {
$return = new $class();
} else {
$return = new Doctrine_Resource_Collection($class);
}
return $return;
}
public function count()
{
return count($this->_data);
}
public function getIterator()
{
return new ArrayIterator($this->_data);
}
public function getChanges()
{
global $gotten;
if ( ! $gotten) {
$gotten = array();
}
$md5Hash = $this->getMd5Hash();
if ( ! in_array($md5Hash, $gotten)) {
$gotten[] = $md5Hash;
}
$array = array();
foreach ($this->_data as $key => $value) {
if ($this->getTable()->hasRelation($key)) {
$relation = $this->getTable()->getRelation($key);
if ($value instanceof Doctrine_Resource_Record) {
if ($value->hasChanges() && !in_array($value->getMd5Hash(), $gotten)) {
$array[$key] = $value->getChanges();
}
} else if($value instanceof Doctrine_Resource_Collection) {
foreach ($value as $key2 => $record) {
if ($record->hasChanges() && !in_array($record->getMd5Hash(), $gotten)) {
$array[$key][$record->getModel() . '_' .$key2] = $record->getChanges();
}
}
}
} else if ($this->getTable()->hasColumn($key)) {
if (isset($this->_changes[$key])) {
$array[$key] = $value;
}
}
}
$identifier = $this->identifier();
$array = array_merge($identifier, $array);
return $array;
}
public function hasChanges()
{
return !empty($this->_changes) ? true:false;
}
public function save()
{
$request = new Doctrine_Resource_Request();
$request->set('action', 'save');
$request->set('model', $this->getModel());
$request->set('identifier', $this->identifier());
$request->set('data', $this->getChanges());
$response = $request->execute();
$this->fromArray($response);
}
public function delete()
{
$request = new Doctrine_Resource_Request();
$request->set('action', 'delete');
$request->set('model', $this->getModel());
$request->set('identifier', $this->identifier());
$response = $request->execute();
}
public function getTable()
{
$model = $this->_model;
return Doctrine_Resource_Client::getInstance()->getTable($model);
}
public function getModel()
{
return $this->_model;
}
public function identifier()
{
$identifier = array();
$schema = $this->getTable()->getSchema();
$columns = $schema['columns'];
if (isset($columns) && is_array($columns)) {
foreach ($columns as $name => $column) {
if ($column['primary'] == true) {
$identifier[$name] = $this->_data[$name];
}
}
}
return $identifier;
}
public function exists()
{
$identifier = $this->identifier();
foreach ($identifier as $key => $value) {
if ( ! $value) {
return false;
}
}
return true;
}
public function toArray($deep = false)
{
$data = array();
foreach ($this->_data as $key => $value) {
if ($deep && $this->getTable()->hasRelation($key)) {
$data[$key] = $this->$key->toArray($deep);
} else if ($this->getTable()->hasColumn($key)) {
$data[$key] = $value;
}
}
return $data;
}
public function fromArray(array $array)
{
foreach ($array as $key => $value) {
if ($this->getTable()->hasRelation($key) && is_array($value)) {
$this->$key->fromArray($value);
} else if ($this->getTable()->hasColumn($key)) {
$this->$key = $value;
}
}
}
public function getMd5Hash()
{
return md5(serialize($this->_data));
}
}
\ No newline at end of file
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Resource_Request
*
* @package Doctrine
* @subpackage Resource
* @author Jonathan H. Wage <jwage@mac.com>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource_Request extends Doctrine_Resource_Params
{
protected $_params = null;
public function getConfig($key = null)
{
return Doctrine_Resource_Client::getInstance()->getConfig($key);
}
public function execute()
{
$url = $this->getConfig()->get('url');
$request = array('request' => Doctrine_Parser::dump($this->getAll(), $this->getFormat()));
$header[0] = 'Accept: ' . $this->getFormat();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Doctrine_Resource_Exception('Request failed');
}
curl_close($ch);
$array = array();
if ($response) {
$array = Doctrine_Parser::load($response, $this->getFormat());
}
if (isset($array['error'])) {
throw new Doctrine_Resource_Exception($array['error']);
}
return $array;
}
public function getFormat()
{
return ($this->getConfig()->has('format') && $this->getConfig()->get('format')) ? $this->getConfig()->get('format'):Doctrine_Resource::FORMAT;
}
}
\ No newline at end of file
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Resource_Server
*
* @package Doctrine
* @subpackage Resource
* @author Jonathan H. Wage <jwage@mac.com>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource_Server extends Doctrine_Resource
{
public function __construct($name = null, $config = null)
{
if ($name) {
$config['name'] = $name;
}
parent::__construct($config);
}
static public function getInstance($name, $config = null)
{
static $instance;
if ( ! $instance) {
$instance = new Doctrine_Resource_Server($name, $config);
}
return $instance;
}
public function validate($errors)
{
if ( ! empty($errors)) {
throw new Doctrine_Resource_Exception(count($errors) . ' error(s) occurred: ' . implode('. ', $errors));
} else {
return true;
}
}
public function validateOpenRecord($request)
{
$errors = array();
if ( ! $request->has('model') || !$request->get('model')) {
$errors[] = 'You must specify the model/class name you are deleting';
}
if ( ! $request->has('identifier') || !is_array($request->get('identifier'))) {
$errors[] = 'You must specify an array containing the identifiers for the model you wish to delete';
}
return $errors;
}
public function validateSave($request)
{
$errors = $this->validateOpenRecord($request);
if ( ! $request->has('data') || !$request->get('data')) {
$errors[] = 'You must specify an containing the changed data to save to the model';
}
return $errors;
}
public function executeSave($request)
{
$model = $request->get('model');
$data = $request->get('data');
$identifier = $request->get('identifier');
$table = Doctrine_Manager::getInstance()->getTable($model);
$existing = true;
foreach ($identifier as $key => $value) {
if ( ! $value) {
$existing = false;
}
}
if ($existing) {
$record = $table->find($identifier);
} else {
$record = new $model();
}
$record->fromArray($data);
$record->save();
return $record->toArray(true, true);
}
public function validateDelete($request)
{
return $this->validateOpenRecord($request);
}
public function executeDelete($request)
{
$model = $request->get('model');
$identifier = $request->get('identifier');
$table = Doctrine_Manager::getInstance()->getTable($model);
$record = $table->find($identifier);
if ($record) {
$record->delete();
} else {
throw new Doctrine_Resource_Exception('Record could not be deleted because it is not a valid record');
}
}
public function validateQuery($request)
{
$errors = array();
if ( ! $request->has('dql') || !$request->get('dql')) {
$errors[] = 'You must specify a dql string in order to execute a query';
}
return $errors;
}
public function executeQuery($request)
{
$dql = $request->get('dql');
$params = $request->get('params') ? $request->get('params'):array();
$conn = Doctrine_Manager::connection();
return $conn->query($dql, $params)->toArray(true, true);
}
public function validateLoad($request)
{
$errors = array();
return $errors;
}
public function executeLoad($request)
{
$path = '/tmp/' . rand();
$models = $this->getConfig('models') ? $this->getConfig('models'):array();
$export = new Doctrine_Export_Schema();
$export->exportSchema($path, $this->getFormat(), null, $models);
$schema = Doctrine_Parser::load($path, $this->getFormat());
unlink($path);
return $schema;
}
public function execute(array $r)
{
if ( ! isset($r['request'])) {
throw new Doctrine_Resource_Exception('You must specify a request '.$this->getFormat().' string in your request');
}
$requestArray = Doctrine_Parser::load($r['request'], $this->getFormat());
$request = new Doctrine_Resource_Request($requestArray);
$funcName = 'execute' . Doctrine::classify($request->get('action'));
if (method_exists($this, $funcName)) {
$validateFuncName = 'validate' . Doctrine::classify($request->get('action'));
$errors = $this->$validateFuncName($request);
if ($this->validate($errors)) {
$result = $this->$funcName($request);
return Doctrine_Parser::dump($result, $this->getFormat());
}
} else {
throw new Doctrine_Resource_Exception('Unknown Doctrine Resource Server function');
}
}
public function run($request)
{
try {
$result = $this->execute($request);
echo $result;
} catch(Exception $e) {
echo $this->exception($e);
}
}
public function exception($e)
{
$error = array('error' => $e->getMessage());
return Doctrine_Parser::dump($error, $this->getFormat());
}
public function getFormat()
{
$headers = getallheaders();
return isset($headers['Accept']) ? $headers['Accept']:Doctrine_Resource::FORMAT;
}
}
\ No newline at end of file
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Resource_Table
*
* @package Doctrine
* @subpackage Resource
* @author Jonathan H. Wage <jwage@mac.com>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource_Table
{
protected $_model = null;
protected $_schema = null;
public function __construct($model)
{
$this->_model = $model;
$schema = $this->getConfig('schema');
if (isset($schema['schema'][$model]) && $schema['schema'][$model]) {
$this->_schema = $schema['schema'][$model];
}
if (isset($schema['relations'][$model]) && $schema['relations'][$model]) {
$this->_schema['relations'] = $schema['relations'][$model];
}
}
public function getSchema()
{
return $this->_schema;
}
public function getRelations()
{
return $this->_schema['relations'];
}
public function getColumns()
{
return $this->_schema['columns'];
}
public function getConfig($key = null)
{
return Doctrine_Resource_Client::getInstance()->getConfig($key);
}
public function find($pk)
{
$model = $this->_model;
$record = new $model();
$pk = is_array($pk) ? $pk:array($pk);
$identifier = $record->identifier();
$identifier = is_array($identifier) ? $identifier:array($identifier);
$where = '';
foreach (array_keys($identifier) as $key => $name) {
$value = $pk[$key];
$where .= $model.'.' . $name . ' = '.$value;
}
$query = new Doctrine_Resource_Query();
$query->from($model)->where($where)->limit(1);
$result = $query->execute();
return $result->getFirst();
}
public function hasColumn($name)
{
return isset($this->_schema['columns'][$name]) ? true:false;
}
public function getColumn($name)
{
if ($this->hasColumn($name)) {
return $this->_columns[$name];
}
}
public function hasRelation($name)
{
return isset($this->_schema['relations'][$name]) ? true:false;
}
public function getRelation($name)
{
if ($this->hasRelation($name)) {
return $this->_schema['relations'][$name];
}
}
public function getIdentifier()
{
$identifier = array();
$schema = $this->getSchema();
$columns = $schema['columns'];
if (isset($columns) && is_array($columns)) {
foreach ($columns as $name => $column) {
if ($column['primary'] == true) {
$identifier[$name] = $name;
}
}
}
return $identifier;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment