Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / engine / data / db / DBDataSourceAdapter.java @ 1956

History | View | Annotate | Download (3.78 KB)

1
package com.hardcode.gdbms.engine.data.db;
2

    
3
import com.hardcode.gdbms.engine.data.AbstractDataSource;
4
import com.hardcode.gdbms.engine.data.HasProperties;
5
import com.hardcode.gdbms.engine.data.driver.DBDriver;
6
import com.hardcode.gdbms.engine.data.driver.DriverCommons;
7
import com.hardcode.gdbms.engine.data.driver.DriverException;
8
import com.hardcode.gdbms.engine.data.driver.DriverInfo;
9
import com.hardcode.gdbms.engine.values.Value;
10

    
11
import java.sql.Connection;
12
import java.sql.SQLException;
13

    
14

    
15
/**
16
 * Adaptador de la interfaz DBDriver a la interfaz DataSource. Adapta las
17
 * interfaces de los drivers de base de datos a la interfaz DataSource.
18
 *
19
 * @author Fernando Gonz?lez Cort?s
20
 */
21
public abstract class DBDataSourceAdapter extends AbstractDataSource
22
    implements DBDataSource {
23
    /*
24
     * datos de la conexi?n
25
     */
26
    protected String host;
27
    protected int port;
28
    protected String dbName;
29
    protected String user;
30
    protected String password;
31

    
32
    //driver de base de datos
33
    protected DBDriver driver;
34

    
35
    //objetos jdbc
36
    protected Connection con;
37

    
38
    //Instrucci?n sql completa que representa el dataSource
39
    protected String sql;
40

    
41
    /**
42
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getRowCount()
43
     */
44
    public long getRowCount() throws DriverException {
45
        return getDriver().getRowCount();
46
    }
47

    
48
    /**
49
     * Establece el driver
50
     *
51
     * @param driver The driver to set.
52
     */
53
    public void setDriver(DBDriver driver) {
54
        this.driver = driver;
55

    
56
        if (driver instanceof DriverCommons) {
57
            if (((DriverCommons) driver).getDriverProperties() != null) {
58
                sourceProperties.putAll(((DriverCommons) driver).getDriverProperties());
59
            }
60
        }
61
    }
62

    
63
    /**
64
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldCount()
65
     */
66
    public int getFieldCount() throws DriverException {
67
        return getDriver().getFieldCount();
68
    }
69

    
70
    /**
71
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldName(int)
72
     */
73
    public String getFieldName(int fieldId) throws DriverException {
74
        return getDriver().getFieldName(fieldId);
75
    }
76

    
77
    /**
78
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
79
     */
80
    public int getFieldType(int i) throws DriverException {
81
        return getDriver().getFieldType(i);
82
    }
83

    
84
    /**
85
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldValue(long,
86
     *      int)
87
     */
88
    public Value getFieldValue(long rowIndex, int fieldId)
89
        throws DriverException {
90
        return getDriver().getFieldValue(rowIndex, fieldId);
91
    }
92

    
93
    /**
94
     * DOCUMENT ME!
95
     *
96
     * @return
97
     */
98
    protected DBDriver getDriver() {
99
        return driver;
100
    }
101

    
102
    /**
103
     * @see com.hardcode.gdbms.engine.data.DataSource#getProperty(java.lang.String)
104
     */
105
    public String getProperty(String propertyName) {
106
        return (String) sourceProperties.get(propertyName);
107
    }
108

    
109
    /**
110
     * DOCUMENT ME!
111
     *
112
     * @param sourceInfo
113
     */
114
    public void setSourceInfo(DriverInfo sourceInfo) {
115
        super.setSourceInfo(sourceInfo);
116

    
117
        this.host = ((DBDriverInfo) sourceInfo).host;
118
        this.port = ((DBDriverInfo) sourceInfo).port;
119
        this.dbName = ((DBDriverInfo) sourceInfo).dbName;
120
        this.user = ((DBDriverInfo) sourceInfo).user;
121
        this.password = ((DBDriverInfo) sourceInfo).password;
122
    }
123

    
124
    /**
125
     * @see com.hardcode.gdbms.engine.data.db.DBDataSource#execute(java.lang.String)
126
     */
127
    public void execute(String sql) throws DriverException {
128
        try {
129
            getDriver().execute(driver.getConnection(host, port, dbName, user, password), sql, (HasProperties) this);
130
        } catch (SQLException e) {
131
            throw new DriverException(e);
132
        }
133
    }
134
}