[Cs-webapplibs-commits] SF.net SVN: cs-webapplibs:[170] trunk/0.3
Status: Beta
Brought to you by:
crazedsanity
|
From: <cra...@us...> - 2010-06-21 14:42:00
|
Revision: 170
http://cs-webapplibs.svn.sourceforge.net/cs-webapplibs/?rev=170&view=rev
Author: crazedsanity
Date: 2010-06-21 14:41:53 +0000 (Mon, 21 Jun 2010)
Log Message:
-----------
Resurrect cs_genericDataLinker, fix some unit tests (uncomment them, fix method references).
Modified Paths:
--------------
trunk/0.3/tests/testOfCSWebAppLibs.php
Added Paths:
-----------
trunk/0.3/abstract/cs_gdlAttrib.abstract.class.php
trunk/0.3/abstract/cs_gdlObject.abstract.class.php
trunk/0.3/abstract/cs_gdlPath.abstract.class.php
trunk/0.3/cs_genericDataLinker.class.php
Copied: trunk/0.3/abstract/cs_gdlAttrib.abstract.class.php (from rev 165, trunk/0.3/abstract/cs_gdlAttrib.abstract.class.php)
===================================================================
--- trunk/0.3/abstract/cs_gdlAttrib.abstract.class.php (rev 0)
+++ trunk/0.3/abstract/cs_gdlAttrib.abstract.class.php 2010-06-21 14:41:53 UTC (rev 170)
@@ -0,0 +1,89 @@
+<?php
+/*
+ * Created on Jan 29, 2009
+ *
+ * FILE INFORMATION:
+ *
+ * $HeadURL$
+ * $Id$
+ * $LastChangedDate$
+ * $LastChangedBy$
+ * $LastChangedRevision$
+ */
+
+abstract class cs_gdlAttribAbstract extends cs_gdlPathAbstract {
+
+
+ const table='cswal_gdl_attribute_table';
+ const tableSeq = 'cswal_gdl_attribute_table_attribute_id_seq';
+
+
+
+ //-------------------------------------------------------------------------
+ public function create_attrib($path, $data, $type=null) {
+
+ $objectId = $this->get_object_id_from_path($path);
+
+ $insertString = "";
+ $attribs = array();
+ if(is_array($data) && count($data)) {
+ foreach($data as $n=>$v) {
+ $n = $this->translate_attrib_name($n);
+ $attribs[$n] = $v;
+ }
+ }
+ elseif(!is_null($type)) {
+ $key = $this->translate_attrib_name($type);
+ $attribs = array($key => $data);
+ }
+ else {
+ throw new exception(__METHOD__ .": data was not array and no type set");
+ }
+
+ if(!is_array($attribs) || !count($attribs)) {
+ throw new exception(__METHOD__ .": failed to create an array of attributes... ". $this->gfObj->debug_print(func_get_args(),0));
+ }
+
+ $attribs['object_id'] = $objectId;
+ $insertString = $this->gfObj->string_from_array($attribs, 'insert');
+
+
+ if(!strlen($insertString)) {
+ throw new exception(__METHOD__ .": invalid insert string (". $insertString .")");
+ }
+ $sql = "INSERT INTO ". self::attrTable ." ". $insertString;
+
+ try {
+ $retval = $this->db->run_insert($sql, self::attrTableSeq);
+ }
+ catch(exception $e) {
+ throw new exception(__METHOD__ .": failed to perform insert::: ". $e->getMessage() .' ---- '. $sql);
+ }
+
+ return($retval);
+ }//end create_attrib()
+ //-------------------------------------------------------------------------
+
+
+
+ //-------------------------------------------------------------------------
+ public function translate_attrib_name($name) {
+ $retval = null;
+ foreach($this->validTypes as $type) {
+ if(preg_match('/^'. $type .'/', $name)) {
+ $retval = 'a_'. $type;
+ break;
+ }
+ }
+
+ if(is_null($retval) || !strlen($retval)) {
+ $this->gfObj->debug_print(__METHOD__ .": name was (". $name ."), retval=(". $retval .")",1);
+ throw new exception(__METHOD__ .": invalid attribute name (". $name .")");
+ }
+
+ return($retval);
+ }//end translate_attrib_name()
+ //-------------------------------------------------------------------------
+
+}
+?>
\ No newline at end of file
Copied: trunk/0.3/abstract/cs_gdlObject.abstract.class.php (from rev 165, trunk/0.3/abstract/cs_gdlObject.abstract.class.php)
===================================================================
--- trunk/0.3/abstract/cs_gdlObject.abstract.class.php (rev 0)
+++ trunk/0.3/abstract/cs_gdlObject.abstract.class.php 2010-06-21 14:41:53 UTC (rev 170)
@@ -0,0 +1,136 @@
+<?php
+/*
+ * Created on Jan 29, 2009
+ *
+ * FILE INFORMATION:
+ *
+ * $HeadURL$
+ * $Id$
+ * $LastChangedDate$
+ * $LastChangedBy$
+ * $LastChangedRevision$
+ */
+
+abstract class cs_gdlObjectAbstract extends cs_webapplibsAbstract {
+
+
+ const table='cswal_gdl_object_table';
+ const tableSeq = 'cswal_gdl_object_table_object_id_seq';
+
+ //-------------------------------------------------------------------------
+ public function get_object_id_from_path($path) {
+ $sql = "SELECT object_id FROM ". self::table ." WHERE object_path='".
+ $this->clean_path($path) ."'";
+
+ try {
+ $data = $this->db->run_query($sql);
+
+ if(is_array($data) && count($data) == 1) {
+ $retval = $data['object_id'];
+ }
+ else {
+ throw new exception(__METHOD__ .": invalid data for path (". $this->clean_path($path) .")::: ". $this->gfObj->debug_var_dump($data,0));
+ }
+ }
+ catch(exception $e) {
+ throw new exception(__METHOD__ .": error retrieving path::: ". $e->getMessage());
+ }
+
+ return($retval);
+ }//end get_object_id_from_path()
+ //-------------------------------------------------------------------------
+
+
+
+ //-------------------------------------------------------------------------
+ public function create_object($name, $data=null, $type=null) {
+ $sql = "INSERT INTO ". self::table ." (object_name) VALUES ('".
+ $this->gfObj->cleanString($this->clean_path($name), 'sql_insert') ."')";
+ try {
+ $retval = $this->db->run_insert($sql, self::tableSeq);
+ }
+ catch(exception $e) {
+ throw new exception(__METHOD__ .": failed to perform insert::: ". $e->getMessage());
+ }
+
+ if(!is_null($data)) {
+ throw new exception(__METHOD__ .": can't create data for objects yet");
+ }
+ return($retval);
+ }//end create_object()
+ //-------------------------------------------------------------------------
+
+
+
+ //-------------------------------------------------------------------------
+ public function build_object_id_list(array $objects) {
+ $this->gfObj->switch_force_sql_quotes(1);
+ $sql = "SELECT * FROM ". self::table ." WHERE object_name IN (".
+ $this->gfObj->string_from_array($objects, null, ',', 'sql') .")";
+ $this->gfObj->switch_force_sql_quotes(0);
+
+ $retval = array();
+ try {
+ $retval = $this->db->run_query($sql, 'object_name', 'object_id');
+ if(!is_array($retval)) {
+ $retval = array();
+ }
+ }
+ catch(exception $e) {
+ //throw new exception(__METHOD__ .": failed to retrieve list");
+ }
+
+ return($retval);
+ }//end build_object_id_list()
+ //-------------------------------------------------------------------------
+
+
+
+ //-------------------------------------------------------------------------
+ public function build_object_name_list(array $objectIds) {
+ $this->gfObj->switch_force_sql_quotes(1);
+//string_from_array($array,$style=NULL,$separator=NULL, $cleanString=NULL, $removeEmptyVals=FALSE)
+ foreach($objectIds as $i=>$myId) {
+ if(!strlen($myId)) {
+ unset($objectIds[$i]);
+ }
+ }
+ $sql = "SELECT * FROM ". self::table ." WHERE object_id IN (".
+ $this->gfObj->string_from_array($objectIds, null, ',', 'sql',true) .")";
+ $this->gfObj->switch_force_sql_quotes(0);
+
+ try {
+ $retval = $this->db->run_query($sql, 'object_id', 'object_name');
+ if(!is_array($retval)) {
+ $retval = array();
+ }
+ }
+ catch(exception $e) {
+ throw new exception(__METHOD__ .": failed to retrieve list::: ". $e->getMessage());
+ }
+
+ return($retval);
+ }//end build_object_id_list()
+ //-------------------------------------------------------------------------
+
+
+
+ //-------------------------------------------------------------------------
+ public function create_objects_enmasse(array $objectList) {
+ $retval = 0;
+ foreach($objectList as $name) {
+ try {
+ $this->create_object($name);
+ $retval++;
+ }
+ catch(exception $e) {
+ //nothing to see here, move along.
+ }
+ }
+ return($retval);
+ }//end create_objects_enmasse()
+ //-------------------------------------------------------------------------
+
+
+}
+?>
Copied: trunk/0.3/abstract/cs_gdlPath.abstract.class.php (from rev 165, trunk/0.3/abstract/cs_gdlPath.abstract.class.php)
===================================================================
--- trunk/0.3/abstract/cs_gdlPath.abstract.class.php (rev 0)
+++ trunk/0.3/abstract/cs_gdlPath.abstract.class.php 2010-06-21 14:41:53 UTC (rev 170)
@@ -0,0 +1,126 @@
+<?php
+/*
+ * Created on Jan 29, 2009
+ *
+ * FILE INFORMATION:
+ *
+ * $HeadURL$
+ * $Id$
+ * $LastChangedDate$
+ * $LastChangedBy$
+ * $LastChangedRevision$
+ */
+
+abstract class cs_gdlPathAbstract extends cs_gdlObjectAbstract {
+
+
+ const table='cswal_gdl_path_table';
+ const tableSeq = 'cswal_gdl_path_table_path_id_seq';
+
+
+
+
+
+ //-------------------------------------------------------------------------
+ public function create_path($path) {
+ $idList = $this->create_path_objects($path);
+ $pathIdList = $this->create_id_path($idList);
+
+ if($this->get_path_from_idlist($pathIdList)) {
+ $retval = $pathIdList;
+ }
+ else {
+
+ $sql = "INSERT INTO ". self::table ." (path_id_list) VALUES ('". $pathIdList ."')";
+
+ try {
+ $insertedId = $this->db->run_insert($sql, self::tableSeq);
+ $retval = $pathIdList;
+ }
+ catch(exception $e) {
+ throw new exception(__METHOD__ .": unable to create path::: ". $e->getMessage());
+ }
+ }
+
+ return($retval);
+ }//end create_path()
+ //-------------------------------------------------------------------------
+
+
+
+ //-------------------------------------------------------------------------
+ public function clean_path($path, $appendBase=true) {
+ if(strlen($path)) {
+ if($appendBase === true && !is_null($this->basePath)) {
+ $path = $this->basePath .'/'. $path;
+ }
+ $newPath = preg_replace('/\/{2,}/', '/', $path);
+
+ if(!strlen($newPath)) {
+ throw new exception(__METHOD__ .": new path is zero-length (". $newPath ."), old path=(". func_get_arg(0) .")");
+ }
+ }
+ else {
+ throw new exception(__METHOD__ .": no valid path given (". $path .")");
+ }
+
+ return($newPath);
+ }//end clean_path()
+ //-------------------------------------------------------------------------
+
+
+
+ //-------------------------------------------------------------------------
+ public function set_base_path($path) {
+ if(is_null($path) || !strlen($path)) {
+ $this->basePath = null;
+ }
+ else {
+ $this->basePath = $this->clean_path($path,false);
+ }
+ }//end set_base_path()
+ //-------------------------------------------------------------------------
+
+
+
+ //-------------------------------------------------------------------------
+ public function explode_path($path, $appendBase=true) {
+ $path = $this->clean_path($path, $appendBase);
+ $path = preg_replace('/^\//', '', $path);
+ $path = preg_replace('/\/$/', '', $path);
+
+ return(explode('/', $path));
+ }//end explode_path()
+ //-------------------------------------------------------------------------
+
+
+
+ //-------------------------------------------------------------------------
+ public function create_id_path(array $idList) {
+ $retval = ':'. $this->gfObj->string_from_array($idList, null, '::') .':';
+ return($retval);
+ }//end create_id_path()
+ //-------------------------------------------------------------------------
+
+
+
+ //-------------------------------------------------------------------------
+ public function get_path_from_idlist($idPath) {
+
+ $idList = explode('::', preg_replace('/^:/', '', preg_replace('/:$/', '', $idPath)));
+
+ $nameList = $this->build_object_name_list($idList);
+
+ $retval = "/";
+ foreach($nameList as $id=>$name) {
+ $retval = $this->gfObj->create_list($retval, $name, '/');
+ }
+
+ $retval = $this->clean_path($retval,false);
+
+ return($retval);
+ }//end get_text_path_from_id_path()
+ //-------------------------------------------------------------------------
+
+}
+?>
\ No newline at end of file
Copied: trunk/0.3/cs_genericDataLinker.class.php (from rev 165, trunk/0.3/cs_genericDataLinker.class.php)
===================================================================
--- trunk/0.3/cs_genericDataLinker.class.php (rev 0)
+++ trunk/0.3/cs_genericDataLinker.class.php 2010-06-21 14:41:53 UTC (rev 170)
@@ -0,0 +1,101 @@
+<?php
+/*
+ * Created on Oct 27, 2009
+ *
+ * THE IDEA:::
+ * 1.) Unix/Linux-like paths lead to an attribute.
+ * 2.) Multiple paths can lead to the same attribute.
+ * 3.) An attribute can be linked to its original path.
+ * 4.) Each "directory" in a path is an object with an ID.
+ * 5.) Paths themselves are only stored on attributes: intermediate paths may be valid if all objects
+ * for that path are also valid (i.e. if "/one/two/three" is valid, so is "/two/one/three" and "/three/two/one").
+ * 6.) Database...
+ * a.) finding an attribute referencing a single object should be straightforward and fast.
+ * b.) objects are unique to avoid excess duplicate pathways
+ * c.) using id paths with each number wrapped in colons is simple (i.e. ":3342:", ":3342::3::3:"
+ *
+ * The idea here is to have a class that generically links data together (in a
+ * database). It is not meant to be a super clean or speedy system, instead meant
+ * as a way of describing relationships between various pieces of data.
+ *
+ * Once a path is created (list object_id's, separated by '::'), it should always have an attribute.
+ *
+ *
+ * 1::2::3 -> select * from <bla> WHERE path = '2' OR path like '2::%' OR path like '%::2'
+ * -OR-
+ * :1::2::3: -> select * from <bla> WHERE path like '%:2:%'
+ *
+ * If an attribute is created with a small path (like "/test") and the id is 1, the attribute shows ":1:"
+ * --> if the id is 7720218, then the attribute shows ":7720218:"
+ *
+ *
+ * SVN INFORMATION:::
+ * -------------------
+ * Last Author::::::::: $Author$
+ * Current Revision:::: $Revision$
+ * Repository Location: $HeadURL$
+ * Last Updated:::::::: $Date$
+ */
+
+
+
+class cs_genericDataLinker extends cs_gdlAttribAbstract {
+
+ const attrTable='cswal_gdl_attribute_table';
+ const attrTableSeq='cswal_gdl_attribute_table_attribute_id_seq';
+
+ protected $validTypes = array('text', 'int', 'dec', 'bool');
+ protected $gfObj;
+ protected $basePath=null;
+
+ public $db;
+
+ //-------------------------------------------------------------------------
+ public function __construct(cs_phpDB $db) {
+
+ parent::__construct($db);
+ if(!$db->is_connected()) {
+ throw new exception(__METHOD__ .": database not connected");
+ }
+ $this->db = $db;
+ $this->gfObj = new cs_globalFunctions;
+ }//end __construct()
+ //-------------------------------------------------------------------------
+
+
+
+ //-------------------------------------------------------------------------
+ public function create_path_objects($path) {
+ $newPath = $this->clean_path($path);
+ $newPath = preg_replace('/^\//', '', $newPath);
+
+ //break it into bits.
+ $bits = explode('/', $newPath);
+
+ $myList = $this->build_object_id_list($bits);
+ if(count($myList) !== count($bits)) {
+ $createThese = array();
+ foreach($bits as $name) {
+ if(!isset($myList[$name])) {
+ $createThese[] = $name;
+ }
+ }
+ $this->create_objects_enmasse($createThese);
+ $myList = $this->build_object_id_list($bits);
+ }
+
+ $retval = array();
+ foreach($bits as $name) {
+ @$retval[$name] = $myList[$name];
+ }
+
+ if(is_null($retval) || !is_array($retval) || !count($retval)) {
+ throw new exception(__METHOD__ .": failed to build path objects... ". $retval);
+ }
+
+ return($retval);
+ }//end create_path_objects()
+ //-------------------------------------------------------------------------
+}
+
+?>
Modified: trunk/0.3/tests/testOfCSWebAppLibs.php
===================================================================
--- trunk/0.3/tests/testOfCSWebAppLibs.php 2010-06-21 14:29:53 UTC (rev 169)
+++ trunk/0.3/tests/testOfCSWebAppLibs.php 2010-06-21 14:41:53 UTC (rev 170)
@@ -203,7 +203,7 @@
//--------------------------------------------------------------------------
- function tst_genericDataLinker() {
+ function test_genericDataLinker() {
$x = new gdlTester($this->create_dbconn());
@@ -260,7 +260,7 @@
}
- /*/basic tests for building text-based paths vs. id-based paths.
+ //basic tests for building text-based paths vs. id-based paths.
{
$myPath = '/character/sheet/Tetra Tealeaf';
@@ -273,8 +273,8 @@
$idList2 = $x->create_path($myPath);
$this->assertEqual($x->create_id_path(array_values($idList)), $idList2);
- $this->assertEqual($myPath, $x->get_text_path_from_id_path($x->create_id_path($idList)));
- $this->gfObj->debug_var_dump($x->get_text_path_from_id_path($x->create_id_path($idList)));
+ $this->assertEqual($myPath, $x->get_path_from_idlist($x->create_id_path($idList)));
+ $this->gfObj->debug_var_dump($x->get_path_from_idlist($x->create_id_path($idList)));
$this->gfObj->debug_var_dump($idList);
$this->gfObj->debug_var_dump($idList2);
@@ -495,7 +495,6 @@
}
}
-/*
class gdlTester extends cs_genericDataLinker {
public $isTest = true;
@@ -507,6 +506,5 @@
return($this->create_path_objects($path));
}
}
-//*/
?>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|