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 @ 44678
History | View | Annotate | Download (5.16 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 | 44678 | jjdelcerro | return this.performCreateTable(conn); |
50 | 43020 | jjdelcerro | } |
51 | |||
52 | 43687 | jjdelcerro | protected List<String> buildCreateIndexesSQL( |
53 | 44058 | jjdelcerro | TableReference table, |
54 | 43687 | jjdelcerro | FeatureType type |
55 | ) { |
||
56 | ArrayList<String> sqls = new ArrayList<>(); |
||
57 | |||
58 | for (FeatureAttributeDescriptor attr : type) {
|
||
59 | if( attr.isIndexed() ) {
|
||
60 | JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
||
61 | if( attr.getType()==org.gvsig.fmap.dal.DataTypes.GEOMETRY ) {
|
||
62 | sqlbuilder.create_index().spatial(); |
||
63 | } |
||
64 | sqlbuilder.create_index().if_not_exist(); |
||
65 | sqlbuilder.create_index().name("idx_" + table + "_" + attr.getName()); |
||
66 | sqlbuilder.create_index().column(attr.getName()); |
||
67 | 44058 | jjdelcerro | sqlbuilder.create_index().table() |
68 | .database(this.table.getDatabase())
|
||
69 | .schema(this.table.getSchema())
|
||
70 | .name(this.table.getTable()
|
||
71 | ); |
||
72 | 43687 | jjdelcerro | sqls.addAll(sqlbuilder.create_index().toStrings()); |
73 | } |
||
74 | } |
||
75 | return sqls;
|
||
76 | } |
||
77 | |||
78 | 44678 | jjdelcerro | public List<String> getSQLs() throws DataException { |
79 | 43020 | jjdelcerro | |
80 | JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
||
81 | 44058 | jjdelcerro | sqlbuilder.create_table().table() |
82 | 44678 | jjdelcerro | .database(table.getDatabase()) |
83 | .schema(table.getSchema()) |
||
84 | .name(table.getTable()); |
||
85 | 43020 | jjdelcerro | for (FeatureAttributeDescriptor attr : type) {
|
86 | 44320 | jjdelcerro | if( attr.isComputed() ) {
|
87 | continue;
|
||
88 | } |
||
89 | 43114 | jjdelcerro | if( attr.getType()==DataTypes.GEOMETRY ) {
|
90 | sqlbuilder.create_table().add_geometry_column( |
||
91 | attr.getName(), |
||
92 | attr.getGeomType().getType(), |
||
93 | attr.getGeomType().getSubType(), |
||
94 | attr.getSRS(), |
||
95 | 43355 | jjdelcerro | attr.isIndexed(), |
96 | 43114 | jjdelcerro | attr.allowNull() |
97 | ); |
||
98 | } else {
|
||
99 | sqlbuilder.create_table().add_column( |
||
100 | attr.getName(), |
||
101 | attr.getType(), |
||
102 | attr.getSize(), |
||
103 | attr.getPrecision(), |
||
104 | 44669 | jjdelcerro | attr.getScale(), |
105 | 43114 | jjdelcerro | attr.isPrimaryKey(), |
106 | 43355 | jjdelcerro | attr.isIndexed(), |
107 | 43114 | jjdelcerro | attr.allowNull(), |
108 | attr.isAutomatic(), |
||
109 | attr.getDefaultValue() |
||
110 | ); |
||
111 | } |
||
112 | 43020 | jjdelcerro | } |
113 | 44678 | jjdelcerro | if( userAndPrivileges!=null ) { |
114 | for (Pair<String, SQLBuilder.Privilege> roleAndPrivilege : userAndPrivileges) { |
||
115 | sqlbuilder.grant().role(roleAndPrivilege.getLeft()).privilege(roleAndPrivilege.getRight()); |
||
116 | } |
||
117 | 43020 | jjdelcerro | } |
118 | |||
119 | List<String> sqls; |
||
120 | sqls = sqlbuilder.create_table().toStrings(); |
||
121 | 44058 | jjdelcerro | sqls.addAll(buildCreateIndexesSQL(this.table,type));
|
122 | 43020 | jjdelcerro | sqls.addAll(sqlbuilder.grant().toStrings()); |
123 | 44678 | jjdelcerro | if( additionalSQLs!=null ) { |
124 | sqls.addAll(additionalSQLs); |
||
125 | } |
||
126 | |||
127 | return sqls;
|
||
128 | } |
||
129 | |||
130 | public boolean performCreateTable(Connection conn) throws DataException { |
||
131 | |||
132 | List<String> sqls = this.getSQLs(); |
||
133 | 43020 | jjdelcerro | Statement st = null; |
134 | try {
|
||
135 | st = conn.createStatement(); |
||
136 | for (String sql : sqls) { |
||
137 | JDBCUtils.execute(st, sql); |
||
138 | } |
||
139 | } catch (SQLException ex) { |
||
140 | throw new JDBCSQLException(ex); |
||
141 | } finally {
|
||
142 | JDBCUtils.closeQuietly(st); |
||
143 | } |
||
144 | return true; |
||
145 | } |
||
146 | } |