Revision 242 trunk/org.gvsig.postgresql/org.gvsig.postgresql.provider/src/main/java/org/gvsig/fmap/dal/store/postgresql/PostgreSQLHelper.java
PostgreSQLHelper.java | ||
---|---|---|
31 | 31 |
package org.gvsig.fmap.dal.store.postgresql; |
32 | 32 |
|
33 | 33 |
import java.sql.Connection; |
34 |
import java.sql.DatabaseMetaData; |
|
34 | 35 |
import java.sql.PreparedStatement; |
35 | 36 |
import java.sql.ResultSet; |
36 | 37 |
import java.sql.ResultSetMetaData; |
... | ... | |
45 | 46 |
import java.util.TreeMap; |
46 | 47 |
import java.util.TreeSet; |
47 | 48 |
|
49 |
import org.apache.commons.lang3.StringUtils; |
|
48 | 50 |
import org.cresques.cts.IProjection; |
49 | 51 |
import org.postgresql.PGResultSetMetaData; |
50 | 52 |
import org.slf4j.Logger; |
... | ... | |
92 | 94 |
|
93 | 95 |
private Map pgSR2SRSID = new TreeMap(); |
94 | 96 |
private Map srsID2pgSR = new TreeMap(); |
95 |
|
|
97 |
|
|
96 | 98 |
private static Properties beforePostgis13 = null; |
97 | 99 |
private int[] postGISVersion = { 0,0,0 }; |
98 | 100 |
private boolean versionSet = false; |
... | ... | |
129 | 131 |
try { |
130 | 132 |
st = conn.createStatement(); |
131 | 133 |
try { |
132 |
rs = JDBCHelper.executeQuery(st, sql);
|
|
134 |
rs = JDBCHelper.executeQuery(st, sql); |
|
133 | 135 |
} catch (java.sql.SQLException e) { |
134 | 136 |
throw new JDBCExecuteSQLException(sql, e); |
135 | 137 |
} |
... | ... | |
350 | 352 |
if (aux != 0) { |
351 | 353 |
return aux; |
352 | 354 |
} |
353 |
|
|
355 |
|
|
354 | 356 |
if (a0.schema == null) { |
355 | 357 |
if (a1.schema == null) { |
356 | 358 |
aux = 0; |
... | ... | |
676 | 678 |
st.setInt(i + 1, Integer.parseInt(abrev[i])); |
677 | 679 |
|
678 | 680 |
try{ |
679 |
rs = JDBCHelper.executeQuery(st,sql);
|
|
680 |
|
|
681 |
rs = JDBCHelper.executeQuery(st,sql); |
|
682 |
|
|
681 | 683 |
} catch (SQLException e){ |
682 | 684 |
throw new JDBCExecutePreparedSQLException(sql, abrev, e); |
683 | 685 |
} |
... | ... | |
814 | 816 |
} |
815 | 817 |
} |
816 | 818 |
|
817 |
|
|
819 |
|
|
818 | 820 |
return result; |
819 | 821 |
} |
820 | 822 |
|
821 | 823 |
protected String getCreateIndexStatement(JDBCNewStoreParameters params,FeatureAttributeDescriptor attr ) { |
822 | 824 |
String indexName = "idx_"+params.getTable()+"_"+attr.getName(); |
823 |
|
|
825 |
|
|
824 | 826 |
String statement = "CREATE "; |
825 | 827 |
if( !attr.allowIndexDuplicateds() ) { |
826 | 828 |
statement += " UNIQUE "; |
... | ... | |
840 | 842 |
} |
841 | 843 |
return statement; |
842 | 844 |
} |
843 |
|
|
845 |
|
|
844 | 846 |
public String getSqlFieldDescription(FeatureAttributeDescriptor attr) |
845 | 847 |
throws DataException { |
846 | 848 |
if (attr.getType() == DataTypes.GEOMETRY){ |
... | ... | |
861 | 863 |
public boolean supportsUnion() { |
862 | 864 |
return true; |
863 | 865 |
} |
864 |
|
|
866 |
|
|
865 | 867 |
public String escapeFieldName(String field) { |
866 | 868 |
/* |
867 |
if (!reservedWord(field) &&
|
|
869 |
if (!reservedWord(field) && |
|
868 | 870 |
field.matches("[a-z][a-z0-9_]*")) { |
869 | 871 |
return field; |
870 | 872 |
} |
... | ... | |
872 | 874 |
String quote = getIdentifierQuoteString(); |
873 | 875 |
return quote + field + quote; |
874 | 876 |
} |
875 |
|
|
877 |
|
|
876 | 878 |
protected EditableFeatureAttributeDescriptor createAttributeFromJDBCNativeType( |
877 | 879 |
EditableFeatureType fType, ResultSetMetaData rsMetadata, int colIndex) |
878 | 880 |
throws SQLException { |
... | ... | |
902 | 904 |
|
903 | 905 |
return super.dalValueToJDBC(attributeDescriptor, object); |
904 | 906 |
} |
905 |
|
|
907 |
|
|
906 | 908 |
// ======================================= |
907 |
|
|
908 | 909 |
|
910 |
|
|
909 | 911 |
public String getFunctionName(String newFuncName) { |
910 |
|
|
912 |
|
|
911 | 913 |
if (!versionSet) { |
912 | 914 |
postGISVersion = getPostgisVersion(); |
913 | 915 |
versionSet = true; |
... | ... | |
916 | 918 |
} |
917 | 919 |
|
918 | 920 |
private String getFunctionNameForVersion(String newFuncName, int[] pv) { |
919 |
|
|
921 |
|
|
920 | 922 |
if (newFuncName == null || pv == null) { |
921 | 923 |
return newFuncName; |
922 | 924 |
} |
923 |
|
|
925 |
|
|
924 | 926 |
if (pv.length < 2) { |
925 | 927 |
// cannot compare |
926 | 928 |
return newFuncName; |
927 | 929 |
} |
928 |
|
|
930 |
|
|
929 | 931 |
if (pv[0] > 1) { |
930 | 932 |
return newFuncName; |
931 | 933 |
} |
932 |
|
|
934 |
|
|
933 | 935 |
if (pv[0] == 1 && pv[1] >= 3) { |
934 | 936 |
return newFuncName; |
935 | 937 |
} |
936 |
|
|
938 |
|
|
937 | 939 |
Properties pp = this.getBeforePostgis13Properties(); |
938 | 940 |
String k = newFuncName.toLowerCase(); |
939 | 941 |
String v = pp.getProperty(k); |
... | ... | |
955 | 957 |
try { |
956 | 958 |
conn = this.getConnection(); |
957 | 959 |
st = conn.createStatement(); |
958 |
rs = JDBCHelper.executeQuery(st,sql);
|
|
960 |
rs = JDBCHelper.executeQuery(st,sql); |
|
959 | 961 |
rs.next(); |
960 | 962 |
v = rs.getString(1); |
961 | 963 |
if (v == null) { |
... | ... | |
982 | 984 |
} |
983 | 985 |
return resp; |
984 | 986 |
} |
985 |
|
|
986 |
|
|
987 |
|
|
987 |
|
|
988 |
|
|
989 |
|
|
988 | 990 |
protected Properties getBeforePostgis13Properties() { |
989 | 991 |
if (beforePostgis13 == null) { |
990 |
|
|
992 |
|
|
991 | 993 |
beforePostgis13 = new Properties(); |
992 | 994 |
// Left side MUST be in lower case |
993 | 995 |
// Right side will be used if Postgis version < 1.3 |
... | ... | |
1000 | 1002 |
} |
1001 | 1003 |
return beforePostgis13; |
1002 | 1004 |
} |
1003 |
|
|
1004 | 1005 |
|
1006 |
/* (non-Javadoc) |
|
1007 |
* @see org.gvsig.fmap.dal.store.jdbc.JDBCHelper#getAttributeFromJDBC(org.gvsig.fmap.dal.feature.EditableFeatureType, java.sql.Connection, java.sql.ResultSetMetaData, int) |
|
1008 |
*/ |
|
1009 |
@Override |
|
1010 |
protected EditableFeatureAttributeDescriptor getAttributeFromJDBC(EditableFeatureType fType, Connection conn, |
|
1011 |
ResultSetMetaData rsMetadata, int colIndex) throws JDBCException { |
|
1012 |
EditableFeatureAttributeDescriptor attr = super.getAttributeFromJDBC(fType, conn, rsMetadata, colIndex); |
|
1013 |
// El siguiente c?digo sirve para recuperar loas valores por defecto de la |
|
1014 |
// base de datos y asign?rselos al atributo, pero de momento lo comentarizamos |
|
1015 |
// porque se produce alg?n comportamiento extra?o que debemos valorar. |
|
1016 |
// if(rsMetadata instanceof PGResultSetMetaData){ |
|
1017 |
// try { |
|
1018 |
// PGResultSetMetaData pgResultSetMetaData = (PGResultSetMetaData) rsMetadata; |
|
1019 |
// String tableName = pgResultSetMetaData.getBaseTableName(colIndex); |
|
1020 |
// String columnName = pgResultSetMetaData.getBaseColumnName(colIndex); |
|
1021 |
// String defaultValue = ""; |
|
1022 |
// defaultValue = getColumnDefaultValue(conn, tableName, columnName); |
|
1023 |
// |
|
1024 |
// if (!StringUtils.isEmpty(defaultValue)) { |
|
1025 |
// |
|
1026 |
// Object coerce = null; |
|
1027 |
// try { |
|
1028 |
// coerce = attr.coerce(defaultValue); |
|
1029 |
// } catch (Throwable e) { |
|
1030 |
// // Do nothing |
|
1031 |
// } |
|
1032 |
// attr.setDefaultValue(coerce); |
|
1033 |
// } |
|
1034 |
// |
|
1035 |
// } catch (SQLException e1) { |
|
1036 |
// throw new JDBCSQLException(e1); |
|
1037 |
// } |
|
1038 |
// } |
|
1039 |
return attr; |
|
1040 |
} |
|
1005 | 1041 |
|
1042 |
private String getColumnDefaultValue(Connection conn, String tableName, String columnName) { |
|
1043 |
String value = ""; |
|
1044 |
try { |
|
1045 |
DatabaseMetaData md = conn.getMetaData(); |
|
1046 |
String catalog = conn.getCatalog(); |
|
1047 |
String schema = getDefaultSchema(conn); |
|
1048 |
ResultSet rs = md.getColumns(catalog, schema, tableName, columnName); |
|
1049 |
|
|
1050 |
if (rs.next()) { |
|
1051 |
value = rs.getString("COLUMN_DEF"); |
|
1052 |
} |
|
1053 |
logger.info("Default value of column '" + columnName + "' is " + value); |
|
1054 |
if (!StringUtils.isEmpty(value)) { |
|
1055 |
value = value.split(":")[0]; |
|
1056 |
if (value.startsWith("'") && value.endsWith("'")) { |
|
1057 |
value = (String) value.subSequence(value.indexOf("'") + 1, value.lastIndexOf("'")); |
|
1058 |
} else { |
|
1059 |
try { |
|
1060 |
Double.parseDouble(value); |
|
1061 |
} catch (Exception e) { |
|
1062 |
return ""; |
|
1063 |
} |
|
1064 |
} |
|
1065 |
} |
|
1066 |
} catch (Throwable e) { |
|
1067 |
logger.info("Can't get default value from column '" + columnName + "'", e); |
|
1068 |
} |
|
1069 |
return value; |
|
1070 |
|
|
1071 |
} |
|
1006 | 1072 |
} |
Also available in: Unified diff