SF.net SVN: postfixadmin:[1710] trunk
Brought to you by:
christian_boltz,
gingerdog
From: <chr...@us...> - 2014-11-01 18:08:14
|
Revision: 1710 http://sourceforge.net/p/postfixadmin/code/1710 Author: christian_boltz Date: 2014-11-01 18:08:11 +0000 (Sat, 01 Nov 2014) Log Message: ----------- Add CliScheme.php: - displays the database scheme (for usage in upgrade.php) PFAHandler: - add "Scheme" to the list of available tasks postfixadmin-cli.php: - add "scheme" to help This is the first patch of a series sponsored by Bund der Deutschen Landjugend (german rural youth) http://bdl.landjugend.info/ Modified Paths: -------------- trunk/model/PFAHandler.php trunk/scripts/postfixadmin-cli.php Added Paths: ----------- trunk/model/CliScheme.php Added: trunk/model/CliScheme.php =================================================================== --- trunk/model/CliScheme.php (rev 0) +++ trunk/model/CliScheme.php 2014-11-01 18:08:11 UTC (rev 1710) @@ -0,0 +1,104 @@ +<?php +# $Id$ +/** + * class to display the database scheme (for usage in upgrade.php) in Cli + * + * extends the "Shell" class + */ + +class CliScheme extends Shell { + + public $handler_to_use = ""; + public $new = 0; + + + /** + * Execution method always used for tasks + */ + public function execute() { + + $module = preg_replace('/Handler$/', '', $this->handler_to_use); + $module = strtolower($module); + + $handler = new $this->handler_to_use($this->new); + $struct = $handler->getStruct(); + + foreach (array_keys($struct) as $field) { + if ($field == 'created') { + $struct[$field]['db_code'] = '{DATE}'; + } elseif ($field == 'modified') { + $struct[$field]['db_code'] = '{DATECURRENT}'; + } else { + switch ($struct[$field]['type']) { + case 'int': + $struct[$field]['db_code'] = '{BIGINT}'; + break; + case 'bool': + $struct[$field]['db_code'] = '{BOOLEAN}'; + break; + default: + $struct[$field]['db_code'] = 'VARCHAR(255) {LATIN1} NOT NULL'; + } + } + } + + $this->out("For creating a new table with upgrade.php:"); + $this->out(""); + + $this->out('db_query_parsed("'); + $this->out(' CREATE TABLE {IF_NOT_EXISTS} " . table_by_key("' . $module . '") . " ('); + # TODO: $module is not really correct - $handler->db_table would be + + foreach (array_keys($struct) as $field) { + if ($struct[$field]['not_in_db'] == 0 && $struct[$field]['dont_write_to_db'] == 0) { + $this->out(" $field " . $struct[$field]['db_code'] . ","); + } + } + + $this->out(" INDEX domain(domain,username), // <--- change as needed"); + $this->out(" PRIMARY KEY (" . $handler->getId_field() . ")"); + $this->out(' ) {MYISAM} '); + $this->out('");'); + + $this->out(''); + $this->hr(); + $this->out('For adding fields with upgrade.php:'); + $this->out(''); + + $prev_field = ''; + foreach (array_keys($struct) as $field) { + if ($struct[$field]['not_in_db'] == 0 && $struct[$field]['dont_write_to_db'] == 0) { + $this->out(" _db_add_field('$module', '$field',\t'" . $struct[$field]['db_code'] . "',\t'$prev_field');"); + $prev_field = $field; + } + } + + $this->out(''); + $this->hr(); + $this->out('Note that the above is only a template.'); + $this->out('You might need to adjust some parts.'); + return; + } + + /** + * Displays help contents + */ + public function help() { + $module = preg_replace('/Handler$/', '', $this->handler_to_use); + $module = strtolower($module); + + $this->out( +"Usage: + + postfixadmin-cli $module scheme + + Print the $module database scheme in a way that can be + pasted into upgrade.php. + +"); + + $this->_stop(); + } + +} +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ Property changes on: trunk/model/CliScheme.php ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +Id \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/model/PFAHandler.php =================================================================== --- trunk/model/PFAHandler.php 2014-11-01 18:05:39 UTC (rev 1709) +++ trunk/model/PFAHandler.php 2014-11-01 18:08:11 UTC (rev 1710) @@ -12,7 +12,7 @@ public $infomsg = array(); # array of tasks available in CLI - public $taskNames = array('Help', 'Add', 'Update', 'Delete', 'View'); + public $taskNames = array('Help', 'Add', 'Update', 'Delete', 'View', 'Scheme'); /** * variables that must be defined in all *Handler classes Modified: trunk/scripts/postfixadmin-cli.php =================================================================== --- trunk/scripts/postfixadmin-cli.php 2014-11-01 18:05:39 UTC (rev 1709) +++ trunk/scripts/postfixadmin-cli.php 2014-11-01 18:08:11 UTC (rev 1710) @@ -472,6 +472,7 @@ $this->stdout(" add Add an item"); $this->stdout(" update Update an item"); $this->stdout(" delete Delete an item"); + $this->stdout(" scheme Print database scheme (useful for developers only)"); $this->stdout(" help Print help output"); $this->stdout(""); $this->stdout(""); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |