| 1 | |
package org.apache.ojb.broker.accesslayer.sql; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
import java.util.ArrayList; |
| 19 | |
import java.util.List; |
| 20 | |
|
| 21 | |
import org.apache.ojb.broker.metadata.ClassDescriptor; |
| 22 | |
import org.apache.ojb.broker.metadata.FieldDescriptor; |
| 23 | |
import org.apache.ojb.broker.util.logging.Logger; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public class SqlInsertStatement extends SqlPkStatement |
| 32 | |
{ |
| 33 | |
private String sql; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public SqlInsertStatement(ClassDescriptor cld, Logger logger) |
| 42 | |
{ |
| 43 | |
super(cld, logger); |
| 44 | |
} |
| 45 | |
|
| 46 | |
|
| 47 | |
public String getStatement() |
| 48 | |
{ |
| 49 | |
if(sql == null) |
| 50 | |
{ |
| 51 | |
StringBuffer stmt = new StringBuffer(1024); |
| 52 | |
ClassDescriptor cld = getClassDescriptor(); |
| 53 | |
|
| 54 | |
stmt.append("INSERT INTO "); |
| 55 | |
appendTable(cld, stmt); |
| 56 | |
stmt.append(" ("); |
| 57 | |
appendListOfColumns(cld, stmt); |
| 58 | |
stmt.append(")"); |
| 59 | |
appendListOfValues(cld, stmt); |
| 60 | |
|
| 61 | |
sql = stmt.toString(); |
| 62 | |
} |
| 63 | |
return sql; |
| 64 | |
} |
| 65 | |
|
| 66 | |
private List appendListOfColumns(ClassDescriptor cld, StringBuffer buf) |
| 67 | |
{ |
| 68 | |
FieldDescriptor[] fields = cld.getAllRwFields(); |
| 69 | |
|
| 70 | |
ArrayList columnList = new ArrayList(); |
| 71 | |
|
| 72 | |
for(int i = 0; i < fields.length; i++) |
| 73 | |
{ |
| 74 | |
if(i > 0) |
| 75 | |
{ |
| 76 | |
buf.append(","); |
| 77 | |
} |
| 78 | |
buf.append(fields[i].getColumnName()); |
| 79 | |
columnList.add(fields[i].getAttributeName()); |
| 80 | |
} |
| 81 | |
return columnList; |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
private void appendListOfValues(ClassDescriptor cld, StringBuffer stmt) |
| 90 | |
{ |
| 91 | |
FieldDescriptor[] fields = cld.getAllRwFields(); |
| 92 | |
|
| 93 | |
if(fields.length == 0) |
| 94 | |
{ |
| 95 | |
return; |
| 96 | |
} |
| 97 | |
|
| 98 | |
stmt.append(" VALUES ("); |
| 99 | |
for(int i = 0; i < fields.length; i++) |
| 100 | |
{ |
| 101 | |
stmt.append("?"); |
| 102 | |
if(i < fields.length - 1) |
| 103 | |
{ |
| 104 | |
stmt.append(","); |
| 105 | |
} |
| 106 | |
} |
| 107 | |
stmt.append(") "); |
| 108 | |
} |
| 109 | |
|
| 110 | |
} |
| 111 | |
|