Skip to content

Commit

Permalink
MAJOR: added write and writeToStage
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Barrett committed Oct 8, 2015
1 parent 15d5b35 commit dca81ed
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 28 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: php
php:
language: php
php:
- 5.3

env:
Expand All @@ -21,6 +21,6 @@ before_script:
- php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss
- cd ~/builds/ss

script:
- vendor/bin/phpunit <module-name>/tests/
script:
- vendor/bin/phpunit batchwrite/tests/

1 change: 1 addition & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
8 changes: 0 additions & 8 deletions _config/config.yml

This file was deleted.

Empty file removed code/.gitkeep
Empty file.
9 changes: 0 additions & 9 deletions code/Extensions/BatchWriteDataObjectExtension.php

This file was deleted.

154 changes: 154 additions & 0 deletions code/Helpers/Batch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

class Batch
{
public function write($dataObjects)
{
return $this->writeTablePostfix($dataObjects);
}

public function writeToStage($dataObjects)
{
$stages = func_get_args();
array_shift($stages);

foreach ($stages as $stage) {
$this->writeTablePostfix($dataObjects, $stage);
}

return;
}

private function writeTablePostfix($dataObjects, $postfix = '')
{
if ($postfix === 'Stage') {
$postfix = '';
}

$types = array();

foreach ($dataObjects as $dataObject) {
$dataObject->onBeforeWrite();

$action = 'insert';
if ($dataObject->exists()) {
$action = 'update';
}

$types[$dataObject->ClassName][$action][] = $dataObject;
}

foreach ($types as $className => $actions) {
foreach ($actions as $action => $objects) {

$classSingleton = singleton($className);
$ancestry = array_filter($classSingleton->getClassAncestry(), function ($class) {
return DataObject::has_own_table($class);
});

foreach ($objects as $obj) {
if (!$obj->Created) {
$obj->Created = date('Y-m-d H:i:s');
}
$obj->LastEdited = date('Y-m-d H:i:s');
}

$rootClass = array_shift($ancestry);

$table = $rootClass . ($postfix ? '_' . $postfix : '');
$this->writeClassTable($objects, $rootClass, $table, false, $action === 'update');

if ($action === 'insert') {
$sql = 'SELECT LAST_INSERT_ID() AS ID, ROW_COUNT() AS Count';

$row = DB::query($sql)->first();

// check count?
$id = intval($row['ID']);

foreach ($objects as $obj) {
$obj->ID = $id;
$id++;
}
}

foreach ($ancestry as $class) {
$table = $class . ($postfix ? '_' . $postfix : '');
$this->writeClassTable($objects, $class, $table, true, $action === 'update');
}

foreach ($objects as $obj) {
$obj->onAfterWrite();
}

$objects[0]->flushCache();
}
}

return $dataObjects;
}

private function writeClassTable($objects, $class, $table, $setID = false, $update = false)
{
$fields = DataObject::database_fields($class);
$singleton = singleton($class);

$columnNames = array();

if ($setID || $update) {
$columnNames[] = 'ID';
}

foreach ($fields as $fieldName => $type) {
if (!$singleton->hasOwnTableDatabaseField($fieldName)) {
continue;
}

$columnNames[] = $fieldName;
}

$values = array();
foreach ($objects as $obj) {
$objectValues = array();

if ($setID || $update) {
$field = $singleton->dbObject('ID');
$objectValues[] = $field->prepValueForDB($obj->ID);
}

foreach ($fields as $fieldName => $type) {
if (!$singleton->hasOwnTableDatabaseField($fieldName)) {
continue;
}

$field = $singleton->dbObject($fieldName);
if (!$field) {
$field = DBField::create_field('Varchar', null, $fieldName);
}
$objectValues[] = $field->prepValueForDB($obj->$fieldName);
}
$values[] = $objectValues;
}

$columns = implode(', ', array_map(function ($name) {
return "`{$name}`";
}, $columnNames));

$values = implode(', ', array_map(function ($objectValues) {
return '(' . implode(', ', $objectValues) . ')';
}, $values));

$sql = "INSERT INTO `{$table}` ({$columns}) VALUES {$values}";

if ($update) {
$columnValues = implode(', ', array_map(function ($name) {
return "`{$name}` = VALUES(`{$name}`)";
}, array_filter($columnNames, function($name) {
return $name !== 'ID';
})));
$sql .= " ON DUPLICATE KEY UPDATE {$columnValues}";
}

DB::query($sql);
}
}
7 changes: 0 additions & 7 deletions phpunit.xml

This file was deleted.

Empty file removed tests/.gitkeep
Empty file.

0 comments on commit dca81ed

Please sign in to comment.