| 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.Executor; |
| 7 | |
import liquibase.executor.ExecutorService; |
| 8 | |
import liquibase.logging.LogFactory; |
| 9 | |
import liquibase.statement.core.GetViewDefinitionStatement; |
| 10 | |
|
| 11 | |
import java.util.HashSet; |
| 12 | |
import java.util.List; |
| 13 | |
import java.util.Set; |
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
public class SybaseDatabase extends AbstractDatabase { |
| 19 | |
public static final String PRODUCT_NAME = "Adaptive Server Enterprise"; |
| 20 | 11 | protected Set<String> systemTablesAndViews = new HashSet<String>(); |
| 21 | |
|
| 22 | |
public String getProductName() { |
| 23 | 0 | return "Sybase SQL Server"; |
| 24 | |
} |
| 25 | |
|
| 26 | |
public String getTypeName() { |
| 27 | 56 | return "sybase"; |
| 28 | |
} |
| 29 | |
|
| 30 | 11 | public SybaseDatabase() { |
| 31 | 11 | systemTablesAndViews.add("syscolumns"); |
| 32 | 11 | systemTablesAndViews.add("syscomments"); |
| 33 | 11 | systemTablesAndViews.add("sysdepends"); |
| 34 | 11 | systemTablesAndViews.add("sysfilegroups"); |
| 35 | 11 | systemTablesAndViews.add("sysfiles"); |
| 36 | 11 | systemTablesAndViews.add("sysfiles1"); |
| 37 | 11 | systemTablesAndViews.add("sysforeignkeys"); |
| 38 | 11 | systemTablesAndViews.add("sysfulltextcatalogs"); |
| 39 | 11 | systemTablesAndViews.add("sysfulltextnotify"); |
| 40 | 11 | systemTablesAndViews.add("sysindexes"); |
| 41 | 11 | systemTablesAndViews.add("sysindexkeys"); |
| 42 | 11 | systemTablesAndViews.add("sysmembers"); |
| 43 | 11 | systemTablesAndViews.add("sysobjects"); |
| 44 | 11 | systemTablesAndViews.add("syspermissions"); |
| 45 | 11 | systemTablesAndViews.add("sysproperties"); |
| 46 | 11 | systemTablesAndViews.add("sysprotects"); |
| 47 | 11 | systemTablesAndViews.add("sysreferences"); |
| 48 | 11 | systemTablesAndViews.add("systypes"); |
| 49 | 11 | systemTablesAndViews.add("sysusers"); |
| 50 | 11 | systemTablesAndViews.add("sysquerymetrics"); |
| 51 | 11 | systemTablesAndViews.add("syssegments"); |
| 52 | 11 | systemTablesAndViews.add("sysconstraints"); |
| 53 | 11 | } |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public int getPriority() { |
| 61 | 0 | return PRIORITY_DEFAULT; |
| 62 | |
} |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
@Override |
| 69 | |
public boolean supportsDDLInTransaction() { |
| 70 | 4 | return false; |
| 71 | |
} |
| 72 | |
|
| 73 | |
@Override |
| 74 | |
public Set<String> getSystemTablesAndViews() { |
| 75 | 0 | return systemTablesAndViews; |
| 76 | |
} |
| 77 | |
|
| 78 | |
public boolean supportsInitiallyDeferrableColumns() { |
| 79 | 0 | return false; |
| 80 | |
} |
| 81 | |
|
| 82 | |
@Override |
| 83 | |
public boolean supportsSequences() { |
| 84 | 8 | return false; |
| 85 | |
} |
| 86 | |
|
| 87 | |
public boolean isCorrectDatabaseImplementation(DatabaseConnection conn) throws DatabaseException { |
| 88 | 0 | String dbProductName = conn.getDatabaseProductName(); |
| 89 | 0 | return isSybaseProductName(dbProductName); |
| 90 | |
} |
| 91 | |
|
| 92 | |
|
| 93 | |
boolean isSybaseProductName(String dbProductName) { |
| 94 | 4 | return "Adaptive Server Enterprise".equals(dbProductName) || "Sybase SQL Server".equals(dbProductName) |
| 95 | |
|| "sql server".equals(dbProductName) || "ASE".equals(dbProductName); |
| 96 | |
} |
| 97 | |
|
| 98 | |
public String getDefaultDriver(String url) { |
| 99 | 0 | if (url.startsWith("jdbc:sybase")) { |
| 100 | 0 | return "com.sybase.jdbc3.jdbc.SybDriver"; |
| 101 | 0 | } else if (url.startsWith("jdbc:jtds:sybase")) { |
| 102 | 0 | return "net.sourceforge.jtds.jdbc.Driver"; |
| 103 | |
} |
| 104 | 0 | return null; |
| 105 | |
} |
| 106 | |
|
| 107 | |
public String getCurrentDateTimeFunction() { |
| 108 | 0 | if (currentDateTimeFunction != null) { |
| 109 | 0 | return currentDateTimeFunction; |
| 110 | |
} |
| 111 | |
|
| 112 | 0 | return "GETDATE()"; |
| 113 | |
} |
| 114 | |
|
| 115 | |
@Override |
| 116 | |
public String getAutoIncrementClause() { |
| 117 | 0 | return "IDENTITY"; |
| 118 | |
} |
| 119 | |
|
| 120 | |
@Override |
| 121 | |
protected String getDefaultDatabaseSchemaName() throws DatabaseException { |
| 122 | 1 | return null; |
| 123 | |
} |
| 124 | |
|
| 125 | |
@Override |
| 126 | |
public String getDefaultCatalogName() throws DatabaseException { |
| 127 | 0 | return getConnection().getCatalog(); |
| 128 | |
} |
| 129 | |
|
| 130 | |
@Override |
| 131 | |
public String getConcatSql(String... values) { |
| 132 | 0 | StringBuffer returnString = new StringBuffer(); |
| 133 | 0 | for (String value : values) { |
| 134 | 0 | returnString.append(value).append(" + "); |
| 135 | |
} |
| 136 | |
|
| 137 | 0 | return returnString.toString().replaceFirst(" \\+ $", ""); |
| 138 | |
} |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
public boolean supportsTablespaces() { |
| 183 | 0 | return true; |
| 184 | |
} |
| 185 | |
|
| 186 | |
@Override |
| 187 | |
public boolean isSystemTable(String catalogName, String schemaName, String tableName) { |
| 188 | 0 | return super.isSystemTable(catalogName, schemaName, tableName) || schemaName.equals("sys") |
| 189 | |
|| tableName.toLowerCase().startsWith("sybfi"); |
| 190 | |
} |
| 191 | |
|
| 192 | |
@Override |
| 193 | |
public boolean isSystemView(String catalogName, String schemaName, String viewName) { |
| 194 | 0 | return super.isSystemView(catalogName, schemaName, viewName) || schemaName.equals("sys") |
| 195 | |
|| viewName.toLowerCase().equals("sybfi"); |
| 196 | |
} |
| 197 | |
|
| 198 | |
public String generateDefaultConstraintName(String tableName, String columnName) { |
| 199 | 0 | return "DF_" + tableName + "_" + columnName; |
| 200 | |
} |
| 201 | |
|
| 202 | |
@Override |
| 203 | |
public String convertRequestedSchemaToCatalog(String requestedSchema) throws DatabaseException { |
| 204 | 0 | return getDefaultCatalogName(); |
| 205 | |
} |
| 206 | |
|
| 207 | |
@Override |
| 208 | |
public String convertRequestedSchemaToSchema(String requestedSchema) throws DatabaseException { |
| 209 | 5 | if (requestedSchema == null) { |
| 210 | 1 | requestedSchema = getDefaultDatabaseSchemaName(); |
| 211 | |
} |
| 212 | |
|
| 213 | 5 | if (requestedSchema == null) { |
| 214 | 1 | return "dbo"; |
| 215 | |
} |
| 216 | 4 | return requestedSchema; |
| 217 | |
} |
| 218 | |
|
| 219 | |
@Override |
| 220 | |
public boolean supportsRestrictForeignKeys() { |
| 221 | 0 | return false; |
| 222 | |
} |
| 223 | |
|
| 224 | |
@Override |
| 225 | |
public String escapeDatabaseObject(String objectName) { |
| 226 | 10 | return "[" + objectName + "]"; |
| 227 | |
} |
| 228 | |
|
| 229 | |
@Override |
| 230 | |
public String getViewDefinition(String schemaName, String viewName) throws DatabaseException { |
| 231 | 3 | GetViewDefinitionStatement statement = new GetViewDefinitionStatement( |
| 232 | |
convertRequestedSchemaToSchema(schemaName), viewName); |
| 233 | 3 | Executor executor = ExecutorService.getInstance().getExecutor(this); |
| 234 | |
@SuppressWarnings("unchecked") |
| 235 | 3 | List<String> definitionRows = (List<String>) executor.queryForList(statement, String.class); |
| 236 | 3 | StringBuilder definition = new StringBuilder(); |
| 237 | 3 | for (String d : definitionRows) { |
| 238 | 4 | definition.append(d); |
| 239 | |
} |
| 240 | 3 | return definition.toString(); |
| 241 | |
} |
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
@Override |
| 248 | |
public int getDatabaseMajorVersion() throws DatabaseException { |
| 249 | |
try { |
| 250 | 2 | return getConnection().getDatabaseMajorVersion(); |
| 251 | 1 | } catch (UnsupportedOperationException e) { |
| 252 | 1 | LogFactory.getLogger().warning( |
| 253 | |
"Your JDBC driver does not support getDatabaseMajorVersion(). Consider upgrading it."); |
| 254 | 1 | return -1; |
| 255 | |
} |
| 256 | |
} |
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
@Override |
| 263 | |
public int getDatabaseMinorVersion() throws DatabaseException { |
| 264 | |
try { |
| 265 | 2 | return getConnection().getDatabaseMinorVersion(); |
| 266 | 1 | } catch (UnsupportedOperationException e) { |
| 267 | 1 | LogFactory.getLogger().warning( |
| 268 | |
"Your JDBC driver does not support getDatabaseMajorVersion(). Consider upgrading it."); |
| 269 | 1 | return -1; |
| 270 | |
} |
| 271 | |
} |
| 272 | |
|
| 273 | |
@Override |
| 274 | |
public String escapeIndexName(String schemaName, String indexName) { |
| 275 | 0 | return super.escapeIndexName(null, indexName); |
| 276 | |
} |
| 277 | |
|
| 278 | |
} |