01: /*
02: * DummySelect.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2007, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.db;
13:
14: import java.sql.SQLException;
15: import workbench.resource.Settings;
16: import workbench.storage.DmlStatement;
17: import workbench.storage.ResultInfo;
18: import workbench.storage.RowData;
19: import workbench.storage.SqlLiteralFormatter;
20: import workbench.storage.StatementFactory;
21: import workbench.util.SqlUtil;
22:
23: /**
24: * @author support@sql-workbench.net
25: */
26: public class DummyInsert implements DbObject {
27: private TableIdentifier table;
28:
29: public DummyInsert(TableIdentifier tbl) {
30: this .table = tbl;
31: }
32:
33: public String getCatalog() {
34: return null;
35: }
36:
37: public String getObjectExpression(WbConnection conn) {
38: return null;
39: }
40:
41: public String getObjectName() {
42: return null;
43: }
44:
45: public String getObjectName(WbConnection conn) {
46: return null;
47: }
48:
49: public String getObjectType() {
50: return "INSERT";
51: }
52:
53: public String getSchema() {
54: return null;
55: }
56:
57: public CharSequence getSource(WbConnection con) throws SQLException {
58: boolean makePrepared = Settings.getInstance().getBoolProperty(
59: "workbench.sql.generate.defaultinsert.prepared", false);
60: ResultInfo info = new ResultInfo(table, con);
61: info.setUpdateTable(table);
62: StatementFactory factory = new StatementFactory(info, con);
63:
64: SqlLiteralFormatter f = new SqlLiteralFormatter(con);
65:
66: RowData dummyData = new RowData(info.getColumnCount());
67:
68: // This is a "trick" to fool the StatementFactory which will
69: // check the type of the Data. In case it does not "know" the
70: // class, it calls toString() to get the value of the column
71: // this way we get a question mark for each value
72: StringBuilder marker = new StringBuilder("?");
73:
74: for (int i = 0; i < info.getColumnCount(); i++) {
75: if (makePrepared) {
76: dummyData.setValue(i, marker);
77: } else {
78: int type = info.getColumnType(i);
79: StringBuilder dummy = new StringBuilder();
80: if (SqlUtil.isCharacterType(type))
81: dummy.append('\'');
82: dummy.append(info.getColumnName(i));
83: dummy.append("_value");
84: if (SqlUtil.isCharacterType(type))
85: dummy.append('\'');
86: dummyData.setValue(i, dummy);
87: }
88: }
89: DmlStatement stmt = factory.createInsertStatement(dummyData,
90: true);
91: String nl = Settings.getInstance()
92: .getInternalEditorLineEnding();
93: String sql = stmt.getExecutableStatement(f) + ";" + nl;
94: return sql;
95: }
96:
97: }
|