Search in sources :

Example 1 with UserVariable

use of net.sf.jsqlparser.expression.UserVariable in project JSqlParser by JSQLParser.

the class DeclareStatementTest method testDeclareTable.

@Test
public void testDeclareTable() throws JSQLParserException {
    String statement = "DECLARE @MyTableVar TABLE (EmpID int NOT NULL, OldVacationHours int, NewVacationHours int, ModifiedDate datetime)";
    Statement parsed = assertSqlCanBeParsedAndDeparsed(statement);
    DeclareStatement created = new DeclareStatement().withUserVariable(new UserVariable("MyTableVar")).withColumnDefinitions(new ArrayList<>()).addColumnDefinitions(new ColumnDefinition("EmpID", new ColDataType().withDataType("int"), asList("NOT", "NULL")), new ColumnDefinition("OldVacationHours", new ColDataType("int"))).addColumnDefinitions(asList(new ColumnDefinition("NewVacationHours", new ColDataType("int")), new ColumnDefinition("ModifiedDate", new ColDataType("datetime")))).withDeclareType(DeclareType.TABLE);
    assertDeparse(created, statement);
    assertEqualsObjectTree(parsed, created);
}
Also used : ArrayList(java.util.ArrayList) ColDataType(net.sf.jsqlparser.statement.create.table.ColDataType) UserVariable(net.sf.jsqlparser.expression.UserVariable) ColumnDefinition(net.sf.jsqlparser.statement.create.table.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Example 2 with UserVariable

use of net.sf.jsqlparser.expression.UserVariable in project jans by JanssenProject.

the class SpannerOperationServiceImpl method lookupImpl.

private List<AttributeData> lookupImpl(TableMapping tableMapping, String key, String... attributes) throws SearchException, EntryConvertationException {
    try {
        String tableName = tableMapping.getTableName();
        // If all requested attributes belong to one table get row by primary key
        Set<String> childTables = connectionProvider.getTableChildAttributes(tableName);
        List<AttributeData> result = null;
        if (childTables == null) {
            // All attributes in one table
            if (attributes == null) {
                // Request all attributes
                try (ResultSet resultSet = databaseClient.singleUse().read(tableName, KeySet.singleKey(Key.of(key)), tableMapping.getColumTypes().keySet())) {
                    result = getAttributeDataList(tableMapping.getObjectClass(), resultSet, true);
                }
            } else {
                // Request only required attributes
                try (ResultSet resultSet = databaseClient.singleUse().read(tableName, KeySet.singleKey(Key.of(key)), Arrays.asList(attributes))) {
                    result = getAttributeDataList(tableMapping.getObjectClass(), resultSet, true);
                }
            }
        } else {
            Table table = buildTable(tableMapping);
            PlainSelect sqlSelectQuery = new PlainSelect();
            sqlSelectQuery.setFromItem(table);
            List<SelectItem> selectItems = buildSelectAttributes(tableMapping, key, attributes);
            sqlSelectQuery.addSelectItems(selectItems);
            Column leftColumn = new Column(tableAlias, DOC_ID);
            UserVariable rightValue = new UserVariable(DOC_ID);
            EqualsTo whereExp = new EqualsTo(leftColumn, rightValue);
            sqlSelectQuery.setWhere(whereExp);
            Limit limit = new Limit();
            limit.setRowCount(new LongValue(1));
            sqlSelectQuery.setLimit(limit);
            Statement statement = Statement.newBuilder(sqlSelectQuery.toString()).bind(DOC_ID).to(key).build();
            LOG.debug("Executing lookup query: '{}'", statement);
            try (ResultSet resultSet = databaseClient.singleUse().executeQuery(statement)) {
                result = getAttributeDataList(tableMapping.getObjectClass(), resultSet, true);
            }
        }
        if (result != null) {
            return result;
        }
    } catch (SpannerException ex) {
        throw new SearchException(String.format("Failed to lookup query by key: '%s'", key), ex);
    }
    throw new SearchException(String.format("Failed to lookup entry by key: '%s'", key));
}
Also used : Table(net.sf.jsqlparser.schema.Table) Statement(com.google.cloud.spanner.Statement) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SearchException(io.jans.orm.exception.operation.SearchException) UserVariable(net.sf.jsqlparser.expression.UserVariable) Column(net.sf.jsqlparser.schema.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) ResultSet(com.google.cloud.spanner.ResultSet) LongValue(net.sf.jsqlparser.expression.LongValue) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) Limit(net.sf.jsqlparser.statement.select.Limit) SpannerException(com.google.cloud.spanner.SpannerException) AttributeData(io.jans.orm.model.AttributeData)

Example 3 with UserVariable

use of net.sf.jsqlparser.expression.UserVariable in project JSqlParser by JSQLParser.

the class DeclareStatementTest method testDeclareTypeList.

@Test
public void testDeclareTypeList() throws JSQLParserException {
    String statement = "DECLARE @group nvarchar (50), @sales money";
    Statement parsed = assertSqlCanBeParsedAndDeparsed(statement);
    DeclareStatement created = new DeclareStatement().addTypeDefExprList(asList(new TypeDefExpr(new UserVariable().withName("group"), new ColDataType().withDataType("nvarchar").addArgumentsStringList("50"), null), new TypeDefExpr(new UserVariable().withName("sales"), new ColDataType().withDataType("money"), null))).withDeclareType(DeclareType.TYPE);
    assertDeparse(created, statement);
    assertEqualsObjectTree(parsed, created);
}
Also used : TypeDefExpr(net.sf.jsqlparser.statement.DeclareStatement.TypeDefExpr) ColDataType(net.sf.jsqlparser.statement.create.table.ColDataType) UserVariable(net.sf.jsqlparser.expression.UserVariable) Test(org.junit.jupiter.api.Test)

Example 4 with UserVariable

use of net.sf.jsqlparser.expression.UserVariable in project JSqlParser by JSQLParser.

the class DeclareStatementTest method testDeclareTypeWithDefault.

@Test
public void testDeclareTypeWithDefault() throws JSQLParserException {
    String statement = "DECLARE @find varchar (30) = 'Man%'";
    Statement parsed = assertSqlCanBeParsedAndDeparsed(statement);
    DeclareStatement created = new DeclareStatement().addTypeDefExprList(new TypeDefExpr(new UserVariable().withName("find"), new ColDataType().withDataType("varchar").addArgumentsStringList("30"), new StringValue().withValue("Man%"))).withDeclareType(DeclareType.TYPE);
    assertDeparse(created, statement);
    assertEqualsObjectTree(parsed, created);
}
Also used : TypeDefExpr(net.sf.jsqlparser.statement.DeclareStatement.TypeDefExpr) ColDataType(net.sf.jsqlparser.statement.create.table.ColDataType) StringValue(net.sf.jsqlparser.expression.StringValue) UserVariable(net.sf.jsqlparser.expression.UserVariable) Test(org.junit.jupiter.api.Test)

Example 5 with UserVariable

use of net.sf.jsqlparser.expression.UserVariable in project JSqlParser by JSQLParser.

the class DeclareStatementTest method testDeclareType.

@Test
public void testDeclareType() throws JSQLParserException {
    String statement = "DECLARE @find nvarchar (30)";
    Statement parsed = assertSqlCanBeParsedAndDeparsed(statement);
    DeclareStatement created = new DeclareStatement().addTypeDefExprList(new TypeDefExpr(new UserVariable().withName("find"), new ColDataType().withDataType("nvarchar").addArgumentsStringList("30"), null)).withDeclareType(DeclareType.TYPE);
    assertDeparse(created, statement);
    assertEqualsObjectTree(parsed, created);
}
Also used : TypeDefExpr(net.sf.jsqlparser.statement.DeclareStatement.TypeDefExpr) ColDataType(net.sf.jsqlparser.statement.create.table.ColDataType) UserVariable(net.sf.jsqlparser.expression.UserVariable) Test(org.junit.jupiter.api.Test)

Aggregations

UserVariable (net.sf.jsqlparser.expression.UserVariable)6 ColDataType (net.sf.jsqlparser.statement.create.table.ColDataType)4 Test (org.junit.jupiter.api.Test)4 TypeDefExpr (net.sf.jsqlparser.statement.DeclareStatement.TypeDefExpr)3 ResultSet (com.google.cloud.spanner.ResultSet)1 SpannerException (com.google.cloud.spanner.SpannerException)1 Statement (com.google.cloud.spanner.Statement)1 StructField (com.google.cloud.spanner.Type.StructField)1 ValueWithStructField (io.jans.orm.cloud.spanner.model.ValueWithStructField)1 SearchException (io.jans.orm.exception.operation.SearchException)1 AttributeData (io.jans.orm.model.AttributeData)1 ArrayList (java.util.ArrayList)1 LongValue (net.sf.jsqlparser.expression.LongValue)1 StringValue (net.sf.jsqlparser.expression.StringValue)1 EqualsTo (net.sf.jsqlparser.expression.operators.relational.EqualsTo)1 Column (net.sf.jsqlparser.schema.Column)1 Table (net.sf.jsqlparser.schema.Table)1 ColumnDefinition (net.sf.jsqlparser.statement.create.table.ColumnDefinition)1 Limit (net.sf.jsqlparser.statement.select.Limit)1 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)1