1 21 package oracle.toplink.essentials.sequencing; 23 24 import java.io.StringWriter ; 25 import oracle.toplink.essentials.internal.helper.DatabaseTable; 26 import oracle.toplink.essentials.queryframework.*; 27 28 33 public class TableSequence extends QuerySequence { 34 35 protected static final String defaultTableName = "SEQUENCE"; 36 37 38 protected DatabaseTable table; 39 40 41 protected String counterFieldName = "SEQ_COUNT"; 42 43 44 protected String nameFieldName = "SEQ_NAME"; 45 protected String qualifier = ""; 46 47 public TableSequence() { 48 super(false, true); 49 setTableName(defaultTableName); 50 } 51 52 public TableSequence(String name) { 53 super(name, false, true); 54 setTableName(defaultTableName); 55 } 56 57 public TableSequence(String name, int size) { 58 super(name, size, false, true); 59 setTableName(defaultTableName); 60 } 61 62 public TableSequence(String name, int size, int initialValue) { 63 super(name, size, initialValue, false, true); 64 setTableName(defaultTableName); 65 } 66 67 public TableSequence(String name, String tableName) { 68 this(name); 69 setTableName(tableName); 70 } 71 72 public TableSequence(String name, String tableName, String nameFieldName, String counterFieldName) { 73 this(name); 74 setTableName(tableName); 75 setNameFieldName(nameFieldName); 76 setCounterFieldName(counterFieldName); 77 } 78 79 public TableSequence(String name, int size, String tableName) { 80 this(name, size); 81 setTableName(tableName); 82 } 83 84 public TableSequence(String name, int size, String tableName, String nameFieldName, String counterFieldName) { 85 this(name, size); 86 setTableName(tableName); 87 setNameFieldName(nameFieldName); 88 setCounterFieldName(counterFieldName); 89 } 90 91 public boolean equals(Object obj) { 92 if (obj instanceof TableSequence) { 93 TableSequence other = (TableSequence)obj; 94 if (equalNameAndSize(this, other)) { 95 return getTableName().equals(other.getTableName()) && getCounterFieldName().equals(other.getCounterFieldName()) && getNameFieldName().equals(other.getNameFieldName()); 96 } else { 97 return false; 98 } 99 } else { 100 return false; 101 } 102 } 103 104 public String getCounterFieldName() { 105 return counterFieldName; 106 } 107 108 public void setCounterFieldName(String name) { 109 counterFieldName = name; 110 } 111 112 public String getNameFieldName() { 113 return nameFieldName; 114 } 115 116 public void setNameFieldName(String name) { 117 nameFieldName = name; 118 } 119 120 public DatabaseTable getTable() { 121 return table; 122 } 123 124 public String getTableName() { 125 return getTable().getQualifiedName(); 126 } 127 128 public String getQualifiedTableName() { 129 if (qualifier.equals("")) { 130 return getTableName(); 131 } else { 132 return qualifier + "." + getTableName(); 133 } 134 } 135 136 public void setTable(DatabaseTable table) { 137 this.table = table; 138 } 139 140 public void setTableName(String name) { 141 table = new DatabaseTable(name); 142 } 143 144 protected ValueReadQuery buildSelectQuery() { 145 ValueReadQuery query = new ValueReadQuery(); 146 query.addArgument(getNameFieldName()); 147 StringWriter writer = new StringWriter (); 148 writer.write("SELECT " + getCounterFieldName()); 149 writer.write(" FROM " + getQualifiedTableName()); 150 writer.write(" WHERE " + getNameFieldName()); 151 writer.write(" = #" + getNameFieldName()); 152 query.setSQLString(writer.toString()); 153 154 return query; 155 } 156 157 protected DataModifyQuery buildUpdateQuery() { 158 DataModifyQuery query = new DataModifyQuery(); 159 query.addArgument(getNameFieldName()); 160 query.addArgument("PREALLOC_SIZE"); 161 StringWriter writer = new StringWriter (); 162 writer.write("UPDATE " + getQualifiedTableName()); 163 writer.write(" SET " + getCounterFieldName()); 164 writer.write(" = " + getCounterFieldName()); 165 writer.write(" + #PREALLOC_SIZE"); 166 writer.write(" WHERE " + getNameFieldName() + " = #" + getNameFieldName()); 167 query.setSQLString(writer.toString()); 168 169 return query; 170 } 171 172 175 public void onConnect() { 176 super.onConnect(); 177 qualifier = getDatasourcePlatform().getTableQualifier(); 178 } 179 180 183 public void onDisconnect() { 184 qualifier = ""; 185 super.onDisconnect(); 186 } 187 } 188 | Popular Tags |