[Beeframework-svn] SF.net SVN: beeframework:[30] trunk
Brought to you by:
b_hartmann,
m_plomer
|
From: <m_p...@us...> - 2013-05-07 11:13:27
|
Revision: 30
http://sourceforge.net/p/beeframework/code/30
Author: m_plomer
Date: 2013-05-07 11:13:24 +0000 (Tue, 07 May 2013)
Log Message:
-----------
- pdo ordering examples
Modified Paths:
--------------
trunk/framework/Bee/Framework.php
trunk/framework/Bee/Persistence/Behaviors/Ordered/Strategy.php
trunk/framework/Bee/Persistence/Pdo/Behaviors/GenericOrderedDelegate.php
Added Paths:
-----------
trunk/examples/
trunk/examples/classes/
trunk/examples/classes/Persistence/
trunk/examples/classes/Persistence/Pdo/
trunk/examples/classes/Persistence/Pdo/OrderedColorsDao.php
trunk/examples/conf/
trunk/examples/conf/context.xml
trunk/examples/conf/log4php.xml
trunk/examples/db/
trunk/examples/index.php
trunk/examples/logs/
Added: trunk/examples/classes/Persistence/Pdo/OrderedColorsDao.php
===================================================================
--- trunk/examples/classes/Persistence/Pdo/OrderedColorsDao.php (rev 0)
+++ trunk/examples/classes/Persistence/Pdo/OrderedColorsDao.php 2013-05-07 11:13:24 UTC (rev 30)
@@ -0,0 +1,93 @@
+<?php
+namespace Persistence\Pdo;
+
+use Bee\Persistence\Behaviors\Ordered\Strategy as OrderedStrategy;
+use Bee\Persistence\Pdo\Behaviors\GenericOrderedDelegate;
+
+/*
+ * Copyright 2008-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * User: mp
+ * Date: 07.05.13
+ * Time: 12:12
+ */
+
+class OrderedColorsDao {
+
+ /**
+ * @var \Logger
+ */
+ private static $log;
+
+ /**
+ * @return \Logger
+ */
+ protected static function getLog() {
+ if (!self::$log) {
+ self::$log = \Bee_Framework::getLoggerForClass(__CLASS__);
+ }
+ return self::$log;
+ }
+
+ /**
+ * @var \PDO
+ */
+ private $pdoConnection;
+
+ /**
+ * @var OrderedStrategy
+ */
+ private $orderedStrategy;
+
+ public function __construct(\PDO $pdoConnection) {
+ self::getLog()->info('DAO constructed, got PDO connection');
+ $this->pdoConnection = $pdoConnection;
+ $pdoOrderedDelagate = new GenericOrderedDelegate('ordered_colors', $pdoConnection);
+ $this->orderedStrategy = new OrderedStrategy($pdoOrderedDelagate);
+ }
+
+ /**
+ * @return \Bee\Persistence\Behaviors\Ordered\Strategy
+ */
+ public function getOrderedStrategy() {
+ return $this->orderedStrategy;
+ }
+
+ public function addColor($colorName, $colorHex) {
+ self::getLog()->info("adding color ($colorName, $colorHex)");
+
+ $this->pdoConnection->beginTransaction();
+ try {
+
+ self::getLog()->debug('inserting');
+ $insertStmt = $this->pdoConnection->prepare('INSERT INTO ordered_colors (name, hex_value) VALUES (:name, :hex_value)');
+ $insertStmt->execute(array(':name' => $colorName, ':hex_value' => $colorHex));
+
+ $id = $this->pdoConnection->lastInsertId();
+ self::getLog()->debug('moving to end of list');
+ $pos = $this->orderedStrategy->moveToEnd($id);
+
+ self::getLog()->debug("committing ($id, $colorName, $colorHex, $pos)");
+ $this->pdoConnection->commit();
+ return $id;
+ } catch(\Exception $e) {
+ self::getLog()->debug('exception caught', $e);
+ $this->pdoConnection->rollBack();
+ throw $e;
+ }
+ }
+}
Added: trunk/examples/conf/context.xml
===================================================================
--- trunk/examples/conf/context.xml (rev 0)
+++ trunk/examples/conf/context.xml 2013-05-07 11:13:24 UTC (rev 30)
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.beeframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:security="http://www.beeframework.org/schema/security"
+ xsi:schemaLocation="http://www.beeframework.org/schema/beans http://www.beeframework.org/schema/beans/bee-beans-1.2.xsd
+ http://www.beeframework.org/schema/security http://www.beeframework.org/schema/security/bee-security-1.0.xsd">
+
+ <!-- Persistence - PDO Connection -->
+ <bean id="pdoConnection" class="PDO">
+ <!-- LOCAL DB -->
+ <constructor-arg index="0" value="sqlite:db/examples.sqlite" />
+ <constructor-arg index="1" value="sa" />
+ <constructor-arg index="2" value="" />
+ </bean>
+
+ <bean id="orderedColorsDao" class="Persistence\Pdo\OrderedColorsDao">
+ <constructor-arg index="0" ref="pdoConnection" />
+ </bean>
+
+</beans>
Added: trunk/examples/conf/log4php.xml
===================================================================
--- trunk/examples/conf/log4php.xml (rev 0)
+++ trunk/examples/conf/log4php.xml 2013-05-07 11:13:24 UTC (rev 30)
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration xmlns="http://logging.apache.org/log4php/">
+ <appender name="default" class="LoggerAppenderFile">
+ <layout class="LoggerLayoutPattern">
+ <param name="conversionPattern" value="%date %logger %-5level %msg%n" />
+ </layout>
+ <param name="file" value="logs/application.log" />
+ <param name="append" value="true" />
+ </appender>
+ <appender name="framework" class="LoggerAppenderFile">
+ <layout class="LoggerLayoutPattern">
+ <param name="conversionPattern" value="%date %logger %-5level %msg%n" />
+ </layout>
+ <param name="file" value="logs/framework.log" />
+ <param name="append" value="true" />
+ </appender>
+ <root>
+ <appender_ref ref="default" />
+ </root>
+ <logger name="Bee" additivity="false">
+ <level value="warn" />
+ <appender_ref ref="framework" />
+ </logger>
+</configuration>
\ No newline at end of file
Index: trunk/examples/db
===================================================================
--- trunk/examples/db 2013-05-07 07:25:49 UTC (rev 29)
+++ trunk/examples/db 2013-05-07 11:13:24 UTC (rev 30)
Property changes on: trunk/examples/db
___________________________________________________________________
Added: svn:ignore
## -0,0 +1 ##
+examples.sqlite
Added: trunk/examples/index.php
===================================================================
--- trunk/examples/index.php (rev 0)
+++ trunk/examples/index.php 2013-05-07 11:13:24 UTC (rev 30)
@@ -0,0 +1,61 @@
+<?php
+/*
+ * Copyright 2008-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * User: mp
+ * Date: 07.05.13
+ * Time: 12:25
+ */
+
+require_once '../framework/Bee/Framework.php';
+
+// Verzeichnis mit Applikations-Klassen zum Classpath hinzufügen
+Bee_Framework::addApplicationIncludePath('classes');
+
+Bee_Framework::addApplicationIncludePath('../libs/apache-log4php-2.3.0');
+Bee_Framework::addApplicationIncludePath('../libs/apache-log4php-2.3.0/helpers');
+Bee_Framework::addApplicationIncludePath('../libs/apache-log4php-2.3.0/pattern');
+Bee_Framework::addApplicationIncludePath('../libs/apache-log4php-2.3.0/layouts');
+Bee_Framework::addApplicationIncludePath('../libs/apache-log4php-2.3.0/appenders');
+Bee_Framework::addApplicationIncludePath('../libs/apache-log4php-2.3.0/configurators');
+Bee_Framework::addApplicationIncludePath('../libs/apache-log4php-2.3.0/renderers');
+
+Logger::configure('conf/log4php.xml');
+
+$ctx = new Bee_Context_Xml('conf/context.xml');
+
+$pdoConn = $ctx->getBean('pdoConnection');
+$pdoConn->exec('CREATE TABLE "ordered_colors" (
+ "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
+ "name" text NOT NULL,
+ "hex_value" text NOT NULL,
+ "pos" integer DEFAULT NULL,
+ UNIQUE (pos ASC)
+)');
+
+$dao = $ctx->getBean('orderedColorsDao');
+
+$dao->addColor('Red', '#ff0000');
+$greenId = $dao->addColor('Green', '#00ff00');
+$dao->addColor('Blue', '#0000ff');
+$dao->addColor('Yellow', '#ffff00');
+$purpleId = $dao->addColor('Purple', '#ff00ff');
+$dao->addColor('Cyan', '#00ffff');
+
+$dao->getOrderedStrategy()->moveAfter($greenId, $purpleId);
+
+//BeeFramework::dispatchRequestUsingSerializedContext('conf/context.serialized');
Index: trunk/examples/logs
===================================================================
--- trunk/examples/logs 2013-05-07 07:25:49 UTC (rev 29)
+++ trunk/examples/logs 2013-05-07 11:13:24 UTC (rev 30)
Property changes on: trunk/examples/logs
___________________________________________________________________
Added: svn:ignore
## -0,0 +1 ##
+*.log
Modified: trunk/framework/Bee/Framework.php
===================================================================
--- trunk/framework/Bee/Framework.php 2013-05-07 07:25:49 UTC (rev 29)
+++ trunk/framework/Bee/Framework.php 2013-05-07 11:13:24 UTC (rev 30)
@@ -78,7 +78,7 @@
* @return void
*/
static function init() {
- self::$beeHiveLocation = dirname(__FILE__);
+ self::$beeHiveLocation = dirname(dirname(__FILE__));
self::addApplicationIncludePath(self::$beeHiveLocation);
require_once dirname(__FILE__) . '/Cache/Manager.php';
Modified: trunk/framework/Bee/Persistence/Behaviors/Ordered/Strategy.php
===================================================================
--- trunk/framework/Bee/Persistence/Behaviors/Ordered/Strategy.php 2013-05-07 07:25:49 UTC (rev 29)
+++ trunk/framework/Bee/Persistence/Behaviors/Ordered/Strategy.php 2013-05-07 11:13:24 UTC (rev 30)
@@ -29,29 +29,54 @@
*/
private $delegate;
+ public function __construct(IDelegate $delgate) {
+ $this->delegate = $delgate;
+ }
+
/**
* @param mixed $subject
* @param mixed $ref
* @param mixed $groupRestriction
+ * @return int
*/
public function moveBefore($subject, $ref, $groupRestriction = false) {
- $this->moveRelative($subject, $ref, true, $groupRestriction);
+ return $this->moveRelative($subject, $ref, true, $groupRestriction);
}
/**
* @param mixed $subject
* @param mixed $ref
* @param mixed $groupRestriction
+ * @return int
*/
- public function moveBehind($subject, $ref, $groupRestriction = false) {
- $this->moveRelative($subject, $ref, false, $groupRestriction);
+ public function moveAfter($subject, $ref, $groupRestriction = false) {
+ return $this->moveRelative($subject, $ref, false, $groupRestriction);
}
/**
* @param mixed $subject
+ * @param mixed $groupRestriction
+ * @return int
+ */
+ public function moveToStart($subject, $groupRestriction = false) {
+ return $this->moveAfter($subject, false, $groupRestriction);
+ }
+
+ /**
+ * @param mixed $subject
+ * @param mixed $groupRestriction
+ * @return int
+ */
+ public function moveToEnd($subject, $groupRestriction = false) {
+ return $this->moveBefore($subject, false, $groupRestriction);
+ }
+
+ /**
+ * @param mixed $subject
* @param mixed $ref
* @param bool $before
* @param mixed $groupRestriction
+ * @return int
*/
public function moveRelative($subject, $ref, $before = true, $groupRestriction = false) {
// determine previous position of subject
@@ -83,5 +108,7 @@
$this->delegate->setPosition($subject, $newPos, $groupRestriction);
}
// that's all, folks!
+
+ return $newPos;
}
}
Modified: trunk/framework/Bee/Persistence/Pdo/Behaviors/GenericOrderedDelegate.php
===================================================================
--- trunk/framework/Bee/Persistence/Pdo/Behaviors/GenericOrderedDelegate.php 2013-05-07 07:25:49 UTC (rev 29)
+++ trunk/framework/Bee/Persistence/Pdo/Behaviors/GenericOrderedDelegate.php 2013-05-07 11:13:24 UTC (rev 30)
@@ -167,9 +167,10 @@
$qryString = sprintf($newPos < $oldPos ? self::SHIFT_UP_QUERY_TEMPLATE : self::SHIFT_DOWN_QUERY_TEMPLATE,
$this->getPosExpression(), $qryDomain, $this->getDomainRestrictionString($orderedEntity, $params, $restriction));
// if this is a single table update, add ORDER clause to avoid unique constraint violation
- if (stripos($qryDomain, ' JOIN ') === false) {
- $qryString .= ' ORDER BY ' . $this->getPosExpression() . ($newPos < $oldPos ? ' DESC' : ' ASC');
- }
+// if (stripos($qryDomain, ' JOIN ') === false) {
+// $qryString .= ' ORDER BY ' . $this->getPosExpression() . ($newPos < $oldPos ? ' DESC' : ' ASC');
+// }
+// var_dump($qryString);
$this->getPdo()->prepare($qryString)->execute($params);
}
@@ -178,7 +179,7 @@
* @param int $newPos
* @param mixed $restriction
*/
- public function setPosition($orderedEntity, $newPos, $restriction) {
+ public function setPosition($orderedEntity, $newPos, $restriction = false) {
$params = array(':newPos' => $newPos);
$qryString = sprintf(self::SET_POS_QUERY_TEMPLATE, $this->getPosExpression(), $this->getQueryDomain(),
$this->getIdentityRestrictionString($orderedEntity, $params), $this->getDomainRestrictionString($orderedEntity, $params, $restriction));
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|