Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / fieldmanagers / JdbcFieldManager.java @ 6609

History | View | Annotate | Download (3.26 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 org.apache.log4j.Logger;
48

    
49
import com.hardcode.gdbms.engine.values.ValueWriter;
50
import com.iver.cit.gvsig.fmap.drivers.XTypes;
51
import com.iver.cit.gvsig.fmap.edition.EditionException;
52

    
53
public class JdbcFieldManager extends AbstractFieldManager {
54
        private static Logger logger = Logger.getLogger(JdbcFieldManager.class.getName()); 
55
        Connection conn;
56

    
57
        String tableName;
58

    
59
        public JdbcFieldManager(Connection conn, String tableName) {
60
                this.conn = conn;
61
                this.tableName = tableName;
62
        }
63

    
64
        public boolean alterTable() throws EditionException {
65
                String sql = "";
66
                Statement st;
67
                try {
68
                        st = conn.createStatement();
69

    
70
                        for (int i = 0; i < fieldCommands.size(); i++) {
71
                                FieldCommand fc = (FieldCommand) fieldCommands.get(i);
72
                                if (fc instanceof AddFieldCommand) {
73
                                        AddFieldCommand addFC = (AddFieldCommand) fc;
74

    
75
                                        sql = "ALTER TABLE "
76
                                                        + tableName
77
                                                        + " ADD COLUMN "
78
                                                        + addFC.getFieldDesc().getFieldName()
79
                                                        + " " 
80
                                                        + XTypes.fieldTypeToString(addFC.getFieldDesc()
81
                                                                        .getFieldType())
82
                                                        + " "
83
                                                        + " DEFAULT " + addFC.getFieldDesc().getDefaultValue().getStringValue(ValueWriter.internalValueWriter)
84
                                                        + ";";
85
                                        st.execute(sql);
86
                                }
87
                                if (fc instanceof RemoveFieldCommand) {
88
                                        RemoveFieldCommand deleteFC = (RemoveFieldCommand) fc;
89
                                        sql = "ALTER TABLE " + tableName + " DROP COLUMN "
90
                                                        + deleteFC.getFieldName() + ";";
91
                                        st.execute(sql);
92
                                }
93
                                if (fc instanceof RenameFieldCommand) {
94
                                        RenameFieldCommand renFC = (RenameFieldCommand) fc;
95
                                        sql = "ALTER TABLE " + tableName + " RENAME COLUMN "
96
                                        + renFC.getAntName() + " TO " + renFC.getNewName() + ";";
97
                                        st.execute(sql);
98
                                }
99
                                logger.debug("Alter Table: " + sql);
100
                        }
101
                        conn.commit();
102
                } catch (SQLException e) {
103
                        e.printStackTrace();
104
                        try {
105
                                conn.rollback();
106
                                conn.setAutoCommit(false);
107
                        } catch (SQLException e1) {
108
                                e1.printStackTrace();
109
                        }
110
                        throw new EditionException(e);
111
                }
112

    
113
                return false;
114
        }
115

    
116
}