Skip to content

Commit 6f7c7d2

Browse files
committed
HHH-8090: Created testing framework for SQL output and implementation in core
1 parent ca4516a commit 6f7c7d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2952
-1312
lines changed

hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseSqlOutputTest.java

Lines changed: 61 additions & 224 deletions
Large diffs are not rendered by default.

hibernate-testing/src/main/java/org/hibernate/testing/junit4/FailureExpectedStatement.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
*/
2424
package org.hibernate.testing.junit4;
2525

26-
import org.hibernate.testing.sqlparser.Statement;
26+
import org.hibernate.testing.sql.Statement;
2727

2828
/**
2929
*
3030
*/
3131
public class FailureExpectedStatement extends Statement {
32+
33+
FailureExpectedStatement() {
34+
super( null );
35+
}
3236
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.testing.sql;
25+
26+
import java.util.Collection;
27+
import java.util.Iterator;
28+
29+
/**
30+
*
31+
*/
32+
public abstract class AbstractSqlObject implements SqlObject {
33+
34+
protected static void collectionToString( StringBuilder builder, Collection< ? > collection ) {
35+
if ( collection.isEmpty() ) {
36+
return;
37+
}
38+
Iterator< ? > iter = collection.iterator();
39+
builder.append( iter.next() );
40+
while ( iter.hasNext() ) {
41+
builder.append( ", " ).append( iter.next() );
42+
}
43+
}
44+
45+
protected static void collectionToStringInParentheses( StringBuilder builder, Collection< ? > collection ) {
46+
builder.append( '(' );
47+
if ( !collection.isEmpty() ) {
48+
builder.append( ' ' );
49+
}
50+
collectionToString( builder, collection );
51+
if ( !collection.isEmpty() ) {
52+
builder.append( ' ' );
53+
}
54+
builder.append( ')' );
55+
}
56+
57+
private SqlObject parent;
58+
59+
AbstractSqlObject( SqlObject parent ) {
60+
this.parent = parent;
61+
}
62+
63+
/**
64+
* {@inheritDoc}
65+
*
66+
* @see org.hibernate.testing.sql.SqlObject#namespaceScope()
67+
*/
68+
@Override
69+
public final LocalScope localScope() {
70+
return this instanceof LocalScope ? ( LocalScope ) this : parent.localScope();
71+
}
72+
73+
/**
74+
* {@inheritDoc}
75+
*
76+
* @see org.hibernate.testing.sql.SqlObject#localObjectInScope(java.lang.String, boolean)
77+
*/
78+
@Override
79+
public SqlObject localObjectInScope( String name, boolean ignoreColumns ) {
80+
LocalScope scope = localScope();
81+
SqlObject obj = scope.localObject( name, ignoreColumns );
82+
return obj == null && scope.parent() != null ? scope.parent().localObjectInScope( name, ignoreColumns ) : obj;
83+
}
84+
85+
/**
86+
* {@inheritDoc}
87+
*
88+
* @see org.hibernate.testing.sql.SqlObject#parent()
89+
*/
90+
@Override
91+
public final SqlObject parent() {
92+
return parent;
93+
}
94+
95+
/**
96+
* {@inheritDoc}
97+
*
98+
* @see org.hibernate.testing.sql.SqlObject#setParent(org.hibernate.testing.sql.SqlObject)
99+
*/
100+
@Override
101+
public final void setParent( SqlObject parent ) {
102+
this.parent = parent;
103+
}
104+
105+
/**
106+
* {@inheritDoc}
107+
*
108+
* @see org.hibernate.testing.sql.SqlObject#statement()
109+
*/
110+
@Override
111+
public final Statement statement() {
112+
return this instanceof Statement ? ( Statement ) this : parent.statement();
113+
}
114+
}

hibernate-testing/src/main/java/org/hibernate/testing/sqlparser/Alias.java renamed to hibernate-testing/src/main/java/org/hibernate/testing/sql/Alias.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,17 @@
2121
* 51 Franklin Street, Fifth Floor
2222
* Boston, MA 02110-1301 USA
2323
*/
24-
package org.hibernate.testing.sqlparser;
24+
package org.hibernate.testing.sql;
25+
2526

2627
/**
2728
*
2829
*/
29-
public class Alias extends Statement {
30+
public class Alias extends Name {
3031

31-
String name;
32-
String sourceCode;
32+
Reference reference;
3333

34-
/**
35-
* {@inheritDoc}
36-
*
37-
* @see java.lang.Object#toString()
38-
*/
39-
@Override
40-
public String toString() {
41-
return "CREATE ALIAS " + name + " AS " + sourceCode;
34+
Alias( SqlObject parent, String text ) {
35+
super( parent, text );
4236
}
4337
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.hibernate.testing.sql;
2+
3+
public class Aliasable extends AbstractSqlObject {
4+
5+
public SqlObject expression;
6+
public Alias alias;
7+
8+
Aliasable( SqlObject parent ) {
9+
super( parent );
10+
}
11+
12+
/**
13+
* {@inheritDoc}
14+
*
15+
* @see java.lang.Object#toString()
16+
*/
17+
@Override
18+
public String toString() {
19+
return alias == null ? ( expression == null ? "" : expression.toString() ) : expression + " AS " + alias;
20+
}
21+
}

hibernate-testing/src/main/java/org/hibernate/testing/sqlparser/Case.java renamed to hibernate-testing/src/main/java/org/hibernate/testing/sql/AlterTable.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
* 51 Franklin Street, Fifth Floor
2222
* Boston, MA 02110-1301 USA
2323
*/
24-
package org.hibernate.testing.sqlparser;
24+
package org.hibernate.testing.sql;
2525

2626
/**
2727
*
2828
*/
29-
public class Case extends Expression {
29+
public class AlterTable extends DdlStatement {
3030

31-
public Object expression;
31+
public Reference table;
32+
public Constraint constraint;
3233

3334
/**
3435
* {@inheritDoc}
@@ -37,14 +38,8 @@ public class Case extends Expression {
3738
*/
3839
@Override
3940
public String toString() {
40-
StringBuilder builder = new StringBuilder( "CASE" );
41-
if ( expression != null ) {
42-
builder.append( ' ' ).append( expression );
43-
}
44-
for ( Object when : operands ) {
45-
builder.append( ' ' ).append( when );
46-
}
47-
builder.append( " END" );
41+
StringBuilder builder = new StringBuilder( "ALTER TABLE " );
42+
builder.append( table ).append( " ADD " ).append( constraint );
4843
return builder.toString();
4944
}
5045
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.testing.sql;
25+
26+
/**
27+
*
28+
*/
29+
public class Between extends Operation {
30+
31+
Between( SqlObject parent, String operator, int precedence ) {
32+
super( parent, operator, precedence );
33+
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*
38+
* @see java.lang.Object#toString()
39+
*/
40+
@Override
41+
public String toString() {
42+
StringBuilder builder = new StringBuilder();
43+
if ( operands.isEmpty() ) {
44+
builder.append( operator );
45+
} else {
46+
builder.append( operands.get( 0 ) ).append( ' ' ).append( operator );
47+
if ( operands.size() > 1 ) {
48+
builder.append( ' ' ).append( operands.get( 1 ) );
49+
}
50+
if ( operands.size() > 2 ) {
51+
builder.append( " AND " ).append( operands.get( 2 ) );
52+
}
53+
}
54+
return builder.toString();
55+
}
56+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.testing.sql;
25+
26+
/**
27+
*
28+
*/
29+
public class BinaryOperation extends Operation {
30+
31+
BinaryOperation( SqlObject parent, String operator, int precedence ) {
32+
super( parent, operator, precedence );
33+
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*
38+
* @see java.lang.Object#toString()
39+
*/
40+
@Override
41+
public String toString() {
42+
StringBuilder builder = new StringBuilder();
43+
if ( operands.isEmpty() ) {
44+
builder.append( operator );
45+
} else {
46+
builder.append( operands.get( 0 ) ).append( ' ' ).append( operator );
47+
if ( operands.size() > 1 ) {
48+
builder.append( ' ' ).append( operands.get( 1 ) );
49+
}
50+
}
51+
return builder.toString();
52+
}
53+
}

hibernate-testing/src/main/java/org/hibernate/testing/sqlparser/Call.java renamed to hibernate-testing/src/main/java/org/hibernate/testing/sql/Call.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package org.hibernate.testing.sqlparser;
1+
package org.hibernate.testing.sql;
22

3-
public class Call extends Statement {
3+
public class Call extends DdlStatement {
44

55
public Function function;
66

0 commit comments

Comments
 (0)