Skip to content

Commit

Permalink
adding update framework and refactored entity manager and db abstract…
Browse files Browse the repository at this point in the history
…ion layer.

moved perform action into api from caller.
added schema update to make tables transactional under mysql
  • Loading branch information
padams committed Jun 28, 2008
1 parent de35bc1 commit 94f9ddf
Show file tree
Hide file tree
Showing 15 changed files with 415 additions and 289 deletions.
2 changes: 2 additions & 0 deletions conf/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
2502 => array("The module was deactivated successfully.",0),
2503 => array("Options reset to Default Values.",0),


//User managment
3000 => array("Success. User Added.", 0),
3001 => array("Error. That user name is already taken.",0),
Expand Down Expand Up @@ -73,6 +74,7 @@
3304 => array("Success. Admin User Added.",0),
3305 => array("Success. Base Database Schema Installed.",0),
3306 => array("Error. User id already exists for some reason.",0),
3307 => array("Updates failed. Check the log file for more details and try again.",0),

// Graph related
3500 => array("There is no data for\nthis time period.",0),
Expand Down
132 changes: 12 additions & 120 deletions modules/base/classes/entityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,41 +95,14 @@ function getColumns() {
*/
function create() {

$all_cols = $this->entity->getColumns();

$cols = array();

// Control loop
foreach ($all_cols as $k => $v){

// drop column is it is marked as auto-incement as DB will take car of that.
if ($this->entity->$v->auto_increment == true):
break;
else:
$cols[$v] = $this->entity->$v->value;
endif;

}

// Persist object
$status = $this->db->save($cols, get_class($this->entity));

// Add to Cache

if ($status == true):
if ($this->config['cache_objects'] == true):
$this->cache->set(get_class($this->entity), 'id'.$this->entity->id->value, $this->entity);
endif;
endif;

return $status;
return $this->entity->create();

}

/**
* Create Table
*
* Handled by DB abstraction layer because the SQ associated with this is way too DB specific
* Handled by DB abstraction layer because the SQL associated with this is way too DB specific
*/
function createTable() {

Expand Down Expand Up @@ -234,24 +207,7 @@ function renameTable($new_table_name) {
*/
function update($where = '') {

if(empty($where)):
$constraint = array('id' => $this->entity->id->value);
else:
$constraint = array($where => $this->entity->$where->value);
endif;


// Persist object
$status = $this->db->update($this->_getProperties(), $constraint, get_class($this->entity));

// Add to Cache
if ($status == true):
if ($this->config['cache_objects'] == true):
$this->cache->replace(get_class($this->entity), 'id'.$this->entity->id->value, $this->entity);
endif;
endif;

return $status;
$this->entity->update($where);

}

Expand All @@ -264,26 +220,7 @@ function update($where = '') {
*/
function partialUpdate($named_properties, $where) {

$properties = array();

foreach ($named_properties as $n) {

$properties[$n] = $this->entity->$n->value;

}


// Persist object
$status = $this->db->update($properties, $where, get_class($this->entity));

// Add to Cache
if ($status == true):
if ($this->config['cache_objects'] == true):
$this->cache->set(get_class($this->entity), 'id'.$this->entity->id->value, $this->entity);
endif;
endif;

return $status;
return $this->entity->partialUpdate($named_properties, $where);

}

Expand All @@ -294,59 +231,19 @@ function partialUpdate($named_properties, $where) {
*/
function delete($id, $col = '') {

if (empty($col)):
$col = 'id';
endif;

// Persist object
$status = $this->db->delete($id, $col, get_class($this->entity));

// Add to Cache
if ($status == true):
if ($this->config['cache_objects'] == true):
$this->cache->remove(get_class($this->entity), 'id'.$this->entity->id->value);
endif;
endif;

return $status;

return $this->entity->delete($id, $col);
}

function getByPk($col, $value) {

return $this->getByColumn($col, $value);
return $this->entity->getByColumn($col, $value);

}

function getByColumn($col, $value) {

$cache_obj = '';

if ($this->config['cache_objects'] == true):
$cache_obj = $this->cache->get(get_class($this->entity), $col.$value);
endif;

if (!empty($cache_obj)):

$this->entity = $cache_obj;

else:

$constraint = array($col => $value);

$properties = $this->db->select($this->_getProperties(), $constraint, get_class($this->entity));

if (!empty($properties)):

$this->setProperties($properties);

if ($this->config['cache_objects'] == true):
$this->cache->set(get_class($this->entity), 'id'.$this->entity->id->value, $this->entity);
endif;
endif;
endif;
return $this->entity->getByColumn($col, $value);

return;
}

function find($params = array()) {
Expand Down Expand Up @@ -401,18 +298,13 @@ function set($name, $value) {
return $this->entity->$name->value = $value;
}


/**
* Sets Values/Properties
* @depricated use setProperties()
*/
function setValues($values) {

$properties = array_keys(get_object_vars($this->entity));

foreach ($properties as $k => $v) {

$this->entity->$v->value = $values[$v];

}

return;
return $this->setProperties($values);

}

Expand Down
4 changes: 2 additions & 2 deletions modules/base/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ function owa_baseModule() {
$this->version = '1.0';
$this->description = 'Base functionality for OWA.';
$this->config_required = false;
$this->required_schema_version = 1;
$this->required_schema_version = 3;


$this->owa_module();
$this->c->set('base', 'schema_version', '1');
//$this->c->set('base', 'schema_version', '1');
return;
}

Expand Down
31 changes: 20 additions & 11 deletions modules/base/updates/_002.php → modules/base/updates/002.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,30 @@
*/


class owa_002_update extends owa_update {

class owa_base_002_update extends owa_update {

function up() {

$tt = owa_coreAPI::entityFactory('base.testtable');
$ret = $tt->createTable();
$db = owa_coreAPI::dbSingleton();
$api = &owa_coreAPI::singleton();

$entities = $api->modules[$this->module_name]->getEntities();

foreach ($entities as $k => $v) {

$ret = $db->alterTableType($this->c->get('base', 'ns').$v, 'InnoDB');

if ($ret == true):
$this->e->debug(sprintf('Changed Table %s to InnoDB', $v));
else:
$this->e->debug(sprintf('Change to Table %s failed', $v));
return false;
endif;

}


if ($ret == true):
print 'table testtable created';
return true;
else:
print 'table testtable creation failed';
return false;
endif;
return true;


}
Expand Down
58 changes: 0 additions & 58 deletions modules/base/updates/_003.php

This file was deleted.

24 changes: 19 additions & 5 deletions modules/base/updatesApply.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,39 @@ function action() {
$api = &owa_coreAPI::singleton();

$modules = $api->getModulesNeedingUpdates();
//print_r($modules);
//return;

// foreach do update in order

$error = false;

foreach ($modules as $k => $v) {

$ret = $api->modules[$v]->update();

if ($ret != true):
$error = true;
break;
endif;

}

if ($error == true):
$data['error_msg'] = 'something went wrong here with the updates.';
$data['view'] = 'base.error';
$data['view_method'] = 'delegate';
else:

// add data to container
$this->data['status_code'] = '';
$this->data['do'] = 'base.optionsGeneral';
$this->data['view_method'] = 'delegate';
// add data to container
$data['status_code'] = 3307;
$data['do'] = 'base.optionsGeneral';
$data['view_method'] = 'redirect';

endif;

return $this->data;
return $data;


}

Expand Down
3 changes: 2 additions & 1 deletion modules/hello/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ function owa_helloModule() {
$this->version = '1.0';
$this->description = 'Hello world sample module.';
$this->config_required = false;
$this->required_schema_version = 2;
$this->required_schema_version = 1;

$this->owa_module();
//$this->c->set('hello', 'schema_version', '1');

return;
}
Expand Down
Loading

0 comments on commit 94f9ddf

Please sign in to comment.