提交 da6e461b authored 作者: Thomas Mueller's avatar Thomas Mueller

--no commit message

--no commit message
上级 7ac143fd
...@@ -4,54 +4,42 @@ ...@@ -4,54 +4,42 @@
*/ */
package org.h2.tools; package org.h2.tools;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import org.h2.message.Message; import org.h2.message.Message;
import org.h2.store.FileLister; import org.h2.store.FileLister;
import org.h2.util.FileUtils; import org.h2.util.FileUtils;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils;
import org.h2.util.StringUtils;
/** /*
* Creates a backup of a database by extracting the schema and data into a SQL script file. * Backs up a H2 database by creating a .zip file from the database files.
* *
* @author Thomas * @author Thomas
*
*/ */
public class Backup { public class Backup {
private void showUsage() { private void showUsage() {
System.out.println("java "+getClass().getName() System.out.println("java "+getClass().getName()
+ " -url <url> -user <user> [-password <pwd>] [-script <file>] [-options <option> ...]"); + " [-file <filename>] [-dir <dir>] [-db <database>] [-quiet]");
} }
/** /**
* The command line interface for this tool. * The command line interface for this tool.
* The options must be split into strings like this: "-user", "sa",... * The options must be split into strings like this: "-db", "test",...
* The following options are supported: * The following options are supported:
* <ul> * <ul>
* <li>-help or -? (print the list of options) * <li>-help or -? (print the list of options)
* </li><li>-url jdbc:h2:... (database URL) * </li><li>-file filename (the default is backup.zip)
* </li><li>-user username * </li><li>-dir directory (the default is the current directory)
* </li><li>-password password * </li><li>-db databaseName (all databases if no name is specified)
* </li><li>-script filename (default file name is backup.sql) * </li><li>-quiet does not print progress information
* </li><li>-options to specify a list of options (only for H2)
* </li></ul> * </li></ul>
* *
* @param args the command line arguments * @param args the command line arguments
...@@ -60,113 +48,42 @@ public class Backup { ...@@ -60,113 +48,42 @@ public class Backup {
public static void main(String[] args) throws SQLException { public static void main(String[] args) throws SQLException {
new Backup().run(args); new Backup().run(args);
} }
private void run(String[] args) throws SQLException { private void run(String[] args) throws SQLException {
String url = null; String zipFileName = "backup.zip";
String user = null; String dir = ".";
String password = ""; String db = null;
String script = "backup.sql"; boolean quiet = false;
String options1 = null, options2 = null;
for(int i=0; args != null && i<args.length; i++) { for(int i=0; args != null && i<args.length; i++) {
if(args[i].equals("-url")) { if(args[i].equals("-dir")) {
url = args[++i]; dir = args[++i];
} else if(args[i].equals("-user")) { } else if(args[i].equals("-db")) {
user = args[++i]; db = args[++i];
} else if(args[i].equals("-password")) { } else if(args[i].equals("-quiet")) {
password = args[++i]; quiet = true;
} else if(args[i].equals("-script")) {
script = args[++i];
} else if(args[i].equals("-options")) {
StringBuffer buff1 = new StringBuffer();
StringBuffer buff2 = new StringBuffer();
i++;
for(; i<args.length; i++) {
String a = args[i];
String upper = StringUtils.toUpperEnglish(a);
if(upper.startsWith("NO") || upper.equals("DROP")) {
buff1.append(' ');
buff1.append(args[i]);
} else {
buff2.append(' ');
buff2.append(args[i]);
}
}
options1 = buff1.toString();
options2 = buff2.toString();
} else { } else {
showUsage(); showUsage();
return; return;
} }
} }
if(url==null || user==null || script == null) { Backup.execute(zipFileName, dir, db, quiet);
showUsage();
return;
}
if(options1 != null) {
executeScript(url, user, password, script, options1, options2);
} else {
execute(url, user, password, script);
}
} }
/**
* INTERNAL
*/
public static void executeScript(String url, String user, String password, String fileName, String options1, String options2) throws SQLException {
Connection conn = null;
Statement stat = null;
try {
org.h2.Driver.load();
conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement();
String sql = "SCRIPT " + options1 + " TO '" + fileName + "' " + options2;
stat.execute(sql);
} catch(Exception e) {
throw Message.convert(e);
} finally {
JdbcUtils.closeSilently(stat);
JdbcUtils.closeSilently(conn);
}
}
/** /**
* Backs up a database to a file. * Backs up database files.
* *
* @param url the database URL * @param zipFileName the name of the backup file
* @param user the user name * @param directory the directory name
* @param password the password * @param db the database name (null for all databases)
* @param script the script file * @param quiet don't print progress information
* @throws SQLException * @throws SQLException
*/ */
public static void execute(String url, String user, String password, String script) throws SQLException { public static void execute(String zipFileName, String directory, String db, boolean quiet) throws SQLException {
Connection conn = null; ArrayList list = FileLister.getDatabaseFiles(directory, db, true);
Statement stat = null; if (list.size() == 0 && !quiet) {
FileWriter fileWriter = null; System.out.println("No database files found");
try { return;
org.h2.Driver.load();
conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement();
fileWriter = new FileWriter(script);
PrintWriter writer = new PrintWriter(new BufferedWriter(fileWriter));
ResultSet rs = stat.executeQuery("SCRIPT");
while(rs.next()) {
String s = rs.getString(1);
writer.println(s + ";");
}
writer.close();
} catch(Exception e) {
throw Message.convert(e);
} finally {
JdbcUtils.closeSilently(stat);
JdbcUtils.closeSilently(conn);
IOUtils.closeSilently(fileWriter);
} }
}
/**
* INTERNAL
*/
public static void backupFiles(String zipFileName, String directory, String db) throws IOException, SQLException {
File file = new File(zipFileName); File file = new File(zipFileName);
if(file.exists()) { if(file.exists()) {
file.delete(); file.delete();
...@@ -175,7 +92,6 @@ public class Backup { ...@@ -175,7 +92,6 @@ public class Backup {
try { try {
out = new FileOutputStream(file); out = new FileOutputStream(file);
ZipOutputStream zipOut = new ZipOutputStream(out); ZipOutputStream zipOut = new ZipOutputStream(out);
ArrayList list = FileLister.getDatabaseFiles(directory, db, true);
for(int i=0; i<list.size(); i++) { for(int i=0; i<list.size(); i++) {
String fileName = (String) list.get(i); String fileName = (String) list.get(i);
ZipEntry entry = new ZipEntry(FileUtils.getFileName(fileName)); ZipEntry entry = new ZipEntry(FileUtils.getFileName(fileName));
...@@ -188,45 +104,17 @@ public class Backup { ...@@ -188,45 +104,17 @@ public class Backup {
IOUtils.closeSilently(in); IOUtils.closeSilently(in);
} }
zipOut.closeEntry(); zipOut.closeEntry();
if (!quiet) {
System.out.println("processed: " + fileName);
}
} }
zipOut.closeEntry(); zipOut.closeEntry();
zipOut.close(); zipOut.close();
} catch(IOException e) {
throw Message.convert(e);
} finally { } finally {
IOUtils.closeSilently(out); IOUtils.closeSilently(out);
} }
} }
/**
* INTERNAL
*/
public static void restoreFiles(String zipFileName, String directory) throws IOException, SQLException {
File file = new File(zipFileName);
if(!file.exists()) {
throw new IOException("File not found: " + zipFileName);
}
FileInputStream in = null;
try {
in = new FileInputStream(file);
ZipInputStream zipIn = new ZipInputStream(in);
while(true) {
ZipEntry entry = zipIn.getNextEntry();
if(entry == null) {
break;
}
String fileName = entry.getName();
FileOutputStream out = null;
try {
out = new FileOutputStream(new File(directory, fileName));
IOUtils.copy(zipIn, out);
} finally {
IOUtils.closeSilently(out);
}
zipIn.closeEntry();
}
zipIn.closeEntry();
zipIn.close();
} finally {
IOUtils.closeSilently(in);
}
}
} }
...@@ -104,7 +104,7 @@ public class CreateCluster { ...@@ -104,7 +104,7 @@ public class CreateCluster {
// cannot change the data while it is restoring the second database. But there is currently no exclusive mode. // cannot change the data while it is restoring the second database. But there is currently no exclusive mode.
String scriptFile = "backup.sql"; String scriptFile = "backup.sql";
Backup.execute(urlSource, user, password, scriptFile); Script.execute(urlSource, user, password, scriptFile);
RunScript.execute(urlTarget, user, password, scriptFile, null, false); RunScript.execute(urlTarget, user, password, scriptFile, null, false);
new File(scriptFile).delete(); new File(scriptFile).delete();
......
...@@ -9,6 +9,7 @@ import java.sql.*; ...@@ -9,6 +9,7 @@ import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
/** /**
...@@ -47,11 +48,13 @@ public class Csv implements SimpleRowSource { ...@@ -47,11 +48,13 @@ public class Csv implements SimpleRowSource {
* @param fileName * @param fileName
* @param rs the result set * @param rs the result set
* @param charset the charset or null to use UTF-8 * @param charset the charset or null to use UTF-8
* @return the number of rows written
* @throws SQLException * @throws SQLException
*/ */
public void write(String fileName, ResultSet rs, String charset) throws SQLException { public int write(String fileName, ResultSet rs, String charset) throws SQLException {
ResultSetMetaData meta = rs.getMetaData(); ResultSetMetaData meta = rs.getMetaData();
init(fileName, charset); init(fileName, charset);
int rows = 0;
try { try {
initWrite(); initWrite();
int columnCount = meta.getColumnCount(); int columnCount = meta.getColumnCount();
...@@ -65,12 +68,15 @@ public class Csv implements SimpleRowSource { ...@@ -65,12 +68,15 @@ public class Csv implements SimpleRowSource {
row[i] = rs.getString(i + 1); row[i] = rs.getString(i + 1);
} }
writeRow(row); writeRow(row);
rows++;
} }
close(); return rows;
} catch(IOException e) { } catch(IOException e) {
throw convertException("IOException writing file " + fileName, e); throw convertException("IOException writing file " + fileName, e);
} finally {
close();
JdbcUtils.closeSilently(rs);
} }
rs.close();
} }
/** /**
...@@ -80,13 +86,15 @@ public class Csv implements SimpleRowSource { ...@@ -80,13 +86,15 @@ public class Csv implements SimpleRowSource {
* @param fileName the file name * @param fileName the file name
* @param sql the query * @param sql the query
* @param charset the charset or null to use UTF-8 * @param charset the charset or null to use UTF-8
* @return the number of rows written
* @throws SQLException * @throws SQLException
*/ */
public void write(Connection conn, String fileName, String sql, String charset) throws SQLException { public int write(Connection conn, String fileName, String sql, String charset) throws SQLException {
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql); ResultSet rs = stat.executeQuery(sql);
write(fileName, rs, charset); int rows = write(fileName, rs, charset);
stat.close(); stat.close();
return rows;
} }
/** /**
......
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.tools;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.h2.message.Message;
import org.h2.store.FileLister;
import org.h2.util.IOUtils;
/*
* Restores a H2 database by extracting the database files from a .zip file.
*
* @author Thomas
*/
public class Restore {
private void showUsage() {
System.out.println("java "+getClass().getName()
+ " [-file <filename>] [-dir <dir>] [-db <database>] [-quiet]");
}
/**
* The command line interface for this tool.
* The options must be split into strings like this: "-db", "test",...
* The following options are supported:
* <ul>
* <li>-help or -? (print the list of options)
* </li><li>-file filename (the default is backup.zip)
* </li><li>-dir directory (the default is the current directory)
* </li><li>-db databaseName (as stored in the backup if no name is specified)
* </li><li>-quiet does not print progress information
* </li></ul>
*
* @param args the command line arguments
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
new Restore().run(args);
}
private void run(String[] args) throws SQLException {
String zipFileName = "backup.zip";
String dir = ".";
String db = null;
boolean quiet = false;
for(int i=0; args != null && i<args.length; i++) {
if(args[i].equals("-dir")) {
dir = args[++i];
} else if(args[i].equals("-db")) {
db = args[++i];
} else if(args[i].equals("-quiet")) {
quiet = true;
} else {
showUsage();
return;
}
}
Restore.execute(zipFileName, dir, db, quiet);
}
private static String getOriginalDbName(File file, String db) throws IOException {
FileInputStream in = null;
try {
in = new FileInputStream(file);
ZipInputStream zipIn = new ZipInputStream(in);
String originalDbName = null;
boolean multiple = false;
while(true) {
ZipEntry entry = zipIn.getNextEntry();
if(entry == null) {
break;
}
String fileName = entry.getName();
zipIn.closeEntry();
String name = FileLister.getDatabaseNameFromFileName(fileName);
if(name != null) {
if(db.equals(name)) {
originalDbName = name;
// we found the correct database
break;
} else if(originalDbName == null) {
originalDbName = name;
// we found a database, but maybe another one
} else {
// we have found multiple databases, but not the correct one
multiple = true;
}
}
}
zipIn.close();
if(multiple && !originalDbName.equals(db)) {
throw new IOException("Multiple databases found, but not " + db);
}
return originalDbName;
} finally {
IOUtils.closeSilently(in);
}
}
/**
* Restores database files.
*
* @param zipFileName the name of the backup file
* @param directory the directory name
* @param db the database name (null for all databases)
* @param quiet don't print progress information
* @throws SQLException
*/
public static void execute(String zipFileName, String directory, String db, boolean quiet) throws SQLException {
FileInputStream in = null;
try {
File file = new File(zipFileName);
if(!file.exists()) {
throw new IOException("File not found: " + zipFileName);
}
String originalDbName = null;
if(db != null) {
originalDbName = getOriginalDbName(file, db);
if(originalDbName == null) {
throw new IOException("No database named " + db + " found");
}
}
in = new FileInputStream(file);
ZipInputStream zipIn = new ZipInputStream(in);
while(true) {
ZipEntry entry = zipIn.getNextEntry();
if(entry == null) {
break;
}
String fileName = entry.getName();
boolean copy = false;
if(db == null) {
copy = true;
} else if(fileName.startsWith(originalDbName)) {
fileName = db + fileName.substring(originalDbName.length());
copy = true;
}
if(copy) {
FileOutputStream out = null;
try {
out = new FileOutputStream(new File(directory, fileName));
IOUtils.copy(zipIn, out);
} finally {
IOUtils.closeSilently(out);
}
}
zipIn.closeEntry();
}
zipIn.closeEntry();
zipIn.close();
} catch(IOException e) {
throw Message.convert(e);
} finally {
IOUtils.closeSilently(in);
}
}
}
...@@ -21,6 +21,7 @@ import java.util.Iterator; ...@@ -21,6 +21,7 @@ import java.util.Iterator;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.message.Message; import org.h2.message.Message;
import org.h2.util.ClassUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.ScriptReader; import org.h2.util.ScriptReader;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
...@@ -89,7 +90,7 @@ public class RunScript { ...@@ -89,7 +90,7 @@ public class RunScript {
} else if (args[i].equals("-driver")) { } else if (args[i].equals("-driver")) {
String driver = args[++i]; String driver = args[++i];
try { try {
Class.forName(driver); ClassUtils.loadClass(driver);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw Message.convert(e); throw Message.convert(e);
} }
......
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.tools;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.message.Message;
import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils;
import org.h2.util.StringUtils;
/**
* Creates a SQL script file by extracting the schema and data of a database.
*
* @author Thomas
*/
public class Script {
private void showUsage() {
System.out.println("java "+getClass().getName()
+ " -url <url> -user <user> [-password <pwd>] [-file <filename>] [-options <option> ...]");
}
/**
* The command line interface for this tool.
* The options must be split into strings like this: "-user", "sa",...
* The following options are supported:
* <ul>
* <li>-help or -? (print the list of options)
* </li><li>-url jdbc:h2:... (database URL)
* </li><li>-user username
* </li><li>-password password
* </li><li>-file filename (default file name is backup.sql)
* </li><li>-options to specify a list of options (only for H2)
* </li></ul>
*
* @param args the command line arguments
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
new Script().run(args);
}
private void run(String[] args) throws SQLException {
String url = null;
String user = null;
String password = "";
String file = "backup.sql";
String options1 = null, options2 = null;
for(int i=0; args != null && i<args.length; i++) {
if(args[i].equals("-url")) {
url = args[++i];
} else if(args[i].equals("-user")) {
user = args[++i];
} else if(args[i].equals("-password")) {
password = args[++i];
} else if(args[i].equals("-file")) {
file = args[++i];
} else if(args[i].equals("-options")) {
StringBuffer buff1 = new StringBuffer();
StringBuffer buff2 = new StringBuffer();
i++;
for(; i<args.length; i++) {
String a = args[i];
String upper = StringUtils.toUpperEnglish(a);
if(upper.startsWith("NO") || upper.equals("DROP")) {
buff1.append(' ');
buff1.append(args[i]);
} else {
buff2.append(' ');
buff2.append(args[i]);
}
}
options1 = buff1.toString();
options2 = buff2.toString();
} else {
showUsage();
return;
}
}
if(url==null || user==null || file == null) {
showUsage();
return;
}
if(options1 != null) {
executeScript(url, user, password, file, options1, options2);
} else {
execute(url, user, password, file);
}
}
/**
* INTERNAL
*/
public static void executeScript(String url, String user, String password, String fileName, String options1, String options2) throws SQLException {
Connection conn = null;
Statement stat = null;
try {
org.h2.Driver.load();
conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement();
String sql = "SCRIPT " + options1 + " TO '" + fileName + "' " + options2;
stat.execute(sql);
} catch(Exception e) {
throw Message.convert(e);
} finally {
JdbcUtils.closeSilently(stat);
JdbcUtils.closeSilently(conn);
}
}
/**
* Backs up a database to a file.
*
* @param url the database URL
* @param user the user name
* @param password the password
* @param fileName the script file
* @throws SQLException
*/
public static void execute(String url, String user, String password, String fileName) throws SQLException {
Connection conn = null;
Statement stat = null;
FileWriter fileWriter = null;
try {
org.h2.Driver.load();
conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement();
fileWriter = new FileWriter(fileName);
PrintWriter writer = new PrintWriter(new BufferedWriter(fileWriter));
ResultSet rs = stat.executeQuery("SCRIPT");
while(rs.next()) {
String s = rs.getString(1);
writer.println(s + ";");
}
writer.close();
} catch(Exception e) {
throw Message.convert(e);
} finally {
JdbcUtils.closeSilently(stat);
JdbcUtils.closeSilently(conn);
IOUtils.closeSilently(fileWriter);
}
}
}
...@@ -209,8 +209,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -209,8 +209,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public byte getByte(int columnIndex) throws SQLException { public byte getByte(int columnIndex) throws SQLException {
Number v = (Number) get(columnIndex); Object o = get(columnIndex);
return v == null ? 0 : v.byteValue(); if(o != null && !(o instanceof Number)) {
o = Byte.decode(o.toString());
}
return o == null ? 0 : ((Number)o).byteValue();
} }
/** /**
...@@ -219,8 +222,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -219,8 +222,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public double getDouble(int columnIndex) throws SQLException { public double getDouble(int columnIndex) throws SQLException {
Number v = (Number) get(columnIndex); Object o = get(columnIndex);
return v == null ? 0 : v.doubleValue(); if(o != null && !(o instanceof Number)) {
return Double.parseDouble(o.toString());
}
return o == null ? 0 : ((Number)o).doubleValue();
} }
/** /**
...@@ -229,8 +235,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -229,8 +235,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public float getFloat(int columnIndex) throws SQLException { public float getFloat(int columnIndex) throws SQLException {
Number v = (Number) get(columnIndex); Object o = get(columnIndex);
return v == null ? 0 : v.floatValue(); if(o != null && !(o instanceof Number)) {
return Float.parseFloat(o.toString());
}
return o == null ? 0 : ((Number)o).floatValue();
} }
/** /**
...@@ -239,8 +248,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -239,8 +248,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public int getInt(int columnIndex) throws SQLException { public int getInt(int columnIndex) throws SQLException {
Number v = (Number) get(columnIndex); Object o = get(columnIndex);
return v == null ? 0 : v.intValue(); if(o != null && !(o instanceof Number)) {
o = Integer.decode(o.toString());
}
return o == null ? 0 : ((Number)o).intValue();
} }
/** /**
...@@ -249,8 +261,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -249,8 +261,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public long getLong(int columnIndex) throws SQLException { public long getLong(int columnIndex) throws SQLException {
Number v = (Number) get(columnIndex); Object o = get(columnIndex);
return v == null ? 0 : v.longValue(); if(o != null && !(o instanceof Number)) {
o = Long.decode(o.toString());
}
return o == null ? 0 : ((Number)o).longValue();
} }
/** /**
...@@ -259,8 +274,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -259,8 +274,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public short getShort(int columnIndex) throws SQLException { public short getShort(int columnIndex) throws SQLException {
Number v = (Number) get(columnIndex); Object o = get(columnIndex);
return v == null ? 0 : v.shortValue(); if(o != null && !(o instanceof Number)) {
o = Short.decode(o.toString());
}
return o == null ? 0 : ((Number)o).shortValue();
} }
/** /**
...@@ -269,8 +287,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -269,8 +287,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public boolean getBoolean(int columnIndex) throws SQLException { public boolean getBoolean(int columnIndex) throws SQLException {
Boolean v = (Boolean) get(columnIndex); Object o = get(columnIndex);
return v == null ? false : v.booleanValue(); if(o != null && !(o instanceof Boolean)) {
o = Boolean.valueOf(o.toString());
}
return o == null ? false : ((Boolean)o).booleanValue();
} }
/** /**
...@@ -307,8 +328,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -307,8 +328,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public byte getByte(String columnName) throws SQLException { public byte getByte(String columnName) throws SQLException {
Number v = (Number) get(columnName); return getByte(findColumn(columnName));
return v == null ? 0 : v.byteValue();
} }
/** /**
...@@ -317,8 +337,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -317,8 +337,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public double getDouble(String columnName) throws SQLException { public double getDouble(String columnName) throws SQLException {
Number v = (Number) get(columnName); return getDouble(findColumn(columnName));
return v == null ? 0 : v.doubleValue();
} }
/** /**
...@@ -327,8 +346,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -327,8 +346,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public float getFloat(String columnName) throws SQLException { public float getFloat(String columnName) throws SQLException {
Number v = (Number) get(columnName); return getFloat(findColumn(columnName));
return v == null ? 0 : v.floatValue();
} }
/** /**
...@@ -353,8 +371,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -353,8 +371,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public int getInt(String columnName) throws SQLException { public int getInt(String columnName) throws SQLException {
Number v = (Number) get(columnName); return getInt(findColumn(columnName));
return v == null ? 0 : v.intValue();
} }
/** /**
...@@ -363,8 +380,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -363,8 +380,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public long getLong(String columnName) throws SQLException { public long getLong(String columnName) throws SQLException {
Number v = (Number) get(columnName); return getLong(findColumn(columnName));
return v == null ? 0 : v.longValue();
} }
/** /**
...@@ -373,8 +389,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -373,8 +389,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public short getShort(String columnName) throws SQLException { public short getShort(String columnName) throws SQLException {
Number v = (Number) get(columnName); return getShort(findColumn(columnName));
return v == null ? 0 : v.shortValue();
} }
/** /**
...@@ -383,8 +398,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -383,8 +398,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public boolean getBoolean(String columnName) throws SQLException { public boolean getBoolean(String columnName) throws SQLException {
Boolean v = (Boolean) get(columnName); return getBoolean(findColumn(columnName));
return v == null ? false : v.booleanValue();
} }
/** /**
...@@ -393,7 +407,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -393,7 +407,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public byte[] getBytes(String columnName) throws SQLException { public byte[] getBytes(String columnName) throws SQLException {
return (byte[]) get(columnName); return getBytes(findColumn(columnName));
} }
/** /**
...@@ -402,7 +416,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -402,7 +416,11 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public BigDecimal getBigDecimal(int columnIndex) throws SQLException { public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
return (BigDecimal) get(columnIndex); Object o = get(columnIndex);
if(o != null && !(o instanceof BigDecimal)) {
o = new BigDecimal(o.toString());
}
return (BigDecimal)o;
} }
/** /**
...@@ -465,7 +483,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -465,7 +483,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public Object getObject(String columnName) throws SQLException { public Object getObject(String columnName) throws SQLException {
return get(columnName); return getObject(findColumn(columnName));
} }
/** /**
...@@ -474,8 +492,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -474,8 +492,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public String getString(String columnName) throws SQLException { public String getString(String columnName) throws SQLException {
Object o = get(columnName); return getString(findColumn(columnName));
return o == null ? null : o.toString();
} }
/** /**
...@@ -484,7 +501,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -484,7 +501,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public BigDecimal getBigDecimal(String columnName) throws SQLException { public BigDecimal getBigDecimal(String columnName) throws SQLException {
return (BigDecimal) get(columnName); return getBigDecimal(findColumn(columnName));
} }
/** /**
...@@ -493,7 +510,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -493,7 +510,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public Date getDate(String columnName) throws SQLException { public Date getDate(String columnName) throws SQLException {
return (Date) get(columnName); return getDate(findColumn(columnName));
} }
/** /**
...@@ -502,7 +519,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -502,7 +519,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public Time getTime(String columnName) throws SQLException { public Time getTime(String columnName) throws SQLException {
return (Time) get(columnName); return getTime(findColumn(columnName));
} }
/** /**
...@@ -511,7 +528,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -511,7 +528,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* @return the value * @return the value
*/ */
public Timestamp getTimestamp(String columnName) throws SQLException { public Timestamp getTimestamp(String columnName) throws SQLException {
return (Timestamp) get(columnName); return getTimestamp(findColumn(columnName));
} }
// ---- result set meta data --------------------------------------------- // ---- result set meta data ---------------------------------------------
...@@ -1202,16 +1219,12 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -1202,16 +1219,12 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
return new SQLException("Feature not supported", "HYC00"); return new SQLException("Feature not supported", "HYC00");
} }
private Object get(String columnName) throws SQLException {
return get(findColumn(columnName));
}
private void checkColumnIndex(int columnIndex) throws SQLException { private void checkColumnIndex(int columnIndex) throws SQLException {
if (columnIndex < 0 || columnIndex >= columns.size()) { if (columnIndex < 0 || columnIndex >= columns.size()) {
throw new SQLException("Invalid column index " + (columnIndex + 1), "90009"); throw new SQLException("Invalid column index " + (columnIndex + 1), "90009");
} }
} }
private Object get(int columnIndex) throws SQLException { private Object get(int columnIndex) throws SQLException {
if (currentRow == null) { if (currentRow == null) {
throw new SQLException("No data is available", "02000"); throw new SQLException("No data is available", "02000");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论