forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yidasanqian
committed
Oct 23, 2017
1 parent
45ac8c8
commit ebe2c3d
Showing
2 changed files
with
84 additions
and
2 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
74 changes: 74 additions & 0 deletions
74
src/test/java/net/sf/jsqlparser/expression/TimestampValueTest.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,74 @@ | ||
package net.sf.jsqlparser.expression; | ||
|
||
import net.sf.jsqlparser.JSQLParserException; | ||
import net.sf.jsqlparser.expression.operators.relational.ExpressionList; | ||
import net.sf.jsqlparser.expression.operators.relational.ItemsListVisitor; | ||
import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList; | ||
import net.sf.jsqlparser.parser.CCJSqlParserUtil; | ||
import net.sf.jsqlparser.schema.Column; | ||
import net.sf.jsqlparser.statement.insert.Insert; | ||
import net.sf.jsqlparser.statement.select.SubSelect; | ||
import org.junit.Test; | ||
|
||
import java.text.DateFormat; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
|
||
/** | ||
* @author Linyu Chen | ||
*/ | ||
public class TimestampValueTest { | ||
|
||
@Test | ||
public void testTimestampValue() throws JSQLParserException { | ||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); | ||
final String currentDate = dateFormat.format(new Date()); | ||
Insert insert = (Insert) CCJSqlParserUtil.parse("insert into mytable (col1) values (1)"); | ||
System.out.println(insert.toString()); | ||
// adding a column | ||
insert.getColumns().add(new Column("create_time")); | ||
// adding a value using a visitor | ||
insert.getItemsList().accept(new ItemsListVisitor() { | ||
|
||
public void visit(SubSelect subSelect) { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
public void visit(ExpressionList expressionList) { | ||
expressionList.getExpressions().add(new TimestampValue(currentDate)); | ||
} | ||
|
||
public void visit(MultiExpressionList multiExprList) { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
}); | ||
System.out.println(insert.toString()); | ||
} | ||
|
||
@Test | ||
public void testTimestampValueWithQuotation() throws JSQLParserException { | ||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); | ||
final String currentDate = dateFormat.format(new Date()); | ||
Insert insert = (Insert) CCJSqlParserUtil.parse("insert into mytable (col1) values (1)"); | ||
System.out.println(insert.toString()); | ||
// adding a column | ||
insert.getColumns().add(new Column("create_time")); | ||
// adding a value using a visitor | ||
insert.getItemsList().accept(new ItemsListVisitor() { | ||
|
||
public void visit(SubSelect subSelect) { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
public void visit(ExpressionList expressionList) { | ||
expressionList.getExpressions().add(new TimestampValue("'" + currentDate + "'")); | ||
} | ||
|
||
public void visit(MultiExpressionList multiExprList) { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
}); | ||
System.out.println(insert.toString()); | ||
} | ||
} |