Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / fieldmanagers / JdbcFieldManager.java @ 6212

History | View | Annotate | Download (2.79 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.edition.fieldmanagers;
42

    
43
import java.sql.Connection;
44
import java.sql.SQLException;
45
import java.sql.Statement;
46

    
47
import com.iver.cit.gvsig.fmap.drivers.XTypes;
48
import com.iver.cit.gvsig.fmap.edition.EditionException;
49

    
50
public class JdbcFieldManager extends AbstractFieldManager {
51
        Connection conn;
52

    
53
        String tableName;
54

    
55
        public JdbcFieldManager(Connection conn, String tableName) {
56
                this.conn = conn;
57
                this.tableName = tableName;
58
        }
59

    
60
        public boolean alterTable() throws EditionException {
61
                String sql;
62
                Statement st;
63
                try {
64
                        st = conn.createStatement();
65

    
66
                        for (int i = 0; i < fieldCommands.size(); i++) {
67
                                FieldCommand fc = (FieldCommand) fieldCommands.get(i);
68
                                if (fc instanceof AddFieldCommand) {
69
                                        AddFieldCommand addFC = (AddFieldCommand) fc;
70

    
71
                                        sql = "ALTER TABLE "
72
                                                        + tableName
73
                                                        + " ADD COLUMN "
74
                                                        + addFC.getFieldDesc().getFieldName()
75
                                                        + XTypes.fieldTypeToString(addFC.getFieldDesc()
76
                                                                        .getFieldType()) + ";";
77
                                        st.execute(sql);
78
                                }
79
                                if (fc instanceof RemoveFieldCommand) {
80
                                        RemoveFieldCommand deleteFC = (RemoveFieldCommand) fc;
81
                                        sql = "ALTER TABLE " + tableName + " DROP COLUMN "
82
                                                        + deleteFC.getFieldName() + ";";
83
                                        st.execute(sql);
84
                                }
85
                                if (fc instanceof RenameFieldCommand) {
86
                                        RenameFieldCommand renFC = (RenameFieldCommand) fc;
87
                                        sql = "ALTER TABLE " + tableName + " RENAME COLUMN "
88
                                        + renFC.getAntName() + " TO " + renFC.getNewName() + ";";
89
                                        st.execute(sql);
90
                                }
91
                        }
92
                        conn.commit();
93
                } catch (SQLException e) {
94
                        e.printStackTrace();
95
                        throw new EditionException(e);
96
                }
97

    
98
                return false;
99
        }
100

    
101
}