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 / DropTableOperation.java @ 44678

History | View | Annotate | Download (1.83 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 44678 jjdelcerro
import java.util.ArrayList;
7
import java.util.List;
8 43020 jjdelcerro
import org.gvsig.fmap.dal.exception.DataException;
9
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
10
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
11
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
12
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
13 44058 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
14 43020 jjdelcerro
15 43377 jjdelcerro
public class DropTableOperation extends AbstractConnectionWritableOperation {
16 43020 jjdelcerro
17 44058 jjdelcerro
    private final TableReference table;
18 43020 jjdelcerro
19
    public DropTableOperation(
20
            JDBCHelper helper
21
    ) {
22 44058 jjdelcerro
        this(helper, null);
23 43020 jjdelcerro
    }
24
25
    public DropTableOperation(
26
            JDBCHelper helper,
27 44058 jjdelcerro
            TableReference table
28 43020 jjdelcerro
    ) {
29
        super(helper);
30 44058 jjdelcerro
        this.table = table;
31 43020 jjdelcerro
    }
32
33
    @Override
34
    public final Object perform(Connection conn) throws DataException {
35 44678 jjdelcerro
        this.dropTable(conn);
36 43020 jjdelcerro
        return true;
37
    }
38
39 44678 jjdelcerro
    public List<String> getSQLs() {
40 43020 jjdelcerro
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
41 44058 jjdelcerro
        sqlbuilder.drop_table().table()
42
                .database(this.table.getDatabase())
43
                .schema(this.table.getSchema())
44
                .name(this.table.getTable());
45 44678 jjdelcerro
      List<String> sqls = sqlbuilder.drop_table().toStrings();
46
      return sqls;
47
    }
48
49
    public void dropTable(Connection conn) throws DataException {
50 43020 jjdelcerro
51
        Statement st = null;
52
        try {
53
            st = conn.createStatement();
54 44678 jjdelcerro
            for (String sql : this.getSQLs()) {
55 43020 jjdelcerro
                JDBCUtils.execute(st, sql);
56
            }
57
        } catch (SQLException ex) {
58
            throw new JDBCSQLException(ex);
59
        } finally {
60
            JDBCUtils.closeQuietly(st);
61
        }
62
    }
63
}