| 1 | |
package liquibase.database.core; |
| 2 | |
|
| 3 | |
import liquibase.database.AbstractDatabase; |
| 4 | |
import liquibase.database.DatabaseConnection; |
| 5 | |
import liquibase.exception.DatabaseException; |
| 6 | |
import liquibase.executor.ExecutorService; |
| 7 | |
import liquibase.logging.LogFactory; |
| 8 | |
import liquibase.statement.core.RawSqlStatement; |
| 9 | |
import liquibase.util.StringUtils; |
| 10 | |
|
| 11 | |
import java.util.ArrayList; |
| 12 | |
import java.util.HashSet; |
| 13 | |
import java.util.List; |
| 14 | |
import java.util.Set; |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
public class PostgresDatabase extends AbstractDatabase { |
| 20 | |
public static final String PRODUCT_NAME = "PostgreSQL"; |
| 21 | |
|
| 22 | 9 | private Set<String> systemTablesAndViews = new HashSet<String>(); |
| 23 | |
|
| 24 | |
private String defaultDatabaseSchemaName; |
| 25 | |
|
| 26 | 9 | public PostgresDatabase() { |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | 9 | } |
| 76 | |
|
| 77 | |
public String getTypeName() { |
| 78 | 55 | return "postgresql"; |
| 79 | |
} |
| 80 | |
|
| 81 | |
@Override |
| 82 | |
public Set<String> getSystemTablesAndViews() { |
| 83 | 0 | return systemTablesAndViews; |
| 84 | |
} |
| 85 | |
|
| 86 | |
public int getPriority() { |
| 87 | 0 | return PRIORITY_DEFAULT; |
| 88 | |
} |
| 89 | |
|
| 90 | |
public boolean supportsInitiallyDeferrableColumns() { |
| 91 | 1 | return true; |
| 92 | |
} |
| 93 | |
|
| 94 | |
public boolean isCorrectDatabaseImplementation(DatabaseConnection conn) throws DatabaseException { |
| 95 | 1 | return PRODUCT_NAME.equalsIgnoreCase(conn.getDatabaseProductName()); |
| 96 | |
} |
| 97 | |
|
| 98 | |
public String getDefaultDriver(String url) { |
| 99 | 0 | if (url.startsWith("jdbc:postgresql:")) { |
| 100 | 0 | return "org.postgresql.Driver"; |
| 101 | |
} |
| 102 | 0 | return null; |
| 103 | |
} |
| 104 | |
|
| 105 | |
@Override |
| 106 | |
public boolean supportsSequences() { |
| 107 | 18 | return true; |
| 108 | |
} |
| 109 | |
|
| 110 | |
public String getCurrentDateTimeFunction() { |
| 111 | 1 | if (currentDateTimeFunction != null) { |
| 112 | 0 | return currentDateTimeFunction; |
| 113 | |
} |
| 114 | |
|
| 115 | 1 | return "NOW()"; |
| 116 | |
} |
| 117 | |
|
| 118 | |
@Override |
| 119 | |
protected String getDefaultDatabaseSchemaName() throws DatabaseException { |
| 120 | |
|
| 121 | 0 | if (defaultDatabaseSchemaName == null) { |
| 122 | |
try { |
| 123 | 0 | List<String> searchPaths = getSearchPaths(); |
| 124 | 0 | if (searchPaths != null && searchPaths.size() > 0) { |
| 125 | 0 | for (String searchPath : searchPaths) { |
| 126 | 0 | if (searchPath != null && searchPath.length() > 0) { |
| 127 | 0 | defaultDatabaseSchemaName = searchPath; |
| 128 | |
|
| 129 | 0 | if (defaultDatabaseSchemaName.equals("$user") |
| 130 | |
&& getConnection().getConnectionUserName() != null) { |
| 131 | 0 | if (!schemaExists(getConnection().getConnectionUserName())) { |
| 132 | 0 | defaultDatabaseSchemaName = null; |
| 133 | |
} else { |
| 134 | 0 | defaultDatabaseSchemaName = getConnection().getConnectionUserName(); |
| 135 | |
} |
| 136 | |
} |
| 137 | |
|
| 138 | 0 | if (defaultDatabaseSchemaName != null) |
| 139 | 0 | break; |
| 140 | |
} |
| 141 | |
} |
| 142 | |
} |
| 143 | 0 | } catch (Exception e) { |
| 144 | |
|
| 145 | 0 | e.printStackTrace(); |
| 146 | 0 | LogFactory.getLogger().severe("Failed to get default catalog name from postgres", e); |
| 147 | 0 | } |
| 148 | |
} |
| 149 | |
|
| 150 | 0 | return defaultDatabaseSchemaName; |
| 151 | |
} |
| 152 | |
|
| 153 | |
@Override |
| 154 | |
public String getDefaultCatalogName() throws DatabaseException { |
| 155 | 0 | return "public"; |
| 156 | |
} |
| 157 | |
|
| 158 | |
@Override |
| 159 | |
public String getDatabaseChangeLogTableName() { |
| 160 | 0 | return super.getDatabaseChangeLogTableName().toLowerCase(); |
| 161 | |
} |
| 162 | |
|
| 163 | |
@Override |
| 164 | |
public String getDatabaseChangeLogLockTableName() { |
| 165 | 0 | return super.getDatabaseChangeLogLockTableName().toLowerCase(); |
| 166 | |
} |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
@Override |
| 188 | |
public boolean isSystemTable(String catalogName, String schemaName, String tableName) { |
| 189 | 0 | return super.isSystemTable(catalogName, schemaName, tableName) || "pg_catalog".equals(schemaName) |
| 190 | |
|| "pg_toast".equals(schemaName) || tableName.endsWith("_seq") || tableName.endsWith("_key") |
| 191 | |
|| tableName.endsWith("_pkey") || tableName.startsWith("idx_") || tableName.startsWith("pk_"); |
| 192 | |
} |
| 193 | |
|
| 194 | |
public boolean supportsTablespaces() { |
| 195 | 0 | return true; |
| 196 | |
} |
| 197 | |
|
| 198 | |
@Override |
| 199 | |
public String getAutoIncrementClause() { |
| 200 | 0 | return ""; |
| 201 | |
} |
| 202 | |
|
| 203 | |
@Override |
| 204 | |
public String convertRequestedSchemaToSchema(String requestedSchema) throws DatabaseException { |
| 205 | 0 | if (requestedSchema == null) |
| 206 | 0 | requestedSchema = getDefaultSchemaName(); |
| 207 | |
|
| 208 | 0 | if (requestedSchema == null) { |
| 209 | |
|
| 210 | 0 | return getDefaultCatalogName(); |
| 211 | |
} else { |
| 212 | 0 | String schema = StringUtils.trimToNull(requestedSchema); |
| 213 | 0 | return (schema != null) ? schema.toLowerCase() : schema; |
| 214 | |
} |
| 215 | |
} |
| 216 | |
|
| 217 | |
@Override |
| 218 | |
public String convertRequestedSchemaToCatalog(String requestedSchema) throws DatabaseException { |
| 219 | 0 | return super.convertRequestedSchemaToCatalog(requestedSchema); |
| 220 | |
} |
| 221 | |
|
| 222 | |
@Override |
| 223 | |
public String escapeDatabaseObject(String objectName) { |
| 224 | 14 | if (objectName == null) { |
| 225 | 6 | return null; |
| 226 | |
} |
| 227 | 8 | if (objectName.contains("-") || hasMixedCase(objectName) || startsWithNumeric(objectName) |
| 228 | |
|| isReservedWord(objectName)) { |
| 229 | 4 | return "\"" + objectName + "\""; |
| 230 | |
} else { |
| 231 | 4 | return super.escapeDatabaseObject(objectName); |
| 232 | |
} |
| 233 | |
|
| 234 | |
} |
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
protected boolean hasMixedCase(String tableName) { |
| 244 | 8 | return tableName.matches(".*[A-Z].*") && tableName.matches(".*[a-z].*"); |
| 245 | |
|
| 246 | |
} |
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
private boolean startsWithNumeric(String tableName) { |
| 252 | 5 | return tableName.matches("^[0-9].*"); |
| 253 | |
|
| 254 | |
} |
| 255 | |
|
| 256 | |
@Override |
| 257 | |
public boolean isReservedWord(String tableName) { |
| 258 | 21 | for (int i = 0; i != this.reservedWords.length; i++) |
| 259 | 17 | if (this.reservedWords[i].toLowerCase().equalsIgnoreCase(tableName)) |
| 260 | 1 | return true; |
| 261 | 4 | return false; |
| 262 | |
} |
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | 9 | private String[] reservedWords = new String[] { "USER", "LIKE", "GROUP", "DATE" |
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
}; |
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
private List<String> getSearchPaths() { |
| 284 | 0 | List<String> searchPaths = null; |
| 285 | |
|
| 286 | |
try { |
| 287 | 0 | DatabaseConnection con = getConnection(); |
| 288 | |
|
| 289 | 0 | if (con != null) { |
| 290 | 0 | String searchPathResult = (String) ExecutorService.getInstance().getExecutor(this) |
| 291 | |
.queryForObject(new RawSqlStatement("SHOW search_path"), String.class); |
| 292 | |
|
| 293 | 0 | if (searchPathResult != null) { |
| 294 | 0 | String dirtySearchPaths[] = searchPathResult.split("\\,"); |
| 295 | 0 | searchPaths = new ArrayList<String>(); |
| 296 | 0 | for (String searchPath : dirtySearchPaths) { |
| 297 | 0 | searchPath = searchPath.trim(); |
| 298 | |
|
| 299 | |
|
| 300 | 0 | if (searchPath.equals("\"$user\"")) { |
| 301 | 0 | searchPath = "$user"; |
| 302 | |
} |
| 303 | |
|
| 304 | 0 | searchPaths.add(searchPath); |
| 305 | |
} |
| 306 | |
} |
| 307 | |
|
| 308 | |
} |
| 309 | 0 | } catch (Exception e) { |
| 310 | |
|
| 311 | 0 | e.printStackTrace(); |
| 312 | 0 | LogFactory.getLogger().severe("Failed to get default catalog name from postgres", e); |
| 313 | 0 | } |
| 314 | |
|
| 315 | 0 | return searchPaths; |
| 316 | |
} |
| 317 | |
|
| 318 | |
private boolean catalogExists(String catalogName) throws DatabaseException { |
| 319 | 0 | if (catalogName != null) { |
| 320 | 0 | return runExistsQuery("select count(*) from information_schema.schemata where catalog_name='" + catalogName |
| 321 | |
+ "'"); |
| 322 | |
} else { |
| 323 | 0 | return false; |
| 324 | |
} |
| 325 | |
} |
| 326 | |
|
| 327 | |
private boolean schemaExists(String schemaName) throws DatabaseException { |
| 328 | 0 | return schemaName != null |
| 329 | |
&& runExistsQuery("select count(*) from information_schema.schemata where schema_name='" + schemaName |
| 330 | |
+ "'"); |
| 331 | |
} |
| 332 | |
|
| 333 | |
private boolean runExistsQuery(String query) throws DatabaseException { |
| 334 | 0 | Long count = ExecutorService.getInstance().getExecutor(this).queryForLong(new RawSqlStatement(query)); |
| 335 | |
|
| 336 | 0 | return count != null && count > 0; |
| 337 | |
} |
| 338 | |
|
| 339 | |
@Override |
| 340 | |
public String escapeIndexName(String schemaName, String indexName) { |
| 341 | 0 | return indexName; |
| 342 | |
} |
| 343 | |
} |