KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > sql > Insert


1 //$Id: Insert.java,v 1.5 2005/05/24 19:06:52 oneovthafew Exp $
2
package org.hibernate.sql;
3
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import org.apache.commons.collections.SequencedHashMap;
8 import org.hibernate.dialect.Dialect;
9 import org.hibernate.type.LiteralType;
10
11 /**
12  * An SQL <tt>INSERT</tt> statement
13  *
14  * @author Gavin King
15  */

16 public class Insert {
17
18     public Insert(Dialect dialect) {
19         this.dialect = dialect;
20     }
21
22     private String JavaDoc comment;
23     public Insert setComment(String JavaDoc comment) {
24         this.comment = comment;
25         return this;
26     }
27
28     private Dialect dialect;
29     private String JavaDoc tableName;
30
31     private Map JavaDoc columns = new SequencedHashMap();
32
33     public Insert addColumn(String JavaDoc columnName) {
34         return addColumn(columnName, "?");
35     }
36
37     public Insert addColumns(String JavaDoc[] columnNames) {
38         for ( int i=0; i<columnNames.length; i++ ) {
39             addColumn( columnNames[i] );
40         }
41         return this;
42     }
43
44     public Insert addColumns(String JavaDoc[] columnNames, boolean[] insertable) {
45         for ( int i=0; i<columnNames.length; i++ ) {
46             if ( insertable[i] ) addColumn( columnNames[i] );
47         }
48         return this;
49     }
50
51     public Insert addColumn(String JavaDoc columnName, String JavaDoc value) {
52         columns.put(columnName, value);
53         return this;
54     }
55
56     public Insert addColumn(String JavaDoc columnName, Object JavaDoc value, LiteralType type) throws Exception JavaDoc {
57         return addColumn( columnName, type.objectToSQLString(value) );
58     }
59
60     public Insert addIdentityColumn(String JavaDoc columnName) {
61         String JavaDoc value = dialect.getIdentityInsertString();
62         if (value!=null) addColumn(columnName, value);
63         return this;
64     }
65
66     public Insert setTableName(String JavaDoc tableName) {
67         this.tableName = tableName;
68         return this;
69     }
70
71     public String JavaDoc toStatementString() {
72         StringBuffer JavaDoc buf = new StringBuffer JavaDoc( columns.size()*15 + tableName.length() + 10 );
73         if (comment!=null) buf.append("/* ").append(comment).append(" */ ");
74         buf.append("insert into ")
75             .append(tableName);
76         if ( columns.size()==0 ) {
77             buf.append(' ').append( dialect.getNoColumnsInsertString() );
78         }
79         else {
80             buf.append(" (");
81             Iterator JavaDoc iter = columns.keySet().iterator();
82             while ( iter.hasNext() ) {
83                 buf.append( iter.next() );
84                 if ( iter.hasNext() ) buf.append(", ");
85             }
86             buf.append(") values (");
87             iter = columns.values().iterator();
88             while ( iter.hasNext() ) {
89                 buf.append( iter.next() );
90                 if ( iter.hasNext() ) buf.append(", ");
91             }
92             buf.append(')');
93         }
94         return buf.toString();
95     }
96 }
97
Popular Tags