start working on horde_db version of rdo
authorChuck Hagenbuch <chuck@horde.org>
Tue, 16 Dec 2008 03:01:00 +0000 (22:01 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Tue, 16 Dec 2008 04:51:54 +0000 (23:51 -0500)
framework/Rdo/lib/Horde/Rdo/Mapper.php
framework/Rdo/lib/Horde/Rdo/Model.php [deleted file]
framework/Rdo/lib/Horde/Rdo/Query/Builder.php
framework/Rdo/package.xml [new file with mode: 0644]
framework/Rdo/rdo.php [new file with mode: 0644]

index e7cb7aa..4291513 100644 (file)
@@ -16,8 +16,8 @@
  *   $inflector - The Horde_Support_Inflector this mapper uses to singularize
  *   and pluralize PHP class, database table, and database field/key names.
  *
- *   $model - The Horde_Rdo_Model object describing the main table of
- *   this entity.
+ *   $table - The Horde_Db_Adapter_Abstract_TableDefinition object describing
+ *   the main table of this entity.
  *
  * @category Horde
  * @package Horde_Rdo
@@ -96,17 +96,17 @@ abstract class Horde_Rdo_Mapper implements Countable {
      * inflector: The Horde_Support_Inflector this mapper uses to singularize
      * and pluralize PHP class, database table, and database field/key names.
      *
-     * model: The Horde_Rdo_Model object describing the table or view
-     * this Mapper manages.
+     * table: The Horde_Db_Adapter_Abstract_TableDefinition object describing
+     * the table or view this Mapper manages.
      *
      * fields: Array of all field names that are loaded up front
-     * (eager loading) from the Model.
+     * (eager loading) from the table.
      *
      * lazyFields: Array of fields that are only loaded when accessed.
      *
-     * relationships: Array of relationships to other Models.
+     * relationships: Array of relationships to other Mappers.
      *
-     * lazyRelationships: Array of relationships to other Models which
+     * lazyRelationships: Array of relationships to other Mappers which
      * are only loaded when accessed.
      *
      * @param string $key Property name to fetch
@@ -123,13 +123,8 @@ abstract class Horde_Rdo_Mapper implements Countable {
         case 'inflector':
             return Horde_Rdo::getInflector();
 
-        case 'model':
-            $this->model = new Horde_Rdo_Model;
-            if ($this->_table) {
-                $this->model->table = $this->_table;
-            } else {
-                $this->model->table = $this->mapperToTable();
-            }
+        case 'table':
+            $table = $this->_table ? $this->_table : $this->mapperToTable();
             $this->model->load($this);
             return $this->model;
 
diff --git a/framework/Rdo/lib/Horde/Rdo/Model.php b/framework/Rdo/lib/Horde/Rdo/Model.php
deleted file mode 100644 (file)
index 83d6a89..0000000
+++ /dev/null
@@ -1,119 +0,0 @@
-<?php
-/**
- * Model class for Rdo.
- *
- * @category Horde
- * @package Horde_Rdo
- */
-
-/**
- * @category Horde
- * @package Horde_Rdo
- */
-class Horde_Rdo_Model {
-
-    /**
-     */
-    protected $_fields = array();
-
-    /**
-     */
-    public $table;
-
-    /**
-     */
-    const INTEGER = 'int';
-
-    /**
-     */
-    const NUMBER = 'number';
-
-    /**
-     */
-    const STRING = 'string';
-
-    /**
-     */
-    const TEXT = 'text';
-
-    /**
-     * Fill the model using the mapper's backend.
-     */
-    public function load($mapper)
-    {
-        $mapper->adapter->loadModel($this);
-    }
-
-    /**
-     */
-    public static function __set_state($properties)
-    {
-        $model = new Horde_Rdo_Model();
-        foreach ($properties as $key => $val) {
-            $model->$key = $val;
-        }
-    }
-
-    /**
-     */
-    public function hasField($field)
-    {
-        return isset($this->_fields[$field]);
-    }
-
-    /**
-     */
-    public function addField($field, $params = array())
-    {
-        $params = array_merge(array('key' => null, 'null' => false), $params);
-
-        if (!strncasecmp($params['null'], 'n', 1)) {
-            $params['null'] = false;
-        } elseif (!strncasecmp($params['null'], 'y', 1)) {
-            $params['null'] = true;
-        }
-
-        $this->_fields[$field] = $params;
-        if (isset($params['type'])) {
-            $this->setFieldType($field, $params['type']);
-        }
-    }
-
-    /**
-     */
-    public function getField($field)
-    {
-        return isset($this->_fields[$field]) ? $this->_fields[$field] : null;
-    }
-
-    /**
-     */
-    public function getFields()
-    {
-        return $this->_fields;
-    }
-
-    /**
-     */
-    public function listFields()
-    {
-        return array_keys($this->_fields);
-    }
-
-    /**
-     */
-    public function setFieldType($field, $rawtype)
-    {
-        if (stripos($rawtype, 'int') !== false) {
-            $this->_fields[$field]['type'] = self::INTEGER;
-        } elseif (stripos($rawtype, 'char') !== false) {
-            $this->_fields[$field]['type'] = self::STRING;
-        } elseif (stripos($rawtype, 'float') !== false
-                  || stripos($rawtype, 'decimal') !== false) {
-            $this->_fields[$field]['type'] = self::NUMBER;
-        } elseif ($rawtype == 'text') {
-            $this->_fields[$field]['type'] = self::TEXT;
-        }
-    }
-
-}
index 36eea12..1623e06 100644 (file)
@@ -66,7 +66,7 @@ abstract class Horde_Rdo_Query_Builder {
             if (count($parts) == 1) {
                 $fields[] = $field;
             } else {
-                $fields[] = str_replace('.@', '.', $field) . ' AS ' . $this->quoteColumnName($parts[0] . '@' . $parts[1]);
+                $fields[] = str_replace('.@', '.', $field) . ' AS ' . $query->mapper->adapter->quoteColumnName($parts[0] . '@' . $parts[1]);
             }
         }
 
@@ -112,7 +112,7 @@ abstract class Horde_Rdo_Query_Builder {
                 }
                 $clause = $query->relationships[$rel]['table'] . '.' . $field . ' ' . $this->getTest($test['test']);
             } else {
-                $clause = $query->mapper->model->table . '.' . $this->quoteColumnName($test['field']) . ' ' . $this->getTest($test['test']);
+                $clause = $query->mapper->model->table . '.' . $query->mapper->adapter->quoteColumnName($test['field']) . ' ' . $this->getTest($test['test']);
             }
 
             if ($test['value'] instanceof Horde_Rdo_Query_Literal) {
diff --git a/framework/Rdo/package.xml b/framework/Rdo/package.xml
new file mode 100644 (file)
index 0000000..5ca5fa3
--- /dev/null
@@ -0,0 +1,72 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<package packagerversion="1.4.9" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0
+http://pear.php.net/dtd/tasks-1.0.xsd
+http://pear.php.net/dtd/package-2.0
+http://pear.php.net/dtd/package-2.0.xsd">
+ <name>Rdo</name>
+ <channel>pear.horde.org</channel>
+ <summary>Rampage Data Objects</summary>
+ <description>Lightweight ORM layer
+ </description>
+ <lead>
+  <name>Chuck Hagenbuch</name>
+  <user>chuck</user>
+  <email>chuck@horde.org</email>
+  <active>yes</active>
+ </lead>
+ <date>2008-03-15</date>
+ <time>16:46:00</time>
+ <version>
+  <release>0.3.0</release>
+  <api>0.3.0</api>
+ </version>
+ <stability>
+  <release>beta</release>
+  <api>beta</api>
+ </stability>
+ <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
+ <notes>Major improvements including inflection, eager or lazy loading of relationships and fields, and more.
+ </notes>
+ <contents>
+  <dir name="/">
+   <dir name="lib">
+    <dir name="Horde">
+     <dir name="Rdo">
+      <dir name="Query">
+       <file name="Builder.php" role="php" />
+       <file name="Literal.php" role="php" />
+      </dir> <!-- /lib/Horde/Rdo/Query -->
+      <file name="Base.php" role="php" />
+      <file name="Iterator.php" role="php" />
+      <file name="List.php" role="php" />
+      <file name="Mapper.php" role="php" />
+      <file name="Query.php" role="php" />
+     </dir> <!-- /lib/Horde/Rdo -->
+     <file name="Rdo.php" role="php" />
+    </dir> <!-- /lib/Horde -->
+   </dir> <!-- /lib -->
+  </dir> <!-- / -->
+ </contents>
+ <dependencies>
+  <required>
+   <php>
+    <min>5.2.0</min>
+   </php>
+   <pearinstaller>
+    <min>1.5.0</min>
+   </pearinstaller>
+  </required>
+ </dependencies>
+ <phprelease>
+  <filelist>
+   <install name="lib/Horde/Rdo/Query/Builder.php" as="Horde/Rdo/Query/Builder.php" />
+   <install name="lib/Horde/Rdo/Query/Literal.php" as="Horde/Rdo/Query/Literal.php" />
+   <install name="lib/Horde/Rdo/Base.php" as="Horde/Rdo/Base.php" />
+   <install name="lib/Horde/Rdo/Iterator.php" as="Horde/Rdo/Iterator.php" />
+   <install name="lib/Horde/Rdo/List.php" as="Horde/Rdo/List.php" />
+   <install name="lib/Horde/Rdo/Mapper.php" as="Horde/Rdo/Mapper.php" />
+   <install name="lib/Horde/Rdo/Query.php" as="Horde/Rdo/Query.php" />
+   <install name="lib/Horde/Rdo.php" as="Horde/Rdo.php" />
+  </filelist>
+ </phprelease>
+</package>
diff --git a/framework/Rdo/rdo.php b/framework/Rdo/rdo.php
new file mode 100644 (file)
index 0000000..56755fc
--- /dev/null
@@ -0,0 +1,87 @@
+<?php
+
+set_include_path('/var/www/horde/horde-framework:/var/www/horde/head/libs');
+require_once 'Horde/Autoloader.php';
+Horde_Autoloader::addClassPath(dirname(__FILE__) . '/lib');
+
+// Dist config file for Rdo examples.
+$conf['sql']['adapter'] = 'pdo_mysql';
+$conf['sql']['dsn'] = 'dbname=horde;host=127.0.0.1';
+$conf['sql']['username'] = 'horde';
+$conf['sql']['password'] = 'apeshit';
+$conf['sql']['charset'] = 'iso-8859-1';
+$conf['sql']['splitread'] = false;
+
+/**
+ */
+class User extends Horde_Rdo_Base {
+}
+
+/**
+ */
+class UserMapper extends Horde_Rdo_Mapper {
+
+    public function getAdapter()
+    {
+        $adapter = isset($GLOBALS['conf']['sql']['adapter']) ? $GLOBALS['conf']['sql']['adapter'] : 'pdo';
+        return Horde_Db_Adapter::factory($GLOBALS['conf']['sql']);
+    }
+
+}
+
+$um = new UserMapper();
+
+// Count all users.
+$userCount = $um->count();
+echo "# users: $userCount\n";
+
+// Get the number of new users in May 2005
+//$userCount = $um->count('created > \'2005-05-01\' AND created <= \'2005-05-31\'');
+//echo "# new: $userCount\n";
+
+// Check if id 1 exists.
+$exists = $um->exists(1);
+echo "exists: " . ($exists ? 'yes' : 'no') . "\n";
+
+// Look for Alice
+$userTwo = $um->find(Horde_Rdo::FIND_FIRST, array('name' => 'Alice'));
+if ($userTwo) {
+    echo "Found Alice: id $userTwo->id\n";
+} else {
+    echo "No Alice found, creating:\n";
+    $userOne = $um->create(array('name' => 'Alice', 'phone' => '212-555-6565'));
+    $userOneId = $userOne->id;
+    echo "Created new user with id: $userOneId\n";
+}
+
+// Change the name of the user and save.
+if ($userTwo) {
+    $userTwo->name = 'Bob';
+    $result = $userTwo->save();
+    var_dump($result);
+}
+
+// List all users.
+echo "Looking for all:\n";
+foreach ($um->find(Horde_Rdo::FIND_ALL) as $userOb) {
+    echo "  (" . $userOb->id . ") " . $userOb->name . "\n";
+}
+
+// Fetch id 2.
+//$user = $um->find(2);
+// Try to delete it.
+//$result = $user->delete();
+//var_dump($result);
+
+/*
+// $user->billingAddresses is an Iterator.
+foreach ($user->billingAddresses as $billingAddress) {
+    echo $billingAddress->zipCode . "\n";
+}
+
+if ($user->favorite) {
+    echo $user->favorite->name . "\n";
+} else {
+    $user->favorite = new User(array('name' => 'Charles'));
+}
+*/