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:
2021-03-16 15:25:04 +01:00
parent 67e3fb521d
commit aeab871d9d

View File

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