@@ -18,6 +18,45 @@ public class NamedConstraint extends Index {
1818
1919 private String indexName ;
2020 private boolean useConstraintKeyword ;
21+ private ConstraintNamePosition constraintNamePosition = ConstraintNamePosition .BEFORE ;
22+
23+ /** Position of the constraint symbol relative to its definition. */
24+ public enum ConstraintNamePosition {
25+ BEFORE , AFTER
26+ }
27+
28+ public ConstraintNamePosition getConstraintNamePosition () {
29+ return constraintNamePosition ;
30+ }
31+
32+ public void setConstraintNamePosition (ConstraintNamePosition position ) {
33+ constraintNamePosition = java .util .Objects .requireNonNull (position , "position" );
34+ }
35+
36+ public NamedConstraint withConstraintNamePosition (ConstraintNamePosition position ) {
37+ setConstraintNamePosition (position );
38+ return this ;
39+ }
40+
41+ /** Appends the leading keyword and, for the usual syntax, the constraint name. */
42+ public void appendConstraintPrefixTo (StringBuilder builder ) {
43+ boolean leadingName = getName () != null
44+ && constraintNamePosition == ConstraintNamePosition .BEFORE ;
45+ if (useConstraintKeyword || leadingName ) {
46+ builder .append ("CONSTRAINT" );
47+ if (leadingName ) {
48+ builder .append (' ' ).append (getName ());
49+ }
50+ builder .append (' ' );
51+ }
52+ }
53+
54+ /** Appends an Informix constraint name after the complete constraint definition. */
55+ public void appendConstraintSuffixTo (StringBuilder builder ) {
56+ if (constraintNamePosition == ConstraintNamePosition .AFTER && getName () != null ) {
57+ builder .append (" CONSTRAINT " ).append (getName ());
58+ }
59+ }
2160
2261 /**
2362 * Returns the optional index name declared after the constraint type. This is distinct from
@@ -44,9 +83,6 @@ public void setUseConstraintKeyword(boolean useConstraintKeyword) {
4483 @ Override
4584 public String toString () {
4685 String idxSpecText = PlainSelect .getStringList (getIndexSpec (), false , false );
47- String head = useConstraintKeyword || getName () != null
48- ? "CONSTRAINT" + (getName () != null ? " " + getName () : "" ) + " "
49- : "" ;
5086 String keyword = getIndexKeyword () != null
5187 && !getType ().toUpperCase (java .util .Locale .ROOT )
5288 .endsWith (getIndexKeyword ().toUpperCase (java .util .Locale .ROOT ))
@@ -61,9 +97,12 @@ public String toString() {
6197 : " " + PlainSelect .getStringList (getColumnsNames (), true , true ))
6298 +
6399 (!"" .equals (idxSpecText ) ? " " + idxSpecText : "" );
64- StringBuilder sql = new StringBuilder (head ).append (tail );
100+ StringBuilder sql = new StringBuilder ();
101+ appendConstraintPrefixTo (sql );
102+ sql .append (tail );
65103 appendConstraintOptionsTo (sql );
66104 if (getKind () != Kind .FOREIGN_KEY ) {
105+ appendConstraintSuffixTo (sql );
67106 appendConstraintAttributesTo (sql );
68107 }
69108 return sql .toString ();
0 commit comments