Java Code Examples for java.sql.Types#CHAR

The following examples show how to use java.sql.Types#CHAR . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Cursor.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public final float getFloat(int column) throws SQLException {
    switch (jdbcTypes_[column - 1]) {
    case Types.BOOLEAN:
        return CrossConverters.getFloatFromBoolean(get_BOOLEAN(column));
    case Types.REAL:
        return get_FLOAT(column);
    case Types.DOUBLE:
        return CrossConverters.getFloatFromDouble(get_DOUBLE(column));
    case Types.DECIMAL:
        // For performance we don't materialize the BigDecimal, but convert directly from decimal bytes to a long.
        return CrossConverters.getFloatFromDouble(getDoubleFromDECIMAL(column));
    case Types.SMALLINT:
        return (float) get_SMALLINT(column);
    case Types.INTEGER:
        return (float) get_INTEGER(column);
    case Types.BIGINT:
        return (float) get_BIGINT(column);
    case Types.CHAR:
        return CrossConverters.getFloatFromString(get_CHAR(column));
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
        return CrossConverters.getFloatFromString(get_VARCHAR(column));
    default:
        throw coercionError( "float", column );
    }
}
 
Example 2
Source File: MatchedColumnsHelper.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public List<ColumnInfo> getStringTypeColumns(TableInfo table, int numCols) {
  List<ColumnInfo> columns = table.getColumnList();
  int colCnt = columns.size();
  List<ColumnInfo> partColumns = new ArrayList<ColumnInfo>();
  for (int i = 0; i < numCols; i++) {
    ColumnInfo col = null;
    boolean f = false;
    for (int j = 0; j < 10 && !f; j++) {
      col = columns.get(random.nextInt(colCnt));
      if ((col.getColumnType() == Types.CHAR
          || col.getColumnType() == Types.LONGNVARCHAR || col.getColumnType() == Types.VARCHAR)
          && !partColumns.contains(col)) {
        f = true;
      }
    }
    if (f)
      partColumns.add(col);
  }
  return partColumns;
}
 
Example 3
Source File: BinaryDecimal.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Set this DECIMAL value from another DataValueDescriptor
 */
protected void setFrom(DataValueDescriptor dvd) throws StandardException
{

	switch (dvd.typeToBigDecimal())
	{
		case Types.CHAR:
		case Types.DECIMAL: // TODO : direct copy
			
			setValue(dvd.getString());
			break;
		case Types.BIGINT:
			setValue(dvd.getLong());
		    break;
		default:
			super.setFrom(dvd);
	}
}
 
Example 4
Source File: DateTypeCompiler.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * User types are storable into other user types that they
 * are assignable to. The other type must be a subclass of
 * this type, or implement this type as one of its interfaces.
 *
 * Built-in types are also storable into user types when the built-in
 * type's corresponding Java type is assignable to the user type.
 *
 * @param otherType the type of the instance to store into this type.
 * @param cf		A ClassFactory
 * @return true if otherType is storable into this type, else false.
 */
public boolean storable(TypeId otherType, ClassFactory cf)
{
	int	otherJDBCTypeId = otherType.getJDBCTypeId();

	if (otherJDBCTypeId == Types.DATE ||
		(otherJDBCTypeId == Types.CHAR) ||
		(otherJDBCTypeId == Types.VARCHAR))
	{
		return true;
	}

	return cf.getClassInspector().assignableTo(
		   otherType.getCorrespondingJavaTypeName(),
		   "java.sql.Date");
}
 
Example 5
Source File: SingleColumnStringAttribute.java    From reladomo with Apache License 2.0 5 votes vote down vote up
public boolean verifyColumn(ColumnInfo info)
{
    if (info.isNullable() != this.isNullable()) return false;
    if (this.getMaxLength() != Integer.MAX_VALUE)
    {
        return info.getSize() == this.getMaxLength();
    }
    // the negative numbers are from JDK 1.6 N* types, like NCHAR
    return info.getType() == Types.CHAR || info.getType() == Types.VARCHAR || info.getType() == -15 || info.getType() == Types.LONGVARCHAR || info.getType() == -16 || info.getType() == -9;
}
 
Example 6
Source File: Heading.java    From sql4es with Apache License 2.0 5 votes vote down vote up
public static int getTypeIdForObject(Object c) {
	if (c instanceof Long)
		return Types.BIGINT;
	if (c instanceof Boolean)
		return Types.BOOLEAN;
	if (c instanceof Character)
		return Types.CHAR;
	if (c instanceof Timestamp)
		return Types.TIMESTAMP;
	if (c instanceof java.sql.Date)
		return Types.DATE;
	if (c instanceof java.util.Date)
		return Types.DATE;
	if (c instanceof Double)
		return Types.DOUBLE;
	if (c instanceof Integer)
		return Types.INTEGER;
	if (c instanceof BigDecimal)
		return Types.NUMERIC;
	if (c instanceof Short)
		return Types.SMALLINT;
	if (c instanceof Float)
		return Types.FLOAT;
	if (c instanceof String)
		return Types.VARCHAR;
	if (c instanceof Time)
		return Types.TIME;
	if (c instanceof Byte)
		return Types.TINYINT;
	if (c instanceof Byte[])
		return Types.VARBINARY;
	if(c instanceof Object[])
		return Types.JAVA_OBJECT;
	if(c instanceof Object)
		return Types.JAVA_OBJECT;
	if (c instanceof Array)
		return Types.ARRAY;
	else
		return Types.OTHER;
}
 
Example 7
Source File: JdbcLink.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
public static int generalType(int t){
	switch (t) {
	case Types.BIGINT:
	case Types.BIT:
	case Types.BOOLEAN:
	case Types.INTEGER:
	case Types.SMALLINT:
	case Types.TINYINT:
		return INTEGRAL;
	
	case Types.VARCHAR:
	case Types.CHAR:
	case Types.LONGVARCHAR:
		return TEXT;
	
	case Types.BINARY:
	case Types.BLOB:
	case Types.CLOB:
	case Types.LONGVARBINARY:
	case Types.VARBINARY:
		return BINARY;
	
	default:
		return OTHER;
	
	}
}
 
Example 8
Source File: Type1BlobResourcesConversionHandler.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public Object getSource(String id, ResultSet rs) throws SQLException
{
	ResultSetMetaData metadata = rs.getMetaData();
	String rv = null;
	switch(metadata.getColumnType(1))
	{
	case Types.BLOB:
		Blob blob = rs.getBlob(1);
		if(blob != null)
		{
			rv = new String(blob.getBytes(1L, (int) blob.length()));
		}
		break;
	case Types.CLOB:
		Clob clob = rs.getClob(1);
		if(clob != null)
		{
			rv = clob.getSubString(1L, (int) clob.length());
		}
		break;
	case Types.CHAR:
	case Types.LONGVARCHAR:
	case Types.VARCHAR:
	case Types.BINARY:
	case Types.VARBINARY:
	case Types.LONGVARBINARY:
		byte[] bytes = rs.getBytes(1);
		if(bytes != null)
		{
			rv = new String(bytes);
		}
		break;
	}
	return rv;
}
 
Example 9
Source File: Type1BlobResourcesConversionHandler.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public Object getValidateSource(String id, ResultSet rs) throws SQLException
{
	ResultSetMetaData metadata = rs.getMetaData();
	byte[] rv = null;
	switch(metadata.getColumnType(1))
	{
	case Types.BLOB:
		Blob blob = rs.getBlob(1);
		if(blob != null)
		{
			rv = blob.getBytes(1L, (int) blob.length());
		}
		else
		{
			log.info("getValidateSource(" + id + ") blob is null" );
		}
		break;
	case Types.CLOB:
		Clob clob = rs.getClob(1);
		if(clob != null)
		{
			rv = clob.getSubString(1L, (int) clob.length()).getBytes();
		}
		break;
	case Types.CHAR:
	case Types.LONGVARCHAR:
	case Types.VARCHAR:
		rv = rs.getString(1).getBytes();
		break;
	case Types.BINARY:
	case Types.VARBINARY:
	case Types.LONGVARBINARY:
		rv = rs.getBytes(1);
		break;
	}
	return rv;

	//return rs.getBytes(1);
}
 
Example 10
Source File: DatabaseMetaDataTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method for testing getIndexInfo - calls dmd.getIndexInfo for the
 * JDBC call, and getIndexInfoODBC for the ODBC procedure
 * @throws SQLException
 */
private ResultSet[] getIndexInfo(String catalog, String schema, String table,
    boolean unique, boolean approximate) throws SQLException
{
    ResultSet[] rss = new ResultSet[2];
    DatabaseMetaData dmd = getDMD();
    rss[0]= dmd.getIndexInfo(catalog, schema, table, unique, approximate);
    rss[1]= getIndexInfoODBC(catalog, schema, table, unique, approximate);

    String [] columnNames = {"TABLE_CAT","TABLE_SCHEM","TABLE_NAME",
            "NON_UNIQUE","INDEX_QUALIFIER","INDEX_NAME","TYPE",
            "ORDINAL_POSITION","COLUMN_NAME","ASC_OR_DESC","CARDINALITY",
            "PAGES","FILTER_CONDITION"};
    int [] columnTypes = {
            Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
            Types.BOOLEAN,Types.VARCHAR,Types.VARCHAR,Types.SMALLINT,
            // ASC_OR_DESC is Types.CHAR rather than VARCHAR...
            Types.SMALLINT,Types.VARCHAR,Types.CHAR,Types.INTEGER,
            Types.INTEGER,Types.VARCHAR};

    // types.boolean is not supported with networkserver
    if (usingDerbyNetClient())
      columnTypes[4 - 1] = Types.SMALLINT;

    boolean [] nullability = {false,false,false,
        false,false,true,true,true,false,false,true,true,true};

    // JDBC result set
    assertMetaDataResultSet(rss[0], columnNames, columnTypes, nullability);

    // Change shape for ODBC.
    columnTypes[4 - 1] = Types.SMALLINT; // types.boolean is not supported with ODBC

    // ODBC result set
    assertMetaDataResultSet(rss[1], columnNames, columnTypes, nullability);

    return rss;
}
 
Example 11
Source File: JDBCAdapter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Class<?> getColumnClass(int column) {
    int type;
    try {
        type = metaData.getColumnType(column + 1);
    } catch (SQLException e) {
        return super.getColumnClass(column);
    }

    switch (type) {
        case Types.CHAR:
        case Types.VARCHAR:
        case Types.LONGVARCHAR:
            return String.class;

        case Types.BIT:
            return Boolean.class;

        case Types.TINYINT:
        case Types.SMALLINT:
        case Types.INTEGER:
            return Integer.class;

        case Types.BIGINT:
            return Long.class;

        case Types.FLOAT:
        case Types.DOUBLE:
            return Double.class;

        case Types.DATE:
            return java.sql.Date.class;

        default:
            return Object.class;
    }
}
 
Example 12
Source File: TypeDescriptorImpl.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Write this object to a stream of stored objects.
 *
 * @param out write bytes here.
 *
 * @exception IOException		thrown on error
 */
public void writeExternal( ObjectOutput out )
	 throws IOException
{
	out.writeObject( typeId );
	out.writeInt( precision );

	//Scale does not apply to character data types. Starting 10.3 release,
	//the scale field in TypeDescriptor in SYSCOLUMNS will be used to save
	//the collation type of the character data types. Because of this, in
	//this method, we check if we are dealing with character types. If yes,
	//then write the collation type into the on-disk scale field of 
	//TypeDescriptor. But if we are dealing with non-character data types,
	//then write the scale of that data type into the on-disk scale field
	//of TypeDescriptor. In other words, the on-disk scale field has 2 
	//different meanings depending on what kind of data type we are dealing 
	//with. For character data types, it really represents the collation 
	//type of the character data type. For all the other data types, it 
	//represents the scale of that data type.
	switch (typeId.getJDBCTypeId()) {
	case Types.CHAR:
	case Types.VARCHAR:
	case Types.LONGVARCHAR:
	case Types.CLOB:
		out.writeInt( collationType );
		break;
	default:
		out.writeInt( scale );
		break;
	}		
	
	out.writeBoolean( isNullable );
	out.writeInt( maximumWidth );
	if (typeId.getJDBCTypeId() == Types.ARRAY) {
           out.writeBoolean(children != null);
           if (children != null) {
               out.writeInt(children.length);
               for (TypeDescriptor aChildren : children) {
                   out.writeObject(aChildren);
               }
           }
       }
}
 
Example 13
Source File: TeradataDialect.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String getSelectClauseNullString(int sqlType) {
	String v = "null";

	switch ( sqlType ) {
		case Types.BIT:
		case Types.TINYINT:
		case Types.SMALLINT:
		case Types.INTEGER:
		case Types.BIGINT:
		case Types.FLOAT:
		case Types.REAL:
		case Types.DOUBLE:
		case Types.NUMERIC:
		case Types.DECIMAL:
			v = "cast(null as decimal)";
			break;
		case Types.CHAR:
		case Types.VARCHAR:
		case Types.LONGVARCHAR:
			v = "cast(null as varchar(255))";
			break;
		case Types.DATE:
		case Types.TIME:
		case Types.TIMESTAMP:
			v = "cast(null as timestamp)";
			break;
		case Types.BINARY:
		case Types.VARBINARY:
		case Types.LONGVARBINARY:
		case Types.NULL:
		case Types.OTHER:
		case Types.JAVA_OBJECT:
		case Types.DISTINCT:
		case Types.STRUCT:
		case Types.ARRAY:
		case Types.BLOB:
		case Types.CLOB:
		case Types.REF:
		case Types.DATALINK:
		case Types.BOOLEAN:
			break;
	}
	return v;
}
 
Example 14
Source File: SQL92.java    From GeoTriples with Apache License 2.0 4 votes vote down vote up
public DataType getDataType(int jdbcType, String name, int size) {
	// TODO: These are in java.sql.Types as of Java 6 but not yet in Java 1.5
	if ("NCHAR".equals(name) || "NVARCHAR".equals(name) || "NCLOB".equals(name)) {
		return new SQLCharacterStringVarying(name, true);
	}

	
	switch (jdbcType) {
	case Types.CHAR:
		return new SQLCharacterString(name, true);

	case Types.VARCHAR:
	case Types.LONGVARCHAR:
	case Types.CLOB:
		return new SQLCharacterStringVarying(name, true);
		
	case Types.BOOLEAN:
		return new SQLBoolean(name);

	case Types.BINARY:
	case Types.VARBINARY:
	case Types.LONGVARBINARY:
	case Types.BLOB:
		return new SQLBinary(name, true);
		
	case Types.BIT:
		return new SQLBit(name);

	case Types.NUMERIC:
	case Types.DECIMAL:
	case Types.TINYINT:
	case Types.SMALLINT:
	case Types.INTEGER:
	case Types.BIGINT:
		return new SQLExactNumeric(name, jdbcType, false);
		
	case Types.REAL:
	case Types.FLOAT:
	case Types.DOUBLE:
		return new SQLApproximateNumeric(name);
	
	case Types.DATE:
		return new SQLDate(name);
		
	case Types.TIME:
		return new SQLTime(name);
		
	case Types.TIMESTAMP:
		return new SQLTimestamp(name);

	case Types.ARRAY:
	case Types.JAVA_OBJECT:
		return new UnsupportedDataType(jdbcType, name);
		
	// TODO: What about the remaining java.sql.Types?
	case Types.DATALINK:
	case Types.DISTINCT:
	case Types.NULL:
	case Types.OTHER:
	case Types.REF:
	}
	
	return null;
}
 
Example 15
Source File: MysqlDefs.java    From tddl5 with Apache License 2.0 4 votes vote down vote up
public static int javaTypeMysql(int javaType) {
    switch (javaType) {
        case Types.NUMERIC:
        case Types.DECIMAL:
            return MysqlDefs.FIELD_TYPE_DECIMAL;

        case Types.TINYINT:
            return MysqlDefs.FIELD_TYPE_TINY;

        case Types.SMALLINT:
            return MysqlDefs.FIELD_TYPE_SHORT;

        case Types.INTEGER:
            return MysqlDefs.FIELD_TYPE_LONG;

        case Types.REAL:
        case Types.FLOAT:
            return MysqlDefs.FIELD_TYPE_FLOAT;

        case Types.DOUBLE:
            return MysqlDefs.FIELD_TYPE_DOUBLE;

        case Types.NULL:
            return MysqlDefs.FIELD_TYPE_NULL;

        case Types.TIMESTAMP:
            return MysqlDefs.FIELD_TYPE_TIMESTAMP;

        case Types.BIGINT:
            return MysqlDefs.FIELD_TYPE_LONGLONG;

        case Types.DATE:
            return MysqlDefs.FIELD_TYPE_DATE;

        case Types.TIME:
            return MysqlDefs.FIELD_TYPE_TIME;

        case Types.VARBINARY:
            return MysqlDefs.FIELD_TYPE_TINY_BLOB;

        case Types.LONGVARBINARY:
            return MysqlDefs.FIELD_TYPE_BLOB;

        case Types.VARCHAR:
            return MysqlDefs.FIELD_TYPE_VAR_STRING;

        case Types.CHAR:
            return MysqlDefs.FIELD_TYPE_STRING;

        case Types.BINARY:
            return MysqlDefs.FIELD_TYPE_GEOMETRY;

        case Types.BIT:
            return MysqlDefs.FIELD_TYPE_BIT;

        case Types.CLOB:
            return MysqlDefs.FIELD_TYPE_VAR_STRING;

        case Types.BLOB:
            return MysqlDefs.FIELD_TYPE_BLOB;

        case Types.BOOLEAN:
            return MysqlDefs.FIELD_TYPE_TINY;

        case DataType.YEAR_SQL_TYPE:
            return MysqlDefs.FIELD_TYPE_YEAR;

        default:
            return MysqlDefs.FIELD_TYPE_STRING;
    }

}
 
Example 16
Source File: MDbMetaData.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
private static Integer getPreferredScalarNativeDataType( Set<Integer> nativeDataTypes )
{
    if( nativeDataTypes.isEmpty() )
        return NULL_NATIVE_DATA_TYPE;          // none available
    if( nativeDataTypes.size() == 1 )
        return nativeDataTypes.iterator().next();   // return the only data type available
    
    // more than one native data types in field

    if( nativeDataTypes.contains( STRING_NATIVE_DATA_TYPE ) )
        return STRING_NATIVE_DATA_TYPE;    // String data type takes precedence over other scalar types
    
    // check if any of the native data types map to an ODA String
    Set<Integer> nonStringNativeDataTypes = new HashSet<Integer>( nativeDataTypes.size() );
    for( Integer nativeDataType : nativeDataTypes )
    {
        if( nativeDataType == NULL_NATIVE_DATA_TYPE || 
                nativeDataType == ARRAY_NATIVE_DATA_TYPE || nativeDataType == OBJECT_NATIVE_DATA_TYPE )
            continue;   // skip non-scalar data types
        int odaDataType = ManifestExplorer.getInstance( ).getDefaultOdaDataTypeCode( nativeDataType, 
                            MongoDBDriver.ODA_DATA_SOURCE_ID, MDbQuery.ODA_DATA_SET_ID );
        if( odaDataType == Types.CHAR )     // maps to ODA String data type
            return nativeDataType;    // String data type takes precedence over other scalar types

        nonStringNativeDataTypes.add( nativeDataType );
    }
    
    if( nonStringNativeDataTypes.isEmpty() )
        return NULL_NATIVE_DATA_TYPE;          // none available
    if( nonStringNativeDataTypes.size() == 1 )
        return nonStringNativeDataTypes.iterator().next();   // return first element by default
    
    // more than one native data types in field are not mapped to ODA String;
    // check if they have mixed data type categories.
    boolean isNumeric = nonStringNativeDataTypes.contains( NUMBER_NATIVE_DATA_TYPE ) ||
                nonStringNativeDataTypes.contains( NUMBER_INT_NATIVE_DATA_TYPE ) ||
                nonStringNativeDataTypes.contains( BOOLEAN_NATIVE_DATA_TYPE);
    boolean isDatetime = nonStringNativeDataTypes.contains( DATE_NATIVE_DATA_TYPE ) ||
                nonStringNativeDataTypes.contains( TIMESTAMP_NATIVE_DATA_TYPE );
    boolean isBinary = nonStringNativeDataTypes.contains( BINARY_NATIVE_DATA_TYPE );

    if( isNumeric && !isDatetime && !isBinary )     // numeric only
    {
        if( nonStringNativeDataTypes.contains( NUMBER_NATIVE_DATA_TYPE ) )
            return NUMBER_NATIVE_DATA_TYPE;     // Number takes precedence over other numeric data types
        return NUMBER_INT_NATIVE_DATA_TYPE;     // Integer takes precedence over Boolean
    }

    if( !isNumeric && isDatetime && !isBinary )    // Date and Timestamp data types only
    {
        return TIMESTAMP_NATIVE_DATA_TYPE;      // Timestamp takes precedence over Date
    }
    
    // multiple non-String native data types must be of mixed data type categories
    return STRING_NATIVE_DATA_TYPE;     // use String to handle mixed data types
}
 
Example 17
Source File: SqoopHCatUtilities.java    From aliyun-maxcompute-data-collectors with Apache License 2.0 4 votes vote down vote up
/**
 * Resolve a database-specific type to HCat data type. Largely follows Sqoop's
 * hive translation.
 * @param sqlType
 *          sql type
 * @return hcat type
 */
public static String toHCatType(int sqlType) {
  switch (sqlType) {

  // Ideally TINYINT and SMALLINT should be mapped to their
  // HCat equivalents tinyint and smallint respectively
  // But the Sqoop Java type conversion has them mapped to Integer
  // Even though the referenced Java doc clearly recommends otherwise.
  // Changing this now can cause many of the sequence file usages to
  // break as value class implementations will change. So, we
  // just use the same behavior here.
    case Types.SMALLINT:
    case Types.TINYINT:
    case Types.INTEGER:
      return "int";

    case Types.VARCHAR:
      return "varchar";
    case Types.CHAR:
      return "char";
    case Types.LONGVARCHAR:
    case Types.NVARCHAR:
    case Types.NCHAR:
    case Types.LONGNVARCHAR:
    case Types.DATE:
    case Types.TIME:
    case Types.TIMESTAMP:
    case Types.CLOB:
      return "string";

    case Types.FLOAT:
    case Types.REAL:
      return "float";

    case Types.NUMERIC:
    case Types.DECIMAL:
      return "decimal";

    case Types.DOUBLE:
      return "double";

    case Types.BIT:
    case Types.BOOLEAN:
      return "boolean";

    case Types.BIGINT:
      return "bigint";

    case Types.BINARY:
    case Types.VARBINARY:
    case Types.BLOB:
    case Types.LONGVARBINARY:
      return "binary";

    default:
      throw new IllegalArgumentException(
        "Cannot convert SQL type to HCatalog type " + sqlType);
  }
}
 
Example 18
Source File: DBFBuiltInMemoryResultSetForColumnsListing.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * @see java.sql.ResultSet#getInt(java.lang.String)
 * @throws SQLNoSuchFieldException if the column does not exist.
 */
@Override public int getInt(String columnLabel) throws SQLNoSuchFieldException {
    logStep("getInt", columnLabel);

    switch(columnLabel) {
        // int => SQL type from java.sql.Types
        case "DATA_TYPE": {
            this.wasNull = false;
            return toSQLDataType();
        }

        // int => column size.
        case "COLUMN_SIZE": {
            this.wasNull = false;
            return toPrecision();
        }

        // int => the number of fractional digits. Null is returned for data types where DECIMAL_DIGITS is not applicable.
        case "DECIMAL_DIGITS": {
            int scale = toScale();
            this.wasNull = toScale() == -1;
            return scale == -1 ? 0 : scale;
        }

        // int => Radix (typically either 10 or 2)
        case "NUM_PREC_RADIX": {
            return 10;
        }

        /**
         * int => is NULL allowed.
         * columnNoNulls - might not allow NULL values
         * columnNullable - definitely allows NULL values
         * columnNullableUnknown - nullability unknown
         */
        case "NULLABLE": {
            this.wasNull = false;
            return DatabaseMetaData.columnNullableUnknown;
        }

        // int => unused
        case "SQL_DATA_TYPE": {
            this.wasNull = false;
            return toSQLDataType();
        }

        // int => for char types the maximum number of bytes in the column
        case "CHAR_OCTET_LENGTH": {
            if (toSQLDataType() == Types.CHAR) {
                return toPrecision();
            }

            return 0;
        }

        // int => index of column in table (starting at 1)
        case "ORDINAL_POSITION": {
            return this.columnIndex;
        }

        /**
         * Columns responding to features that aren't handled by DBase 3.
         * and return always a default value NULL or "NO"...
         */

        // short => source type of a distinct type or user-generated Ref type, SQL type from java.sql.Types (null if DATA_TYPE isn't DISTINCT or user-generated REF)
        case "SOURCE_DATA_TYPE": {
            this.wasNull = true;
            return 0;
        }

        // is not used.
        case "BUFFER_LENGTH": {
            this.wasNull = false;
            return 0;
        }

        // int => unused
        case "SQL_DATETIME_SUB": {
            this.wasNull = false;
            return 0;
        }

        default:
            // FIXME : this function is not perfect. It a column label is given that refers to a field described in getString(..) this function
            // will tell that the field doesn't exist. It's not true : the field is not numeric. But as getString(..) defaults to getInt(...),
            // getInt(..) cannot default to getString(..), else the function will run in a cycle.
            String message = format(Level.WARNING, "excp.no_desc_field", columnLabel, getTableName());
            throw new SQLNoSuchFieldException(message, "asking columns desc", getFile(), columnLabel);
    }
}
 
Example 19
Source File: ConnManager.java    From aliyun-maxcompute-data-collectors with Apache License 2.0 4 votes vote down vote up
/**
 * Resolve a database-specific type to Avro data type.
 * @param sqlType     sql type
 * @return            avro type
 */
public Type toAvroType(int sqlType) {
  switch (sqlType) {
  case Types.TINYINT:
  case Types.SMALLINT:
  case Types.INTEGER:
    return Type.INT;
  case Types.BIGINT:
    return Type.LONG;
  case Types.BIT:
  case Types.BOOLEAN:
    return Type.BOOLEAN;
  case Types.REAL:
    return Type.FLOAT;
  case Types.FLOAT:
  case Types.DOUBLE:
    return Type.DOUBLE;
  case Types.NUMERIC:
  case Types.DECIMAL:
    return Type.STRING;
  case Types.CHAR:
  case Types.VARCHAR:
  case Types.LONGVARCHAR:
  case Types.LONGNVARCHAR:
  case Types.NVARCHAR:
  case Types.NCHAR:
    return Type.STRING;
  case Types.DATE:
  case Types.TIME:
  case Types.TIMESTAMP:
    return Type.LONG;
  case Types.BLOB:
  case Types.BINARY:
  case Types.VARBINARY:
  case Types.LONGVARBINARY:
    return Type.BYTES;
  default:
    throw new IllegalArgumentException("Cannot convert SQL type "
        + sqlType);
  }
}
 
Example 20
Source File: CallableStatement.java    From r-course with MIT License 4 votes vote down vote up
/**
 * Used to fake up some metadata when we don't have access to
 * SHOW CREATE PROCEDURE or mysql.proc.
 * 
 * @throws SQLException
 *             if we can't build the metadata.
 */
private void fakeParameterTypes(boolean isReallyProcedure) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        Field[] fields = new Field[13];

        fields[0] = new Field("", "PROCEDURE_CAT", Types.CHAR, 0);
        fields[1] = new Field("", "PROCEDURE_SCHEM", Types.CHAR, 0);
        fields[2] = new Field("", "PROCEDURE_NAME", Types.CHAR, 0);
        fields[3] = new Field("", "COLUMN_NAME", Types.CHAR, 0);
        fields[4] = new Field("", "COLUMN_TYPE", Types.CHAR, 0);
        fields[5] = new Field("", "DATA_TYPE", Types.SMALLINT, 0);
        fields[6] = new Field("", "TYPE_NAME", Types.CHAR, 0);
        fields[7] = new Field("", "PRECISION", Types.INTEGER, 0);
        fields[8] = new Field("", "LENGTH", Types.INTEGER, 0);
        fields[9] = new Field("", "SCALE", Types.SMALLINT, 0);
        fields[10] = new Field("", "RADIX", Types.SMALLINT, 0);
        fields[11] = new Field("", "NULLABLE", Types.SMALLINT, 0);
        fields[12] = new Field("", "REMARKS", Types.CHAR, 0);

        String procName = isReallyProcedure ? extractProcedureName() : null;

        byte[] procNameAsBytes = null;

        try {
            procNameAsBytes = procName == null ? null : StringUtils.getBytes(procName, "UTF-8");
        } catch (UnsupportedEncodingException ueEx) {
            procNameAsBytes = StringUtils.s2b(procName, this.connection);
        }

        ArrayList<ResultSetRow> resultRows = new ArrayList<ResultSetRow>();

        for (int i = 0; i < this.parameterCount; i++) {
            byte[][] row = new byte[13][];
            row[0] = null; // PROCEDURE_CAT
            row[1] = null; // PROCEDURE_SCHEM
            row[2] = procNameAsBytes; // PROCEDURE/NAME
            row[3] = StringUtils.s2b(String.valueOf(i), this.connection); // COLUMN_NAME

            row[4] = StringUtils.s2b(String.valueOf(java.sql.DatabaseMetaData.procedureColumnIn), this.connection);

            row[5] = StringUtils.s2b(String.valueOf(Types.VARCHAR), this.connection); // DATA_TYPE
            row[6] = StringUtils.s2b("VARCHAR", this.connection); // TYPE_NAME
            row[7] = StringUtils.s2b(Integer.toString(65535), this.connection); // PRECISION
            row[8] = StringUtils.s2b(Integer.toString(65535), this.connection); // LENGTH
            row[9] = StringUtils.s2b(Integer.toString(0), this.connection); // SCALE
            row[10] = StringUtils.s2b(Integer.toString(10), this.connection); // RADIX

            row[11] = StringUtils.s2b(Integer.toString(java.sql.DatabaseMetaData.procedureNullableUnknown), this.connection); // nullable

            row[12] = null;

            resultRows.add(new ByteArrayRow(row, getExceptionInterceptor()));
        }

        java.sql.ResultSet paramTypesRs = DatabaseMetaData.buildResultSet(fields, resultRows, this.connection);

        convertGetProcedureColumnsToInternalDescriptors(paramTypesRs);
    }
}