use of org.jkiss.dbeaver.model.DBPDataKind in project dbeaver by dbeaver.
the class ConsoleTextPresentation method printGrid.
private void printGrid() {
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();
grid.append("> ").append(controller.getDataContainer().getName()).append("\n");
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;
}
}
}
// 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 = totalRows;
boolean newLines = false;
for (int i = firstRow; i < allRows.size(); i++) {
newLines = true;
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);
totalRows = allRows.size();
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\n");
if (newLines) {
text.append(grid.toString());
text.append(totalRows + " row(s) fetched.\n\n");
}
}
use of org.jkiss.dbeaver.model.DBPDataKind in project dbeaver by serge-rider.
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(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;
}
}
use of org.jkiss.dbeaver.model.DBPDataKind in project dbeaver by serge-rider.
the class ConsoleTextPresentation method printGrid.
private void printGrid() {
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();
grid.append("> ").append(controller.getDataContainer().getName()).append("\n");
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;
}
}
}
// 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 = totalRows;
boolean newLines = false;
for (int i = firstRow; i < allRows.size(); i++) {
newLines = true;
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);
totalRows = allRows.size();
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\n");
if (newLines) {
text.append(grid.toString());
text.append(totalRows + " row(s) fetched.\n\n");
}
}
use of org.jkiss.dbeaver.model.DBPDataKind in project dbeaver by serge-rider.
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;
}
use of org.jkiss.dbeaver.model.DBPDataKind in project dbeaver by serge-rider.
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;
}
}
Aggregations