Search in sources :

Example 46 with DataType

use of io.crate.types.DataType in project crate by crate.

the class SQLTransportExecutor method executeAndConvertResult.

private static SQLResponse executeAndConvertResult(PreparedStatement preparedStatement) throws SQLException {
    if (preparedStatement.execute()) {
        ResultSetMetaData metadata = preparedStatement.getMetaData();
        ResultSet resultSet = preparedStatement.getResultSet();
        List<Object[]> rows = new ArrayList<>();
        List<String> columnNames = new ArrayList<>(metadata.getColumnCount());
        DataType[] dataTypes = new DataType[metadata.getColumnCount()];
        for (int i = 0; i < metadata.getColumnCount(); i++) {
            columnNames.add(metadata.getColumnName(i + 1));
        }
        while (resultSet.next()) {
            Object[] row = new Object[metadata.getColumnCount()];
            for (int i = 0; i < row.length; i++) {
                Object value;
                String typeName = metadata.getColumnTypeName(i + 1);
                value = getObject(resultSet, i, typeName);
                row[i] = value;
            }
            rows.add(row);
        }
        return new SQLResponse(columnNames.toArray(new String[0]), rows.toArray(new Object[0][]), dataTypes, rows.size());
    } else {
        int updateCount = preparedStatement.getUpdateCount();
        if (updateCount < 0) {
            /*
                 * In Crate -1 means row-count unknown, and -2 means error. In JDBC -2 means row-count unknown and -3 means error.
                 * See {@link java.sql.Statement#EXECUTE_FAILED}
                 */
            updateCount += 1;
        }
        return new SQLResponse(new String[0], new Object[0][], new DataType[0], updateCount);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) DataType(io.crate.types.DataType) PGpoint(org.postgresql.geometric.PGpoint)

Example 47 with DataType

use of io.crate.types.DataType in project crate by crate.

the class SymbolMatchers method isFunction.

public static Matcher<Symbol> isFunction(final String name, @Nullable final List<DataType> argumentTypes) {
    if (argumentTypes == null) {
        return isFunction(name);
    }
    Matcher[] argMatchers = new Matcher[argumentTypes.size()];
    ListIterator<DataType> it = argumentTypes.listIterator();
    while (it.hasNext()) {
        int i = it.nextIndex();
        DataType type = it.next();
        argMatchers[i] = hasDataType(type);
    }
    // noinspection unchecked
    return isFunction(name, argMatchers);
}
Also used : Matcher(org.hamcrest.Matcher) DataType(io.crate.types.DataType)

Example 48 with DataType

use of io.crate.types.DataType in project crate by crate.

the class DataTypeTest method testForValueWithArrayWithNullValues.

@Test
public void testForValueWithArrayWithNullValues() {
    DataType dataType = DataTypes.guessType(new String[] { "foo", null, "bar" });
    assertEquals(dataType, new ArrayType(DataTypes.STRING));
}
Also used : ArrayType(io.crate.types.ArrayType) DataType(io.crate.types.DataType) Test(org.junit.Test)

Example 49 with DataType

use of io.crate.types.DataType in project crate by crate.

the class UpdateAnalyzerTest method testUpdateDynamicNestedArrayParamLiteral.

@Test
public void testUpdateDynamicNestedArrayParamLiteral() throws Exception {
    AnalyzedUpdateStatement update = analyze("update users set new=[[1.9, 4.8], [9.7, 12.7]]");
    DataType dataType = update.assignmentByTargetCol().values().iterator().next().valueType();
    assertThat(dataType, is(new ArrayType(new ArrayType(DoubleType.INSTANCE))));
}
Also used : ArrayType(io.crate.types.ArrayType) DataType(io.crate.types.DataType) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 50 with DataType

use of io.crate.types.DataType in project crate by crate.

the class UpdateAnalyzerTest method testUpdateDynamicNestedArrayParam.

@Test
public void testUpdateDynamicNestedArrayParam() throws Exception {
    Object[] params = { new Object[] { new Object[] { 1.9, 4.8 }, new Object[] { 9.7, 12.7 } } };
    AnalyzedUpdateStatement update = analyze("update users set new=? where id=1");
    Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
    Symbol[] sources = assignments.bindSources(((DocTableInfo) update.table().tableInfo()), new RowN(params), SubQueryResults.EMPTY);
    DataType dataType = sources[0].valueType();
    assertThat(dataType, is(new ArrayType(new ArrayType(DoubleType.INSTANCE))));
}
Also used : ArrayType(io.crate.types.ArrayType) DocTableInfo(io.crate.metadata.doc.DocTableInfo) RowN(io.crate.data.RowN) ParameterSymbol(io.crate.expression.symbol.ParameterSymbol) Symbol(io.crate.expression.symbol.Symbol) Assignments(io.crate.expression.symbol.Assignments) DataType(io.crate.types.DataType) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

DataType (io.crate.types.DataType)95 ArrayType (io.crate.types.ArrayType)35 Test (org.junit.Test)33 ArrayList (java.util.ArrayList)17 Map (java.util.Map)17 CrateUnitTest (io.crate.test.integration.CrateUnitTest)14 List (java.util.List)12 Symbol (io.crate.expression.symbol.Symbol)11 Literal (io.crate.expression.symbol.Literal)9 ColumnIdent (io.crate.metadata.ColumnIdent)9 HashMap (java.util.HashMap)9 FunctionIdent (io.crate.metadata.FunctionIdent)8 NodeContext (io.crate.metadata.NodeContext)8 Reference (io.crate.metadata.Reference)8 Row (io.crate.data.Row)7 Symbols (io.crate.expression.symbol.Symbols)7 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)7 Locale (java.util.Locale)7 Lists2 (io.crate.common.collections.Lists2)6 FunctionInfo (io.crate.metadata.FunctionInfo)6