-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add check for not valid constraints (#383)
* Add check for not valid constraints * fix :: check style * modify :: request * fix :: check * fix :: coverage * A fix for Constraint code coverage * Improve tests and update README.md * ForeignKey now extends Constraint * Add IndexWithSingleColumnExtractor --------- Co-authored-by: BLoHny <[email protected]>
- Loading branch information
Showing
27 changed files
with
754 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/constraint/Constraint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Copyright (c) 2019-2024. Ivan Vakhrushev and others. | ||
* https://github.com/mfvanek/pg-index-health | ||
* | ||
* This file is a part of "pg-index-health" - a Java library for | ||
* analyzing and maintaining indexes health in PostgreSQL databases. | ||
* | ||
* Licensed under the Apache License 2.0 | ||
*/ | ||
|
||
package io.github.mfvanek.pg.model.constraint; | ||
|
||
import io.github.mfvanek.pg.model.DbObject; | ||
import io.github.mfvanek.pg.model.table.TableNameAware; | ||
import io.github.mfvanek.pg.model.validation.Validators; | ||
|
||
import java.util.Objects; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.concurrent.Immutable; | ||
|
||
/** | ||
* A representation of constraint in a database. | ||
* | ||
* @author Blohny | ||
* @see TableNameAware | ||
* @since 0.10.4 | ||
*/ | ||
@Immutable | ||
public class Constraint implements DbObject, TableNameAware { | ||
|
||
private final String tableName; | ||
private final String constraintName; | ||
private final ConstraintType constraintType; | ||
|
||
/** | ||
* Constructs a {@code Constraint} object with given {@code ConstraintType}. | ||
* | ||
* @param tableName table name; should be non-blank. | ||
* @param constraintName constraint name; should be non-blank. | ||
* @param constraintType constraint type; should be non-null. | ||
*/ | ||
protected Constraint( | ||
@Nonnull final String tableName, | ||
@Nonnull final String constraintName, | ||
@Nonnull final ConstraintType constraintType) { | ||
this.tableName = Validators.tableNameNotBlank(tableName); | ||
this.constraintName = Validators.notBlank(constraintName, "constraintName"); | ||
this.constraintType = Objects.requireNonNull(constraintType, "constraintType cannot be null"); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Nonnull | ||
@Override | ||
public final String getName() { | ||
return getConstraintName(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Nonnull | ||
@Override | ||
public String getTableName() { | ||
return tableName; | ||
} | ||
|
||
/** | ||
* Gets the name of constraint. | ||
* | ||
* @return the name of constraint | ||
*/ | ||
@Nonnull | ||
public String getConstraintName() { | ||
return constraintName; | ||
} | ||
|
||
/** | ||
* Gets type of constraint. | ||
* | ||
* @return type of constraint | ||
* @see ConstraintType | ||
*/ | ||
@Nonnull | ||
public ConstraintType getConstraintType() { | ||
return constraintType; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public final boolean equals(final Object other) { | ||
if (this == other) { | ||
return true; | ||
} | ||
|
||
if (!(other instanceof Constraint)) { | ||
return false; | ||
} | ||
|
||
final Constraint that = (Constraint) other; | ||
return Objects.equals(tableName, that.tableName) && | ||
Objects.equals(constraintName, that.constraintName); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public final int hashCode() { | ||
return Objects.hash(tableName, constraintName); | ||
} | ||
|
||
/** | ||
* An auxiliary utility method for implementing {@code toString()} in child classes. | ||
* | ||
* @return string representation of the internal fields of this class | ||
*/ | ||
@Nonnull | ||
final String innerToString() { | ||
return "tableName='" + tableName + '\'' + | ||
", constraintName='" + constraintName + '\''; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public String toString() { | ||
return Constraint.class.getSimpleName() + '{' + | ||
innerToString() + | ||
", constraintType=" + constraintType + | ||
'}'; | ||
} | ||
|
||
/** | ||
* Constructs a {@code Constraint} object with given {@code ConstraintType}. | ||
* | ||
* @param tableName table name; should be non-blank. | ||
* @param constraintName constraint name; should be non-blank. | ||
* @param constraintType constraint type; should be non-null. | ||
* @return {@code Constraint} | ||
*/ | ||
@Nonnull | ||
public static Constraint ofType(@Nonnull final String tableName, | ||
@Nonnull final String constraintName, | ||
@Nonnull final ConstraintType constraintType) { | ||
return new Constraint(tableName, constraintName, constraintType); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...ndex-health-model/src/main/java/io/github/mfvanek/pg/model/constraint/ConstraintType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) 2019-2024. Ivan Vakhrushev and others. | ||
* https://github.com/mfvanek/pg-index-health | ||
* | ||
* This file is a part of "pg-index-health" - a Java library for | ||
* analyzing and maintaining indexes health in PostgreSQL databases. | ||
* | ||
* Licensed under the Apache License 2.0 | ||
*/ | ||
|
||
package io.github.mfvanek.pg.model.constraint; | ||
|
||
import java.util.Objects; | ||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* A mapping to PostgreSQL constraint types. | ||
* | ||
* @author Blohny | ||
* @since 0.10.4 | ||
* @see <a href="https://www.postgresql.org/docs/current/catalog-pg-constraint.html">pg_constraint</a> | ||
*/ | ||
public enum ConstraintType { | ||
|
||
/** | ||
* Check constraint. | ||
*/ | ||
CHECK("c"), | ||
/** | ||
* Foreign key constraint. | ||
*/ | ||
FOREIGN_KEY("f"); | ||
|
||
private final String pgConType; | ||
|
||
ConstraintType(@Nonnull final String pgConType) { | ||
this.pgConType = Objects.requireNonNull(pgConType, "pgConType"); | ||
} | ||
|
||
/** | ||
* Gets internal PostgreSQL constraint type. | ||
* | ||
* @return pgConType | ||
*/ | ||
@Nonnull | ||
public String getPgConType() { | ||
return pgConType; | ||
} | ||
|
||
/** | ||
* Gets {@code ConstraintType} from internal PostgreSQL constraint type. | ||
* | ||
* @param pgConType internal PostgreSQL constraint type; should be non-null. | ||
* @return {@code ConstraintType} | ||
*/ | ||
@Nonnull | ||
public static ConstraintType valueFrom(@Nonnull final String pgConType) { | ||
Objects.requireNonNull(pgConType, "pgConType cannot be null"); | ||
for (final ConstraintType ct : values()) { | ||
if (ct.getPgConType().equals(pgConType)) { | ||
return ct; | ||
} | ||
} | ||
throw new IllegalArgumentException("Unknown pgConType: " + pgConType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.