Search in sources :

Example 6 with JDBCCollection

use of org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection in project dbeaver by serge-rider.

the class PostgreArrayValueHandler method convertArrayToString.

private String convertArrayToString(@NotNull DBSTypedObject column, Object value, @NotNull DBDDisplayFormat format, boolean nested) {
    if (!DBUtils.isNullValue(value) && value instanceof DBDCollection) {
        DBDCollection collection = (DBDCollection) value;
        boolean isNativeFormat = format == DBDDisplayFormat.NATIVE;
        boolean isStringArray = collection.getComponentType().getDataKind() == DBPDataKind.STRING;
        DBDValueHandler valueHandler = collection.getComponentValueHandler();
        StringBuilder str = new StringBuilder();
        if (isNativeFormat && !nested) {
            str.append("'");
        }
        str.append("{");
        for (int i = 0; i < collection.getItemCount(); i++) {
            if (i > 0) {
                // $NON-NLS-1$
                str.append(',');
            }
            final Object item = collection.getItem(i);
            String itemString;
            if (item instanceof JDBCCollection) {
                // Multi-dimensional arrays case
                itemString = convertArrayToString(column, item, format, true);
            } else {
                itemString = valueHandler.getValueDisplayString(collection.getComponentType(), item, format);
            }
            if (isNativeFormat) {
                if (item instanceof String)
                    str.append('"');
                str.append(SQLUtils.escapeString(collection.getComponentType().getDataSource(), itemString));
                if (item instanceof String)
                    str.append('"');
            } else {
                str.append(itemString);
            }
        }
        str.append("}");
        if (isNativeFormat && !nested) {
            str.append("'");
        }
        return str.toString();
    }
    return super.getValueDisplayString(column, value, format);
}
Also used : JDBCCollection(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection) DBDCollection(org.jkiss.dbeaver.model.data.DBDCollection) DBDValueHandler(org.jkiss.dbeaver.model.data.DBDValueHandler) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject)

Example 7 with JDBCCollection

use of org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection in project dbeaver by dbeaver.

the class PostgreArrayValueHandler method getValueFromObject.

@Override
public DBDCollection getValueFromObject(@NotNull DBCSession session, @NotNull DBSTypedObject type, Object object, boolean copy) throws DBCException {
    if (object != null) {
        String className = object.getClass().getName();
        if (object instanceof String || className.equals(PostgreConstants.PG_OBJECT_CLASS)) {
            PostgreDataType itemType = null;
            final PostgreDataType arrayType = PostgreUtils.findDataType((PostgreDataSource) session.getDataSource(), type);
            if (arrayType != null) {
                itemType = arrayType.getElementType();
            }
            if (itemType != null) {
                if (className.equals(PostgreConstants.PG_OBJECT_CLASS)) {
                    final Object value = PostgreUtils.extractPGObjectValue(object);
                    if (value == null) {
                        return null;
                    } else if (value instanceof String) {
                        return convertStringToArray(session, itemType, (String) value);
                    } else {
                        // Can't parse
                        return new JDBCCollection(itemType, DBUtils.findValueHandler(session, itemType), new Object[] { value });
                    }
                } else if (object instanceof String) {
                    return convertStringToArray(session, itemType, (String) object);
                }
            }
        }
    }
    return super.getValueFromObject(session, type, object, copy);
}
Also used : JDBCCollection(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection) PostgreDataType(org.jkiss.dbeaver.ext.postgresql.model.PostgreDataType) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject)

Example 8 with JDBCCollection

use of org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection in project dbeaver by serge-rider.

the class PostgreArrayValueHandler method convertStringToCollection.

private JDBCCollection convertStringToCollection(@NotNull DBCSession session, @NotNull DBSTypedObject arrayType, @NotNull PostgreDataType itemType, @NotNull String value) throws DBCException {
    String delimiter;
    PostgreDataType arrayDataType = PostgreUtils.findDataType(session, (PostgreDataSource) session.getDataSource(), arrayType);
    if (arrayDataType != null) {
        delimiter = CommonUtils.toString(arrayDataType.getArrayDelimiter(), PostgreConstants.DEFAULT_ARRAY_DELIMITER);
    } else {
        delimiter = PostgreConstants.DEFAULT_ARRAY_DELIMITER;
    }
    if (itemType.getDataKind() == DBPDataKind.STRUCT) {
        // Items are structures. Parse them as CSV
        List<Object> itemStrings = PostgreValueParser.parseArrayString(value, delimiter);
        Object[] itemValues = new Object[itemStrings.size()];
        DBDValueHandler itemValueHandler = DBUtils.findValueHandler(session, itemType);
        for (int i = 0; i < itemStrings.size(); i++) {
            Object itemString = itemStrings.get(i);
            Object itemValue = itemValueHandler.getValueFromObject(session, itemType, itemString, false, false);
            itemValues[i] = itemValue;
        }
        return new JDBCCollection(itemType, itemValueHandler, itemValues);
    } else {
        List<Object> strings = PostgreValueParser.parseArrayString(value, delimiter);
        Object[] contents = new Object[strings.size()];
        for (int i = 0; i < strings.size(); i++) {
            contents[i] = PostgreValueParser.convertStringToValue(session, itemType, String.valueOf(strings.get(i)));
        }
        return new JDBCCollection(itemType, DBUtils.findValueHandler(session, itemType), contents);
    }
}
Also used : JDBCCollection(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection) PostgreDataType(org.jkiss.dbeaver.ext.postgresql.model.PostgreDataType) DBDValueHandler(org.jkiss.dbeaver.model.data.DBDValueHandler) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject)

Example 9 with JDBCCollection

use of org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection in project dbeaver by serge-rider.

the class PostgreArrayValueHandler method getValueFromObject.

@Override
public DBDCollection getValueFromObject(@NotNull DBCSession session, @NotNull DBSTypedObject type, Object object, boolean copy, boolean validateValue) throws DBCException {
    if (object != null) {
        String className = object.getClass().getName();
        if (object instanceof String || className.equals(PostgreConstants.PG_OBJECT_CLASS) || className.equals(PostgreConstants.PG_ARRAY_CLASS)) {
            final PostgreDataType arrayType = PostgreUtils.findDataType(session, (PostgreDataSource) session.getDataSource(), type);
            if (arrayType == null) {
                throw new DBCException("Can't resolve data type " + type.getFullTypeName());
            }
            PostgreDataType itemType = arrayType.getElementType(session.getProgressMonitor());
            if (itemType == null) {
                throw new DBCException("Array type " + arrayType.getFullTypeName() + " doesn't have a component type");
            }
            if (className.equals(PostgreConstants.PG_ARRAY_CLASS)) {
                // Convert arrays to string representation (#7468)
                // Otherwise we may have problems with domain types decoding (as they come in form of PgObject)
                String strValue = object.toString();
                return convertStringArrayToCollection(session, arrayType, itemType, strValue);
            } else if (className.equals(PostgreConstants.PG_OBJECT_CLASS)) {
                final Object value = PostgreUtils.extractPGObjectValue(object);
                if (value instanceof String) {
                    return convertStringToCollection(session, type, itemType, (String) value);
                } else {
                    log.error("Can't parse array");
                    return new JDBCCollection(itemType, DBUtils.findValueHandler(session, itemType), value == null ? null : new Object[] { value });
                }
            } else {
                return convertStringToCollection(session, type, itemType, (String) object);
            }
        }
    }
    return super.getValueFromObject(session, type, object, copy, validateValue);
}
Also used : JDBCCollection(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection) PostgreDataType(org.jkiss.dbeaver.ext.postgresql.model.PostgreDataType) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject)

Example 10 with JDBCCollection

use of org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection in project dbeaver by serge-rider.

the class PostgreValueParserTest method convertStringToValue.

@Test
public void convertStringToValue() throws DBCException {
    Assert.assertEquals(1, PostgreValueParser.convertStringToValue(session, intItemType, "1"));
    Assert.assertEquals(1.111, PostgreValueParser.convertStringToValue(session, doubleItemType, "1.111"));
    Assert.assertEquals("A", PostgreValueParser.convertStringToValue(session, stringItemType, "A"));
    Assert.assertNotEquals("ABC", PostgreValueParser.convertStringToValue(session, stringItemType, "A"));
    Assert.assertNotEquals(123, PostgreValueParser.convertStringToValue(session, intItemType, "1"));
    Assert.assertArrayEquals(new String[] { "A", "B" }, (Object[]) PostgreValueParser.convertStringToValue(session, arrayStringItemType, "{\"A\",\"B\"}"));
    Assert.assertArrayEquals(new Integer[] { 1, 22 }, (Object[]) PostgreValueParser.convertStringToValue(session, arrayIntItemType, "{1,22}"));
    Assert.assertArrayEquals(new Double[] { 1.1, 22.22 }, (Object[]) PostgreValueParser.convertStringToValue(session, arrayDoubleItemType, "{1.1,22.22}"));
    Assert.assertNotEquals(new Integer[] { 33333, 22 }, (Object[]) PostgreValueParser.convertStringToValue(session, arrayIntItemType, "{1,22}"));
    JDBCCollection innerCollection1 = new JDBCCollection(doubleItemType, new JDBCNumberValueHandler(doubleItemType, dbdFormatSettings), new Double[] { 1.1, 22.22 });
    JDBCCollection innerCollection2 = new JDBCCollection(doubleItemType, new JDBCNumberValueHandler(doubleItemType, dbdFormatSettings), new Double[] { 3.3, 44.44 });
    Assert.assertArrayEquals(new Object[] { innerCollection1, innerCollection2 }, (Object[]) PostgreValueParser.convertStringToValue(session, arrayDoubleItemType, "{{1.1,22.22},{3.3,44.44}}"));
    JDBCCollection innerCollection3 = new JDBCCollection(doubleItemType, new JDBCNumberValueHandler(doubleItemType, dbdFormatSettings), new Object[] { innerCollection1, innerCollection2 });
    Assert.assertArrayEquals(new Object[] { innerCollection3, innerCollection3 }, (Object[]) PostgreValueParser.convertStringToValue(session, arrayDoubleItemType, "{{{1.1,22.22},{3.3,44.44}},{{1.1,22.22},{3.3,44.44}}}"));
    Assert.assertEquals("{{{1.1,22.22},{3.3,44.44}},{1.1,22.22},{3.3,44.44}}}", PostgreValueParser.convertStringToValue(session, arrayDoubleItemType, "{{{1.1,22.22},{3.3,44.44}},{1.1,22.22},{3.3,44.44}}}"));
    // Bad input data tests
    Assert.assertEquals("{{{1.1,22.22},3.3,44.44}},{1.1,22.22},{3.3,44.44}", PostgreValueParser.convertStringToValue(session, arrayDoubleItemType, "{{{1.1,22.22},3.3,44.44}},{1.1,22.22},{3.3,44.44}"));
    Assert.assertEquals("{{1.1,22.22},3.3,44.44}},{1.1,22.22},{3.3,", PostgreValueParser.convertStringToValue(session, arrayDoubleItemType, "{{1.1,22.22},3.3,44.44}},{1.1,22.22},{3.3,"));
    Assert.assertEquals("{{1.1,22.22},,44.44}},{1.1,22.22},{3.3,}", PostgreValueParser.convertStringToValue(session, arrayDoubleItemType, "{{1.1,22.22},,44.44}},{1.1,22.22},{3.3,}"));
    Boolean[] booleans = { true, false };
    Assert.assertEquals(true, PostgreValueParser.convertStringToValue(session, booleanItemType, "true"));
    Assert.assertNotEquals(false, PostgreValueParser.convertStringToValue(session, booleanItemType, "true"));
    Assert.assertArrayEquals(booleans, (Object[]) PostgreValueParser.convertStringToValue(session, arrayBooleanItemType, "{TRUE,FALSE}"));
// todo: add support alternatives to "true/false"
// Assert.assertArrayEquals(booleans, (Object[]) PostgreValueParser.convertStringToValue(session, arrayBooleanItemType, "{'t','f'}", true));
// Assert.assertArrayEquals(booleans, (Object[]) PostgreValueParser.convertStringToValue(session, arrayBooleanItemType, "{'true','false'}", true));
// Assert.assertArrayEquals(booleans, (Object[]) PostgreValueParser.convertStringToValue(session, arrayBooleanItemType,"{'1','0'}", true));
// Assert.assertArrayEquals(booleans, (Object[]) PostgreValueParser.convertStringToValue(session, arrayBooleanItemType,"{'y','n'}", true));
// Assert.assertArrayEquals(booleans, (Object[]) PostgreValueParser.convertStringToValue(session, arrayBooleanItemType,"{'yes,'no'}", true));
// Assert.assertArrayEquals(booleans, (Object[]) PostgreValueParser.convertStringToValue(session, arrayBooleanItemType,"{'on,'off'}", true));
}
Also used : JDBCCollection(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection) JDBCNumberValueHandler(org.jkiss.dbeaver.model.impl.jdbc.data.handlers.JDBCNumberValueHandler) Test(org.junit.Test)

Aggregations

JDBCCollection (org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection)10 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)8 DBDValueHandler (org.jkiss.dbeaver.model.data.DBDValueHandler)4 PostgreDataType (org.jkiss.dbeaver.ext.postgresql.model.PostgreDataType)3 DBDCollection (org.jkiss.dbeaver.model.data.DBDCollection)3 ArrayList (java.util.ArrayList)2 StringTokenizer (java.util.StringTokenizer)2 NotNull (org.jkiss.code.NotNull)2 Test (org.junit.Test)2 PostgreArrayValueHandler (org.jkiss.dbeaver.ext.postgresql.model.data.PostgreArrayValueHandler)1 DBCException (org.jkiss.dbeaver.model.exec.DBCException)1 JDBCNumberValueHandler (org.jkiss.dbeaver.model.impl.jdbc.data.handlers.JDBCNumberValueHandler)1