File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\dml\RunScriptCommand.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.dml;
8
9 import java.io.BufferedReader;
10 import java.io.IOException;
11 import java.io.InputStreamReader;
12 import org.h2.command.CommandInterface;
13 import org.h2.command.Prepared;
14 import org.h2.engine.Constants;
15 import org.h2.engine.Session;
16 import org.h2.message.DbException;
17 import org.h2.result.ResultInterface;
18 import org.h2.util.ScriptReader;
19
20 /**
21 * This class represents the statement
22 * RUNSCRIPT
23 */
24 public class RunScriptCommand extends ScriptBase {
25
26 /**
27 * The byte order mark.
28 * 0xfeff because this is the Unicode char
29 * represented by the UTF-8 byte order mark (EF BB BF).
30 */
31 private static final char UTF8_BOM = '\uFEFF';
32
33 private String charset = Constants.UTF8;
34
35 public RunScriptCommand(Session session) {
36 super(session);
37 }
38
39 public int update() {
40 session.getUser().checkAdmin();
41 int count = 0;
42 try {
43 openInput();
44 BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
45 // if necessary, strip the BOM from the front of the file
46 reader.mark(1);
47 if (reader.read() != UTF8_BOM) {
48 reader.reset();
49 }
50 ScriptReader r = new ScriptReader(reader);
51 while (true) {
52 String sql = r.readStatement();
53 if (sql == null) {
54 break;
55 }
56 execute(sql);
57 count++;
58 if ((count & 127) == 0) {
59 checkCanceled();
60 }
61 }
62 reader.close();
63 } catch (IOException e) {
64 throw DbException.convertIOException(e, null);
65 } finally {
66 closeIO();
67 }
68 return count;
69 }
70
71 private void execute(String sql) {
72 try {
73 Prepared command = session.prepare(sql);
74 if (command.isQuery()) {
75 command.query(0);
76 } else {
77 command.update();
78 }
79 if (session.getAutoCommit()) {
80 session.commit(false);
81 }
82 } catch (DbException e) {
83 throw e.addSQL(sql);
84 }
85 }
86
87 public void setCharset(String charset) {
88 this.charset = charset;
89 }
90
91 public ResultInterface queryMeta() {
92 return null;
93 }
94
95 public int getType() {
96 return CommandInterface.RUNSCRIPT;
97 }
98
99 }
100