001: /*
002: * TableCreator.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db;
013:
014: import java.sql.SQLException;
015: import java.sql.Statement;
016: import java.util.ArrayList;
017: import java.util.Collection;
018: import java.util.List;
019:
020: import workbench.log.LogMgr;
021: import workbench.util.SqlUtil;
022:
023: /**
024: *
025: * @author support@sql-workbench.net
026: */
027: public class TableCreator {
028: private WbConnection connection;
029: private List<ColumnIdentifier> columnDefinition;
030: private TableIdentifier tablename;
031: private TypeMapper mapper;
032: private boolean useDbmsDataType = false;
033:
034: public TableCreator(WbConnection target, TableIdentifier newTable,
035: Collection<ColumnIdentifier> columns) throws SQLException {
036: this .connection = target;
037: this .tablename = newTable.createCopy();
038:
039: // As we are sorting the columns we have to create a copy of the array
040: // to ensure that the caller does not see a different ordering
041: this .columnDefinition = new ArrayList<ColumnIdentifier>(columns);
042:
043: // Now sort the columns according to their DBMS position
044: ColumnIdentifier.sortByPosition(columnDefinition);
045:
046: this .mapper = new TypeMapper(this .connection);
047: }
048:
049: public void useDbmsDataType(boolean flag) {
050: this .useDbmsDataType = flag;
051: }
052:
053: public TableIdentifier getTable() {
054: return this .tablename;
055: }
056:
057: public void createTable() throws SQLException {
058: StringBuilder sql = new StringBuilder(100);
059: sql.append("CREATE TABLE ");
060: String name = this .tablename
061: .getTableExpression(this .connection);
062: sql.append(name);
063: sql.append(" (");
064: int numCols = 0;
065: List<String> pkCols = new ArrayList<String>();
066:
067: for (ColumnIdentifier col : columnDefinition) {
068: if (col.isPkColumn())
069: pkCols.add(col.getColumnName());
070: String def = this .getColumnDefintionString(col);
071: if (def == null)
072: continue;
073:
074: if (numCols > 0)
075: sql.append(", ");
076: sql.append(def);
077: numCols++;
078: }
079: sql.append(')');
080: LogMgr.logInfo("TableCreator.createTable()",
081: "Creating table using sql: " + sql);
082: Statement stmt = this .connection.createStatement();
083: try {
084: stmt.executeUpdate(sql.toString());
085:
086: if (pkCols.size() > 0) {
087: CharSequence pkSql = this .connection.getMetadata()
088: .getPkSource(this .tablename, pkCols, null);
089: if (pkSql.length() > 0) {
090: LogMgr.logInfo("TableCreator.createTable()",
091: "Adding primary key using: "
092: + pkSql.toString());
093: stmt.executeUpdate(pkSql.toString());
094: }
095: }
096:
097: if (this .connection.getDbSettings().ddlNeedsCommit()
098: && !this .connection.getAutoCommit()) {
099: LogMgr.logDebug("TableCreator.createTable()",
100: "Commiting the changes");
101: this .connection.commit();
102: }
103: } finally {
104: SqlUtil.closeStatement(stmt);
105: }
106: }
107:
108: /**
109: * Return the SQL string for the column definition of the
110: * given column index (index into the columnDefinition array)
111: * The method expects the typeInfo map to be filled!
112: */
113: private String getColumnDefintionString(ColumnIdentifier col) {
114: if (col == null)
115: return null;
116:
117: int type = col.getDataType();
118: int size = col.getColumnSize();
119: int digits = col.getDecimalDigits();
120: String def = col.getDefaultValue();
121:
122: StringBuilder result = new StringBuilder(30);
123: result.append(col.getColumnName());
124: result.append(' ');
125:
126: String typeName = null;
127: if (this .useDbmsDataType) {
128: typeName = col.getDbmsType();
129: } else {
130: typeName = this .mapper.getTypeName(type, size, digits);
131: }
132: result.append(typeName);
133:
134: if (!col.isNullable()) {
135: result.append(" NOT NULL");
136: }
137:
138: return result.toString();
139: }
140:
141: }
|