Skip to content

Commit

Permalink
- added first version of datastoretransfer test
Browse files Browse the repository at this point in the history
 - refactoring in databaseDatastore
  • Loading branch information
sebastianmonzel committed Dec 4, 2016
1 parent 5b557b7 commit 0c5583f
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 141 deletions.
2 changes: 1 addition & 1 deletion source/core/datastore/MCombinedDatastore.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function getLatestWebfiles($count = 5)

}

public function searchByTemplate(MWebfile $webfile)
public function searchByTemplate(MWebfile $template)
{

}
Expand Down
13 changes: 6 additions & 7 deletions source/core/datastore/MDatastoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace simpleserv\webfilesframework\core\datastore;

use simpleserv\webfilesframework\core\datastore\types\database\MDatabaseDatastore;
use simpleserv\webfilesframework\core\datastore\types\directory\MDirectoryDatastore;
use simpleserv\webfilesframework\core\datasystem\database\MDatabaseConnection;
use simpleserv\webfilesframework\core\datasystem\file\system\MDirectory;
use simpleserv\webfilesframework\core\datastore\types\directory\MDirectoryDatasourceDatastore;

use simpleserv\webfilesframework\core\datastore\types\database\MDatabaseDatasourceDatastore;
use simpleserv\webfilesframework\MItem;

/**
Expand Down Expand Up @@ -36,16 +35,16 @@ class MDatastoreFactory
/**
*
* @param MItem $item
* @return null|MDatabaseDatasourceDatastore|MDirectoryDatasourceDatastore
* @return MAbstractDatastore
*/
public static function createDatastore(MItem $item)
{
if ($item instanceof MDirectory) {
return new MDirectoryDatasourceDatastore($item);
return new MDirectoryDatastore($item);
} else if ($item instanceof MDatabaseConnection) {
return new MDatabaseDatasourceDatastore($item);
return new MDatabaseDatastore($item);
} else {
return null;
throw new Exeption("Unsupported type to create datastore.");
}
}
}
2 changes: 1 addition & 1 deletion source/core/datastore/MDatastoreTransfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function transfer()
throw new MDatastoreException("Cannot transfer data to a read-only datastore.");
}

$webfiles = $this->source->getWebfilesFromDatastore();
$webfiles = $this->source->getWebfilesAsArray();

foreach ($webfiles as $webfile) {
$this->target->storeWebfile($webfile);
Expand Down
178 changes: 112 additions & 66 deletions source/core/datastore/types/database/MDatabaseDatastore.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,26 @@ public function getWebfilesAsStream()
*/
public function getWebfilesAsArray()
{
return $this->getByCondition();
$result = array();
if (!$this->tableExistsByTablename($this->databaseConnection->getTablePrefix() . "metadata")) {
return $result;
}

$oDatabaseResultHandler = $this->databaseConnection->queryAndHandle("SELECT * FROM " . $this->databaseConnection->getTablePrefix() . "metadata");

if ($oDatabaseResultHandler->getResultSize() > 0) {
throw new \Exception("no tables given in metadata.");
}
while ( $result = $oDatabaseResultHandler->fetchNextResultObject() ) {

$webfilesForTable = $this->getWebfilesByTableName($result->tablename, $result->classname);

foreach ( $webfilesForTable as $webfile ) {
array_push($result,$webfile);
}

}
return $result;
}

/**
Expand Down Expand Up @@ -132,9 +151,11 @@ private function tableExistsByTablename($tableName)
*/
private function getAllTableNames()
{

echo "SHOW TABLES FROM " . $this->databaseConnection->getDatabaseName();
$handler = $this->databaseConnection->queryAndHandle("SHOW TABLES FROM " . $this->databaseConnection->getDatabaseName());

//var_dump($this->databaseConnection);

$tableNames = array();

if ($handler->getResultSize() > 0) {
Expand Down Expand Up @@ -460,101 +481,87 @@ private function resolveMetadataForTablename($tablename)

/**
* @see \simpleserv\webfilesframework\core\datastore\MAbstractDatastore::searchByTemplate()
* @param MWebfile $webfile
* @param MWebfile $template
* @return array
*/
public function searchByTemplate(MWebfile $webfile)
public function searchByTemplate(MWebfile $template)
{

$webfileArray = array();

if ($this->tableExistsByWebfile($webfile)) {

$first = true;
$order = "";
if ($this->tableExistsByWebfile($template)) {

$attributes = $webfile->getAttributes(true);
$tableName = $this->resolveTableNameFromWebfile($template);

// SORTING
foreach ($attributes as $attribute) {
$sorting = $this->translateTemplateIntoSorting($template);
$condition = $this->translateTemplateIntoCondition($template);

$attribute->setAccessible(true);
$webfileArray = $this->getWebfilesByTablename(
$tableName,$template::$m__sClassName,$condition,$sorting);

$name = $attribute->getName();
$value = $attribute->getValue($webfile);

if ($value instanceof MAscendingSorting) {
} else {
$this->createTable($template, false);
}

if (!$first) {
$order .= " , ";
}
$order .= " " . MWebfile::getSimplifiedAttributeName($name) . " ASC ";
$first = false;
} else if ($value instanceof MDescendingSorting) {
return $webfileArray;
}

if (!$first) {
$order .= " , ";
}
$order .= " " . MWebfile::getSimplifiedAttributeName($name) . " DESC ";
$first = false;
}
}
$tableName = $this->resolveTableNameFromWebfile($webfile);
$query = "SELECT * FROM " . $tableName;
private function getWebfilesByTablename($tableName,$className = null,$condition = null,$order = null)
{
$webfileArray = array();
$query = "SELECT * FROM " . $tableName;

if (!empty($condition)) {
$query .= " WHERE " . $condition;
}

$condition = $this->translateTemplateIntoCondition($webfile);
if (!empty($condition)) {
$query .= " WHERE " . $condition;
}
if (!empty($order)) {
$query .= " ORDER BY " . $order;
}

if (!empty($order)) {
$query .= " ORDER BY " . $order;
}
$resultHandler = $this->databaseConnection->queryAndHandle($query);

$resultHandler = $this->databaseConnection->queryAndHandle($query);
if ($resultHandler != false) {
if ($resultHandler->getResultSize() > 0) {
while ($databaseResultObject = $resultHandler->fetchNextResultObject()) {

if ($resultHandler != false) {
if ($resultHandler->getResultSize() > 0) {
while ($databaseResultObject = $resultHandler->fetchNextResultObject()) {
if ( $className == null ) {
$className = $this->resolveClassNameFromTableName($tableName);
}

$className = $webfile::$m__sClassName;
$targetWebfile = new $className();
$attributes = $targetWebfile->getAttributes(true);

$webfile = new $className();
foreach ($attributes as $oAttribute) {
foreach ($attributes as $oAttribute) {

$oAttribute->setAccessible(true);
$oAttribute->setAccessible(true);

$sAttributeName = $oAttribute->getName();
if (MWebfile::isSimpleDatatype($sAttributeName)) {
$sDatabaseFieldName = MWebfile::getSimplifiedAttributeName($sAttributeName);
$oAttribute->setValue($webfile, $databaseResultObject->$sDatabaseFieldName);
} else if (MWebfile::isObject($sAttributeName)) {
eval("\$sClassName = static::\$s__oAggregation[\$sAttributeName];");
/** @noinspection PhpUndefinedVariableInspection */
eval("\$oSubAttributeArray = $sClassName::getAttributes(1);");
/** @noinspection PhpUndefinedVariableInspection */
foreach ($oSubAttributeArray as $oSubAttribute) {
$sAttributeName = $oAttribute->getName();
if (MWebfile::isSimpleDatatype($sAttributeName)) {
$sDatabaseFieldName = MWebfile::getSimplifiedAttributeName($sAttributeName);
$oAttribute->setValue($targetWebfile, $databaseResultObject->$sDatabaseFieldName);
} else if (MWebfile::isObject($sAttributeName)) {
eval("\$sClassName = static::\$s__oAggregation[\$sAttributeName];");
/** @noinspection PhpUndefinedVariableInspection */
eval("\$oSubAttributeArray = $sClassName::getAttributes(1);");
/** @noinspection PhpUndefinedVariableInspection */
foreach ($oSubAttributeArray as $oSubAttribute) {

$oSubAttributeName = $oSubAttribute->getName();
if (MWebfile::isSimpleDatatype($oSubAttributeName)) {
$oSubAttributeName = $oSubAttribute->getName();
if (MWebfile::isSimpleDatatype($oSubAttributeName)) {

$sDatabaseFieldName = $this->resolveTableNameFromWebfile(new $tableName()) . "_" . MWebfile::getSimplifiedAttributeName($oSubAttributeName);
$webfile->$sAttributeName->$oSubAttributeName = $databaseResultObject->$sDatabaseFieldName;
}
$sDatabaseFieldName = $this->resolveTableNameFromWebfile(
new $tableName()) . "_" . MWebfile::getSimplifiedAttributeName($oSubAttributeName);
$targetWebfile->$sAttributeName->$oSubAttributeName = $databaseResultObject->$sDatabaseFieldName;
}
}
}
array_push($webfileArray, $webfile);
}
array_push($webfileArray, $targetWebfile);
}
}
} else {
$this->createTable($webfile, false);
}

return $webfileArray;

}

/**
Expand Down Expand Up @@ -636,4 +643,43 @@ public function deleteByTemplate(MWebfile $webfile)
}
}

/**
* @param MWebfile $template
* @param $first
* @param $order
* @return string
*/
public function translateTemplateIntoSorting(MWebfile $template)
{
$attributes = $template->getAttributes(true);

$order = "";
$first = true;

foreach ($attributes as $attribute) {

$attribute->setAccessible(true);

$name = $attribute->getName();
$value = $attribute->getValue($template);

if ($value instanceof MAscendingSorting) {

if (!$first) {
$order .= " , ";
}
$order .= " " . MWebfile::getSimplifiedAttributeName($name) . " ASC ";
$first = false;
} else if ($value instanceof MDescendingSorting) {

if (!$first) {
$order .= " , ";
}
$order .= " " . MWebfile::getSimplifiedAttributeName($name) . " DESC ";
$first = false;
}
}
return $order;
}

}
4 changes: 2 additions & 2 deletions source/core/datastore/types/mail/MImapDatastore.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ public function hasItem(MWebfile $item)
return $file->exists();
}

public function searchByTemplate(MWebfile $webfile)
public function searchByTemplate(MWebfile $template)
{
if (!$webfile instanceof MMail) {
if (!$template instanceof MMail) {
throw new MDatastoreException("Cannot search in imap datastore for webfiles appart of type 'MMail'.");
}
}
Expand Down
4 changes: 2 additions & 2 deletions source/core/datastore/types/remote/MRemoteDatastore.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public function getLatestWebfiles($count = 5)
// TODO
}

public function searchByTemplate(MWebfile $webfile)
public function searchByTemplate(MWebfile $template)
{

$data = array();
$data['method'] = "getByTemplate";
$data['template'] = $webfile->marshall();
$data['template'] = $template->marshall();

return $this->getWebfilesAsStream($data)->getWebfiles();
}
Expand Down
8 changes: 6 additions & 2 deletions source/core/datasystem/file/system/MDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ public function getFiles()

$filenames = array();

if ($oDiractoryHandle = opendir($this->m_sPath)) {
while (false !== ($filename = readdir($oDiractoryHandle))) {
if ( ! $this->exists() ) {
throw new \Exception("file '" . $this->m_sPath . "' does not exist.");
}

if ($oDirectoryHandle = opendir($this->m_sPath)) {
while (false !== ($filename = readdir($oDirectoryHandle))) {
if ($filename != "." && $filename != ".." && (!is_dir($this->m_sPath . "/" . $filename))) {
$file = new MFile($this->getPath() . "/" . $filename);
array_push($filenames, $file);
Expand Down
10 changes: 10 additions & 0 deletions tests/resources/folderDatastore2/sampleWebfile1.webfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<object classname="simpleserv\webfilesframework\core\datastore\types\database\MSampleWebfile">
<firstname><![CDATA[Sebastian]]></firstname>
<lastname><![CDATA[Monzel]]></lastname>
<street><![CDATA[Blumenstraße]]></street>
<housenumber><![CDATA[4]]></housenumber>
<postcode><![CDATA[67433]]></postcode>
<city><![CDATA[Neustadt an der Weinstraße]]></city>
<id><![CDATA[1]]></id>
</object>
10 changes: 10 additions & 0 deletions tests/resources/folderDatastore2/sampleWebfile2.webfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<object classname="simpleserv\webfilesframework\core\datastore\types\database\MSampleWebfile">
<firstname><![CDATA[Sergey]]></firstname>
<lastname><![CDATA[Brin]]></lastname>
<street><![CDATA[Blumenstraße]]></street>
<housenumber><![CDATA[8]]></housenumber>
<postcode><![CDATA[67433]]></postcode>
<city><![CDATA[Neustadt an der Weinstraße]]></city>
<id><![CDATA[2]]></id>
</object>
40 changes: 0 additions & 40 deletions tests/source/core/datastore/MDatastoreFactoryTest.php

This file was deleted.

Loading

0 comments on commit 0c5583f

Please sign in to comment.