This is an automated email from the ASF dual-hosted git repository.
aradzinski pushed a commit to branch NLPCRAFT-168
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-168 by this push:
new 6c5a84a WIP.
6c5a84a is described below
commit 6c5a84ae0bcb154dcd24d3ed5a95e56240aceecd
Author: Aaron Radzinski <[email protected]>
AuthorDate: Sun Nov 1 21:10:41 2020 -0800
WIP.
---
.../nlpcraft/model/tools/cmdline/NCCli.scala | 280 +++++++++++++++++----
.../sqlgen/impl/NCSqlModelGeneratorImpl.scala | 8 +-
2 files changed, 242 insertions(+), 46 deletions(-)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
index 7b21809..1432ec9 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
@@ -77,13 +77,13 @@ object NCCli extends App {
// +==================================================================+
// | MAKE SURE TO UPDATE THIS VAR WHEN NUMBER OF SERVICES IS CHANGED. |
// +==================================================================+
- private final val NUM_SRV_SERVICES = 30/*services*/ + 1/*progress start*/
+ private final val NUM_SRV_SERVICES = 30 /*services*/ + 1 /*progress start*/
private final val SRV_BEACON_PATH = ".nlpcraft/server_beacon"
private final val HIST_PATH = ".nlpcraft/.cli_history"
private final lazy val VER = NCVersion.getCurrent
- private final lazy val JAVA = U.sysEnv("NLPCRAFT_CLI_JAVA").getOrElse(new
File(SystemUtils.getJavaHome,s"bin/java${if (SystemUtils.IS_OS_UNIX) "" else
".exe"}").getAbsolutePath)
+ private final lazy val JAVA = U.sysEnv("NLPCRAFT_CLI_JAVA").getOrElse(new
File(SystemUtils.getJavaHome, s"bin/java${if (SystemUtils.IS_OS_UNIX) "" else
".exe"}").getAbsolutePath)
private final lazy val INSTALL_HOME =
U.sysEnv("NLPCRAFT_CLI_INSTALL_HOME").getOrElse(SystemUtils.USER_DIR)
private final lazy val JAVA_CP =
U.sysEnv("NLPCRAFT_CLI_JAVA_CP").getOrElse(ManagementFactory.getRuntimeMXBean.getClassPath)
private final lazy val SCRIPT_NAME =
U.sysEnv("NLPCRAFT_CLI_SCRIPT").getOrElse(s"nlpcraft.${if
(SystemUtils.IS_OS_UNIX) "sh" else "cmd"}")
@@ -138,33 +138,42 @@ object NCCli extends App {
case class SplitError(index: Int)
extends Exception
+
case class NoLocalServer()
extends IllegalStateException(s"Local REST server not found.")
+
case class MissingParameter(cmd: Command, paramId: String)
extends IllegalArgumentException(
s"Missing mandatory parameter $C${"'" + cmd.params.find(_.id ==
paramId).get.names.head + "'"}$RST, " +
- s"type $C'help --cmd=${cmd.name}'$RST to get help."
+ s"type $C'help --cmd=${cmd.name}'$RST to get help."
)
+
case class MissingMandatoryJsonParameters(cmd: Command, path: String)
extends IllegalArgumentException(
s"Missing mandatory JSON parameter for $C${"'" + cmd.name + s"
--path=$path'"}$RST, type $C'help --cmd=${cmd.name}'$RST to get help."
)
+
case class InvalidParameter(cmd: Command, paramId: String)
extends IllegalArgumentException(
s"Invalid parameter $C${"'" + cmd.params.find(_.id ==
paramId).get.names.head + "'"}$RST, " +
- s"type $C'help --cmd=${cmd.name}'$RST to get help."
+ s"type $C'help --cmd=${cmd.name}'$RST to get help."
)
+
case class InvalidJsonParameter(cmd: Command, param: String)
extends IllegalArgumentException(
s"Invalid JSON parameter $C${"'" + param + "'"}$RST, " +
- s"type $C'help --cmd=${cmd.name}'$RST to get help."
+ s"type $C'help --cmd=${cmd.name}'$RST to get help."
)
+
case class HttpError(httpCode: Int)
extends IllegalStateException(s"REST error (HTTP ${c(httpCode)}).")
+
case class MalformedJson()
extends IllegalStateException("Malformed JSON.")
+
case class TooManyArguments(cmd: Command)
extends IllegalArgumentException(s"Too many arguments, type $C'help
--cmd=${cmd.name}'$RST to get help.")
+
case class NotEnoughArguments(cmd: Command)
extends IllegalArgumentException(s"Not enough arguments, type $C'help
--cmd=${cmd.name}'$RST to get help.")
@@ -176,10 +185,15 @@ object NCCli extends App {
)
sealed trait JsonType
+
case object STRING extends JsonType
+
case object BOOLEAN extends JsonType
+
case object NUMERIC extends JsonType
+
case object OBJECT extends JsonType
+
case object ARRAY extends JsonType
case class RestSpecParameter(
@@ -440,7 +454,7 @@ object NCCli extends App {
RestSpecParameter(name = "acsTok", kind = STRING),
RestSpecParameter(name = "usrId", kind = STRING, optional =
true),
RestSpecParameter(name = "extUsrId", kind = STRING, optional =
true),
- RestSpecParameter(name = "srvReqId", kind = STRING, optional
= true)
+ RestSpecParameter(name = "srvReqId", kind = STRING, optional =
true)
)
),
RestSpec(
@@ -518,11 +532,13 @@ object NCCli extends App {
def findParameterById(id: String): Parameter =
findParameterByIdOpt(id).get
}
+
// Single command's example.
case class Example(
usage: Seq[String],
desc: String
)
+
// Single command's parameter.
case class Parameter(
id: String,
@@ -755,6 +771,177 @@ object NCCli extends App {
)
),
Command(
+ name = "sqlgen",
+ group = "4. Miscellaneous Tools",
+ synopsis = s"Generates NLPCraft model stub from SQL databases.",
+ desc = Some(
+ s"You can choose database schema, set of tables and columns
for which you want to generate NLPCraft " +
+ s"model. After the model is generated you can further
configure and customize it for your specific needs."
+ ),
+ body = cmdSqlGen,
+ params = Seq(
+ Parameter(
+ id = "url",
+ names = Seq("--url", "-r"),
+ value = Some("url"),
+ desc =
+ s"Database JDBC URL."
+ ),
+ Parameter(
+ id = "driver",
+ names = Seq("--driver", "-d"),
+ value = Some("class"),
+ desc =
+ s"Mandatory JDBC driver class. Note that 'class' must
be a fully qualified class name. " +
+ s"It should also be available on the classpath."
+ ),
+ Parameter(
+ id = "schema",
+ names = Seq("--schema", "-s"),
+ value = Some("schema"),
+ desc =
+ s"Database schema to scan."
+ ),
+ Parameter(
+ id = "out",
+ names = Seq("--out", "-o"),
+ value = Some("filename"),
+ desc =
+ s"Name of the output JSON or YAML model file. " +
+ s"It should have one of the following extensions: .js,
.json, .yml, or .yaml. " +
+ s"File extension determines the output file format."
+ ),
+ Parameter(
+ id = "user",
+ names = Seq("--user", "-u"),
+ value = Some("username"),
+ optional = true,
+ desc = s"Database user name."
+ ),
+ Parameter(
+ id = "password",
+ names = Seq("--password", "-w"),
+ value = Some("password"),
+ optional = true,
+ desc = s"Database password."
+ ),
+ Parameter(
+ id = "modelId",
+ names = Seq("--model-id", "-x"),
+ value = Some("id"),
+ optional = true,
+ desc = s"Generated model ID. By default, the model ID is
${c("'sql.model.id'")}."
+ ),
+ Parameter(
+ id = "modelVer",
+ names = Seq("--model-ver", "-v"),
+ value = Some("version"),
+ optional = true,
+ desc = s"Generated model version. By default, the model
version is ${c("'1.0.0-timestamp'")}."
+ ),
+ Parameter(
+ id = "modelName",
+ names = Seq("--model-name", "-n"),
+ value = Some("name"),
+ optional = true,
+ desc = s"Generated model name. By default, the model name
is ${c("'SQL-based-model'")}."
+ ),
+ Parameter(
+ id = "exclude",
+ names = Seq("--exclude", "-e"),
+ value = Some("list"),
+ optional = true,
+ desc =
+ s"Semicolon-separate list of tables and/or columns to
exclude. By default, none of the " +
+ s"tables and columns in the schema are excluded. See
${c("--help")} parameter to get more details."
+ ),
+ Parameter(
+ id = "include",
+ names = Seq("--include", "-i"),
+ value = Some("list"),
+ optional = true,
+ desc =
+ s"Semicolon-separate list of tables and/or columns to
include. By default, all of the " +
+ s"tables and columns in the schema are included. See
${c("--help")} parameter to get more details."
+ ),
+ Parameter(
+ id = "prefix",
+ names = Seq("--prefix", "-f"),
+ value = Some("list"),
+ optional = true,
+ desc =
+ s"Comma-separate list of table or column name prefixes
to remove. These prefixes will be " +
+ s"removed when name is used for model elements
synonyms. By default, no prefixes will be removed."
+ ),
+ Parameter(
+ id = "suffix",
+ names = Seq("--suffix", "-q"),
+ value = Some("list"),
+ optional = true,
+ desc =
+ s"Comma-separate list of table or column name suffixes
to remove. These suffixes will be " +
+ s"removed when name is used for model elements
synonyms. By default, no suffixes will be removed."
+ ),
+ Parameter(
+ id = "synonyms",
+ names = Seq("--synonyms", "-y"),
+ value = Some("true|false"),
+ optional = true,
+ desc = s"Flag on whether or not to generated auto synonyms
for the model elements. Default is ${c("'true'")}."
+ ),
+ Parameter(
+ id = "override",
+ names = Seq("--override", "-z"),
+ value = Some("true|false"),
+ optional = true,
+ desc =
+ s"Flag to determine whether or not to override output
file if it already exist. " +
+ s"If override is disabled (default) and output file
exists - a unique file name " +
+ s"will be used instead. Default is ${c("'false'")}."
+ ),
+ Parameter(
+ id = "parent",
+ names = Seq("--parent", "-p"),
+ value = Some("true|false"),
+ optional = true,
+ desc =
+ s"Flag on whether or not to use element's parent
relationship for defining " +
+ s"SQL columns and their containing (i.e. parent)
tables. Default is ${c("'false'")}."
+ ),
+ Parameter(
+ id = "help",
+ names = Seq("--help", "-h"),
+ optional = true,
+ desc =
+ s"Gets extended help and usage information for the
${c("'sqlgen'")} command. " +
+ s"Includes information on how to run this tool
standalone."
+ )
+ ),
+ examples = Seq(
+ Example(
+ usage = Seq(
+ s"$PROMPT $SCRIPT_NAME sqlgen --help"
+ ),
+ desc =
+ s"Shows full help and usage information for
${c("sqlgen")} command."
+ ),
+ Example(
+ usage = Seq(
+ s"$PROMPT $SCRIPT_NAME sqlgen",
+ " -r=jdbc:postgresql://localhost:5432/mydb",
+ " -d=org.postgresql.Driver",
+ """ -f="tbl_, col_"""",
+ """ -q="_tmp, _old, _unused"""",
+ " -s=public",
+ """ -e="#_.+"""",
+ " -o=model.json"
+ ),
+ desc =
+ s"Generates model stub from given SQL database
connection."
+ )
+ )
+ ),
+ Command(
name = "sugsyn",
group = "2. REST Commands",
synopsis = s"Wrapper for ${c("'/model/sugsyn'")} REST call.",
@@ -1075,7 +1262,7 @@ object NCCli extends App {
synopsis = s"Displays help for ${y(s"'$SCRIPT_NAME'")}.",
desc = Some(
s"By default, without ${y("'--all'")} or ${y("'--cmd'")}
parameters, displays the abbreviated form of manual " +
- s"only listing the commands without parameters or examples."
+ s"only listing the commands without parameters or
examples."
),
body = cmdHelp,
params = Seq(
@@ -1136,16 +1323,16 @@ object NCCli extends App {
"Dup commands."
)
- private final val NO_ANSI_CMD = CMDS.find(_.name == "no-ansi").get
- private final val ANSI_CMD = CMDS.find(_.name == "ansi").get
- private final val QUIT_CMD = CMDS.find(_.name == "quit").get
- private final val HELP_CMD = CMDS.find(_.name == "help").get
- private final val REST_CMD = CMDS.find(_.name == "rest").get
- private final val CALL_CMD = CMDS.find(_.name == "call").get
- private final val ASK_CMD = CMDS.find(_.name == "ask").get
- private final val SUGSYN_CMD = CMDS.find(_.name == "sugsyn").get
- private final val STOP_SRV_CMD = CMDS.find(_.name == "stop-server").get
- private final val START_SRV_CMD = CMDS.find(_.name == "start-server").get
+ private final val NO_ANSI_CMD = CMDS.find(_.name == "no-ansi").get
+ private final val ANSI_CMD = CMDS.find(_.name == "ansi").get
+ private final val QUIT_CMD = CMDS.find(_.name == "quit").get
+ private final val HELP_CMD = CMDS.find(_.name == "help").get
+ private final val REST_CMD = CMDS.find(_.name == "rest").get
+ private final val CALL_CMD = CMDS.find(_.name == "call").get
+ private final val ASK_CMD = CMDS.find(_.name == "ask").get
+ private final val SUGSYN_CMD = CMDS.find(_.name == "sugsyn").get
+ private final val STOP_SRV_CMD = CMDS.find(_.name == "stop-server").get
+ private final val START_SRV_CMD = CMDS.find(_.name == "start-server").get
/**
*
@@ -1197,7 +1384,7 @@ object NCCli extends App {
}
/**
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not running from REPL.
*/
@@ -1321,7 +1508,7 @@ object NCCli extends App {
progressBar.ticked()
})
- .start()
+ .start()
val tailer = Tailer.create(
state.serverLog.get,
@@ -1398,7 +1585,7 @@ object NCCli extends App {
}
/**
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
*/
@@ -1408,7 +1595,7 @@ object NCCli extends App {
try
Integer.parseInt(arg.value.get)
catch {
- case _ :Exception ⇒ throw InvalidParameter(cmd, "lines")
+ case _: Exception ⇒ throw InvalidParameter(cmd, "lines")
}
case None ⇒ 20 // Default.
@@ -1446,7 +1633,7 @@ object NCCli extends App {
}
/**
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
*/
@@ -1458,7 +1645,7 @@ object NCCli extends App {
try
Integer.parseInt(arg.value.get)
catch {
- case _ :Exception ⇒ throw InvalidParameter(cmd, "number")
+ case _: Exception ⇒ throw InvalidParameter(cmd, "number")
}
case None ⇒ 1 // Default.
@@ -1506,7 +1693,7 @@ object NCCli extends App {
i += 1
if (i < num)
- // Pause between pings.
+ // Pause between pings.
Thread.sleep(500.ms)
}
}
@@ -1529,8 +1716,8 @@ object NCCli extends App {
) acquireAndGet {
_.readObject()
}
- )
- .asInstanceOf[NCCliServerBeacon]
+ )
+ .asInstanceOf[NCCliServerBeacon]
ProcessHandle.of(beacon.pid).asScala match {
case Some(ph) ⇒
@@ -1603,7 +1790,7 @@ object NCCli extends App {
}
/**
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
*/
@@ -1612,7 +1799,7 @@ object NCCli extends App {
}
/**
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
*/
@@ -1624,7 +1811,7 @@ object NCCli extends App {
}
/**
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
*/
@@ -1650,7 +1837,7 @@ object NCCli extends App {
}
/**
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
*/
@@ -1659,7 +1846,7 @@ object NCCli extends App {
}
/**
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
*/
@@ -1668,7 +1855,7 @@ object NCCli extends App {
}
/**
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
*/
@@ -1713,7 +1900,7 @@ object NCCli extends App {
if (param.value.isDefined)
T___ +
param.names.zip(Stream.continually(param.value.get)).map(t ⇒
s"${t._1}=${t._2}").mkString(", ")
else
- s"$T___${param.names.mkString(", ")}"
+ s"$T___${param.names.mkString(",")}"
lines += c(line)
@@ -1741,7 +1928,7 @@ object NCCli extends App {
}
def helpHelp(): Unit =
- logln(s"\nType ${c("help --cmd=xxx")} to get help for ${c("xxx")}
command.")
+ logln(s"\nType ${c("help --cmd=xxx")} to get help for ${c("xxx")}
command.")
if (args.isEmpty) { // Default - show abbreviated help.
if (!repl)
@@ -1847,7 +2034,7 @@ object NCCli extends App {
tbl += (" Expiration timeout", s"$G${beacon.acsToksExpireMins}
mins$RST")
tbl += ("External config:", "")
tbl += (" URL", s"${g(beacon.extConfigUrl)}")
- tbl += (" Check MD5",s"${g(beacon.extConfigCheckMd5)}")
+ tbl += (" Check MD5", s"${g(beacon.extConfigCheckMd5)}")
tbl += ("Log file", logPath)
tbl += ("Started on", s"${g(DateFormat.getDateTimeInstance.format(new
Date(beacon.startMs)))}")
@@ -1887,7 +2074,7 @@ object NCCli extends App {
/**
*
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
*/
@@ -1900,7 +2087,7 @@ object NCCli extends App {
/**
*
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
*/
@@ -1909,7 +2096,7 @@ object NCCli extends App {
/**
*
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
*/
@@ -1961,7 +2148,7 @@ object NCCli extends App {
/**
*
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
*/
@@ -1990,7 +2177,7 @@ object NCCli extends App {
/**
*
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
*/
@@ -2011,12 +2198,12 @@ object NCCli extends App {
|""".stripMargin
)
- case Some(_) ⇒ error(s"Already signed in. See ${c("'signout'")}
command.")
+ case Some(_) ⇒ error(s"Already signed in. See ${c("'signout'")}
command.")
}
/**
*
- * @param cmd Command descriptor.
+ * @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
*/
@@ -2055,6 +2242,15 @@ object NCCli extends App {
/**
*
+ * @param cmd Command descriptor.
+ * @param args Arguments, if any, for this command.
+ * @param repl Whether or not executing from REPL.
+ */
+ private def cmdSqlGen(cmd: Command, args: Seq[Argument], repl: Boolean):
Unit = {
+ }
+
+ /**
+ *
* @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
* @param repl Whether or not executing from REPL.
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlModelGeneratorImpl.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlModelGeneratorImpl.scala
index a7253ab..b42d357 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlModelGeneratorImpl.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlModelGeneratorImpl.scala
@@ -601,13 +601,13 @@ object NCSqlModelGeneratorImpl {
System.err.println(
s"""
|NAME:
- | NCSqlModelGenerator -- NLPCraft model generator for
SQL databases.
+ | NCSqlModelGenerator -- NLPCraft model generator from
SQL databases.
|
|SYNOPSIS:
| java -cp
apache-nlpcraft-incubating-${ver.version}-all-deps.jar
org.apache.nlpcraft.model.tools.sqlgen.NCSqlModelGenerator [PARAMETERS]
|
|DESCRIPTION:
- | This utility generates NLPCraft model stub for a given
SQL database schema. You
+ | This utility generates NLPCraft model stub from a
given SQL database schema. You
| can choose database schema, set of tables and columns
for which you
| want to generate NLPCraft model. After the model is
generated you can
| further configure and customize it for your specific
needs.
@@ -647,7 +647,7 @@ object NCSqlModelGeneratorImpl {
| Optional generated model ID. By default, the model ID
will be 'sql.model.id'.
|
| [--model-ver|-v] version
- | Optional generated model version. By default, the
model ID will be '1.0.0-timestamp'.
+ | Optional generated model version. By default, the
model version will be '1.0.0-timestamp'.
|
| [--model-name|-n] name
| Optional generated model name. By default, the model
name will be 'SQL-based model'.
@@ -677,7 +677,7 @@ object NCSqlModelGeneratorImpl {
| Default is true.
|
| [--override|-z] [true|false]
- | Flag to determine whether or not to override output
file if it already exist.
+ | Optional flag to determine whether or not to override
output file if it already exist.
| If override is disabled (default) and output file
exists - a unique file name will
| be used instead.
| Default is false.