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 |
/**
|
---|---|
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 |
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 |
import java.util.ArrayList; |
30 |
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 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
41 |
import org.gvsig.fmap.geom.DataTypes; |
42 |
|
43 |
public class CreateTableOperation extends AbstractConnectionWritableOperation { |
44 |
|
45 |
protected final TableReference table; |
46 |
protected 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 |
this(helper, null, null, null, null); |
54 |
} |
55 |
|
56 |
public CreateTableOperation(
|
57 |
JDBCHelper helper, |
58 |
TableReference table, |
59 |
FeatureType type, |
60 |
List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges, |
61 |
List<String> additionalSQLs |
62 |
) { |
63 |
super(helper);
|
64 |
this.table = table;
|
65 |
this.type = type;
|
66 |
this.userAndPrivileges = userAndPrivileges;
|
67 |
this.additionalSQLs = additionalSQLs;
|
68 |
} |
69 |
|
70 |
public FeatureType getType() {
|
71 |
return type;
|
72 |
} |
73 |
|
74 |
@Override
|
75 |
public final Object perform(Connection conn) throws DataException { |
76 |
return this.performCreateTable(conn); |
77 |
} |
78 |
|
79 |
protected List<String> buildCreateIndexesSQL( |
80 |
TableReference table, |
81 |
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 |
sqlbuilder.create_index().name("idx_" + table.toString().replace(".", "_") + "_" + attr.getName()); |
93 |
sqlbuilder.create_index().column(attr.getName()); |
94 |
sqlbuilder.create_index().table() |
95 |
.database(this.table.getDatabase())
|
96 |
.schema(this.table.getSchema())
|
97 |
.name(this.table.getTable()
|
98 |
); |
99 |
if(!attr.allowIndexDuplicateds()){
|
100 |
sqlbuilder.create_index().unique(); |
101 |
} |
102 |
sqls.addAll(sqlbuilder.create_index().toStrings()); |
103 |
} |
104 |
} |
105 |
return sqls;
|
106 |
} |
107 |
|
108 |
public List<String> getSQLs() throws DataException { |
109 |
|
110 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
111 |
sqlbuilder.create_table().table() |
112 |
.database(table.getDatabase()) |
113 |
.schema(table.getSchema()) |
114 |
.name(table.getTable()); |
115 |
for (FeatureAttributeDescriptor attr : type) {
|
116 |
if( attr.isComputed() ) {
|
117 |
continue;
|
118 |
} |
119 |
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 |
attr.isIndexed(), |
126 |
attr.allowNull() |
127 |
); |
128 |
} else {
|
129 |
sqlbuilder.create_table().add_column( |
130 |
attr.getName(), |
131 |
attr.getType(), |
132 |
attr.getSize(), |
133 |
attr.getPrecision(), |
134 |
attr.getScale(), |
135 |
attr.isPrimaryKey(), |
136 |
attr.isIndexed(), |
137 |
attr.allowNull(), |
138 |
attr.isAutomatic(), |
139 |
attr.getDefaultValue() |
140 |
); |
141 |
} |
142 |
} |
143 |
if( userAndPrivileges!=null ) { |
144 |
for (Pair<String, SQLBuilder.Privilege> roleAndPrivilege : userAndPrivileges) { |
145 |
sqlbuilder.grant().role(roleAndPrivilege.getLeft()).privilege(roleAndPrivilege.getRight()); |
146 |
} |
147 |
} |
148 |
|
149 |
List<String> sqls; |
150 |
sqls = sqlbuilder.create_table().toStrings(); |
151 |
sqls.addAll(buildCreateIndexesSQL(this.table,type));
|
152 |
sqls.addAll(sqlbuilder.grant().toStrings()); |
153 |
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 |
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 |
} |