You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CliScheme.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. # $Id: CliScheme.php 1710 2014-11-01 18:08:11Z christian_boltz $
  3. /**
  4. * class to display the database scheme (for usage in upgrade.php) in Cli
  5. *
  6. * extends the "Shell" class
  7. */
  8. class CliScheme extends Shell {
  9. public $handler_to_use = "";
  10. public $new = 0;
  11. /**
  12. * Execution method always used for tasks
  13. */
  14. public function execute() {
  15. $module = preg_replace('/Handler$/', '', $this->handler_to_use);
  16. $module = strtolower($module);
  17. $handler = new $this->handler_to_use($this->new);
  18. $struct = $handler->getStruct();
  19. foreach (array_keys($struct) as $field) {
  20. if ($field == 'created') {
  21. $struct[$field]['db_code'] = '{DATE}';
  22. } elseif ($field == 'modified') {
  23. $struct[$field]['db_code'] = '{DATECURRENT}';
  24. } else {
  25. switch ($struct[$field]['type']) {
  26. case 'int':
  27. $struct[$field]['db_code'] = '{BIGINT}';
  28. break;
  29. case 'bool':
  30. $struct[$field]['db_code'] = '{BOOLEAN}';
  31. break;
  32. default:
  33. $struct[$field]['db_code'] = 'VARCHAR(255) {LATIN1} NOT NULL';
  34. }
  35. }
  36. }
  37. $this->out("For creating a new table with upgrade.php:");
  38. $this->out("");
  39. $this->out('db_query_parsed("');
  40. $this->out(' CREATE TABLE {IF_NOT_EXISTS} " . table_by_key("' . $module . '") . " (');
  41. # TODO: $module is not really correct - $handler->db_table would be
  42. foreach (array_keys($struct) as $field) {
  43. if ($struct[$field]['not_in_db'] == 0 && $struct[$field]['dont_write_to_db'] == 0) {
  44. $this->out(" $field " . $struct[$field]['db_code'] . ",");
  45. }
  46. }
  47. $this->out(" INDEX domain(domain,username), // <--- change as needed");
  48. $this->out(" PRIMARY KEY (" . $handler->getId_field() . ")");
  49. $this->out(' ) {MYISAM} ');
  50. $this->out('");');
  51. $this->out('');
  52. $this->hr();
  53. $this->out('For adding fields with upgrade.php:');
  54. $this->out('');
  55. $prev_field = '';
  56. foreach (array_keys($struct) as $field) {
  57. if ($struct[$field]['not_in_db'] == 0 && $struct[$field]['dont_write_to_db'] == 0) {
  58. $this->out(" _db_add_field('$module', '$field',\t'" . $struct[$field]['db_code'] . "',\t'$prev_field');");
  59. $prev_field = $field;
  60. }
  61. }
  62. $this->out('');
  63. $this->hr();
  64. $this->out('Note that the above is only a template.');
  65. $this->out('You might need to adjust some parts.');
  66. return;
  67. }
  68. /**
  69. * Displays help contents
  70. */
  71. public function help() {
  72. $module = preg_replace('/Handler$/', '', $this->handler_to_use);
  73. $module = strtolower($module);
  74. $this->out(
  75. "Usage:
  76. postfixadmin-cli $module scheme
  77. Print the $module database scheme in a way that can be
  78. pasted into upgrade.php.
  79. ");
  80. $this->_stop();
  81. }
  82. }
  83. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */