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 @ 45534
History | View | Annotate | Download (6.31 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 | 45131 | fdiaz | protected final TableReference table; |
46 | 45152 | fdiaz | protected FeatureType type;
|
47 | 43020 | jjdelcerro | 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 | 45152 | fdiaz | public FeatureType getType() {
|
71 | return type;
|
||
72 | } |
||
73 | |||
74 | 43020 | jjdelcerro | @Override
|
75 | public final Object perform(Connection conn) throws DataException { |
||
76 | 44678 | jjdelcerro | return this.performCreateTable(conn); |
77 | 43020 | jjdelcerro | } |
78 | |||
79 | 43687 | jjdelcerro | protected List<String> buildCreateIndexesSQL( |
80 | 44058 | jjdelcerro | TableReference table, |
81 | 43687 | jjdelcerro | FeatureType type |
82 | ) { |
||
83 | ArrayList<String> sqls = new ArrayList<>(); |
||
84 | |||
85 | for (FeatureAttributeDescriptor attr : type) {
|
||
86 | if( attr.isIndexed() ) {
|
||
87 | JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
||
88 | if( attr.getType()==org.gvsig.fmap.dal.DataTypes.GEOMETRY ) {
|
||
89 | sqlbuilder.create_index().spatial(); |
||
90 | } |
||
91 | sqlbuilder.create_index().if_not_exist(); |
||
92 | 45460 | fdiaz | sqlbuilder.create_index().name("idx_" + table.toString().replace(".", "_") + "_" + attr.getName()); |
93 | 43687 | jjdelcerro | sqlbuilder.create_index().column(attr.getName()); |
94 | 44058 | jjdelcerro | sqlbuilder.create_index().table() |
95 | .database(this.table.getDatabase())
|
||
96 | .schema(this.table.getSchema())
|
||
97 | .name(this.table.getTable()
|
||
98 | ); |
||
99 | 45434 | fdiaz | if(!attr.allowIndexDuplicateds()){
|
100 | sqlbuilder.create_index().unique(); |
||
101 | } |
||
102 | 43687 | jjdelcerro | sqls.addAll(sqlbuilder.create_index().toStrings()); |
103 | } |
||
104 | } |
||
105 | return sqls;
|
||
106 | } |
||
107 | |||
108 | 44678 | jjdelcerro | public List<String> getSQLs() throws DataException { |
109 | 43020 | jjdelcerro | |
110 | JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
||
111 | 44058 | jjdelcerro | sqlbuilder.create_table().table() |
112 | 44678 | jjdelcerro | .database(table.getDatabase()) |
113 | .schema(table.getSchema()) |
||
114 | .name(table.getTable()); |
||
115 | 43020 | jjdelcerro | for (FeatureAttributeDescriptor attr : type) {
|
116 | 44320 | jjdelcerro | if( attr.isComputed() ) {
|
117 | continue;
|
||
118 | } |
||
119 | 43114 | jjdelcerro | if( attr.getType()==DataTypes.GEOMETRY ) {
|
120 | sqlbuilder.create_table().add_geometry_column( |
||
121 | attr.getName(), |
||
122 | attr.getGeomType().getType(), |
||
123 | attr.getGeomType().getSubType(), |
||
124 | attr.getSRS(), |
||
125 | 43355 | jjdelcerro | attr.isIndexed(), |
126 | 43114 | jjdelcerro | attr.allowNull() |
127 | ); |
||
128 | } else {
|
||
129 | sqlbuilder.create_table().add_column( |
||
130 | attr.getName(), |
||
131 | attr.getType(), |
||
132 | attr.getSize(), |
||
133 | attr.getPrecision(), |
||
134 | 44669 | jjdelcerro | attr.getScale(), |
135 | 43114 | jjdelcerro | attr.isPrimaryKey(), |
136 | 43355 | jjdelcerro | attr.isIndexed(), |
137 | 43114 | jjdelcerro | attr.allowNull(), |
138 | attr.isAutomatic(), |
||
139 | attr.getDefaultValue() |
||
140 | ); |
||
141 | } |
||
142 | 43020 | jjdelcerro | } |
143 | 44678 | jjdelcerro | if( userAndPrivileges!=null ) { |
144 | for (Pair<String, SQLBuilder.Privilege> roleAndPrivilege : userAndPrivileges) { |
||
145 | sqlbuilder.grant().role(roleAndPrivilege.getLeft()).privilege(roleAndPrivilege.getRight()); |
||
146 | } |
||
147 | 43020 | jjdelcerro | } |
148 | |||
149 | List<String> sqls; |
||
150 | sqls = sqlbuilder.create_table().toStrings(); |
||
151 | 44058 | jjdelcerro | sqls.addAll(buildCreateIndexesSQL(this.table,type));
|
152 | 43020 | jjdelcerro | sqls.addAll(sqlbuilder.grant().toStrings()); |
153 | 44678 | jjdelcerro | if( additionalSQLs!=null ) { |
154 | sqls.addAll(additionalSQLs); |
||
155 | } |
||
156 | |||
157 | return sqls;
|
||
158 | } |
||
159 | |||
160 | public boolean performCreateTable(Connection conn) throws DataException { |
||
161 | |||
162 | List<String> sqls = this.getSQLs(); |
||
163 | 43020 | jjdelcerro | Statement st = null; |
164 | try {
|
||
165 | st = conn.createStatement(); |
||
166 | for (String sql : sqls) { |
||
167 | JDBCUtils.execute(st, sql); |
||
168 | } |
||
169 | } catch (SQLException ex) { |
||
170 | throw new JDBCSQLException(ex); |
||
171 | } finally {
|
||
172 | JDBCUtils.closeQuietly(st); |
||
173 | } |
||
174 | return true; |
||
175 | } |
||
176 | } |