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 @ 43020
History | View | Annotate | Download (3.51 KB)
1 | 43020 | jjdelcerro | 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 | |||
17 | public class CreateTableOperation extends AbstractConnectionOperation { |
||
18 | |||
19 | private final String schemaName; |
||
20 | private final String tableName; |
||
21 | private final FeatureType type; |
||
22 | private final List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges; |
||
23 | private final List<String> additionalSQLs; |
||
24 | private final String dbName; |
||
25 | |||
26 | public CreateTableOperation(
|
||
27 | JDBCHelper helper |
||
28 | ) { |
||
29 | this(helper, null, null, null, null, null, null); |
||
30 | } |
||
31 | |||
32 | public CreateTableOperation(
|
||
33 | JDBCHelper helper, |
||
34 | String dbName,
|
||
35 | String schemaName,
|
||
36 | String tableName,
|
||
37 | FeatureType type, |
||
38 | List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges, |
||
39 | List<String> additionalSQLs |
||
40 | ) { |
||
41 | super(helper);
|
||
42 | this.dbName = dbName;
|
||
43 | this.schemaName = schemaName;
|
||
44 | this.tableName = tableName;
|
||
45 | this.type = type;
|
||
46 | this.userAndPrivileges = userAndPrivileges;
|
||
47 | this.additionalSQLs = additionalSQLs;
|
||
48 | } |
||
49 | |||
50 | @Override
|
||
51 | public final Object perform(Connection conn) throws DataException { |
||
52 | return this.performCreateTable(conn, |
||
53 | dbName, schemaName, tableName, |
||
54 | type, userAndPrivileges, additionalSQLs); |
||
55 | } |
||
56 | |||
57 | public boolean performCreateTable(Connection conn, |
||
58 | String dbName,
|
||
59 | String schemaName,
|
||
60 | String tableName,
|
||
61 | FeatureType type, |
||
62 | List<Pair<String, SQLBuilder.Privilege>> rolesAndPrivileges, |
||
63 | List<String> additionalSQLs) throws DataException { |
||
64 | |||
65 | JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
||
66 | sqlbuilder.create_table().table().database(dbName).schema(schemaName).name(tableName); |
||
67 | for (FeatureAttributeDescriptor attr : type) {
|
||
68 | sqlbuilder.create_table().add_column( |
||
69 | attr.getName(), |
||
70 | attr.getType(), |
||
71 | attr.getSize(), |
||
72 | attr.getPrecision(), |
||
73 | attr.isPrimaryKey(), |
||
74 | attr.allowNull(), |
||
75 | attr.isAutomatic(), |
||
76 | attr.getDefaultValue() |
||
77 | ); |
||
78 | } |
||
79 | for (Pair<String, SQLBuilder.Privilege> roleAndPrivilege : rolesAndPrivileges) { |
||
80 | sqlbuilder.grant().role(roleAndPrivilege.getLeft()).privilege(roleAndPrivilege.getRight()); |
||
81 | } |
||
82 | |||
83 | List<String> sqls; |
||
84 | sqls = sqlbuilder.create_table().toStrings(); |
||
85 | sqls.addAll(sqlbuilder.grant().toStrings()); |
||
86 | sqls.addAll(additionalSQLs); |
||
87 | |||
88 | Statement st = null; |
||
89 | try {
|
||
90 | st = conn.createStatement(); |
||
91 | for (String sql : sqls) { |
||
92 | |||
93 | JDBCUtils.execute(st, sql); |
||
94 | } |
||
95 | } catch (SQLException ex) { |
||
96 | throw new JDBCSQLException(ex); |
||
97 | } finally {
|
||
98 | JDBCUtils.closeQuietly(st); |
||
99 | } |
||
100 | return true; |
||
101 | } |
||
102 | } |