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 @ 47606
History | View | Annotate | Download (6.93 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.SQLException; |
27 |
import java.sql.Statement; |
28 |
import java.util.ArrayList; |
29 |
import java.util.List; |
30 |
import org.apache.commons.lang3.StringUtils; |
31 |
import org.apache.commons.lang3.tuple.Pair; |
32 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
33 |
import org.gvsig.fmap.dal.exception.DataException; |
34 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
35 |
import org.gvsig.fmap.dal.feature.FeatureType; |
36 |
import org.gvsig.fmap.dal.SQLBuilder; |
37 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
38 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
39 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
40 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
41 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
42 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
43 |
import org.gvsig.fmap.geom.DataTypes; |
44 |
|
45 |
public class CreateTableOperation extends AbstractConnectionWritableOperation { |
46 |
|
47 |
protected final TableReference table; |
48 |
protected FeatureType type;
|
49 |
protected final List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges; |
50 |
protected final List<String> additionalSQLs; |
51 |
|
52 |
public CreateTableOperation(
|
53 |
JDBCHelper helper |
54 |
) { |
55 |
this(helper, null, null, null, null); |
56 |
} |
57 |
|
58 |
public CreateTableOperation(
|
59 |
JDBCHelper helper, |
60 |
TableReference table, |
61 |
FeatureType type, |
62 |
List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges, |
63 |
List<String> additionalSQLs |
64 |
) { |
65 |
super(helper);
|
66 |
this.table = table;
|
67 |
this.type = type;
|
68 |
this.userAndPrivileges = userAndPrivileges;
|
69 |
this.additionalSQLs = additionalSQLs;
|
70 |
} |
71 |
|
72 |
public FeatureType getType() {
|
73 |
return type;
|
74 |
} |
75 |
|
76 |
@Override
|
77 |
public final Object perform(JDBCConnection conn) throws DataException { |
78 |
return this.performCreateTable(conn); |
79 |
} |
80 |
|
81 |
protected List<String> buildCreateIndexesSQL( |
82 |
TableReference table, |
83 |
FeatureType type |
84 |
) { |
85 |
ArrayList<String> sqls = new ArrayList<>(); |
86 |
|
87 |
for (FeatureAttributeDescriptor attr : type) {
|
88 |
if( attr.isIndexed() && !attr.isPrimaryKey() ) {
|
89 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
90 |
SQLBuilder.CreateIndexBuilder createIndex = sqlbuilder.create_index(); |
91 |
createIndex.setFeatureType(type); |
92 |
if( attr.getType()==org.gvsig.fmap.dal.DataTypes.GEOMETRY ) {
|
93 |
createIndex.spatial(); |
94 |
} |
95 |
createIndex.if_not_exist(); |
96 |
createIndex.name("idx_" + table.toString().replace(".", "_") + "_" + attr.getName()); |
97 |
createIndex.column(attr.getName()); |
98 |
createIndex.table() |
99 |
.database(this.table.getDatabase())
|
100 |
.schema(this.table.getSchema())
|
101 |
.name(this.table.getTable()
|
102 |
); |
103 |
if(!attr.allowIndexDuplicateds()){
|
104 |
createIndex.unique(); |
105 |
} |
106 |
sqls.addAll(createIndex.toStrings()); |
107 |
} |
108 |
} |
109 |
return sqls;
|
110 |
} |
111 |
|
112 |
public List<String> getSQLs() throws DataException { |
113 |
|
114 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
115 |
sqlbuilder.create_table().table() |
116 |
.database(table.getDatabase()) |
117 |
.schema(table.getSchema()) |
118 |
.name(table.getTable()); |
119 |
for (FeatureAttributeDescriptor attr : type) {
|
120 |
if( attr.isComputed() ) {
|
121 |
continue;
|
122 |
} |
123 |
if( attr.getType()==DataTypes.GEOMETRY ) {
|
124 |
sqlbuilder.create_table().add_column(attr); |
125 |
// sqlbuilder.create_table().add_geometry_column(attr);
|
126 |
// attr.getName(),
|
127 |
// attr.getGeomType().getType(),
|
128 |
// attr.getGeomType().getSubType(),
|
129 |
// attr.getSRS(),
|
130 |
// attr.isIndexed(),
|
131 |
// attr.allowNull()
|
132 |
// );
|
133 |
} else {
|
134 |
Object defaultValue = attr.getDefaultValue();
|
135 |
if( defaultValue instanceof CharSequence ) { |
136 |
if( ExpressionUtils.isDynamicText(defaultValue.toString())) {
|
137 |
defaultValue = null;
|
138 |
} |
139 |
} |
140 |
sqlbuilder.create_table().add_column( |
141 |
attr.getName(), |
142 |
attr.getType(), |
143 |
attr.getSize(), |
144 |
attr.getPrecision(), |
145 |
attr.getScale(), |
146 |
attr.isPrimaryKey(), |
147 |
attr.isIndexed(), |
148 |
attr.allowNull(), |
149 |
attr.isAutomatic(), |
150 |
defaultValue |
151 |
); |
152 |
} |
153 |
} |
154 |
if( userAndPrivileges!=null ) { |
155 |
for (Pair<String, SQLBuilder.Privilege> roleAndPrivilege : userAndPrivileges) { |
156 |
sqlbuilder.grant().role(roleAndPrivilege.getLeft()).privilege(roleAndPrivilege.getRight()); |
157 |
} |
158 |
} |
159 |
|
160 |
List<String> sqls; |
161 |
sqls = sqlbuilder.create_table().toStrings(); |
162 |
sqls.addAll(buildCreateIndexesSQL(this.table,type));
|
163 |
sqls.addAll(sqlbuilder.grant().toStrings()); |
164 |
if( additionalSQLs!=null ) { |
165 |
sqls.addAll(additionalSQLs); |
166 |
} |
167 |
|
168 |
return sqls;
|
169 |
} |
170 |
|
171 |
public boolean performCreateTable(JDBCConnection conn) throws DataException { |
172 |
|
173 |
List<String> sqls = this.getSQLs(); |
174 |
Statement st = null; String currsql = null; |
175 |
try {
|
176 |
st = conn.createStatement(StringUtils.join(sqls, "; "));
|
177 |
for (String sql : sqls) { |
178 |
currsql = sql; JDBCUtils.execute(st, sql); |
179 |
} |
180 |
} catch (SQLException ex) { |
181 |
throw new JDBCSQLException(ex, currsql); |
182 |
} finally {
|
183 |
JDBCUtils.closeQuietly(st); |
184 |
} |
185 |
return true; |
186 |
} |
187 |
} |