svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / writers / JdbcWriter.java @ 6259
History | View | Annotate | Download (4.03 KB)
1 |
package com.iver.cit.gvsig.fmap.edition.writers; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.ResultSet; |
5 |
import java.sql.ResultSetMetaData; |
6 |
import java.sql.SQLException; |
7 |
|
8 |
import com.hardcode.gdbms.engine.values.Value; |
9 |
import com.iver.cit.gvsig.fmap.core.IRow; |
10 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
11 |
import com.iver.cit.gvsig.fmap.drivers.XTypes; |
12 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
13 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
14 |
|
15 |
public class JdbcWriter extends AbstractWriter { |
16 |
Connection conn;
|
17 |
ResultSet rs;
|
18 |
Value[] record;
|
19 |
int numRecord;
|
20 |
|
21 |
private boolean bWriteAll; |
22 |
private boolean bCreateTable; |
23 |
|
24 |
private ResultSetMetaData metaData = null; |
25 |
|
26 |
public JdbcWriter(){
|
27 |
} |
28 |
public void initialize(Connection conn, ResultSet rs) throws SQLException{ |
29 |
this.conn = conn;
|
30 |
this.rs = rs;
|
31 |
metaData = rs.getMetaData(); |
32 |
System.out.println("INICIO CONEXI?N DE ESCRITURA"); |
33 |
} |
34 |
public void preProcess() throws EditionException { |
35 |
numRecord = 0;
|
36 |
try {
|
37 |
conn.setAutoCommit(false);
|
38 |
} catch (SQLException e) { |
39 |
e.printStackTrace(); |
40 |
throw new EditionException(e); |
41 |
} |
42 |
/* Statement st = conn.createStatement();
|
43 |
|
44 |
if (bCreateTable) {
|
45 |
try {
|
46 |
st.execute("DROP TABLE " + lyrDef.getTableName() + ";");
|
47 |
} catch (SQLException e1) {
|
48 |
// Si no existe la tabla, no hay que borrarla.
|
49 |
}
|
50 |
|
51 |
String sqlCreate = PostGIS.getSqlCreateSpatialTable(lyrDef,
|
52 |
lyrDef.getFieldsDesc(), true);
|
53 |
System.out.println("sqlCreate =" + sqlCreate);
|
54 |
st.execute(sqlCreate);
|
55 |
} */
|
56 |
|
57 |
} |
58 |
|
59 |
public void process(IRowEdited editedRow) throws EditionException { |
60 |
IRow row = (IRow) editedRow.getLinkedRow(); |
61 |
|
62 |
try {
|
63 |
System.out.println("Intento escribir el registro " + |
64 |
numRecord + " de la capa " + metaData.getTableName(1)); |
65 |
switch (editedRow.getStatus())
|
66 |
{ |
67 |
case IRowEdited.STATUS_ADDED:
|
68 |
record=row.getAttributes(); |
69 |
rs.moveToInsertRow(); |
70 |
for (int i=0; i < record.length; i++) |
71 |
XTypes.updateValue(rs, i, record[i]); |
72 |
rs.insertRow(); |
73 |
break;
|
74 |
case IRowEdited.STATUS_MODIFIED:
|
75 |
record=row.getAttributes(); |
76 |
rs.absolute(editedRow.getIndex()+1);
|
77 |
for (int i=0; i < record.length; i++) |
78 |
XTypes.updateValue(rs, i, record[i]); |
79 |
rs.updateRow(); |
80 |
break;
|
81 |
case IRowEdited.STATUS_ORIGINAL:
|
82 |
if (bWriteAll)
|
83 |
{ |
84 |
record=row.getAttributes(); |
85 |
rs.moveToInsertRow(); |
86 |
for (int i=0; i < record.length; i++) |
87 |
XTypes.updateValue(rs, i, record[i]); |
88 |
rs.insertRow(); |
89 |
} |
90 |
break;
|
91 |
case IRowEdited.STATUS_DELETED:
|
92 |
rs.absolute(editedRow.getIndex()+1);
|
93 |
rs.deleteRow(); |
94 |
break;
|
95 |
} |
96 |
numRecord++; |
97 |
rs.next(); |
98 |
|
99 |
} catch (SQLException e) { |
100 |
System.out.println(e.getSQLState() + " " + e.getMessage()); |
101 |
try {
|
102 |
conn.rollback(); |
103 |
} catch (SQLException e1) { |
104 |
e1.printStackTrace(); |
105 |
throw new EditionException(e); |
106 |
} |
107 |
|
108 |
throw new EditionException(e.getMessage(),e); |
109 |
} |
110 |
|
111 |
} |
112 |
|
113 |
public void postProcess() throws EditionException { |
114 |
try {
|
115 |
conn.commit(); |
116 |
} catch (SQLException e) { |
117 |
try {
|
118 |
conn.rollback(); |
119 |
} catch (SQLException e1) { |
120 |
e1.printStackTrace(); |
121 |
throw new EditionException(e); |
122 |
} |
123 |
e.printStackTrace(); |
124 |
throw new EditionException(e); |
125 |
} |
126 |
} |
127 |
|
128 |
public boolean canWriteAttribute(int sqlType) { |
129 |
return true; |
130 |
} |
131 |
|
132 |
public String getName() { |
133 |
return "JDBC Writer"; |
134 |
} |
135 |
/**
|
136 |
* @return Returns the bCreateTable.
|
137 |
*/
|
138 |
public boolean isCreateTable() { |
139 |
return bCreateTable;
|
140 |
} |
141 |
|
142 |
/**
|
143 |
* @param createTable
|
144 |
* The bCreateTable to set.
|
145 |
*/
|
146 |
public void setCreateTable(boolean createTable) { |
147 |
bCreateTable = createTable; |
148 |
} |
149 |
|
150 |
/**
|
151 |
* @return Returns the bWriteAll.
|
152 |
*/
|
153 |
public boolean isWriteAll() { |
154 |
return bWriteAll;
|
155 |
} |
156 |
|
157 |
/**
|
158 |
* @param writeAll
|
159 |
* The bWriteAll to set.
|
160 |
*/
|
161 |
public void setWriteAll(boolean writeAll) { |
162 |
bWriteAll = writeAll; |
163 |
} |
164 |
|
165 |
public void close() throws SQLException |
166 |
{ |
167 |
rs.close(); |
168 |
// conn.close();
|
169 |
System.out.println("CIERRO CONEXI?N DE ESCRITURA"); |
170 |
} |
171 |
} |