Search in sources :

Example 1 with DBPDataKind

use of org.jkiss.dbeaver.model.DBPDataKind in project dbeaver by serge-rider.

the class PostgreAttribute method loadInfo.

private void loadInfo(JDBCResultSet dbResult) throws DBException {
    setName(JDBCUtils.safeGetString(dbResult, "attname"));
    setOrdinalPosition(JDBCUtils.safeGetInt(dbResult, "attnum"));
    setRequired(JDBCUtils.safeGetBoolean(dbResult, "attnotnull"));
    final long typeId = JDBCUtils.safeGetLong(dbResult, "atttypid");
    dataType = getTable().getDatabase().getDataType(typeId);
    if (dataType == null) {
        throw new DBException("Attribute data type '" + typeId + "' not found");
    }
    setTypeName(dataType.getTypeName());
    setValueType(dataType.getTypeID());
    setDefaultValue(JDBCUtils.safeGetString(dbResult, "def_value"));
    int typeMod = JDBCUtils.safeGetInt(dbResult, "atttypmod");
    int maxLength = PostgreUtils.getAttributePrecision(typeId, typeMod);
    DBPDataKind dataKind = dataType.getDataKind();
    if (dataKind == DBPDataKind.NUMERIC || dataKind == DBPDataKind.DATETIME) {
        setMaxLength(0);
    } else {
        if (maxLength <= 0) {
            maxLength = PostgreUtils.getDisplaySize(typeId, typeMod);
        }
        if (maxLength >= 0) {
            setMaxLength(maxLength);
        } else {
        // TypeMod can be anything.
        // It is often used in packed format and has no numeric meaning at all
        //setMaxLength(typeMod);
        }
    }
    setPrecision(maxLength);
    setScale(PostgreUtils.getScale(typeId, typeMod));
    this.description = JDBCUtils.safeGetString(dbResult, "description");
    this.arrayDim = JDBCUtils.safeGetInt(dbResult, "attndims");
    this.inheritorsCount = JDBCUtils.safeGetInt(dbResult, "attinhcount");
    setPersisted(true);
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPDataKind(org.jkiss.dbeaver.model.DBPDataKind)

Example 2 with DBPDataKind

use of org.jkiss.dbeaver.model.DBPDataKind in project dbeaver by dbeaver.

the class JDBCStandardValueHandlerProvider method getValueHandler.

@Override
public DBDValueHandler getValueHandler(DBPDataSource dataSource, DBDPreferences preferences, DBSTypedObject typedObject) {
    int valueType = typedObject.getTypeID();
    // JDBCUtils.resolveDataKind(dataSource, typedObject.getTypeName(), valueType);
    DBPDataKind dataKind = typedObject.getDataKind();
    switch(dataKind) {
        case BOOLEAN:
            return new JDBCBooleanValueHandler();
        case STRING:
            if (valueType == java.sql.Types.LONGVARCHAR || valueType == java.sql.Types.LONGNVARCHAR) {
                // Eval long varchars as LOBs
                return JDBCContentValueHandler.INSTANCE;
            } else {
                return JDBCStringValueHandler.INSTANCE;
            }
        case NUMERIC:
            return new JDBCNumberValueHandler(typedObject, preferences.getDataFormatterProfile());
        case DATETIME:
            return new JDBCDateTimeValueHandler(preferences.getDataFormatterProfile());
        case BINARY:
        case CONTENT:
            return JDBCContentValueHandler.INSTANCE;
        case ARRAY:
            return JDBCArrayValueHandler.INSTANCE;
        case STRUCT:
            return JDBCStructValueHandler.INSTANCE;
        case REFERENCE:
            return JDBCReferenceValueHandler.INSTANCE;
        case ROWID:
            return JDBCObjectValueHandler.INSTANCE;
        default:
            // Unknown type
            return null;
    }
}
Also used : DBPDataKind(org.jkiss.dbeaver.model.DBPDataKind)

Example 3 with DBPDataKind

use of org.jkiss.dbeaver.model.DBPDataKind in project dbeaver by dbeaver.

the class PlainTextPresentation method printGrid.

private void printGrid(boolean append) {
    DBPPreferenceStore prefs = getController().getPreferenceStore();
    int maxColumnSize = prefs.getInt(ResultSetPreferences.RESULT_TEXT_MAX_COLUMN_SIZE);
    boolean delimLeading = prefs.getBoolean(ResultSetPreferences.RESULT_TEXT_DELIMITER_LEADING);
    boolean delimTrailing = prefs.getBoolean(ResultSetPreferences.RESULT_TEXT_DELIMITER_TRAILING);
    boolean extraSpaces = prefs.getBoolean(ResultSetPreferences.RESULT_TEXT_EXTRA_SPACES);
    this.showNulls = getController().getPreferenceStore().getBoolean(ResultSetPreferences.RESULT_TEXT_SHOW_NULLS);
    DBDDisplayFormat displayFormat = DBDDisplayFormat.safeValueOf(prefs.getString(ResultSetPreferences.RESULT_TEXT_VALUE_FORMAT));
    StringBuilder grid = new StringBuilder(512);
    ResultSetModel model = controller.getModel();
    List<DBDAttributeBinding> attrs = model.getVisibleAttributes();
    List<ResultSetRow> allRows = model.getAllRows();
    int extraSpacesNum = extraSpaces ? 2 : 0;
    if (colWidths == null) {
        // Calculate column widths
        colWidths = new int[attrs.size()];
        for (int i = 0; i < attrs.size(); i++) {
            DBDAttributeBinding attr = attrs.get(i);
            colWidths[i] = getAttributeName(attr).length() + extraSpacesNum;
            if (showNulls && !attr.isRequired()) {
                colWidths[i] = Math.max(colWidths[i], DBConstants.NULL_VALUE_LABEL.length());
            }
            for (ResultSetRow row : allRows) {
                String displayString = getCellString(model, attr, row, displayFormat);
                colWidths[i] = Math.max(colWidths[i], getStringWidth(displayString) + extraSpacesNum);
            }
        }
        for (int i = 0; i < colWidths.length; i++) {
            if (colWidths[i] > maxColumnSize) {
                colWidths[i] = maxColumnSize;
            }
        }
    }
    if (!append) {
        // Print header
        if (delimLeading)
            grid.append("|");
        for (int i = 0; i < attrs.size(); i++) {
            if (i > 0)
                grid.append("|");
            if (extraSpaces)
                grid.append(" ");
            DBDAttributeBinding attr = attrs.get(i);
            String attrName = getAttributeName(attr);
            grid.append(attrName);
            for (int k = colWidths[i] - attrName.length() - extraSpacesNum; k > 0; k--) {
                grid.append(" ");
            }
            if (extraSpaces)
                grid.append(" ");
        }
        if (delimTrailing)
            grid.append("|");
        grid.append("\n");
        // Print header
        if (delimLeading)
            grid.append("|");
        for (int i = 0; i < attrs.size(); i++) {
            if (i > 0)
                grid.append("|");
            for (int k = colWidths[i]; k > 0; k--) {
                grid.append("-");
            }
        }
        if (delimTrailing)
            grid.append("|");
        grid.append("\n");
    }
    // Print rows
    int firstRow = append ? totalRows : 0;
    if (append) {
        grid.append("\n");
    }
    for (int i = firstRow; i < allRows.size(); i++) {
        ResultSetRow row = allRows.get(i);
        if (delimLeading)
            grid.append("|");
        for (int k = 0; k < attrs.size(); k++) {
            if (k > 0)
                grid.append("|");
            DBDAttributeBinding attr = attrs.get(k);
            String displayString = getCellString(model, attr, row, displayFormat);
            if (displayString.length() >= colWidths[k]) {
                displayString = CommonUtils.truncateString(displayString, colWidths[k]);
            }
            int stringWidth = getStringWidth(displayString);
            if (extraSpaces)
                grid.append(" ");
            DBPDataKind dataKind = attr.getDataKind();
            if ((dataKind == DBPDataKind.NUMERIC && rightJustifyNumbers) || (dataKind == DBPDataKind.DATETIME && rightJustifyDateTime)) {
                // Right justify value
                for (int j = colWidths[k] - stringWidth - extraSpacesNum; j > 0; j--) {
                    grid.append(" ");
                }
                grid.append(displayString);
            } else {
                grid.append(displayString);
                for (int j = colWidths[k] - stringWidth - extraSpacesNum; j > 0; j--) {
                    grid.append(" ");
                }
            }
            if (extraSpaces)
                grid.append(" ");
        }
        if (delimTrailing)
            grid.append("|");
        grid.append("\n");
    }
    // cut last line feed
    grid.setLength(grid.length() - 1);
    if (append) {
        text.append(grid.toString());
    } else {
        text.setText(grid.toString());
    }
    totalRows = allRows.size();
}
Also used : DBDDisplayFormat(org.jkiss.dbeaver.model.data.DBDDisplayFormat) DBPDataKind(org.jkiss.dbeaver.model.DBPDataKind) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBPPreferenceStore(org.jkiss.dbeaver.model.preferences.DBPPreferenceStore)

Example 4 with DBPDataKind

use of org.jkiss.dbeaver.model.DBPDataKind in project dbeaver by dbeaver.

the class DataImporterCSV method readColumnsInfo.

@NotNull
@Override
public List<StreamDataImporterColumnInfo> readColumnsInfo(StreamEntityMapping entityMapping, @NotNull InputStream inputStream) throws DBException {
    List<StreamDataImporterColumnInfo> columnsInfo = new ArrayList<>();
    Map<String, Object> processorProperties = getSite().getProcessorProperties();
    HeaderPosition headerPosition = getHeaderPosition(processorProperties);
    try (Reader reader = openStreamReader(inputStream, processorProperties)) {
        try (CSVReader csvReader = openCSVReader(reader, processorProperties)) {
            String[] header = getNextLine(csvReader);
            if (header == null) {
                return columnsInfo;
            }
            for (int i = 0; i < header.length; i++) {
                String column = null;
                if (headerPosition == HeaderPosition.top) {
                    column = DBUtils.getUnQuotedIdentifier(entityMapping.getDataSource(), header[i]);
                }
                if (CommonUtils.isEmptyTrimmed(column)) {
                    column = "Column" + (i + 1);
                }
                StreamDataImporterColumnInfo columnInfo = new StreamDataImporterColumnInfo(entityMapping, i, column, null, MAX_COLUMN_LENGTH, DBPDataKind.UNKNOWN);
                columnInfo.setMappingMetadataPresent(headerPosition != HeaderPosition.none);
                columnsInfo.add(columnInfo);
            }
            for (int sample = 0; sample < MAX_DATA_TYPE_SAMPLES; sample++) {
                String[] line;
                if (sample == 0 && headerPosition == HeaderPosition.none) {
                    // Include first line (header that does not exist) for sampling
                    line = header;
                } else {
                    line = getNextLine(csvReader);
                    if (line == null) {
                        break;
                    }
                }
                for (int i = 0; i < Math.min(line.length, header.length); i++) {
                    Pair<DBPDataKind, String> dataType = getDataType(line[i]);
                    StreamDataImporterColumnInfo columnInfo = columnsInfo.get(i);
                    switch(dataType.getFirst()) {
                        case STRING:
                            columnInfo.setDataKind(dataType.getFirst());
                            columnInfo.setTypeName(dataType.getSecond());
                            break;
                        case NUMERIC:
                        case BOOLEAN:
                            if (columnInfo.getDataKind() == DBPDataKind.UNKNOWN) {
                                columnInfo.setDataKind(dataType.getFirst());
                                columnInfo.setTypeName(dataType.getSecond());
                            }
                            break;
                    }
                }
            }
            for (StreamDataImporterColumnInfo columnInfo : columnsInfo) {
                if (columnInfo.getDataKind() == DBPDataKind.UNKNOWN) {
                    log.warn("Cannot guess data type for column '" + columnInfo.getName() + "', defaulting to VARCHAR");
                    columnInfo.setDataKind(DBPDataKind.STRING);
                    columnInfo.setTypeName("VARCHAR");
                }
            }
        }
    } catch (IOException e) {
        throw new DBException("IO error reading CSV", e);
    }
    return columnsInfo;
}
Also used : DBException(org.jkiss.dbeaver.DBException) CSVReader(au.com.bytecode.opencsv.CSVReader) ArrayList(java.util.ArrayList) CSVReader(au.com.bytecode.opencsv.CSVReader) DBPDataKind(org.jkiss.dbeaver.model.DBPDataKind) NotNull(org.jkiss.code.NotNull)

Example 5 with DBPDataKind

use of org.jkiss.dbeaver.model.DBPDataKind in project dbeaver by dbeaver.

the class JDBCStandardValueHandlerProvider method getValueHandler.

@Override
public DBDValueHandler getValueHandler(DBPDataSource dataSource, DBDFormatSettings preferences, DBSTypedObject typedObject) {
    int valueType = typedObject.getTypeID();
    // JDBCUtils.resolveDataKind(dataSource, typedObject.getTypeName(), valueType);
    DBPDataKind dataKind = typedObject.getDataKind();
    switch(dataKind) {
        case BOOLEAN:
            return new JDBCBooleanValueHandler();
        case STRING:
            if (LONGVARCHAR_AS_LOB && (valueType == java.sql.Types.LONGVARCHAR || valueType == java.sql.Types.LONGNVARCHAR)) {
                // Eval long varchars as LOBs
                return JDBCContentValueHandler.INSTANCE;
            } else {
                return JDBCStringValueHandler.INSTANCE;
            }
        case NUMERIC:
            return new JDBCNumberValueHandler(typedObject, preferences);
        case DATETIME:
            return new JDBCDateTimeValueHandler(preferences);
        case BINARY:
        case CONTENT:
            if ("UUID".equalsIgnoreCase(typedObject.getTypeName())) {
                return JDBCUUIDValueHandler.INSTANCE;
            }
            return JDBCContentValueHandler.INSTANCE;
        case ARRAY:
            return JDBCArrayValueHandler.INSTANCE;
        case STRUCT:
            return JDBCStructValueHandler.INSTANCE;
        case REFERENCE:
            return JDBCReferenceValueHandler.INSTANCE;
        case ROWID:
            return JDBCObjectValueHandler.INSTANCE;
        default:
            // Unknown type
            return null;
    }
}
Also used : DBPDataKind(org.jkiss.dbeaver.model.DBPDataKind)

Aggregations

DBPDataKind (org.jkiss.dbeaver.model.DBPDataKind)11 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)4 DBDDisplayFormat (org.jkiss.dbeaver.model.data.DBDDisplayFormat)4 DBPPreferenceStore (org.jkiss.dbeaver.model.preferences.DBPPreferenceStore)4 DBException (org.jkiss.dbeaver.DBException)3 CSVReader (au.com.bytecode.opencsv.CSVReader)2 ArrayList (java.util.ArrayList)2 NotNull (org.jkiss.code.NotNull)2