| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Since 4.0 the JSQLParser framework includes a validaton framework.
The validation framework maps several types of validation, which are implementations of the interface ValidationCapability.
Currently the following implementations exist:
Check if a parsed statement is valid to the database selected
String sql = "DROP INDEX IF EXISTS idx_tab2_id;";
// validate statement if it's valid for all given databases.
Validation validation = new Validation(Arrays.asList(DatabaseType.SQLSERVER, DatabaseType.MARIADB,
DatabaseType.POSTGRESQL, DatabaseType.H2), sql);
List<ValidationError> errors = validation.validate();Check if a parsed statement is valid to a specific database version.
// validate statement if it's valid for all given databases.
Validation validation = new Validation(Arrays.asList(PostgresqlVersion.V10), sql);
List<ValidationError> errors = validation.validate();Validate against pre-defined FeaturesAllowed.DML set
// validate against pre-defined FeaturesAllowed.DML set
String sql = "CREATE TABLE tab1 (id NUMERIC(10), val VARCHAR(30))";
Validation validation = new Validation(Arrays.asList(FeaturesAllowed.DML), sql);
List<ValidationError> errors = validation.validate();
// only DML is allowed, got error for using a DDL statement
log.error (errors);Validate against pre-defined FeaturesAllowed.SELECT set
String sql = "SELECT * FROM myview v JOIN secondview v2 ON v.id = v2.ref";
Validation validation = new Validation(Arrays.asList(FeaturesAllowed.SELECT), sql);
List<ValidationError> errors = validation.validate();
// no errors, select - statement is allowed
if (errors.isEmpty()) {
// do something else with the parsed statements
Statements statements = validation.getParsedStatements();
}Validate against your own FeatureSet
FeaturesAllowed exec = new FeaturesAllowed("EXECUTE", Feature.execute).unmodifyable();Combine multiple pre-defined FeaturesAllowed set's
FeaturesAllowed myAllowedFeatures = new FeaturesAllowed("myAllowedFeatures")
.add (FeaturesAllowed.DDL, FeaturesAllowed.DML);Validates metadata such as names of tables, views, columns for their existence or non-existence
java.sql.Connection connection = ...;
String sql = "ALTER TABLE mytable ADD price numeric(10,5) not null";
Validation validation = new Validation(Arrays.asList(new JdbcDatabaseMetaDataCapability(connection,
// NamesLookup: Databases handle names differently
NamesLookup.UPPERCASE)), sql);
List<ValidationError> errors = validation.validate();
// do something else with the parsed statements
Statements statements = validation.getParsedStatements();
// check for validation-errors
if (!errors.isEmpty()) {
...
}java.sql.Connection connection = ...;
String sql = "ALTER TABLE mytable ADD price numeric(10,5) not null";
Validation validation = new Validation(Arrays.asList(
DatabaseType.POSTGRESQL,
new JdbcDatabaseMetaDataCapability(connection,NamesLookup.LOWERCASE)), sql);
List<ValidationError> errors = validation.validate(); Statement statement = ...;
ValidationContext context = Validation.createValidationContext(
new FeatureConfiguration(), Arrays.asList(DatabaseType.POSTGRESQL));
Map<ValidationCapability, Set<ValidationException>> errors =
Validation.validate(statement, context);For further development it is important to know that Feature 's are only constants to describe a feature. This feature must first be grouped for validation:
The new constant must of course be checked in the corresponding AbstractValidator implementation: For this purpose there are methods which simply need to be passed the new feature as a parameter, (i.e. #validateOptionalFeature, #validateFeature, ... ).
If a new feature also contains metadata like names of tables, views, columns or similar the content must be validated by the appropriate validators, which in turn use the predefined method #validateName to check metadata against a database catalog. (see also NamedObject)
| Back | FazBrowse Home | New Git URL |