001: package org.geotools.data.jdbc;
002:
003: import java.io.IOException;
004: import java.sql.Connection;
005: import java.sql.SQLException;
006: import java.sql.Statement;
007: import java.util.List;
008: import java.util.logging.Logger;
009:
010: import javax.sql.DataSource;
011:
012: import org.geotools.data.Transaction;
013: import org.geotools.data.store.ContentDataStore;
014: import org.geotools.data.store.ContentEntry;
015: import org.geotools.data.store.ContentFeatureSource;
016: import org.geotools.data.store.ContentState;
017: import org.geotools.feature.FeatureType;
018: import org.opengis.feature.simple.SimpleTypeFactory;
019:
020: public class JDBCDataStore extends ContentDataStore {
021: /**
022: * logging instance
023: */
024: static final Logger LOGGER = org.geotools.util.logging.Logging
025: .getLogger("org.geotools.data.jdbc");
026: /**
027: * data source
028: */
029: protected DataSource dataSource;
030: /**
031: * The database schema.
032: */
033: protected String databaseSchema;
034:
035: public void setDatabaseSchema(String databaseSchema) {
036: this .databaseSchema = databaseSchema;
037: }
038:
039: public String getDatabaseSchema() {
040: return databaseSchema;
041: }
042:
043: public void setDataSource(DataSource dataSource) {
044: this .dataSource = dataSource;
045: }
046:
047: public DataSource getDataSource() {
048: return dataSource;
049: }
050:
051: public Logger getLogger() {
052: return LOGGER;
053: }
054:
055: public void createSchema(final FeatureType featureType)
056: throws IOException {
057:
058: if (entry(name(featureType.getTypeName())) != null) {
059: String msg = "Schema '" + featureType.getTypeName()
060: + "' already exists";
061: throw new IllegalArgumentException(msg);
062: }
063:
064: //create a content entry for the type
065: ContentEntry entry = new ContentEntry(this , name(featureType
066: .getTypeName()));
067:
068: //cache the feature type
069: entry.getState(Transaction.AUTO_COMMIT).setMemberType(
070: featureType);
071:
072: JDBCUtils.statement(this , new JDBCRunnable() {
073: public Object run(Statement st) throws IOException,
074: SQLException {
075: SQLBuilder sqlBuilder = new SQLBuilder(
076: JDBCDataStore.this );
077: st.execute(sqlBuilder.createTable(featureType));
078: return null;
079: }
080: });
081: }
082:
083: public PrimaryKey getPrimaryKey(ContentEntry entry)
084: throws IOException {
085: try {
086: return JDBCUtils.primaryKey(entry.getName(), this );
087: } catch (Exception e) {
088: throw (IOException) new IOException().initCause(e);
089: }
090: }
091:
092: public PrimaryKey getPrimaryKey(FeatureType featureType)
093: throws IOException {
094: return getPrimaryKey(ensureEntry((name(featureType
095: .getTypeName()))));
096: }
097:
098: /**
099: * @return A type builder for building feature types.
100: */
101: protected JDBCTypeBuilder createTypeBuilder() {
102: return new JDBCTypeBuilder(getTypeFactory());
103: }
104:
105: /**
106: * @return A type builder for building feature types.
107: */
108: protected JDBCTypeBuilder createTypeBuilder(
109: SimpleTypeFactory typeFactory) {
110: return new JDBCTypeBuilder(typeFactory);
111: }
112:
113: /**
114: * @return A sql builder for encoding sql statements.
115: */
116: protected SQLBuilder createSQLBuilder() {
117: return new SQLBuilder(this );
118: }
119:
120: /**
121: * Convenience method for grabbing a new connection.
122: * <p>
123: * Callers of this method should close the connection when done with it.
124: * </p>.
125: *
126: */
127: public final Connection connection() {
128: try {
129: return getDataSource().getConnection();
130: } catch (SQLException e) {
131: throw new RuntimeException("Unable to obtain connection", e);
132: }
133: }
134:
135: protected ContentFeatureSource createFeatureSource(
136: ContentEntry entry) throws IOException {
137: return new JDBCFeatureSource(entry);
138: }
139:
140: protected ContentState createContentState(ContentEntry entry) {
141: return new JDBCState(entry);
142: }
143:
144: protected List createTypeNames() throws IOException {
145: try {
146: return JDBCUtils.typeNames(this );
147: } catch (Exception e) {
148: throw (IOException) new IOException().initCause(e);
149: }
150: }
151:
152: }
|