sqlMigrate : allow to return at the specified step by getopt. Use readline to provide the already defined valued in configuration file (if exists)
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
* informations will be stored. The file will be created if it doesn't exists.
|
||||
*/
|
||||
|
||||
require_once ("domframework/console.php");
|
||||
require_once ("domframework/dblayeroo.php");
|
||||
require_once ("domframework/getopts.php");
|
||||
require_once ("domframework/config.php");
|
||||
@@ -56,13 +57,28 @@ class main
|
||||
*/
|
||||
public function __construct ()
|
||||
{
|
||||
global $argv;
|
||||
if (! key_exists (1, $argv))
|
||||
// Manage the Getopts
|
||||
$getopts = new getopts ();
|
||||
$getopts->add ("Help", "?h", array ("help"), "Help of the software");
|
||||
$getopts->add ("Step", "", array ("step:"), "Restart at provided step", "StepIdentifier");
|
||||
if ($getopts->get ("Help"))
|
||||
die ($getopts->help ());
|
||||
if ($getopts->get ("Step"))
|
||||
{
|
||||
if (intval ($getopts->get ("Step")) < 0 ||
|
||||
intval ($getopts->get ("Step")) > 6)
|
||||
die ("Invalid Step provided (Must be between 0 and 6)\n");
|
||||
}
|
||||
if (empty ($getopts->restOfLine ()))
|
||||
throw new Exception ("No configuration file provided");
|
||||
if (! file_exists ($argv[1]))
|
||||
touch ($argv[1]);
|
||||
if (count ($getopts->restOfLine ()) > 1)
|
||||
throw new Exception ("Too much configuration file provided");
|
||||
$configurationFile = current ($getopts->restOfLine ());
|
||||
if (! file_exists ($configurationFile))
|
||||
touch ($configurationFile);
|
||||
$config = new configuration ();
|
||||
$config->confFile = $argv[1];
|
||||
$config->confFile = $configurationFile;
|
||||
|
||||
$db = $config->get ("db");
|
||||
$tables = $config->get ("tables");
|
||||
$tablesRel = $config->get ("tablesRel");
|
||||
@@ -84,7 +100,9 @@ class main
|
||||
else
|
||||
$step = 6;
|
||||
|
||||
// TODO : Overload here the steps with the getopts
|
||||
if ($getopts->get ("Step"))
|
||||
$step = intval ($getopts->get ("Step"));
|
||||
|
||||
if ($step === 0)
|
||||
{
|
||||
// Step 0: ask the connection parameters
|
||||
@@ -177,7 +195,10 @@ class main
|
||||
echo "# For each destination table, if a source table is provided,\n".
|
||||
"# for each field, provide the source SQL request (can be field,\n".
|
||||
"# CONCAT(), text between simple quotes, null...)\n";
|
||||
$value = array ();
|
||||
if ($config->get("fields"))
|
||||
$value = $config->get("fields");
|
||||
else
|
||||
$value = array ();
|
||||
foreach ($tablesRel as $dstTable=>$srcTable)
|
||||
{
|
||||
echo "- Destination Table '$dstTable': ";
|
||||
@@ -199,11 +220,14 @@ class main
|
||||
$value[$dstTable][$field] = "";
|
||||
while (1)
|
||||
{
|
||||
$value[$dstTable][$field] = $this->ask (
|
||||
"Destination Table '$dstTable' field '$field'");
|
||||
$oldval = (isset ($config->get("fields")[$dstTable][$field])) ?
|
||||
$config->get("fields")[$dstTable][$field] : "";
|
||||
$answer = $this->ask (
|
||||
"Destination Table '$dstTable' field '$field'", $oldval);
|
||||
$value[$dstTable][$field] = $answer;
|
||||
if (trim ($value[$dstTable][$field]) !== "")
|
||||
continue 2;
|
||||
// TODO : Check if the field is provided (think about function)
|
||||
break;
|
||||
// TODO : Check if the field is valid (think about function)
|
||||
$this->error ("The field source must be defined !");
|
||||
}
|
||||
}
|
||||
@@ -322,12 +346,11 @@ class main
|
||||
* @param string $question the question message
|
||||
* @return string The answer
|
||||
*/
|
||||
public function ask ($question)
|
||||
public function ask ($question, $proposal = "")
|
||||
{
|
||||
echo "$question: ";
|
||||
$fp = fopen ("php://stdin", "r");
|
||||
$rc = trim (fgets ($fp));
|
||||
return $rc;
|
||||
$console = new console ();
|
||||
$console->echo ("$question: ");
|
||||
return trim ($console->readline ($proposal));
|
||||
}
|
||||
|
||||
/** Display an error to the user
|
||||
|
||||
Reference in New Issue
Block a user