/
InsertSelect.java
88 lines (75 loc) · 2.15 KB
/
InsertSelect.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.sql;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
/**
* Implementation of InsertSelect.
*
* @author Steve Ebersole
*/
public class InsertSelect {
protected String tableName;
protected String comment;
protected List<String> columnNames = new ArrayList<>();
protected Select select;
public InsertSelect(Dialect dialect) {
//This is no longer used. Deprecate & remove?
// this.dialect = dialect;
}
public InsertSelect setTableName(String tableName) {
this.tableName = tableName;
return this;
}
public InsertSelect setComment(String comment) {
this.comment = comment;
return this;
}
public InsertSelect addColumn(String columnName) {
columnNames.add( columnName );
return this;
}
public InsertSelect addColumns(String[] columnNames) {
for ( String columnName : columnNames ) {
this.columnNames.add( columnName );
}
return this;
}
public InsertSelect setSelect(Select select) {
this.select = select;
return this;
}
public String toStatementString() {
if ( tableName == null ) {
throw new HibernateException( "no table name defined for insert-select" );
}
if ( select == null ) {
throw new HibernateException( "no select defined for insert-select" );
}
StringBuilder buf = new StringBuilder( (columnNames.size() * 15) + tableName.length() + 10 );
if ( comment!=null ) {
buf.append( "/* " ).append( Dialect.escapeComment( comment ) ).append( " */ " );
}
buf.append( "insert into " ).append( tableName );
if ( !columnNames.isEmpty() ) {
buf.append( " (" );
Iterator<String> itr = columnNames.iterator();
while ( itr.hasNext() ) {
buf.append( itr.next() );
if ( itr.hasNext() ) {
buf.append( ", " );
}
}
buf.append( ")" );
}
buf.append( ' ' ).append( select.toStatementString() );
return buf.toString();
}
}