svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / operations / CreateTableOperation.java @ 43377
History | View | Annotate | Download (4.07 KB)
1 |
package org.gvsig.fmap.dal.store.jdbc2.spi.operations; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.SQLException; |
5 |
import java.sql.Statement; |
6 |
import java.util.List; |
7 |
import org.apache.commons.lang3.tuple.Pair; |
8 |
import org.gvsig.fmap.dal.exception.DataException; |
9 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
10 |
import org.gvsig.fmap.dal.feature.FeatureType; |
11 |
import org.gvsig.fmap.dal.SQLBuilder; |
12 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
13 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
14 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
15 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
16 |
import org.gvsig.fmap.geom.DataTypes; |
17 |
|
18 |
public class CreateTableOperation extends AbstractConnectionWritableOperation { |
19 |
|
20 |
private final String schemaName; |
21 |
private final String tableName; |
22 |
private final FeatureType type; |
23 |
private final List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges; |
24 |
private final List<String> additionalSQLs; |
25 |
private final String dbName; |
26 |
|
27 |
public CreateTableOperation(
|
28 |
JDBCHelper helper |
29 |
) { |
30 |
this(helper, null, null, null, null, null, null); |
31 |
} |
32 |
|
33 |
public CreateTableOperation(
|
34 |
JDBCHelper helper, |
35 |
String dbName,
|
36 |
String schemaName,
|
37 |
String tableName,
|
38 |
FeatureType type, |
39 |
List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges, |
40 |
List<String> additionalSQLs |
41 |
) { |
42 |
super(helper);
|
43 |
this.dbName = dbName;
|
44 |
this.schemaName = schemaName;
|
45 |
this.tableName = tableName;
|
46 |
this.type = type;
|
47 |
this.userAndPrivileges = userAndPrivileges;
|
48 |
this.additionalSQLs = additionalSQLs;
|
49 |
} |
50 |
|
51 |
@Override
|
52 |
public final Object perform(Connection conn) throws DataException { |
53 |
return this.performCreateTable(conn, |
54 |
dbName, schemaName, tableName, |
55 |
type, userAndPrivileges, additionalSQLs); |
56 |
} |
57 |
|
58 |
public boolean performCreateTable(Connection conn, |
59 |
String dbName,
|
60 |
String schemaName,
|
61 |
String tableName,
|
62 |
FeatureType type, |
63 |
List<Pair<String, SQLBuilder.Privilege>> rolesAndPrivileges, |
64 |
List<String> additionalSQLs) throws DataException { |
65 |
|
66 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
67 |
sqlbuilder.create_table().table().database(dbName).schema(schemaName).name(tableName); |
68 |
for (FeatureAttributeDescriptor attr : type) {
|
69 |
if( attr.getType()==DataTypes.GEOMETRY ) {
|
70 |
sqlbuilder.create_table().add_geometry_column( |
71 |
attr.getName(), |
72 |
attr.getGeomType().getType(), |
73 |
attr.getGeomType().getSubType(), |
74 |
attr.getSRS(), |
75 |
attr.isIndexed(), |
76 |
attr.allowNull() |
77 |
); |
78 |
} else {
|
79 |
sqlbuilder.create_table().add_column( |
80 |
attr.getName(), |
81 |
attr.getType(), |
82 |
attr.getSize(), |
83 |
attr.getPrecision(), |
84 |
attr.isPrimaryKey(), |
85 |
attr.isIndexed(), |
86 |
attr.allowNull(), |
87 |
attr.isAutomatic(), |
88 |
attr.getDefaultValue() |
89 |
); |
90 |
} |
91 |
} |
92 |
for (Pair<String, SQLBuilder.Privilege> roleAndPrivilege : rolesAndPrivileges) { |
93 |
sqlbuilder.grant().role(roleAndPrivilege.getLeft()).privilege(roleAndPrivilege.getRight()); |
94 |
} |
95 |
|
96 |
List<String> sqls; |
97 |
sqls = sqlbuilder.create_table().toStrings(); |
98 |
sqls.addAll(sqlbuilder.grant().toStrings()); |
99 |
sqls.addAll(additionalSQLs); |
100 |
|
101 |
Statement st = null; |
102 |
try {
|
103 |
st = conn.createStatement(); |
104 |
for (String sql : sqls) { |
105 |
|
106 |
JDBCUtils.execute(st, sql); |
107 |
} |
108 |
} catch (SQLException ex) { |
109 |
throw new JDBCSQLException(ex); |
110 |
} finally {
|
111 |
JDBCUtils.closeQuietly(st); |
112 |
} |
113 |
return true; |
114 |
} |
115 |
} |