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 @ 45131
History | View | Annotate | Download (6.1 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 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 |
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 |
@Override
|
71 |
public final Object perform(Connection conn) throws DataException { |
72 |
return this.performCreateTable(conn); |
73 |
} |
74 |
|
75 |
protected List<String> buildCreateIndexesSQL( |
76 |
TableReference table, |
77 |
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 |
sqlbuilder.create_index().table() |
91 |
.database(this.table.getDatabase())
|
92 |
.schema(this.table.getSchema())
|
93 |
.name(this.table.getTable()
|
94 |
); |
95 |
sqls.addAll(sqlbuilder.create_index().toStrings()); |
96 |
} |
97 |
} |
98 |
return sqls;
|
99 |
} |
100 |
|
101 |
public List<String> getSQLs() throws DataException { |
102 |
|
103 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
104 |
sqlbuilder.create_table().table() |
105 |
.database(table.getDatabase()) |
106 |
.schema(table.getSchema()) |
107 |
.name(table.getTable()); |
108 |
for (FeatureAttributeDescriptor attr : type) {
|
109 |
if( attr.isComputed() ) {
|
110 |
continue;
|
111 |
} |
112 |
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 |
attr.isIndexed(), |
119 |
attr.allowNull() |
120 |
); |
121 |
} else {
|
122 |
sqlbuilder.create_table().add_column( |
123 |
attr.getName(), |
124 |
attr.getType(), |
125 |
attr.getSize(), |
126 |
attr.getPrecision(), |
127 |
attr.getScale(), |
128 |
attr.isPrimaryKey(), |
129 |
attr.isIndexed(), |
130 |
attr.allowNull(), |
131 |
attr.isAutomatic(), |
132 |
attr.getDefaultValue() |
133 |
); |
134 |
} |
135 |
} |
136 |
if( userAndPrivileges!=null ) { |
137 |
for (Pair<String, SQLBuilder.Privilege> roleAndPrivilege : userAndPrivileges) { |
138 |
sqlbuilder.grant().role(roleAndPrivilege.getLeft()).privilege(roleAndPrivilege.getRight()); |
139 |
} |
140 |
} |
141 |
|
142 |
List<String> sqls; |
143 |
sqls = sqlbuilder.create_table().toStrings(); |
144 |
sqls.addAll(buildCreateIndexesSQL(this.table,type));
|
145 |
sqls.addAll(sqlbuilder.grant().toStrings()); |
146 |
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 |
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 |
} |