You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by cr...@apache.org on 2025/08/22 09:03:29 UTC
(iotdb) branch master updated: Modified implementation of exporting table schema to sql file using show create table (#16226)
This is an automated email from the ASF dual-hosted git repository.
critas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new 6924ab0be4a Modified implementation of exporting table schema to sql file using show create table (#16226)
6924ab0be4a is described below
commit 6924ab0be4a3552a724745819270502405bc0366
Author: LimJiaWenBrenda <88...@users.noreply.github.com>
AuthorDate: Fri Aug 22 17:03:23 2025 +0800
Modified implementation of exporting table schema to sql file using show create table (#16226)
* Modified implementation of exporting schema to sql file using show create table
* Added double quotation marks to tableName dropped
* Used constant sql + escapeSqlIdentifier in sql sentence
---
.../org/apache/iotdb/tool/common/Constants.java | 2 +
.../iotdb/tool/schema/ExportSchemaTable.java | 44 +++++++++++++++++-----
2 files changed, 36 insertions(+), 10 deletions(-)
diff --git a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/common/Constants.java b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/common/Constants.java
index 049291ac248..fe354559a3a 100644
--- a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/common/Constants.java
+++ b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/common/Constants.java
@@ -304,6 +304,8 @@ public class Constants {
public static final String EXPORT_SCHEMA_COLUMNS_SELECT =
"select * from information_schema.columns where database like '%s' and table_name like '%s'";
public static final String EXPORT_SCHEMA_COLUMNS_DESC = "desc %s.%s details";
+ public static final String SHOW_CREATE_TABLE = "SHOW CREATE TABLE %s.%s";
+ public static final String DROP_TABLE_IF_EXIST = "DROP TABLE IF EXISTS %s";
// import constants
public static final String IMPORT_SCHEMA_CLI_PREFIX = "ImportSchema";
diff --git a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ExportSchemaTable.java b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ExportSchemaTable.java
index 8a322665fca..41adc3b4cc7 100644
--- a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ExportSchemaTable.java
+++ b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ExportSchemaTable.java
@@ -176,6 +176,16 @@ public class ExportSchemaTable extends AbstractExportSchema {
}
}
+ private String escapeSqlIdentifer(String identifier) {
+ if (StringUtils.isBlank(identifier)) {
+ return identifier;
+ }
+ if (identifier.contains("\"")) {
+ identifier = identifier.replace("\"", "\"\"");
+ }
+ return "\"" + identifier + "\"";
+ }
+
@Override
protected void exportSchemaToSqlFile() {
File file = new File(targetDirectory);
@@ -191,17 +201,13 @@ public class ExportSchemaTable extends AbstractExportSchema {
try (ITableSession session = sessionPool.getSession()) {
sessionDataSet =
session.executeQueryStatement(
- String.format(Constants.EXPORT_SCHEMA_COLUMNS_SELECT, database, tableName));
- exportSchemaBySelect(sessionDataSet, fileName, tableName, comment);
+ String.format(
+ Constants.SHOW_CREATE_TABLE,
+ escapeSqlIdentifer(database),
+ escapeSqlIdentifer(tableName)));
+ exportSchemaByShowCreate(sessionDataSet, fileName, tableName);
} catch (IoTDBConnectionException | StatementExecutionException | IOException e) {
- try (ITableSession session = sessionPool.getSession()) {
- sessionDataSet =
- session.executeQueryStatement(
- String.format(Constants.EXPORT_SCHEMA_COLUMNS_DESC, database, tableName));
- exportSchemaByDesc(sessionDataSet, fileName, tableName, comment);
- } catch (IoTDBConnectionException | StatementExecutionException | IOException e1) {
- ioTPrinter.println(Constants.COLUMN_SQL_MEET_ERROR_MSG + e.getMessage());
- }
+ ioTPrinter.println(Constants.COLUMN_SQL_MEET_ERROR_MSG + e.getMessage());
} finally {
if (ObjectUtils.isNotEmpty(sessionDataSet)) {
try {
@@ -214,6 +220,24 @@ public class ExportSchemaTable extends AbstractExportSchema {
}
}
+ private void exportSchemaByShowCreate(
+ SessionDataSet sessionDataSet, String fileName, String tableName)
+ throws IoTDBConnectionException, StatementExecutionException, IOException {
+ String dropSql =
+ String.format(Constants.DROP_TABLE_IF_EXIST, escapeSqlIdentifer(tableName)) + ";\n";
+ StringBuilder sb = new StringBuilder(dropSql);
+ try (FileWriter writer = new FileWriter(fileName, true)) {
+ while (sessionDataSet.hasNext()) {
+ RowRecord rowRecord = sessionDataSet.next();
+ String res = rowRecord.getField(1).getStringValue();
+ sb.append(res);
+ sb.append(";\n");
+ }
+ writer.append(sb.toString());
+ writer.flush();
+ }
+ }
+
private void exportSchemaByDesc(
SessionDataSet sessionDataSet, String fileName, String tableName, String tableComment)
throws IoTDBConnectionException, StatementExecutionException, IOException {