org.mvnsearch.boot.xtermjs.commands.sql.DatabaseCommands Maven / Gradle / Ivy
package org.mvnsearch.boot.xtermjs.commands.sql;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mvnsearch.boot.xtermjs.commands.CustomizedCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.standard.ShellComponent;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
* database commands
*
* @author linux_china
*/
@ShellComponent("db: execute database commands")
public class DatabaseCommands implements CustomizedCommand {
@Autowired(required = false)
private DataSource ds;
@Override
public String[] getNames() {
return new String[] { "db-select", "db-show", "db-update", "db-insert", "db-delete", "db-desc" };
}
@Override
public @Nullable Object execute(@NotNull String command, @Nullable String arguments) throws Exception {
switch (command) {
case "db-select":
return select(trimSemicolon("select " + arguments));
case "db-show":
return select(trimSemicolon("show " + arguments));
case "db-desc":
return select(trimSemicolon("desc " + arguments));
default:
return execute(trimSemicolon(command.replace("db-", "") + " " + arguments));
}
}
public String select(String sql) throws Exception {
QueryResult rowResult = new QueryResult();
rowResult.setSQL(sql);
try (Connection conn = ds.getConnection()) {
long start = System.currentTimeMillis();
Statement statement = conn.createStatement();
ResultSet resultset = statement.executeQuery(sql);
ResultSetMetaData metaData = resultset.getMetaData();
int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
rowResult.addColumn(metaData.getColumnLabel(i));
rowResult.addColumnType(metaData.getColumnTypeName(i));
}
while (resultset.next()) {
List