Skip to content

Commit

Permalink
2.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
chengyuxing committed May 24, 2024
1 parent 22a6e3a commit 13b2256
Show file tree
Hide file tree
Showing 20 changed files with 162 additions and 173 deletions.
21 changes: 14 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.chengyuxing</groupId>
<artifactId>sqlc</artifactId>
<version>2.1.0</version>
<version>2.1.2</version>
<packaging>jar</packaging>
<name>sqlc</name>
<description>simple sql terminal for ddl, dml, batch import/export data.</description>
Expand All @@ -21,13 +21,13 @@
<dependency>
<groupId>com.github.chengyuxing</groupId>
<artifactId>rabbit-sql</artifactId>
<version>7.0.19</version>
<version>7.9.5</version>
</dependency>

<dependency>
<groupId>com.github.chengyuxing</groupId>
<artifactId>rabbit-excel</artifactId>
<version>4.1.17</version>
<version>4.3.5</version>
</dependency>

<dependency>
Expand All @@ -39,19 +39,19 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.7.1</version>
<version>2.17.0</version>
</dependency>

<dependency>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
<version>3.21.0</version>
<version>3.25.1</version>
</dependency>

<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-jna</artifactId>
<version>3.21.0</version>
<version>3.26.1</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -189,7 +189,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<version>3.6.0</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
Expand All @@ -210,4 +210,11 @@
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>maven_central</id>
<name>Maven Central</name>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>
</project>
10 changes: 5 additions & 5 deletions src/main/java/com/github/chengyuxing/sql/terminal/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class App {

public static void main(String[] args) {
try {
if (args.length == 0 || Arrays.toString(args).trim().equals("")) {
if (args.length == 0 || Arrays.toString(args).trim().isEmpty()) {
System.out.println("-u is required, -h to get some help.");
System.exit(0);
}
Expand Down Expand Up @@ -273,7 +273,7 @@ public static void startInteractiveMode(DataSourceLoader dataSourceLoader) {
while (true) {
try {
String line = lineReader.readLine(prompt.getValue()).trim();
if (!line.equals("")) {
if (!line.isEmpty()) {
if (line.startsWith(":")) {
sqlBuilder.clear();
label:
Expand Down Expand Up @@ -309,7 +309,7 @@ public static void startInteractiveMode(DataSourceLoader dataSourceLoader) {
commandRegistry.invoke(session, "nano", "-$", path);
if (Files.exists(path)) {
String sqlContent = String.join("\n", Files.readAllLines(path, StandardCharsets.UTF_8)).trim();
if (!sqlContent.equals("")) {
if (!sqlContent.isEmpty()) {
Exec executor = new Exec(baki, lineReader);
executor.exec(sqlContent);
prompt.newLine();
Expand Down Expand Up @@ -370,7 +370,7 @@ public static void startInteractiveMode(DataSourceLoader dataSourceLoader) {
Data.xqlFileManager.add(alias, "file:" + filePath);
Data.xqlFileManager.setDelimiter(StatusManager.sqlDelimiter.get());
Data.xqlFileManager.init();
Data.xqlFileManager.foreach((k, v) -> System.out.println("+[" + TerminalColor.colorful(k, Color.DARK_CYAN) + "]:" + TerminalColor.highlightSql(v)));
Data.xqlFileManager.foreach((k, v) -> v.getEntry().forEach((name, sql) -> System.out.println("+[" + TerminalColor.colorful(name, Color.DARK_CYAN) + "]:" + TerminalColor.highlightSql(sql.getContent()))));
if (baki.getXqlFileManager() == null) {
baki.setXqlFileManager(Data.xqlFileManager);
}
Expand Down Expand Up @@ -454,7 +454,7 @@ public static void startInteractiveMode(DataSourceLoader dataSourceLoader) {
String sql = com.github.chengyuxing.sql.utils.SqlUtil.trimEnd(String.join(" ", sqlBuilder));
// execute sql
// ---------
if (!sql.equals("")) {
if (!sql.isEmpty()) {
Exec executor = new Exec(baki, lineReader);
executor.exec(sql);
sqlBuilder.clear();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.github.chengyuxing.sql.terminal;

public final class Version {
public static final String RELEASE = "2.1.0";
public static final String RELEASE = "2.1.2";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.chengyuxing.common.console.Color;
import com.github.chengyuxing.common.console.Printer;
import com.github.chengyuxing.sql.utils.SqlHighlighter;
import com.github.chengyuxing.sql.utils.SqlUtil;

import static com.github.chengyuxing.sql.terminal.vars.Constants.IS_XTERM;
Expand Down Expand Up @@ -34,6 +35,6 @@ public static void printf(String str, Color color, Object... args) {
}

public static String highlightSql(String sql) {
return SqlUtil.buildPrintSql(sql, IS_XTERM);
return SqlHighlighter.highlightIfAnsiCapable(sql);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void exec(String cmd) throws Exception {
commandRegistry.invoke(session, "nano", "-$", procedurePath);
String newDef = String.join("\n", Files.readAllLines(procedurePath, StandardCharsets.UTF_8));
if (!def.trim().equals(newDef.trim())) {
baki.execute(newDef);
baki.of(newDef).execute();
PrintHelper.printlnNotice(cmd + " change submitted!");
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@ public static void agentmain(final String a, final Instrumentation inst) {
Agent.inst = inst;
}

public static boolean addClassPath(File jar) {
public static void addClassPath(File jar) {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
try {
// jdk9+
if (!(classLoader instanceof URLClassLoader)) {
inst.appendToSystemClassLoaderSearch(new JarFile(jar));
return true;
return;
}
//jdk8
Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
m.setAccessible(true);
m.invoke(classLoader, jar.toURI().toURL());
return true;
} catch (IOException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.chengyuxing.common.DataRow;
import com.github.chengyuxing.common.io.Lines;
import com.github.chengyuxing.common.utils.StringUtil;
import com.github.chengyuxing.excel.Excels;
import com.github.chengyuxing.excel.io.ExcelReader;
Expand Down Expand Up @@ -86,9 +85,9 @@ public static void readInsertSqlScriptBatchExecute(UserBaki baki, Path path) {
try (Stream<String> lineStream = Files.lines(path, StandardCharsets.UTF_8)) {
StringBuilder sb = new StringBuilder();
lineStream.map(String::trim)
.filter(sql -> !sql.equals("") && !StringUtil.startsWithsIgnoreCase(sql, "--", "#", "/*"))
.filter(sql -> !sql.isEmpty() && !StringUtil.startsWithsIgnoreCase(sql, "--", "#", "/*"))
.forEach(sql -> {
if (delimiter.equals("")) {
if (delimiter.isEmpty()) {
chunk.add(sql);
} else {
sb.append(sql).append("\n");
Expand All @@ -97,10 +96,10 @@ public static void readInsertSqlScriptBatchExecute(UserBaki baki, Path path) {
sb.setLength(0);
}
}
if (example.get().equals("")) {
if (example.get().isEmpty()) {
if (!chunk.isEmpty()) {
example.set(chunk.get(0));
boolean isPrepared = !SqlUtil.sqlTranslator.getPreparedSql(chunk.get(0), Collections.emptyMap()).getItem2().isEmpty();
boolean isPrepared = !SqlUtil.sqlTranslator.generatePreparedSql(chunk.get(0), Collections.emptyMap()).getItem2().isEmpty();
prepared.set(isPrepared);
}
}
Expand All @@ -112,7 +111,7 @@ public static void readInsertSqlScriptBatchExecute(UserBaki baki, Path path) {
throw new RuntimeException(e);
}
} else {
baki.batchExecute(chunk);
baki.executeBatch(chunk, 1000);
}
chunk.clear();
pp.increment();
Expand All @@ -126,7 +125,7 @@ public static void readInsertSqlScriptBatchExecute(UserBaki baki, Path path) {
if (prepared.get()) {
preparedInsert4BlobBatchExecute(baki, chunk, path);
} else {
baki.batchExecute(chunk);
baki.executeBatch(chunk, 1000);
}
pp.increment();
}
Expand All @@ -137,22 +136,22 @@ public static void readInsertSqlScriptBatchExecute(UserBaki baki, Path path) {
}
}

public static boolean preparedInsert4BlobBatchExecute(UserBaki baki, List<String> sqls, Path path) throws IOException {
public static void preparedInsert4BlobBatchExecute(UserBaki baki, List<String> sqls, Path path) throws IOException {
Path blobsDir = path.getParent().resolve("blobs");
if (Files.exists(blobsDir)) {
if (!StatusManager.txActive.get()) {
Tx.using(() -> {
for (String sql : sqls) {
List<String> names = SqlUtil.sqlTranslator.getPreparedSql(sql, Collections.emptyMap()).getItem2();
List<String> names = SqlUtil.sqlTranslator.generatePreparedSql(sql, Collections.emptyMap()).getItem2();
Map<String, Object> arg = new HashMap<>();
for (String name : names) {
arg.put(name, blobsDir.resolve(name).toFile());
}
baki.executeNonQuery(sql, Collections.singletonList(arg));
baki.executeBatchUpdate(sql, Collections.singletonList(arg), 1000);
}
});
}
return true;
return;
}
throw new FileNotFoundException("cannot find 'blobs' folder on " + path.getParent() + ".");
}
Expand All @@ -168,18 +167,19 @@ public static void readJson4batch(UserBaki baki, Path path, String tableName) {
try (MappingIterator<Map<String, Object>> iterator = JSON.reader().forType(Map.class).readValues(path.toFile())) {
while (iterator.hasNext()) {
Map<String, Object> obj = iterator.next();
chunk.add(SqlUtil.sqlTranslator.generateInsert(tableName, obj, Collections.emptyList()));
if (example.get().equals("")) {
String insert = SqlUtil.sqlTranslator.generateNamedParamInsert(tableName, obj, Collections.emptyList(), true);
chunk.add(SqlUtil.sqlTranslator.generateSql(insert, obj));
if (example.get().isEmpty()) {
example.set(chunk.get(0));
}
if (chunk.size() == 1000) {
baki.batchExecute(chunk);
baki.executeBatch(chunk, 1000);
chunk.clear();
pp.increment();
}
}
if (!chunk.isEmpty()) {
baki.batchExecute(chunk);
baki.executeBatch(chunk, 1000);
pp.increment();
}
pp.stop();
Expand All @@ -198,7 +198,8 @@ public static void readDSV4batch(UserBaki baki, Path path, String tableName, Str
pp.whenStopped(whenStoppedFunc(chunk, example, "lines", "insert")).start();
int start = headerIdx;
String[] nameGeneric = new String[0];
try (Stream<List<String>> lines = Lines.readLines(path, delimiter, StandardCharsets.UTF_8)) {
try (Stream<String> s = Files.lines(path, StandardCharsets.UTF_8)) {
Stream<List<String>> lines = s.map(l -> Arrays.asList(l.split(delimiter)));
List<String> tableFields = new ArrayList<>();
if (start < 0) {
tableFields.addAll(baki.getTableFields(tableName));
Expand All @@ -214,21 +215,22 @@ public static void readDSV4batch(UserBaki baki, Path path, String tableName, Str
.skip(next)
.map(cols -> {
DataRow row = DataRow.of(tableFields.toArray(nameGeneric), cols.toArray());
return SqlUtil.sqlTranslator.generateInsert(tableName, row, Collections.emptyList());
String insert = SqlUtil.sqlTranslator.generateNamedParamInsert(tableName, row, Collections.emptyList(), true);
return SqlUtil.sqlTranslator.generateSql(insert, row);
})
.forEach(insert -> {
chunk.add(insert);
if (example.get().equals("")) {
if (example.get().isEmpty()) {
example.set(chunk.get(0));
}
if (chunk.size() == 1000) {
baki.batchExecute(chunk);
baki.executeBatch(chunk, 1000);
chunk.clear();
pp.increment();
}
});
if (!chunk.isEmpty()) {
baki.batchExecute(chunk);
baki.executeBatch(chunk, 1000);
pp.increment();
}
pp.stop();
Expand Down Expand Up @@ -257,23 +259,26 @@ public static void readExcel4batch(UserBaki baki, Path path, String tableName, i
}
try (Stream<DataRow> s = reader.stream()) {
s.skip(skip)
.peek(d -> d.removeIf((k, v) -> k == null || k.trim().equals("")))
.peek(d -> d.removeIf((k, v) -> v == null || v.toString().equals("")))
.peek(d -> d.removeIf((k, v) -> k == null || k.trim().isEmpty()))
.peek(d -> d.removeIf((k, v) -> v == null || v.toString().isEmpty()))
.filter(d -> !d.isEmpty())
.map(d -> SqlUtil.sqlTranslator.generateInsert(tableName, d, Collections.emptyList()))
.map(d -> {
String insert = SqlUtil.sqlTranslator.generateNamedParamInsert(tableName, d, Collections.emptyList(), true);
return SqlUtil.sqlTranslator.generateSql(insert, d);
})
.forEach(insert -> {
chunk.add(insert);
if (example.get().equals("")) {
if (example.get().isEmpty()) {
example.set(chunk.get(0));
}
if (chunk.size() == 999) {
baki.batchExecute(chunk);
baki.executeBatch(chunk, 1000);
chunk.clear();
pp.increment();
}
});
if (!chunk.isEmpty()) {
baki.batchExecute(chunk);
baki.executeBatch(chunk, 1000);
pp.increment();
}
pp.stop();
Expand Down
Loading

0 comments on commit 13b2256

Please sign in to comment.