1 28 29 package com.caucho.db.table; 30 31 import com.caucho.db.Database; 32 import com.caucho.db.sql.Expr; 33 import com.caucho.util.L10N; 34 35 import java.io.IOException ; 36 import java.sql.SQLException ; 37 import java.util.ArrayList ; 38 39 public class TableFactory { 40 private static final L10N L = new L10N(TableFactory.class); 41 42 private Database _database; 43 44 private String _name; 45 private Row _row; 46 47 private ArrayList <Constraint> _constraints = new ArrayList <Constraint>(); 48 49 public TableFactory(Database database) 50 { 51 _database = database; 52 } 53 54 57 public String getName() 58 { 59 return _name; 60 } 61 62 65 public Row getRow() 66 { 67 return _row; 68 } 69 70 73 public void startTable(String name) 74 { 75 _name = name; 76 _row = new Row(); 77 } 78 79 82 public Column addVarchar(String name, int size) 83 { 84 _row.allocateColumn(); 85 86 if (size <= 128) 87 return _row.addColumn(new StringColumn(_row, name, size)); 88 else 89 return _row.addColumn(new BigStringColumn(_row, name, size)); 90 } 91 92 95 public Column addBlob(String name) 96 { 97 _row.allocateColumn(); 98 99 return _row.addColumn(new BlobColumn(_row, name)); 100 } 101 102 105 public Column addInteger(String name) 106 { 107 _row.allocateColumn(); 108 109 return _row.addColumn(new IntColumn(_row, name)); 110 } 111 112 115 public Column addLong(String name) 116 { 117 _row.allocateColumn(); 118 119 return _row.addColumn(new LongColumn(_row, name)); 120 } 121 122 125 public Column addDouble(String name) 126 { 127 _row.allocateColumn(); 128 129 return _row.addColumn(new DoubleColumn(_row, name)); 130 } 131 132 135 public Column addDateTime(String name) 136 { 137 _row.allocateColumn(); 138 139 return _row.addColumn(new DateColumn(_row, name)); 140 } 141 142 145 public Column addNumeric(String name, int precision, int scale) 146 { 147 _row.allocateColumn(); 148 149 return _row.addColumn(new NumericColumn(_row, name, precision, scale)); 150 } 151 152 155 public void setPrimaryKey(String name) 156 throws SQLException 157 { 158 Column column = _row.getColumn(name); 159 160 if (column == null) 161 throw new SQLException (L.l("`{0}' is not a valid column for primary key", 162 name)); 163 164 column.setUnique(); 165 column.setNotNull(); 166 167 } 169 170 173 public void setNotNull(String name) 174 throws SQLException 175 { 176 Column column = _row.getColumn(name); 177 178 if (column == null) 179 throw new SQLException (L.l("`{0}' is not a valid column for NOT NULL", 180 name)); 181 182 column.setNotNull(); 183 } 184 185 188 public void setDefault(String name, Expr expr) 189 throws SQLException 190 { 191 Column column = _row.getColumn(name); 192 193 if (column == null) 194 throw new SQLException (L.l("`{0}' is not a valid column for DEFAULT", 195 name)); 196 197 column.setDefault(expr); 198 } 199 200 203 public void setUnique(String name) 204 throws SQLException 205 { 206 Column column = _row.getColumn(name); 207 208 if (column == null) 209 throw new SQLException (L.l("'{0}' is not a valid column for NOT NULL", 210 name)); 211 212 column.setUnique(); 213 214 } 217 218 221 public void setAutoIncrement(String name, int min) 222 throws SQLException 223 { 224 Column column = _row.getColumn(name); 225 226 if (column == null) 227 throw new SQLException (L.l("'{0}' is not a valid column for auto_increment", 228 name)); 229 230 column.setAutoIncrement(min); 231 } 232 233 234 237 public void addUnique(ArrayList <String > names) 238 throws SQLException 239 { 240 if (names.size() == 1) { 241 setUnique(names.get(0)); 242 return; 243 } 244 245 ArrayList <Column> columns = new ArrayList <Column>(); 246 247 for (int i = 0; i < names.size(); i++) { 248 String name = names.get(i); 249 250 Column column = _row.getColumn(name); 251 252 if (column == null) 253 throw new SQLException (L.l("`{0}' is not a valid column for UNIQUE", 254 name)); 255 } 256 257 Column []columnArray = new Column[columns.size()]; 258 columns.toArray(columnArray); 259 260 if (columnArray.length == 1) { 261 columnArray[0].setUnique(); 262 263 } 265 else 266 addConstraint(new UniqueConstraint(columnArray)); 267 } 268 269 272 public void addPrimaryKey(ArrayList <String > names) 273 throws SQLException 274 { 275 if (names.size() == 1) { 276 setPrimaryKey(names.get(0)); 277 return; 278 } 279 280 ArrayList <Column> columns = new ArrayList <Column>(); 281 282 for (int i = 0; i < names.size(); i++) { 283 String name = names.get(i); 284 285 Column column = _row.getColumn(name); 286 287 if (column == null) 288 throw new SQLException (L.l("`{0}' is not a valid column for PRIMARY KEY", 289 name)); 290 } 291 292 Column []columnArray = new Column[columns.size()]; 293 columns.toArray(columnArray); 294 295 if (columnArray.length == 1) { 296 columnArray[0].setPrimaryKey(true); 297 } 299 else 300 addConstraint(new PrimaryKeyConstraint(columnArray)); 301 } 302 303 306 public void addConstraint(Constraint constraint) 307 { 308 _constraints.add(constraint); 309 } 310 311 314 public Constraint []getConstraints() 315 { 316 Constraint []constraints = new Constraint[_constraints.size()]; 317 _constraints.toArray(constraints); 318 319 return constraints; 320 } 321 322 325 public void create() 326 throws java.sql.SQLException , IOException 327 { 328 Table table = new Table(_database, _name, _row, getConstraints()); 329 330 table.create(); 331 } 332 } 333 | Popular Tags |