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

History | View | Annotate | Download (5.05 KB)

1 43020 jjdelcerro
package org.gvsig.fmap.dal.store.jdbc2.spi.operations;
2
3
import java.sql.Connection;
4
import java.sql.SQLException;
5
import java.sql.Statement;
6 43687 jjdelcerro
import java.util.ArrayList;
7 43020 jjdelcerro
import java.util.List;
8
import org.apache.commons.lang3.tuple.Pair;
9
import org.gvsig.fmap.dal.exception.DataException;
10
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
11
import org.gvsig.fmap.dal.feature.FeatureType;
12
import org.gvsig.fmap.dal.SQLBuilder;
13
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
14
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
15
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
16
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
17 44058 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
18 43114 jjdelcerro
import org.gvsig.fmap.geom.DataTypes;
19 43020 jjdelcerro
20 43377 jjdelcerro
public class CreateTableOperation extends AbstractConnectionWritableOperation {
21 43020 jjdelcerro
22 44058 jjdelcerro
    private final TableReference table;
23 43020 jjdelcerro
    private final FeatureType type;
24
    private final List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges;
25
    private final List<String> additionalSQLs;
26
27
    public CreateTableOperation(
28
            JDBCHelper helper
29
        ) {
30 44058 jjdelcerro
        this(helper, null, null, null, null);
31 43020 jjdelcerro
    }
32
33
    public CreateTableOperation(
34
            JDBCHelper helper,
35 44058 jjdelcerro
            TableReference table,
36 43020 jjdelcerro
            FeatureType type,
37
            List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges,
38
            List<String> additionalSQLs
39
        ) {
40
        super(helper);
41 44058 jjdelcerro
        this.table = table;
42 43020 jjdelcerro
        this.type = type;
43
        this.userAndPrivileges = userAndPrivileges;
44
        this.additionalSQLs = additionalSQLs;
45
    }
46
47
    @Override
48
    public final Object perform(Connection conn) throws DataException {
49
        return this.performCreateTable(conn,
50 44058 jjdelcerro
                table, type, userAndPrivileges, additionalSQLs);
51 43020 jjdelcerro
    }
52
53 43687 jjdelcerro
    protected List<String> buildCreateIndexesSQL(
54 44058 jjdelcerro
            TableReference table,
55 43687 jjdelcerro
            FeatureType type
56
        ) {
57
        ArrayList<String> sqls = new ArrayList<>();
58
59
        for (FeatureAttributeDescriptor attr : type) {
60
            if( attr.isIndexed() ) {
61
                JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
62
                if( attr.getType()==org.gvsig.fmap.dal.DataTypes.GEOMETRY ) {
63
                    sqlbuilder.create_index().spatial();
64
                }
65
                sqlbuilder.create_index().if_not_exist();
66
                sqlbuilder.create_index().name("idx_" + table + "_" + attr.getName());
67
                sqlbuilder.create_index().column(attr.getName());
68 44058 jjdelcerro
                sqlbuilder.create_index().table()
69
                        .database(this.table.getDatabase())
70
                        .schema(this.table.getSchema())
71
                        .name(this.table.getTable()
72
                );
73 43687 jjdelcerro
                sqls.addAll(sqlbuilder.create_index().toStrings());
74
            }
75
        }
76
        return sqls;
77
    }
78
79 43020 jjdelcerro
    public boolean performCreateTable(Connection conn,
80 44058 jjdelcerro
            TableReference table,
81 43020 jjdelcerro
            FeatureType type,
82
            List<Pair<String, SQLBuilder.Privilege>> rolesAndPrivileges,
83
            List<String> additionalSQLs) throws DataException {
84
85
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
86 44058 jjdelcerro
        sqlbuilder.create_table().table()
87
                .database(this.table.getDatabase())
88
                .schema(this.table.getSchema())
89
                .name(this.table.getTable());
90 43020 jjdelcerro
        for (FeatureAttributeDescriptor attr : type) {
91 43114 jjdelcerro
            if( attr.getType()==DataTypes.GEOMETRY ) {
92
                sqlbuilder.create_table().add_geometry_column(
93
                        attr.getName(),
94
                        attr.getGeomType().getType(),
95
                        attr.getGeomType().getSubType(),
96
                        attr.getSRS(),
97 43355 jjdelcerro
                        attr.isIndexed(),
98 43114 jjdelcerro
                        attr.allowNull()
99
                );
100
            } else {
101
                sqlbuilder.create_table().add_column(
102
                        attr.getName(),
103
                        attr.getType(),
104
                        attr.getSize(),
105
                        attr.getPrecision(),
106
                        attr.isPrimaryKey(),
107 43355 jjdelcerro
                        attr.isIndexed(),
108 43114 jjdelcerro
                        attr.allowNull(),
109
                        attr.isAutomatic(),
110
                        attr.getDefaultValue()
111
                );
112
            }
113 43020 jjdelcerro
        }
114
        for (Pair<String, SQLBuilder.Privilege> roleAndPrivilege : rolesAndPrivileges) {
115
            sqlbuilder.grant().role(roleAndPrivilege.getLeft()).privilege(roleAndPrivilege.getRight());
116
        }
117
118
        List<String> sqls;
119
        sqls = sqlbuilder.create_table().toStrings();
120 44058 jjdelcerro
        sqls.addAll(buildCreateIndexesSQL(this.table,type));
121 43020 jjdelcerro
        sqls.addAll(sqlbuilder.grant().toStrings());
122
        sqls.addAll(additionalSQLs);
123
124
        Statement st = null;
125
        try {
126
            st = conn.createStatement();
127
            for (String sql : sqls) {
128
                JDBCUtils.execute(st, sql);
129
            }
130
        } catch (SQLException ex) {
131
            throw new JDBCSQLException(ex);
132
        } finally {
133
            JDBCUtils.closeQuietly(st);
134
        }
135
        return true;
136
    }
137
}