Search in sources :

Example 1 with JDBCCollection

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

the class PostgreArrayValueHandler method getValueDisplayString.

@NotNull
@Override
public String getValueDisplayString(@NotNull DBSTypedObject column, Object value, @NotNull DBDDisplayFormat format) {
    DBDCollection collection = (DBDCollection) value;
    if (!DBUtils.isNullValue(value)) {
        DBDValueHandler valueHandler = collection.getComponentValueHandler();
        StringBuilder str = new StringBuilder();
        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 = getValueDisplayString(column, item, format);
            } else {
                itemString = valueHandler.getValueDisplayString(collection.getComponentType(), item, DBDDisplayFormat.NATIVE);
            }
            str.append(itemString);
        }
        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) NotNull(org.jkiss.code.NotNull)

Example 2 with JDBCCollection

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

the class PostgreArrayValueHandler method convertStringToArray.

private JDBCCollection convertStringToArray(@NotNull DBCSession session, @NotNull PostgreDataType itemType, @NotNull String value) {
    List<String> strings = new ArrayList<>(10);
    StringTokenizer st = new StringTokenizer(value, " ");
    while (st.hasMoreTokens()) {
        strings.add(st.nextToken());
    }
    Object[] contents = new Object[strings.size()];
    for (int i = 0; i < strings.size(); i++) {
        contents[i] = PostgreUtils.convertStringToValue(itemType, strings.get(i), false);
    }
    return new JDBCCollection(itemType, DBUtils.findValueHandler(session, itemType), contents);
}
Also used : JDBCCollection(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection) StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject)

Example 3 with JDBCCollection

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

the class PostgreArrayValueHandler method getValueDisplayString.

@NotNull
@Override
public String getValueDisplayString(@NotNull DBSTypedObject column, Object value, @NotNull DBDDisplayFormat format) {
    DBDCollection collection = (DBDCollection) value;
    if (!DBUtils.isNullValue(value)) {
        DBDValueHandler valueHandler = collection.getComponentValueHandler();
        StringBuilder str = new StringBuilder();
        if (format == DBDDisplayFormat.NATIVE) {
            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 = getValueDisplayString(column, item, format);
            } else {
                itemString = valueHandler.getValueDisplayString(collection.getComponentType(), item, DBDDisplayFormat.NATIVE);
            }
            str.append(itemString);
        }
        str.append("}");
        if (format == DBDDisplayFormat.NATIVE) {
            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) NotNull(org.jkiss.code.NotNull)

Example 4 with JDBCCollection

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

the class PostgreArrayValueHandler method convertStringToArray.

private JDBCCollection convertStringToArray(@NotNull DBCSession session, @NotNull PostgreDataType itemType, @NotNull String value) {
    List<String> strings = new ArrayList<>(10);
    StringTokenizer st = new StringTokenizer(value, " ");
    while (st.hasMoreTokens()) {
        strings.add(st.nextToken());
    }
    Object[] contents = new Object[strings.size()];
    for (int i = 0; i < strings.size(); i++) {
        contents[i] = PostgreUtils.convertStringToValue(itemType, strings.get(i), false);
    }
    return new JDBCCollection(itemType, DBUtils.findValueHandler(session, itemType), contents);
}
Also used : JDBCCollection(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection) StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject)

Example 5 with JDBCCollection

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

the class PostgreValueParserTest method convertArrayToString.

@Test
public void convertArrayToString() {
    PostgreArrayValueHandler arrayVH = new PostgreArrayValueHandler();
    String[] stringItems = new String[] { "one", "two", " four with spaces ", "f{i,v}e" };
    JDBCCollection array = new JDBCCollection(stringItemType, arrayVH, stringItems);
    JDBCCollection array3D = new JDBCCollection(stringItemType, arrayVH, new Object[] { array, array });
    String arrayString = arrayVH.getValueDisplayString(arrayStringItemType, array, DBDDisplayFormat.NATIVE);
    Assert.assertEquals("'{\"one\",\"two\",\" four with spaces \",\"f{i,v}e\"}'", arrayString);
    String arrayString3D = arrayVH.getValueDisplayString(arrayStringItemType, array3D, DBDDisplayFormat.NATIVE);
    Assert.assertEquals("'{{\"one\",\"two\",\" four with spaces \",\"f{i,v}e\"},{\"one\",\"two\",\" four with spaces \",\"f{i,v}e\"}}'", arrayString3D);
}
Also used : JDBCCollection(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection) PostgreArrayValueHandler(org.jkiss.dbeaver.ext.postgresql.model.data.PostgreArrayValueHandler) 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