Skip to content

Commit

Permalink
TEIID-2880 allowing for property driven way to set the source comment
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Mar 25, 2014
1 parent 64f5a74 commit 6f655f2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<module name="javax.api"/>
<module name="org.jboss.teiid.common-core" />
<module name="org.jboss.teiid.api" />
<module name="org.jboss.teiid.admin" />
<module name="org.hibernate" />
</dependencies>
</module>
5 changes: 5 additions & 0 deletions connectors/translator-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<artifactId>teiid-common-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.teiid</groupId>
<artifactId>teiid-admin</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.resource</groupId>
<artifactId>connector-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ public enum StructRetrieval {
ARRAY
}

private static final ThreadLocal<MessageFormat> COMMENT = new ThreadLocal<MessageFormat>() {
private final ThreadLocal<MessageFormat> comment = new ThreadLocal<MessageFormat>() {
protected MessageFormat initialValue() {
return new MessageFormat("/*teiid sessionid:{0}, requestid:{1}.{2}*/ "); //$NON-NLS-1$
return new MessageFormat(commentFormat);
}
};
public final static TimeZone DEFAULT_TIME_ZONE = TimeZone.getDefault();
Expand Down Expand Up @@ -142,6 +142,7 @@ protected Calendar initialValue() {
private StructRetrieval structRetrieval = StructRetrieval.OBJECT;
protected SQLDialect dialect;
private boolean enableDependentJoins;
private String commentFormat = "/*teiid sessionid:{0}, requestid:{1}.{2}*/ "; //$NON-NLS-1$

private AtomicBoolean initialConnection = new AtomicBoolean(true);

Expand All @@ -159,6 +160,10 @@ public JDBCExecutionFactory() {
public void start() throws TranslatorException {
super.start();
this.databaseCalender = new DatabaseCalender(this.databaseTimeZone);
if (useCommentsInSourceQuery) {
//will throw an exception if not valid
new MessageFormat(commentFormat);
}
}

@TranslatorProperty(display="Database Version", description= "Database Version")
Expand Down Expand Up @@ -745,7 +750,8 @@ public String getSetOperationString(SetQuery.Operation operation) {
*/
public String getSourceComment(ExecutionContext context, Command command) {
if (addSourceComment() && context != null) {
return COMMENT.get().format(new Object[] {context.getConnectionId(), context.getRequestId(), context.getPartIdentifier()});
return comment.get().format(new Object[] {context.getConnectionId(), context.getRequestId(), context.getPartIdentifier(),
context.getExecutionCountIdentifier(), context.getSession().getUserName(), context.getVdbName(), context.getVdbVersion(), context.isTransactional() });
}
return ""; //$NON-NLS-1$
}
Expand Down Expand Up @@ -1404,4 +1410,13 @@ public boolean useWithRollup() {
return false;
}

@TranslatorProperty(display="Comment Format", description= "Comment format string used with useCommentsInSourceQuery")
public String getCommentFormat() {
return commentFormat;
}

public void setCommentFormat(String commentFormat) {
this.commentFormat = commentFormat;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@

import org.junit.BeforeClass;
import org.junit.Test;
import org.teiid.adminapi.impl.SessionMetadata;
import org.teiid.dqp.internal.datamgr.ExecutionContextImpl;
import org.teiid.dqp.internal.datamgr.TestDeleteImpl;
import org.teiid.dqp.internal.datamgr.TestInsertImpl;
import org.teiid.dqp.internal.datamgr.TestProcedureImpl;
import org.teiid.dqp.internal.datamgr.TestQueryImpl;
import org.teiid.dqp.internal.datamgr.TestUpdateImpl;
import org.teiid.language.LanguageObject;
import org.teiid.translator.ExecutionContext;
import org.teiid.translator.TranslatorException;

/**
*/
@SuppressWarnings("nls")
public class TestSQLConversionVisitor {

public static final ExecutionContext context = new ExecutionContextImpl("VDB", //$NON-NLS-1$
public static final ExecutionContextImpl context = new ExecutionContextImpl("VDB", //$NON-NLS-1$
1,
"Payload", //$NON-NLS-1$
"ConnectionID", //$NON-NLS-1$
Expand All @@ -50,6 +50,10 @@ public class TestSQLConversionVisitor {
"PartID", //$NON-NLS-1$
"ExecCount"); //$NON-NLS-1$

static {
context.setSession(new SessionMetadata());
}

private static JDBCExecutionFactory TRANSLATOR;

@BeforeClass public static void oneTimeSetup() throws TranslatorException {
Expand Down Expand Up @@ -77,11 +81,18 @@ public void helpTestVisitor(String vdb, String input, String expectedOutput, boo
throw new RuntimeException(e);
}
}

private String getStringWithContext(LanguageObject obj) throws TranslatorException {
return getStringWithContext(obj, null);
}

private String getStringWithContext(LanguageObject obj, String comment) throws TranslatorException {
JDBCExecutionFactory env = new JDBCExecutionFactory();
env.setUseCommentsInSourceQuery(true);
env.setUseBindVariables(false);
if (comment != null) {
env.setCommentFormat(comment);
}
env.start();

SQLConversionVisitor visitor = env.getSQLConversionVisitor();
Expand Down Expand Up @@ -378,6 +389,11 @@ public void defer_testPreparedStatementCreationWithLeftConstant() {
assertEquals(expected, getStringWithContext(TestQueryImpl.example(false)));
expected = "SELECT /*teiid sessionid:ConnectionID, requestid:ConnectionID.1.PartID*/ DISTINCT g1.e1, g1.e2, g1.e3, g1.e4 FROM g1, g2 AS myAlias, g3, g4 WHERE 100 >= 200 AND 500 < 600 GROUP BY g1.e1, g1.e2, g1.e3, g1.e4 HAVING 100 >= 200 AND 500 < 600 ORDER BY g1.e1, g1.e2 DESC, g1.e3, g1.e4 DESC"; //$NON-NLS-1$
assertEquals(expected, getStringWithContext(TestQueryImpl.example(true)));
}

@Test public void testVisitSelectWithCustomComment() throws Exception {
String expected = "SELECT /* foo ConnectionID ConnectionID.1 PartID ExecCount null VDB 1 false */g1.e1, g1.e2, g1.e3, g1.e4 FROM g1, g2 AS myAlias, g3, g4 WHERE 100 >= 200 AND 500 < 600 GROUP BY g1.e1, g1.e2, g1.e3, g1.e4 HAVING 100 >= 200 AND 500 < 600 ORDER BY g1.e1, g1.e2 DESC, g1.e3, g1.e4 DESC"; //$NON-NLS-1$
assertEquals(expected, getStringWithContext(TestQueryImpl.example(false), "/* foo {0} {1} {2} {3} {4} {5} {6} {7} */"));
}

@Test public void testVisitIUpdateWithComment() throws Exception {
Expand Down

0 comments on commit 6f655f2

Please sign in to comment.