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 @ 44376
History | View | Annotate | Download (5.12 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 | 43687 | jjdelcerro | import java.util.ArrayList; |
7 | 43020 | jjdelcerro | import java.util.List; |
8 | import org.apache.commons.lang3.tuple.Pair; |
||
9 | import org.gvsig.fmap.dal.exception.DataException; |
||
10 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
11 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
12 | import org.gvsig.fmap.dal.SQLBuilder; |
||
13 | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
||
14 | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
||
15 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
||
16 | import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
||
17 | 44058 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
18 | 43114 | jjdelcerro | import org.gvsig.fmap.geom.DataTypes; |
19 | 43020 | jjdelcerro | |
20 | 43377 | jjdelcerro | public class CreateTableOperation extends AbstractConnectionWritableOperation { |
21 | 43020 | jjdelcerro | |
22 | 44058 | jjdelcerro | private final TableReference table; |
23 | 43020 | jjdelcerro | private final FeatureType type; |
24 | private final List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges; |
||
25 | private final List<String> additionalSQLs; |
||
26 | |||
27 | public CreateTableOperation(
|
||
28 | JDBCHelper helper |
||
29 | ) { |
||
30 | 44058 | jjdelcerro | this(helper, null, null, null, null); |
31 | 43020 | jjdelcerro | } |
32 | |||
33 | public CreateTableOperation(
|
||
34 | JDBCHelper helper, |
||
35 | 44058 | jjdelcerro | TableReference table, |
36 | 43020 | jjdelcerro | FeatureType type, |
37 | List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges, |
||
38 | List<String> additionalSQLs |
||
39 | ) { |
||
40 | super(helper);
|
||
41 | 44058 | jjdelcerro | this.table = table;
|
42 | 43020 | jjdelcerro | this.type = type;
|
43 | this.userAndPrivileges = userAndPrivileges;
|
||
44 | this.additionalSQLs = additionalSQLs;
|
||
45 | } |
||
46 | |||
47 | @Override
|
||
48 | public final Object perform(Connection conn) throws DataException { |
||
49 | return this.performCreateTable(conn, |
||
50 | 44058 | jjdelcerro | table, type, userAndPrivileges, additionalSQLs); |
51 | 43020 | jjdelcerro | } |
52 | |||
53 | 43687 | jjdelcerro | protected List<String> buildCreateIndexesSQL( |
54 | 44058 | jjdelcerro | TableReference table, |
55 | 43687 | jjdelcerro | FeatureType type |
56 | ) { |
||
57 | ArrayList<String> sqls = new ArrayList<>(); |
||
58 | |||
59 | for (FeatureAttributeDescriptor attr : type) {
|
||
60 | if( attr.isIndexed() ) {
|
||
61 | JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
||
62 | if( attr.getType()==org.gvsig.fmap.dal.DataTypes.GEOMETRY ) {
|
||
63 | sqlbuilder.create_index().spatial(); |
||
64 | } |
||
65 | sqlbuilder.create_index().if_not_exist(); |
||
66 | sqlbuilder.create_index().name("idx_" + table + "_" + attr.getName()); |
||
67 | sqlbuilder.create_index().column(attr.getName()); |
||
68 | 44058 | jjdelcerro | sqlbuilder.create_index().table() |
69 | .database(this.table.getDatabase())
|
||
70 | .schema(this.table.getSchema())
|
||
71 | .name(this.table.getTable()
|
||
72 | ); |
||
73 | 43687 | jjdelcerro | sqls.addAll(sqlbuilder.create_index().toStrings()); |
74 | } |
||
75 | } |
||
76 | return sqls;
|
||
77 | } |
||
78 | |||
79 | 43020 | jjdelcerro | public boolean performCreateTable(Connection conn, |
80 | 44058 | jjdelcerro | TableReference table, |
81 | 43020 | jjdelcerro | FeatureType type, |
82 | List<Pair<String, SQLBuilder.Privilege>> rolesAndPrivileges, |
||
83 | List<String> additionalSQLs) throws DataException { |
||
84 | |||
85 | JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
||
86 | 44058 | jjdelcerro | sqlbuilder.create_table().table() |
87 | .database(this.table.getDatabase())
|
||
88 | .schema(this.table.getSchema())
|
||
89 | .name(this.table.getTable());
|
||
90 | 43020 | jjdelcerro | for (FeatureAttributeDescriptor attr : type) {
|
91 | 44320 | jjdelcerro | if( attr.isComputed() ) {
|
92 | continue;
|
||
93 | } |
||
94 | 43114 | jjdelcerro | if( attr.getType()==DataTypes.GEOMETRY ) {
|
95 | sqlbuilder.create_table().add_geometry_column( |
||
96 | attr.getName(), |
||
97 | attr.getGeomType().getType(), |
||
98 | attr.getGeomType().getSubType(), |
||
99 | attr.getSRS(), |
||
100 | 43355 | jjdelcerro | attr.isIndexed(), |
101 | 43114 | jjdelcerro | attr.allowNull() |
102 | ); |
||
103 | } else {
|
||
104 | sqlbuilder.create_table().add_column( |
||
105 | attr.getName(), |
||
106 | attr.getType(), |
||
107 | attr.getSize(), |
||
108 | attr.getPrecision(), |
||
109 | attr.isPrimaryKey(), |
||
110 | 43355 | jjdelcerro | attr.isIndexed(), |
111 | 43114 | jjdelcerro | attr.allowNull(), |
112 | attr.isAutomatic(), |
||
113 | attr.getDefaultValue() |
||
114 | ); |
||
115 | } |
||
116 | 43020 | jjdelcerro | } |
117 | for (Pair<String, SQLBuilder.Privilege> roleAndPrivilege : rolesAndPrivileges) { |
||
118 | sqlbuilder.grant().role(roleAndPrivilege.getLeft()).privilege(roleAndPrivilege.getRight()); |
||
119 | } |
||
120 | |||
121 | List<String> sqls; |
||
122 | sqls = sqlbuilder.create_table().toStrings(); |
||
123 | 44058 | jjdelcerro | sqls.addAll(buildCreateIndexesSQL(this.table,type));
|
124 | 43020 | jjdelcerro | sqls.addAll(sqlbuilder.grant().toStrings()); |
125 | sqls.addAll(additionalSQLs); |
||
126 | |||
127 | Statement st = null; |
||
128 | try {
|
||
129 | st = conn.createStatement(); |
||
130 | for (String sql : sqls) { |
||
131 | JDBCUtils.execute(st, sql); |
||
132 | } |
||
133 | } catch (SQLException ex) { |
||
134 | throw new JDBCSQLException(ex); |
||
135 | } finally {
|
||
136 | JDBCUtils.closeQuietly(st); |
||
137 | } |
||
138 | return true; |
||
139 | } |
||
140 | } |