1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.efaps.db.wrapper;
22
23 import java.sql.Connection;
24 import java.sql.PreparedStatement;
25 import java.sql.ResultSet;
26 import java.sql.SQLException;
27 import java.sql.Statement;
28
29 import org.efaps.db.Context;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class SQLInsert
48 extends AbstractSQLInsertUpdate<SQLInsert>
49 {
50
51
52
53 private static final Logger LOG = LoggerFactory.getLogger(SQLInsert.class);
54
55
56
57
58 private final boolean newId;
59
60
61
62
63
64
65
66
67
68
69
70
71 public SQLInsert(final String _tableName,
72 final String _idCol,
73 final boolean _newId)
74 {
75 super(_tableName, _idCol);
76 this.newId = _newId;
77 }
78
79
80
81
82
83
84
85
86
87 public Long execute(final Connection _con)
88 throws SQLException
89 {
90 final boolean supGenKey = Context.getDbType().supportsGetGeneratedKeys();
91
92 Long ret = null;
93 if (this.newId && !supGenKey) {
94 ret = Context.getDbType().getNewId(_con, getTableName(), getIdColumn());
95 this.column(getIdColumn(), ret);
96 }
97
98 final StringBuilder cmd = new StringBuilder()
99 .append(Context.getDbType().getSQLPart(SQLPart.INSERT))
100 .append(" ")
101 .append(Context.getDbType().getSQLPart(SQLPart.INTO))
102 .append(" ")
103 .append(Context.getDbType().getTableQuote())
104 .append(getTableName())
105 .append(Context.getDbType().getTableQuote())
106 .append(" ")
107 .append(Context.getDbType().getSQLPart(SQLPart.PARENTHESIS_OPEN));
108
109 final StringBuilder val = new StringBuilder();
110
111
112 boolean first = true;
113 for (final ColumnWithSQLValue col : getColumnWithSQLValues()) {
114 if (first) {
115 first = false;
116 } else {
117 cmd.append(Context.getDbType().getSQLPart(SQLPart.COMMA));
118 val.append(Context.getDbType().getSQLPart(SQLPart.COMMA));
119 }
120 cmd.append(Context.getDbType().getColumnQuote())
121 .append(col.getColumnName())
122 .append(Context.getDbType().getColumnQuote());
123 val.append(col.getSqlValue());
124 }
125
126
127 for (final AbstractColumnWithValue<?> col : getColumnWithValues()) {
128 if (first) {
129 first = false;
130 } else {
131 cmd.append(Context.getDbType().getSQLPart(SQLPart.COMMA));
132 val.append(Context.getDbType().getSQLPart(SQLPart.COMMA));
133 }
134 cmd.append(Context.getDbType().getColumnQuote())
135 .append(col.getColumnName())
136 .append(Context.getDbType().getColumnQuote());
137 val.append('?');
138 }
139 cmd.append(Context.getDbType().getSQLPart(SQLPart.PARENTHESIS_CLOSE))
140 .append(Context.getDbType().getSQLPart(SQLPart.VALUES))
141 .append(Context.getDbType().getSQLPart(SQLPart.PARENTHESIS_OPEN))
142 .append(val)
143 .append(Context.getDbType().getSQLPart(SQLPart.PARENTHESIS_CLOSE));
144
145 SQLInsert.LOG.debug("Executing SQL: {}", cmd.toString());
146
147 PreparedStatement stmt;
148 if (this.newId && supGenKey) {
149 if (Context.getDbType().supportsMultiGeneratedKeys()) {
150 stmt = _con.prepareStatement(cmd.toString(), new String[]{getIdColumn()});
151 } else {
152 stmt = _con.prepareStatement(cmd.toString(), Statement.RETURN_GENERATED_KEYS);
153 }
154 } else {
155 stmt = _con.prepareStatement(cmd.toString());
156 }
157
158 int index = 1;
159 for (final AbstractColumnWithValue<?> col : getColumnWithValues()) {
160 if (SQLInsert.LOG.isDebugEnabled()) {
161 SQLInsert.LOG.debug(" " + index + " = " + col.getValue());
162 }
163 col.set(index++, stmt);
164 }
165
166 try {
167 final int rows = stmt.executeUpdate();
168 if (rows == 0) {
169 throw new SQLException("Object for SQL table '" + getTableName()
170 + "' does not exists and was not inserted.");
171 }
172
173
174 if (this.newId && supGenKey) {
175 final ResultSet resultset = stmt.getGeneratedKeys();
176 if (resultset.next()) {
177 ret = resultset.getLong(1);
178 }
179 resultset.close();
180 }
181 } finally {
182 stmt.close();
183 }
184
185 if (this.newId && SQLInsert.LOG.isDebugEnabled()) {
186 SQLInsert.LOG.debug("new generated id " + ret);
187 }
188
189 return ret;
190 }
191 }