Statistics
| Revision:

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 @ 46315

History | View | Annotate | Download (6.71 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.tuple.Pair;
31
import org.gvsig.expressionevaluator.ExpressionUtils;
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.JDBCConnection;
40
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
41
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
42
import org.gvsig.fmap.geom.DataTypes;
43

    
44
public class CreateTableOperation extends AbstractConnectionWritableOperation {
45

    
46
    protected final TableReference table;
47
    protected FeatureType type;
48
    private final List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges;
49
    private final List<String> additionalSQLs;
50

    
51
    public CreateTableOperation(
52
            JDBCHelper helper
53
        ) {
54
        this(helper, null, null, null, null);
55
    }
56
    
57
    public CreateTableOperation(
58
            JDBCHelper helper,
59
            TableReference table,
60
            FeatureType type,
61
            List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges,
62
            List<String> additionalSQLs
63
        ) {
64
        super(helper);
65
        this.table = table;
66
        this.type = type;
67
        this.userAndPrivileges = userAndPrivileges;
68
        this.additionalSQLs = additionalSQLs;
69
    }
70

    
71
    public FeatureType getType() {
72
        return type;
73
    }
74

    
75
    @Override
76
    public final Object perform(JDBCConnection conn) throws DataException {
77
        return this.performCreateTable(conn);
78
    }
79

    
80
    protected List<String> buildCreateIndexesSQL(
81
            TableReference table,
82
            FeatureType type
83
        ) {
84
        ArrayList<String> sqls = new ArrayList<>();
85
        
86
        for (FeatureAttributeDescriptor attr : type) {
87
            if( attr.isIndexed() ) {
88
                JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
89
                if( attr.getType()==org.gvsig.fmap.dal.DataTypes.GEOMETRY ) {
90
                    sqlbuilder.create_index().spatial();
91
                }
92
                sqlbuilder.create_index().if_not_exist();
93
                sqlbuilder.create_index().name("idx_" + table.toString().replace(".", "_") + "_" + attr.getName());
94
                sqlbuilder.create_index().column(attr.getName());
95
                sqlbuilder.create_index().table()
96
                        .database(this.table.getDatabase())
97
                        .schema(this.table.getSchema())
98
                        .name(this.table.getTable()
99
                );
100
                if(!attr.allowIndexDuplicateds()){
101
                    sqlbuilder.create_index().unique();
102
                }
103
                sqls.addAll(sqlbuilder.create_index().toStrings());
104
            }
105
        }
106
        return sqls;
107
    }
108
    
109
    public List<String> getSQLs() throws DataException {
110

    
111
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
112
        sqlbuilder.create_table().table()
113
                .database(table.getDatabase())
114
                .schema(table.getSchema())
115
                .name(table.getTable());
116
        for (FeatureAttributeDescriptor attr : type) {
117
            if( attr.isComputed() ) {
118
                continue;
119
            }
120
            if( attr.getType()==DataTypes.GEOMETRY ) {
121
                sqlbuilder.create_table().add_geometry_column(
122
                        attr.getName(),
123
                        attr.getGeomType().getType(),
124
                        attr.getGeomType().getSubType(),
125
                        attr.getSRS(),
126
                        attr.isIndexed(),
127
                        attr.allowNull()
128
                );
129
            } else {
130
                Object defaultValue = attr.getDefaultValue();
131
                if( defaultValue instanceof CharSequence ) {
132
                    if( ExpressionUtils.isDynamicText(defaultValue.toString())) {
133
                        defaultValue = null;
134
                    }
135
                }
136
                sqlbuilder.create_table().add_column(
137
                        attr.getName(),
138
                        attr.getType(),
139
                        attr.getSize(),
140
                        attr.getPrecision(),
141
                        attr.getScale(),
142
                        attr.isPrimaryKey(),
143
                        attr.isIndexed(),
144
                        attr.allowNull(),
145
                        attr.isAutomatic(),
146
                        defaultValue
147
                );
148
            }
149
        }
150
        if( userAndPrivileges!=null ) {
151
          for (Pair<String, SQLBuilder.Privilege> roleAndPrivilege : userAndPrivileges) {
152
              sqlbuilder.grant().role(roleAndPrivilege.getLeft()).privilege(roleAndPrivilege.getRight());
153
          }
154
        }
155

    
156
        List<String> sqls;
157
        sqls = sqlbuilder.create_table().toStrings();
158
        sqls.addAll(buildCreateIndexesSQL(this.table,type));
159
        sqls.addAll(sqlbuilder.grant().toStrings());
160
        if( additionalSQLs!=null ) {
161
          sqls.addAll(additionalSQLs);
162
        }
163
        
164
        return sqls;
165
    }
166
    
167
    public boolean performCreateTable(JDBCConnection conn) throws DataException {
168
      
169
        List<String> sqls = this.getSQLs();
170
        Statement st = null; String currsql = null;
171
        try {
172
            st = conn.createStatement();
173
            for (String sql : sqls) {
174
                currsql = sql; JDBCUtils.execute(st, sql);
175
            }
176
        } catch (SQLException ex) {
177
            throw new JDBCSQLException(ex, currsql);
178
        } finally {
179
            JDBCUtils.closeQuietly(st);
180
        }
181
        return true;
182
    }
183
}