下面列出了java.sql.ResultSetMetaData#columnNullable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected Schema inferSchemaResultSetMetaData(ResultSetMetaData metadata) throws SQLException {
List<Field> fields = new ArrayList<>();
int count = metadata.getColumnCount();
for (int i = 1; i <= count; i++) {
int size = metadata.getPrecision(i);
int scale = metadata.getScale(i);
boolean nullable = ResultSetMetaData.columnNullable == metadata.isNullable(i);
int dbtype = metadata.getColumnType(i);
String fieldName = metadata.getColumnLabel(i);
String dbColumnName = metadata.getColumnName(i);
// not necessary for the result schema from the query statement
boolean isKey = false;
Field field = sqlType2Avro(size, scale, dbtype, nullable, fieldName, dbColumnName, null, isKey);
fields.add(field);
}
return Schema.createRecord("DYNAMIC", null, null, false, fields);
}
public static ClickHouseRowSerializer create(ResultSetMetaData meta) throws SQLException {
ClickHouseRowSerializer serializer = new ClickHouseRowSerializer();
for (int i = 1; i <= meta.getColumnCount(); i++) {
int precision = meta.getPrecision(i);
int scale = meta.getScale(i);
ExtractorConverter<?> ser = ClickHouseConverter.getSerializerBySQLType(meta.getColumnType(i), precision, scale);
boolean isNullable = meta.isNullable(i) == ResultSetMetaData.columnNullable;
ClickHouseFieldSerializer<?> fieldSerializer = new ClickHouseFieldSerializer<>(isNullable, ser);
serializer.add(fieldSerializer);
}
return serializer;
}
@DataProvider(name = "validSetNullableValues")
private Object[][] validSetNullableValues() {
return new Object[][]{
{ResultSetMetaData.columnNoNulls},
{ResultSetMetaData.columnNullable},
{ResultSetMetaData.columnNullableUnknown}
};
}
public static Schema infer(ResultSetMetaData metadata, Dbms mapping, boolean enableSpecialTableName) throws SQLException {
List<Field> fields = new ArrayList<>();
Set<String> existNames = new HashSet<String>();
int index = 0;
int count = metadata.getColumnCount();
for (int i = 1; i <= count; i++) {
int size = metadata.getPrecision(i);
int scale = metadata.getScale(i);
boolean nullable = ResultSetMetaData.columnNullable == metadata.isNullable(i);
int dbtype = metadata.getColumnType(i);
String fieldName = metadata.getColumnLabel(i);
String dbColumnName = metadata.getColumnName(i);
// not necessary for the result schema from the query statement
boolean isKey = false;
String columnTypeName = metadata.getColumnTypeName(i).toUpperCase();
String validName = NameUtil.correct(fieldName, index++, existNames);
existNames.add(validName);
Field field = sqlType2Avro(size, scale, dbtype, nullable, validName, dbColumnName, null, isKey, mapping,
columnTypeName);
if(enableSpecialTableName && !validName.equals(dbColumnName)){
field.addProp(ENABLE_SPECIAL_TABLENAME,"true");
}
fields.add(field);
}
return Schema.createRecord("DYNAMIC", null, null, false, fields);
}
@DataProvider(name = "validSetNullableValues")
private Object[][] validSetNullableValues() {
return new Object[][]{
{ResultSetMetaData.columnNoNulls},
{ResultSetMetaData.columnNullable},
{ResultSetMetaData.columnNullableUnknown}
};
}
@DataProvider(name = "validSetNullableValues")
private Object[][] validSetNullableValues() {
return new Object[][]{
{ResultSetMetaData.columnNoNulls},
{ResultSetMetaData.columnNullable},
{ResultSetMetaData.columnNullableUnknown}
};
}
@DataProvider(name = "validSetNullableValues")
private Object[][] validSetNullableValues() {
return new Object[][]{
{ResultSetMetaData.columnNoNulls},
{ResultSetMetaData.columnNullable},
{ResultSetMetaData.columnNullableUnknown}
};
}
/**
* Process a TDS 5.0 result set packet.
*
* @throws IOException
* @throws ProtocolException
*/
private void tds5ResultToken() throws IOException, ProtocolException {
in.readShort(); // Packet length
int colCnt = in.readShort();
columns = new ColInfo[colCnt];
rowData = new Object[colCnt];
tables = null;
for (int colNum = 0; colNum < colCnt; ++colNum) {
//
// Get the column name
//
ColInfo col = new ColInfo();
int colNameLen = in.read();
col.realName = in.readNonUnicodeString(colNameLen);
col.name = col.realName;
int column_flags = in.read(); /* Flags */
col.isCaseSensitive = false;
col.nullable = ((column_flags & 0x20) != 0)?
ResultSetMetaData.columnNullable:
ResultSetMetaData.columnNoNulls;
col.isWriteable = (column_flags & 0x10) != 0;
col.isIdentity = (column_flags & 0x40) != 0;
col.isKey = (column_flags & 0x02) != 0;
col.isHidden = (column_flags & 0x01) != 0;
col.userType = in.readInt();
TdsData.readType(in, col);
// Skip locale information
in.skip(1);
columns[colNum] = col;
}
endOfResults = false;
}
/**
* {@inheritDoc}.
*/
@Override
public int isNullable(final int column) throws SQLException {
final Column dto = this.getColumn(column);
if (dto.isNullable()) {
return ResultSetMetaData.columnNullable;
}
return ResultSetMetaData.columnNoNulls;
}
/**
* Process a TDS 4.2 column format token.
*
* @throws IOException
* @throws ProtocolException
*/
private void tds4ColFormatToken()
throws IOException, ProtocolException {
final int pktLen = in.readShort();
int bytesRead = 0;
int numColumns = 0;
while (bytesRead < pktLen) {
if (numColumns > columns.length) {
throw new ProtocolException("Too many columns in TDS_COL_FMT packet");
}
ColInfo col = columns[numColumns];
if (serverType == Driver.SQLSERVER) {
col.userType = in.readShort();
int flags = in.readShort();
col.nullable = ((flags & 0x01) != 0)?
ResultSetMetaData.columnNullable:
ResultSetMetaData.columnNoNulls;
col.isCaseSensitive = (flags & 0x02) != 0;
col.isWriteable = (flags & 0x0C) != 0;
col.isIdentity = (flags & 0x10) != 0;
} else {
// Sybase does not send column flags
col.isCaseSensitive = false;
col.isWriteable = true;
if (col.nullable == ResultSetMetaData.columnNoNulls) {
col.nullable = ResultSetMetaData.columnNullableUnknown;
}
col.userType = in.readInt();
}
bytesRead += 4;
bytesRead += TdsData.readType(in, col);
numColumns++;
}
if (numColumns != columns.length) {
throw new ProtocolException("Too few columns in TDS_COL_FMT packet");
}
endOfResults = false;
}
@Override
public int isNullable(int index) throws SQLException {
return getParam(index).isNullable() ? ResultSetMetaData.columnNullable : ResultSetMetaData.columnNoNulls;
}
/**
*
* @param table
* @param formDefinition
* @param vendor
* @param md
* @return
*/
public String[] formAdjustTable(String table, String[] formDefinition, String vendor, ResultSetMetaData md) {
ArrayList<String> rv = new ArrayList<String>();
for (String formField : formDefinition) {
Properties info = parseFormString(formField);
String field = info.getProperty("field", null);
String type = info.getProperty("type", null);
if ( "header".equals(type) ) continue;
String maxs = adjustMax(info.getProperty("maxlength", null));
int maxlength = 0;
if (maxs != null) maxlength = (new Integer(maxs)).intValue();
if (maxlength < 1) maxlength = 80;
String sqlType = null;
boolean autoIncrement = false;
int sqlLength = -1;
boolean isNullable = false;
try {
for( int i = 1; i <= md.getColumnCount(); i++ ) {
if ( field.equalsIgnoreCase(md.getColumnLabel(i)) ) {
sqlLength = md.getColumnDisplaySize(i);
autoIncrement = md.isAutoIncrement(i);
sqlType = getSuperType(md.getColumnClassName(i));
isNullable = (md.isNullable(i) == ResultSetMetaData.columnNullable);
break;
}
}
} catch(Exception e) {
// ignore
}
log.debug("{} ({}) type={}", field, maxlength, type);
log.debug("{} ({}) auto={} type={} null={}", field, sqlLength, autoIncrement, sqlType, isNullable);
// If the field is not there...
if ( sqlType == null ) {
if ( "oracle".equals(vendor) ) {
rv.add("ALTER TABLE "+table+" ADD ( " + formSql(formField, vendor) + " )");
} else if ( "mysql".equals(vendor) ) {
rv.add("ALTER TABLE "+table+" ADD " + formSql(formField, vendor));
} else {
rv.add("ALTER TABLE "+table+" ADD COLUMN " + formSql(formField, vendor));
}
continue;
}
String ff = formSql(formField, vendor);
// BLTI-220, BLTI-238 - Required will be enforced in software - not the DB
boolean shouldAlter = false;
if ("key".equals(type)) {
if ( ! NUMBER_TYPE.equals(sqlType) ) log.warn("{} must be Integer and Auto Increment", field);
} else if ("autodate".equals(type)) {
} else if ("url".equals(type) || "text".equals(type) || "textarea".equals(type)) {
if ( "oracle.sql.CLOB".equals(sqlType) || "oracle.jdbc.OracleClob".equals(sqlType) ) continue; // CLOBS large enough :)
if ( ! STRING_TYPE.equals(sqlType)) {
log.warn("{} must be String field", field);
continue;
}
if ( sqlLength < maxlength ) shouldAlter = true;
if ( ! isNullable ) shouldAlter = true; // BLTI-220, BLTI-238
// shouldAlter = true; // Temporary SAK-31695 to force ALTER statements to be emitted
} else if ("radio".equals(type) || "checkbox".equals(type) || "integer".equals(type) ) {
if ( NUMBER_TYPE.equals(sqlType)) continue;
log.warn("{} must be Integer field", field);
}
if ( shouldAlter ) {
if ( "oracle".equals(vendor) ) {
rv.add("ALTER TABLE "+table+" MODIFY ( " + ff + " )");
} else if ( "mysql".equals(vendor) ) {
rv.add("ALTER TABLE "+table+" MODIFY " + ff);
} else {
rv.add("ALTER TABLE "+table+" ALTER COLUMN " + ff);
}
}
}
return rv.toArray(new String[rv.size()]);
}
/**
* Process Sybase 12+ wide result token which provides enhanced
* column meta data.
*
* @throws IOException
*/
private void tds5WideResultToken()
throws IOException, ProtocolException
{
in.readInt(); // Packet length
int colCnt = in.readShort();
columns = new ColInfo[colCnt];
rowData = new Object[colCnt];
tables = null;
for (int colNum = 0; colNum < colCnt; ++colNum) {
ColInfo col = new ColInfo();
//
// Get the alias name
//
int nameLen = in.read();
col.name = in.readNonUnicodeString(nameLen);
//
// Get the catalog name
//
nameLen = in.read();
col.catalog = in.readNonUnicodeString(nameLen);
//
// Get the schema name
//
nameLen = in.read();
col.schema = in.readNonUnicodeString(nameLen);
//
// Get the table name
//
nameLen = in.read();
col.tableName = in.readNonUnicodeString(nameLen);
//
// Get the column name
//
nameLen = in.read();
col.realName = in.readNonUnicodeString(nameLen);
if (col.name == null || col.name.length() == 0) {
col.name = col.realName;
}
int column_flags = in.readInt(); /* Flags */
col.isCaseSensitive = false;
col.nullable = ((column_flags & 0x20) != 0)?
ResultSetMetaData.columnNullable:
ResultSetMetaData.columnNoNulls;
col.isWriteable = (column_flags & 0x10) != 0;
col.isIdentity = (column_flags & 0x40) != 0;
col.isKey = (column_flags & 0x02) != 0;
col.isHidden = (column_flags & 0x01) != 0;
col.userType = in.readInt();
TdsData.readType(in, col);
// Skip locale information
in.skip(1);
columns[colNum] = col;
}
endOfResults = false;
}
@Override
public int isNullable(int column) throws SQLException {
return ResultSetMetaData.columnNullable;
}
@Override
public int isNullable(int column) throws SQLException {
return rowProjector.getColumnProjector(column-1).getExpression().isNullable() ? ResultSetMetaData.columnNullable : ResultSetMetaData.columnNoNulls;
}
@Override
public int isNullable(int column) throws SQLException {
return ResultSetMetaData.columnNullable;
}
public int isNullable() {
return ResultSetMetaData.columnNullable;
}
/**
* Process TDS 5 Dynamic results parameter descriptors.
* <p>
* With Sybase 12+ this has been superseded by the TDS5_PARAMFMT2_TOKEN
* except when used to return extended error information.
*
* @throws IOException
* @throws ProtocolException
*/
private void tds5ParamFmtToken() throws IOException, ProtocolException {
in.readShort(); // Packet length
int paramCnt = in.readShort();
ColInfo[] params = new ColInfo[paramCnt];
for (int i = 0; i < paramCnt; i++) {
//
// Get the parameter details using the
// ColInfo class as the server format is the same.
//
ColInfo col = new ColInfo();
int colNameLen = in.read();
col.realName = in.readNonUnicodeString(colNameLen);
int column_flags = in.read(); /* Flags */
col.isCaseSensitive = false;
col.nullable = ((column_flags & 0x20) != 0)?
ResultSetMetaData.columnNullable:
ResultSetMetaData.columnNoNulls;
col.isWriteable = (column_flags & 0x10) != 0;
col.isIdentity = (column_flags & 0x40) != 0;
col.isKey = (column_flags & 0x02) != 0;
col.isHidden = (column_flags & 0x01) != 0;
col.userType = in.readInt();
if ((byte)in.peek() == TDS_DONE_TOKEN) {
// Sybase 11.92 bug data type missing!
currentToken.dynamParamInfo = null;
currentToken.dynamParamData = null;
// error trapped in sybasePrepare();
messages.addDiagnostic(9999, 0, 16,
"Prepare failed", "", "", 0);
return; // Give up
}
TdsData.readType(in, col);
// Skip locale information
in.skip(1);
params[i] = col;
}
currentToken.dynamParamInfo = params;
currentToken.dynamParamData = new Object[paramCnt];
}
@Override
public int isNullable(int column) throws SQLException {
return ResultSetMetaData.columnNullable;
}
/**
* Is the data type nullable.
*
* @param dtd
* data type descriptor
*/
public static int isNullable(DataTypeDescriptor dtd) {
return dtd.isNullable() ? ResultSetMetaData.columnNullable
: ResultSetMetaData.columnNoNulls;
}