forked from p81075629/datagear
添加可用于连接内置Derby数据库执行SQL的命令行客户端
This commit is contained in:
parent
f130e3d46e
commit
6be78f24f4
|
@ -370,10 +370,7 @@ public class DbVersionManager extends AbstractVersionContentReader
|
|||
|
||||
if (!sql.isEmpty())
|
||||
{
|
||||
if (sql.endsWith(";"))
|
||||
sql = sql.substring(0, sql.length() - 1);
|
||||
|
||||
contents.add(sql);
|
||||
contents.add(deleteTailSemicolon(sql));
|
||||
|
||||
cache.delete(0, cache.length());
|
||||
}
|
||||
|
@ -395,7 +392,9 @@ public class DbVersionManager extends AbstractVersionContentReader
|
|||
String sql = cache.toString().trim();
|
||||
|
||||
if (!sql.isEmpty())
|
||||
contents.add(sql);
|
||||
{
|
||||
contents.add(deleteTailSemicolon(sql));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -421,6 +420,20 @@ public class DbVersionManager extends AbstractVersionContentReader
|
|||
return Version.valueOf(version);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除SQL语句末尾的分号。
|
||||
*
|
||||
* @param sql
|
||||
* @return
|
||||
*/
|
||||
protected String deleteTailSemicolon(String sql)
|
||||
{
|
||||
if (sql.endsWith(";"))
|
||||
sql = sql.substring(0, sql.length() - 1);
|
||||
|
||||
return sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取SQL脚本输入流。
|
||||
*
|
||||
|
|
|
@ -74,7 +74,6 @@
|
|||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
|
|
|
@ -0,0 +1,200 @@
|
|||
/*
|
||||
* Copyright (c) 2018 datagear.tech. All Rights Reserved.
|
||||
*/
|
||||
|
||||
package org.datagear.web.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.Reader;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.Statement;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.datagear.util.IOUtil;
|
||||
import org.datagear.util.JdbcUtil;
|
||||
import org.datagear.util.SqlScriptParser;
|
||||
import org.datagear.util.SqlScriptParser.SqlStatement;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
|
||||
/**
|
||||
* Derby数据库SQL客户端。
|
||||
*
|
||||
* @author datagear@163.com
|
||||
*
|
||||
*/
|
||||
public class DerbySqlClient
|
||||
{
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
ClassPathXmlApplicationContext applicationContext = null;
|
||||
Connection cn = null;
|
||||
|
||||
try
|
||||
{
|
||||
applicationContext = new ClassPathXmlApplicationContext("datagear-applicationContext-DerbySqlClient.xml");
|
||||
|
||||
DriverManagerDataSource dataSource = applicationContext.getBean(DriverManagerDataSource.class);
|
||||
|
||||
println("*****************************************");
|
||||
println("Derby SQL client");
|
||||
println("URL :" + dataSource.getUrl());
|
||||
println("*****************************************");
|
||||
println();
|
||||
println("Print SQL for executing single sql");
|
||||
println("Print @<sql-file-path> for executing sql scripts");
|
||||
println("Print [exist] for exist");
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
cn = dataSource.getConnection();
|
||||
|
||||
while (scanner.hasNextLine())
|
||||
{
|
||||
String input = scanner.nextLine().trim();
|
||||
|
||||
if (input.isEmpty())
|
||||
;
|
||||
else if ("exit".equalsIgnoreCase(input))
|
||||
{
|
||||
println("Bye!");
|
||||
System.exit(0);
|
||||
}
|
||||
else if (input.startsWith("@"))
|
||||
{
|
||||
String filePath = input.substring(1);
|
||||
|
||||
println("start execute sql scripts in file :" + filePath);
|
||||
|
||||
Reader reader = IOUtil.getReader(new File(filePath), "UTF-8");
|
||||
|
||||
try
|
||||
{
|
||||
SqlScriptParser sqlScriptParser = new SqlScriptParser(reader);
|
||||
SqlStatement sql = null;
|
||||
|
||||
while ((sql = sqlScriptParser.parseNext()) != null)
|
||||
{
|
||||
println(sql.getSql());
|
||||
executeSql(cn, sql.getSql());
|
||||
}
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
t.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close(reader);
|
||||
}
|
||||
|
||||
println("finish execute sql scripts in file :" + filePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
executeSql(cn, input);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
JdbcUtil.closeConnection(cn);
|
||||
applicationContext.close();
|
||||
}
|
||||
}
|
||||
|
||||
protected static void executeSql(Connection cn, String sql)
|
||||
{
|
||||
Statement st = null;
|
||||
|
||||
try
|
||||
{
|
||||
st = cn.createStatement();
|
||||
|
||||
boolean query = st.execute(sql);
|
||||
|
||||
println("-----------------------------------------");
|
||||
if (!query)
|
||||
{
|
||||
println(st.getUpdateCount() + " records affected.");
|
||||
}
|
||||
else
|
||||
{
|
||||
ResultSet rs = st.getResultSet();
|
||||
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
int columnCount = metaData.getColumnCount();
|
||||
|
||||
for (int i = 1; i <= columnCount; i++)
|
||||
{
|
||||
String columnLabel = metaData.getColumnLabel(i);
|
||||
|
||||
if (i > 1)
|
||||
print(", ");
|
||||
|
||||
print(columnLabel);
|
||||
}
|
||||
|
||||
println();
|
||||
|
||||
while (rs.next())
|
||||
{
|
||||
for (int i = 1; i <= columnCount; i++)
|
||||
{
|
||||
Object cv = rs.getObject(i);
|
||||
|
||||
if (i > 1)
|
||||
print(", ");
|
||||
|
||||
print(cv);
|
||||
}
|
||||
|
||||
println();
|
||||
}
|
||||
}
|
||||
println("-----------------------------------------");
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
t.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
JdbcUtil.closeStatement(st);
|
||||
}
|
||||
}
|
||||
|
||||
protected static void print(Object o)
|
||||
{
|
||||
String str = "NULL";
|
||||
|
||||
if (o == null)
|
||||
;
|
||||
else if (o instanceof String)
|
||||
str = (String) o;
|
||||
else
|
||||
str = o.toString();
|
||||
|
||||
System.out.print(str);
|
||||
}
|
||||
|
||||
protected static void println(Object o)
|
||||
{
|
||||
String str = "NULL";
|
||||
|
||||
if (o == null)
|
||||
;
|
||||
else if (o instanceof String)
|
||||
str = (String) o;
|
||||
else
|
||||
str = o.toString();
|
||||
|
||||
System.out.println(str);
|
||||
}
|
||||
|
||||
protected static void println()
|
||||
{
|
||||
System.out.println();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
|
||||
http://www.springframework.org/schema/tx
|
||||
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
|
||||
http://www.springframework.org/schema/aop
|
||||
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
|
||||
|
||||
<!-- 仅用于初始化DerbySqlClient独立应用,与系统功能无关 -->
|
||||
|
||||
<bean id="propertyConfigurer"
|
||||
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
|
||||
<property name="locations">
|
||||
<list>
|
||||
<value>classpath:datagear.properties</value>
|
||||
</list>
|
||||
</property>
|
||||
<property name="fileEncoding" value="UTF-8" />
|
||||
</bean>
|
||||
|
||||
<bean id="derbyRootDirectoryFactory" class="org.datagear.web.util.DirectoryFactory" init-method="init">
|
||||
<property name="directoryName" value="${directory.derby}" />
|
||||
<property name="createIfInexistence" value="false" />
|
||||
</bean>
|
||||
|
||||
<bean id="dataSource"
|
||||
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
|
||||
<property name="driverClassName">
|
||||
<value>org.apache.derby.jdbc.EmbeddedDriver</value>
|
||||
</property>
|
||||
<property name="url">
|
||||
<value>jdbc:derby:#{derbyRootDirectoryFactory.getDirectoryAbsolutePath()};create=true</value>
|
||||
</property>
|
||||
<!--
|
||||
<property name="username">
|
||||
<value></value>
|
||||
</property>
|
||||
<property name="password">
|
||||
<value></value>
|
||||
</property>
|
||||
-->
|
||||
</bean>
|
||||
|
||||
</beans>
|
|
@ -0,0 +1 @@
|
|||
java -cp classes;lib/* org.datagear.web.util.DerbySqlClient
|
|
@ -0,0 +1 @@
|
|||
java -cp classes:lib/* org.datagear.web.util.DerbySqlClient
|
Loading…
Reference in New Issue