Search in sources :

Example 81 with Literal

use of org.teiid.language.Literal in project teiid by teiid.

the class TestODataIntegration method testInsertDifferentTypes.

@Test
public void testInsertDifferentTypes() throws Exception {
    AutoUpdateHardCodedExecutionFactory hc = new AutoUpdateHardCodedExecutionFactory();
    hc.addUpdate("INSERT INTO PostTable (intkey, intnum, stringkey, stringval, booleanval, " + "decimalval, timeval, dateval, timestampval) " + "VALUES (4, 4, '4', 'value_4', FALSE, -20.4, {t '00:00:04'}, " + "{d '2004-04-04'}, {ts '2004-01-01 00:00:04.0'})", new int[] { 1 });
    hc.addData("SELECT PostTable.intkey, PostTable.intnum, PostTable.stringkey, " + "PostTable.stringval, PostTable.booleanval, PostTable.decimalval, " + "PostTable.timeval, PostTable.dateval, PostTable.timestampval, " + "PostTable.clobval FROM PostTable " + "WHERE PostTable.intkey = 4", Arrays.asList(Arrays.asList(4, 4, "4", "value_4", false, new BigDecimal("-20.4"), new java.sql.Time(0), new java.sql.Date(0), new java.sql.Timestamp(0), null)));
    teiid.addTranslator("x11", hc);
    try {
        ModelMetaData mmd = new ModelMetaData();
        mmd.setName("m");
        mmd.addSourceMetadata("ddl", "CREATE foreign TABLE PostTable(\n" + "intkey integer PRIMARY KEY,\n" + "intnum integer,\n" + "stringkey varchar(20),\n" + "stringval varchar(20),\n" + "booleanval boolean,\n" + "  decimalval decimal(20, 10),\n" + "timeval time,\n" + "dateval date,\n" + "timestampval timestamp,\n" + "clobval clob) options (updatable true);");
        mmd.addSourceMapping("x11", "x11", null);
        teiid.deployVDB("northwind", mmd);
        localClient = getClient(teiid.getDriver(), "northwind", new Properties());
        String payload = "\n" + "<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\" " + "xmlns:georss=\"http://www.georss.org/georss\" " + "xmlns:gml=\"http://www.opengis.net/gml\" " + "xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\">\n" + "   <category scheme=\"http://docs.oasis-open.org/odata/ns/scheme\" />\n" + "   <content type=\"application/xml\">\n" + "      <m:properties>\n" + "         <d:intkey m:type=\"Int32\">4</d:intkey>\n" + "         <d:intnum m:type=\"Int32\">4</d:intnum>\n" + "         <d:stringkey>4</d:stringkey>\n" + "         <d:stringval>value_4</d:stringval>\n" + "         <d:booleanval m:type=\"Boolean\">false</d:booleanval>\n" + "         <d:decimalval m:type=\"Double\">-20.4</d:decimalval>\n" + "         <d:timeval m:type=\"TimeOfDay\">00:00:04</d:timeval>\n" + "         <d:dateval m:type=\"Date\">2004-04-04</d:dateval>\n" + "         <d:timestampval m:type=\"DateTimeOffset\">2004-01-01T00:00:04Z</d:timestampval>\n" + "      </m:properties>\n" + "   </content>\n" + "</entry>";
        ContentResponse response = http.newRequest(baseURL + "/northwind/m/PostTable").method("POST").content(new StringContentProvider(payload)).header("Content-Type", "application/xml").header("Prefer", "return=minimal").send();
        assertEquals(204, response.getStatus());
        assertTrue(response.getHeaders().get("OData-EntityId"), response.getHeaders().get("OData-EntityId").endsWith("northwind/m/PostTable(4)"));
        String jsonPlayload = "{\n" + "  \"intkey\":4,\n" + "  \"intnum\":4,\n" + "  \"stringkey\":\"4\",\n" + "  \"stringval\":\"value_4\",\n" + "  \"booleanval\":false,\n" + "  \"decimalval\":-20.4,\n" + "  \"timeval\":\"00:00:04\",\n" + "  \"dateval\":\"2004-04-04\",\n" + "  \"timestampval\":\"2004-01-01T00:00:04Z\"" + "}";
        response = http.newRequest(baseURL + "/northwind/m/PostTable").method("POST").content(new StringContentProvider(jsonPlayload)).header("Content-Type", "application/json").header("Prefer", "return=representation").send();
        assertEquals(201, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#PostTable\"," + "\"intkey\":4," + "\"intnum\":4," + "\"stringkey\":\"4\"," + "\"stringval\":\"value_4\"," + "\"booleanval\":false," + "\"decimalval\":-20.4," + "\"timeval\":\"00:00:00\"," + "\"dateval\":\"1970-01-01\"," + "\"timestampval\":\"1970-01-01T00:00:00Z\"" + "}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/PostTable(4)/clobval").method("POST").content(new StringContentProvider("clob value")).send();
        assertEquals(405, response.getStatus());
        hc.validator = (org.teiid.language.Command c) -> {
            Update update = (Update) c;
            Literal value = (Literal) update.getChanges().get(0).getValue();
            Clob clob = (Clob) value.getValue();
            try {
                assertEquals("clob value", clob.getSubString(1, (int) clob.length()));
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        };
        response = http.newRequest(baseURL + "/northwind/m/PostTable(4)/clobval").method("PUT").content(new StringContentProvider("clob value")).send();
        assertEquals(204, response.getStatus());
        hc.validator = null;
        response = http.newRequest(baseURL + "/northwind/m/PostTable(4)/clobval").method("DELETE").send();
        assertEquals(204, response.getStatus());
    } finally {
        localClient = null;
        teiid.undeployVDB("northwind");
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) SQLException(java.sql.SQLException) Properties(java.util.Properties) Update(org.teiid.language.Update) BigDecimal(java.math.BigDecimal) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) Command(org.teiid.query.sql.lang.Command) Literal(org.teiid.language.Literal) Clob(java.sql.Clob) Test(org.junit.Test)

Example 82 with Literal

use of org.teiid.language.Literal in project teiid by teiid.

the class TestEmbeddedServer method testDynamicUpdate.

@Test
public void testDynamicUpdate() throws Exception {
    EmbeddedConfiguration ec = new EmbeddedConfiguration();
    MockTransactionManager tm = new MockTransactionManager();
    ec.setTransactionManager(tm);
    ec.setUseDisk(false);
    es.start(ec);
    es.addTranslator("t", new ExecutionFactory<Void, Void>() {

        @Override
        public boolean supportsCompareCriteriaEquals() {
            return true;
        }

        @Override
        public boolean isSourceRequired() {
            return false;
        }

        @Override
        public UpdateExecution createUpdateExecution(Command command, ExecutionContext executionContext, RuntimeMetadata metadata, Void connection) throws TranslatorException {
            Collection<Literal> values = CollectorVisitor.collectObjects(Literal.class, command);
            assertEquals(2, values.size());
            for (Literal literal : values) {
                assertFalse(literal.getValue() instanceof Reference);
            }
            return new UpdateExecution() {

                @Override
                public void execute() throws TranslatorException {
                }

                @Override
                public void close() {
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public int[] getUpdateCounts() throws DataNotAvailableException, TranslatorException {
                    return new int[] { 1 };
                }
            };
        }
    });
    ModelMetaData mmd1 = new ModelMetaData();
    mmd1.setName("accounts");
    mmd1.setSchemaSourceType("ddl");
    mmd1.setSchemaText(ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("dynamic_update.sql")));
    mmd1.addSourceMapping("y", "t", null);
    es.deployVDB("vdb", mmd1);
    Connection c = es.getDriver().connect("jdbc:teiid:vdb", null);
    PreparedStatement ps = c.prepareStatement("update hello1 set SchemaName=? where Name=?");
    ps.setString(1, "test1223");
    ps.setString(2, "Columns");
    assertEquals(1, ps.executeUpdate());
}
Also used : Reference(org.teiid.query.sql.symbol.Reference) UpdateExecution(org.teiid.translator.UpdateExecution) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) Literal(org.teiid.language.Literal) Collection(java.util.Collection) TranslatorException(org.teiid.translator.TranslatorException) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) Test(org.junit.Test)

Aggregations

Literal (org.teiid.language.Literal)82 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)18 Expression (org.teiid.language.Expression)17 TranslatorException (org.teiid.translator.TranslatorException)16 Function (org.teiid.language.Function)15 Argument (org.teiid.language.Argument)12 ColumnReference (org.teiid.language.ColumnReference)10 Column (org.teiid.metadata.Column)10 Comparison (org.teiid.language.Comparison)9 ExpressionValueSource (org.teiid.language.ExpressionValueSource)7 List (java.util.List)5 Call (org.teiid.language.Call)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 BinaryWSProcedureExecution (org.teiid.translator.ws.BinaryWSProcedureExecution)4 DBCollection (com.mongodb.DBCollection)3 Timestamp (java.sql.Timestamp)3 Array (org.teiid.language.Array)3 Command (org.teiid.language.Command)3 Insert (org.teiid.language.Insert)3