001: package org.geotools.data.postgis;
002:
003: import java.sql.Connection;
004: import java.sql.ResultSet;
005: import java.sql.SQLException;
006: import java.sql.Statement;
007: import java.util.logging.Level;
008: import java.util.logging.Logger;
009:
010: import org.geotools.data.Transaction;
011: import org.geotools.data.jdbc.JDBCUtils;
012:
013: public class PostgisDBInfo {
014:
015: /** The logger for the postgis module. */
016: protected static final Logger LOGGER = org.geotools.util.logging.Logging
017: .getLogger("org.geotools.data.postgis");
018:
019: String postgisVersion;
020: int postgisMajorVersion;
021: int postgisMinorVersion;
022:
023: String postgresVersion;
024: int postgresMajorVersion;
025: int postgresMinorVersion;
026:
027: boolean schemaEnabled = true;
028: boolean byteaEnabled = false;
029: boolean geosEnabled = false;
030:
031: /**
032: * Creates a new DBInfo object and populates its values.
033: */
034: public PostgisDBInfo(Connection conn) {
035: //get postgis version
036: try {
037: String sqlStatement = "SELECT postgis_version();";
038:
039: Statement statement = conn.createStatement();
040: ResultSet result = statement.executeQuery(sqlStatement);
041:
042: if (result.next()) {
043: postgisVersion = result.getString(1);
044: LOGGER.fine("PostGIS version is " + postgisVersion);
045:
046: int[] versionNumbers;
047:
048: try {
049: String[] values = postgisVersion.trim().split(" ");
050: String[] versionNumbersStr = values[0].trim()
051: .split("\\.");
052: versionNumbers = new int[versionNumbersStr.length];
053:
054: for (int i = 0; i < versionNumbers.length; i++) {
055: versionNumbers[i] = Integer
056: .parseInt(versionNumbersStr[i]);
057: }
058:
059: postgisMajorVersion = versionNumbers[0];
060: postgisMinorVersion = versionNumbers[1];
061:
062: // bytea function has been introduced in 0.7.2
063: if ((postgisMajorVersion > 0)
064: || (postgisMinorVersion > 7)
065: || ((versionNumbers.length > 2) && (versionNumbers[2] >= 2))) {
066: byteaEnabled = true;
067: }
068: } catch (Exception e) {
069: LOGGER
070: .log(
071: Level.WARNING,
072: "Exception occurred while parsing the version number.",
073: e);
074: }
075:
076: if (postgisVersion.indexOf("USE_GEOS=1") != -1) {
077: this .geosEnabled = true;
078: } else {
079: //warn about not using GEOS
080: LOGGER
081: .warning("GEOS is NOT enabled. This will result in limited functionality and performance.");
082: }
083:
084: //check postgres version to determine if schemas should be
085: // enabled, pre 7.3 -> no
086: postgresVersion = conn.getMetaData()
087: .getDatabaseProductVersion();
088: LOGGER.fine("Postgres version is " + postgresVersion);
089: postgresMajorVersion = conn.getMetaData()
090: .getDatabaseMajorVersion();
091: postgresMinorVersion = conn.getMetaData()
092: .getDatabaseMinorVersion();
093:
094: if (postgresMajorVersion < 7
095: || (postgresMajorVersion == 7 && postgresMinorVersion < 3)) {
096: schemaEnabled = false; //pre 7.3
097: }
098: }
099: } catch (SQLException sqle) {
100: String message = sqle.getMessage();
101: LOGGER.severe(message);
102: } finally {
103: JDBCUtils.close(conn, Transaction.AUTO_COMMIT, null);
104: }
105:
106: }
107:
108: public String getVersion() {
109: return postgisVersion;
110: }
111:
112: public int getMajorVersion() {
113: return postgisMajorVersion;
114: }
115:
116: public int getMinorVersion() {
117: return postgisMinorVersion;
118: }
119:
120: public String getPostgresVersion() {
121: return postgresVersion;
122: }
123:
124: public int getPostgresMajorVersion() {
125: return postgresMajorVersion;
126: }
127:
128: public int getPostgresMinorVersion() {
129: return postgresMinorVersion;
130: }
131:
132: public boolean isSchemaEnabled() {
133: return schemaEnabled;
134: }
135:
136: public boolean isGeosEnabled() {
137: return geosEnabled;
138: }
139:
140: public boolean isByteaEnabled() {
141: return byteaEnabled;
142: }
143: }
|