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 @ 45065
History | View | Annotate | Download (6.1 KB)
1 | 45065 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2020 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 43020 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2.spi.operations; |
25 | |||
26 | import java.sql.Connection; |
||
27 | import java.sql.SQLException; |
||
28 | import java.sql.Statement; |
||
29 | 43687 | jjdelcerro | import java.util.ArrayList; |
30 | 43020 | jjdelcerro | import java.util.List; |
31 | import org.apache.commons.lang3.tuple.Pair; |
||
32 | import org.gvsig.fmap.dal.exception.DataException; |
||
33 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
34 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
35 | import org.gvsig.fmap.dal.SQLBuilder; |
||
36 | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
||
37 | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
||
38 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
||
39 | import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
||
40 | 44058 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
41 | 43114 | jjdelcerro | import org.gvsig.fmap.geom.DataTypes; |
42 | 43020 | jjdelcerro | |
43 | 43377 | jjdelcerro | public class CreateTableOperation extends AbstractConnectionWritableOperation { |
44 | 43020 | jjdelcerro | |
45 | 44058 | jjdelcerro | private final TableReference table; |
46 | 43020 | jjdelcerro | private final FeatureType type; |
47 | private final List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges; |
||
48 | private final List<String> additionalSQLs; |
||
49 | |||
50 | public CreateTableOperation(
|
||
51 | JDBCHelper helper |
||
52 | ) { |
||
53 | 44058 | jjdelcerro | this(helper, null, null, null, null); |
54 | 43020 | jjdelcerro | } |
55 | |||
56 | public CreateTableOperation(
|
||
57 | JDBCHelper helper, |
||
58 | 44058 | jjdelcerro | TableReference table, |
59 | 43020 | jjdelcerro | FeatureType type, |
60 | List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges, |
||
61 | List<String> additionalSQLs |
||
62 | ) { |
||
63 | super(helper);
|
||
64 | 44058 | jjdelcerro | this.table = table;
|
65 | 43020 | jjdelcerro | this.type = type;
|
66 | this.userAndPrivileges = userAndPrivileges;
|
||
67 | this.additionalSQLs = additionalSQLs;
|
||
68 | } |
||
69 | |||
70 | @Override
|
||
71 | public final Object perform(Connection conn) throws DataException { |
||
72 | 44678 | jjdelcerro | return this.performCreateTable(conn); |
73 | 43020 | jjdelcerro | } |
74 | |||
75 | 43687 | jjdelcerro | protected List<String> buildCreateIndexesSQL( |
76 | 44058 | jjdelcerro | TableReference table, |
77 | 43687 | jjdelcerro | FeatureType type |
78 | ) { |
||
79 | ArrayList<String> sqls = new ArrayList<>(); |
||
80 | |||
81 | for (FeatureAttributeDescriptor attr : type) {
|
||
82 | if( attr.isIndexed() ) {
|
||
83 | JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
||
84 | if( attr.getType()==org.gvsig.fmap.dal.DataTypes.GEOMETRY ) {
|
||
85 | sqlbuilder.create_index().spatial(); |
||
86 | } |
||
87 | sqlbuilder.create_index().if_not_exist(); |
||
88 | sqlbuilder.create_index().name("idx_" + table + "_" + attr.getName()); |
||
89 | sqlbuilder.create_index().column(attr.getName()); |
||
90 | 44058 | jjdelcerro | sqlbuilder.create_index().table() |
91 | .database(this.table.getDatabase())
|
||
92 | .schema(this.table.getSchema())
|
||
93 | .name(this.table.getTable()
|
||
94 | ); |
||
95 | 43687 | jjdelcerro | sqls.addAll(sqlbuilder.create_index().toStrings()); |
96 | } |
||
97 | } |
||
98 | return sqls;
|
||
99 | } |
||
100 | |||
101 | 44678 | jjdelcerro | public List<String> getSQLs() throws DataException { |
102 | 43020 | jjdelcerro | |
103 | JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
||
104 | 44058 | jjdelcerro | sqlbuilder.create_table().table() |
105 | 44678 | jjdelcerro | .database(table.getDatabase()) |
106 | .schema(table.getSchema()) |
||
107 | .name(table.getTable()); |
||
108 | 43020 | jjdelcerro | for (FeatureAttributeDescriptor attr : type) {
|
109 | 44320 | jjdelcerro | if( attr.isComputed() ) {
|
110 | continue;
|
||
111 | } |
||
112 | 43114 | jjdelcerro | if( attr.getType()==DataTypes.GEOMETRY ) {
|
113 | sqlbuilder.create_table().add_geometry_column( |
||
114 | attr.getName(), |
||
115 | attr.getGeomType().getType(), |
||
116 | attr.getGeomType().getSubType(), |
||
117 | attr.getSRS(), |
||
118 | 43355 | jjdelcerro | attr.isIndexed(), |
119 | 43114 | jjdelcerro | attr.allowNull() |
120 | ); |
||
121 | } else {
|
||
122 | sqlbuilder.create_table().add_column( |
||
123 | attr.getName(), |
||
124 | attr.getType(), |
||
125 | attr.getSize(), |
||
126 | attr.getPrecision(), |
||
127 | 44669 | jjdelcerro | attr.getScale(), |
128 | 43114 | jjdelcerro | attr.isPrimaryKey(), |
129 | 43355 | jjdelcerro | attr.isIndexed(), |
130 | 43114 | jjdelcerro | attr.allowNull(), |
131 | attr.isAutomatic(), |
||
132 | attr.getDefaultValue() |
||
133 | ); |
||
134 | } |
||
135 | 43020 | jjdelcerro | } |
136 | 44678 | jjdelcerro | if( userAndPrivileges!=null ) { |
137 | for (Pair<String, SQLBuilder.Privilege> roleAndPrivilege : userAndPrivileges) { |
||
138 | sqlbuilder.grant().role(roleAndPrivilege.getLeft()).privilege(roleAndPrivilege.getRight()); |
||
139 | } |
||
140 | 43020 | jjdelcerro | } |
141 | |||
142 | List<String> sqls; |
||
143 | sqls = sqlbuilder.create_table().toStrings(); |
||
144 | 44058 | jjdelcerro | sqls.addAll(buildCreateIndexesSQL(this.table,type));
|
145 | 43020 | jjdelcerro | sqls.addAll(sqlbuilder.grant().toStrings()); |
146 | 44678 | jjdelcerro | if( additionalSQLs!=null ) { |
147 | sqls.addAll(additionalSQLs); |
||
148 | } |
||
149 | |||
150 | return sqls;
|
||
151 | } |
||
152 | |||
153 | public boolean performCreateTable(Connection conn) throws DataException { |
||
154 | |||
155 | List<String> sqls = this.getSQLs(); |
||
156 | 43020 | jjdelcerro | Statement st = null; |
157 | try {
|
||
158 | st = conn.createStatement(); |
||
159 | for (String sql : sqls) { |
||
160 | JDBCUtils.execute(st, sql); |
||
161 | } |
||
162 | } catch (SQLException ex) { |
||
163 | throw new JDBCSQLException(ex); |
||
164 | } finally {
|
||
165 | JDBCUtils.closeQuietly(st); |
||
166 | } |
||
167 | return true; |
||
168 | } |
||
169 | } |