Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / fmap / data / feature / db / jdbc / postgresqlbin / PostgresqlBinStore.java @ 23303

History | View | Annotate | Download (4.87 KB)

1
package org.gvsig.fmap.data.feature.db.jdbc.postgresqlbin;
2

    
3
import java.lang.ref.WeakReference;
4
import java.sql.Connection;
5
import java.sql.ResultSet;
6
import java.sql.Statement;
7
import java.util.Locale;
8

    
9
import org.gvsig.fmap.data.DataCollection;
10
import org.gvsig.fmap.data.DataException;
11
import org.gvsig.fmap.data.DataExplorer;
12
import org.gvsig.fmap.data.ReadException;
13
import org.gvsig.fmap.data.feature.Feature;
14
import org.gvsig.fmap.data.feature.FeatureCollection;
15
import org.gvsig.fmap.data.feature.FeatureType;
16
import org.gvsig.fmap.data.feature.db.DBFeatureType;
17
import org.gvsig.fmap.data.feature.db.jdbc.JDBCFeaturesWriter;
18
import org.gvsig.fmap.data.feature.db.jdbc.JDBCResource;
19
import org.gvsig.fmap.data.feature.db.jdbc.JDBCStoreParameters;
20
import org.gvsig.fmap.data.feature.db.jdbc.postgresql.PostgresqlStore;
21
import org.gvsig.metadata.IMetadata;
22
import org.gvsig.tools.exception.BaseException;
23

    
24
public class PostgresqlBinStore extends PostgresqlStore{
25
        public static final String CONNECTION_STRING = "postgresql";
26
        public static String DATASTORE_NAME = "PostgresqlStore";
27
        protected static Locale ukLocale = new Locale("en", "UK"); // English, UK version
28
    PostgresqlBinStoreParameters getParametersPostgresql(){
29
                return (PostgresqlBinStoreParameters)this.parameters;
30
        }
31

    
32
        /* (non-Javadoc)
33
         * @see org.gvsig.fmap.data.feature.db.jdbc.JDBCStore#createResource(org.gvsig.fmap.data.feature.file.dbf.DBFStoreParameters)
34
         */
35
        protected JDBCResource createResource(JDBCStoreParameters params) {
36
                return new PostgresqlBinResource(params);
37
        }
38

    
39
        public String getName() {
40
                return DATASTORE_NAME;
41
        }
42

    
43
        public DataCollection getDataCollection(FeatureType type, String filter, String order) throws ReadException {
44
                try {
45
                        type = this.checkFeatureTypeForCollection(type);
46
                } catch (DataException e) {
47
                        throw new ReadException(this.getName(), e);
48
                }
49

    
50

    
51
                if (useSqlSource ){
52
                        if (filter != null || order != null || type != getDefaultFeatureType()){
53
                                throw new ReadException("Unsuported filter/order in sqlSource mode",this.getName());
54
                        }
55
                }
56
                FeatureCollection coll=null;
57
                if (featureManager == null){
58
                        coll=new PostgresqlBinFeatureCollection(this,type,filter,order);
59
                }else{
60
                        throw new ReadException("Unsuported mode",this.getName());
61
//                        if ((order != null && order != "")){
62
//                                coll=new H2FeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
63
//                        } else{
64
//                                if (filter == null || filter == ""){
65
//                                        coll=new H2FeatureCollectionEditing(featureManager,this,type);
66
//                                } else {
67
//                                        coll=new H2FeatureCollectionEditingFiltered(featureManager,this,type,filter);
68
//                                }
69
//                        }
70
//
71

    
72
                }
73
                this.addObserver(new WeakReference(coll));
74

    
75
                return coll;
76
        }
77

    
78

    
79
        public Feature getFeatureByID(FeatureType featureType2, Object[] featureKey) throws ReadException{
80
                if (useSqlSource){
81
                        throw new ReadException(this.getName(),
82
                                        new UnsupportedOperationException("Unsuported featureByID in sqlSource mode"));
83
                }
84
                ResultSet rs=null;
85
                try{
86
                        this.open();
87

    
88
                        Statement st=this.getConnection().createStatement(
89
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
90
                                        ResultSet.CONCUR_READ_ONLY);
91
                        String sql = this.getSqlSelectPart() +
92
                                        " WHERE "+
93
                                        this.getFilterForID(
94
                                                        (DBFeatureType)this.getDefaultFeatureType(),
95
                                                        featureKey);
96
                        String cursorName = PostgresqlBinStoreUtils.createCursorName();
97
                        String mSql="BEGIN";
98
                        st.execute(mSql);
99
                        mSql="declare " + cursorName + " binary cursor for " + sql;
100
                        st.execute(mSql);
101
                        rs=st.executeQuery("fetch 1 in "+ cursorName);
102
                        if (rs.isLast()) {
103

    
104
                                return null;
105
                        }else{
106
                                if(rs.next()){
107
                                        return this.createFeatureFromResulset(
108
                                                        rs,
109
                                                        (DBFeatureType)this.getDefaultFeatureType());
110
                                }
111

    
112
                        }
113

    
114
                } catch (java.sql.SQLException e) {
115
                        e.printStackTrace();
116
                        throw new ReadException(this.getName(), e);
117
                } finally{
118
                        if (rs != null) {
119
                                try {
120
                                        rs.close();
121
                                } catch (java.sql.SQLException e) {
122
                                        // TODO ?????
123
                                        e.printStackTrace();
124
                                }
125
                        }
126
                }
127
                return null;
128
        }
129

    
130

    
131

    
132
        /* (non-Javadoc)
133
         * @see org.gvsig.fmap.data.feature.db.jdbc.postgresql.PostgresqlStore#getConnection()
134
         */
135
        protected Connection getConnection() throws ReadException {
136
                return super.getConnection();
137
        }
138

    
139
        public boolean canAlterFeatureType() {
140
                return true;
141
        }
142

    
143

    
144

    
145
        public JDBCFeaturesWriter getFeaturesWriter() {
146
//                FeaturesWriter writer = new H2FeaturesWriter();
147
//                writer.init(this);
148
//                return writer;
149
                return null;
150
        }
151

    
152
        public DataExplorer getExplorer() {
153
                // TODO Auto-generated method stub
154
                return null;
155
        }
156

    
157
        public boolean isEditable() {
158
                return false;
159

    
160
//                return super.isEditable();
161
        }
162

    
163

    
164
        public IMetadata getMetadata() throws BaseException {
165
                if (metadata==null){
166
                        IMetadata tmp=super.getMetadata();
167

    
168
                        return tmp;
169
                }else{
170
                        return super.getMetadata();
171
                }
172

    
173
        }
174

    
175

    
176
        protected Feature createFeatureFromResulset(ResultSet rs, DBFeatureType featureType2) throws ReadException {
177
                return new PostgresqlBinFeature(featureType2,this,rs);
178
        }
179

    
180
}