01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.command.dml;
07:
08: import java.io.BufferedReader;
09: import java.io.IOException;
10: import java.io.InputStreamReader;
11: import java.io.Reader;
12: import java.sql.SQLException;
13:
14: import org.h2.command.Prepared;
15: import org.h2.engine.Session;
16: import org.h2.message.Message;
17: import org.h2.result.LocalResult;
18: import org.h2.util.ScriptReader;
19: import org.h2.util.StringUtils;
20:
21: /**
22: * This class represents the statement
23: * RUNSCRIPT
24: */
25: public class RunScriptCommand extends ScriptBase {
26:
27: private String charset = StringUtils.getDefaultCharset();
28:
29: public RunScriptCommand(Session session) {
30: super (session);
31: }
32:
33: public int update() throws SQLException {
34: session.getUser().checkAdmin();
35: int count = 0;
36: try {
37: openInput();
38: Reader reader = new InputStreamReader(in, charset);
39: ScriptReader r = new ScriptReader(
40: new BufferedReader(reader));
41: while (true) {
42: String sql = r.readStatement();
43: if (sql == null) {
44: break;
45: }
46: execute(sql);
47: count++;
48: }
49: reader.close();
50: } catch (IOException e) {
51: throw Message.convertIOException(e, null);
52: } finally {
53: closeIO();
54: }
55: return count;
56: }
57:
58: private void execute(String sql) throws SQLException {
59: try {
60: Prepared command = session.prepare(sql);
61: if (command.isQuery()) {
62: command.query(0);
63: } else {
64: command.update();
65: }
66: if (session.getAutoCommit()) {
67: session.commit(false);
68: }
69: } catch (SQLException e) {
70: throw Message.addSQL(e, sql);
71: }
72: }
73:
74: public void setCharset(String charset) {
75: this .charset = charset;
76: }
77:
78: public LocalResult queryMeta() {
79: return null;
80: }
81:
82: }
|