Apache Derby RSMD.py

TableOfContents

back to ApacheDerby Example page.

#--------------------------------------------------------------------------
# RSMD() - Routine used to gather the "interesting" ResultSet MetaData
#--------------------------------------------------------------------------
def RSMD( rs, NumTypes = None ) :
  from java.sql import Types as types
  #------------------------------------------------------------------------
  # Check for first invocation, so we can initialize NumType
  #------------------------------------------------------------------------
  if NumTypes == None :
    NumTypes = [ 
      types.BIGINT  ,
      types.DECIMAL ,
      types.DOUBLE  ,
      types.FLOAT   ,
      types.INTEGER ,
      types.NUMERIC ,
      types.REAL    ,
      types.SMALLINT,
      types.TINYINT  
    ]                

  #------------------------------------------------------------------------
  # Obtain MetaData for specified ResultSet
  #------------------------------------------------------------------------
  result = ()
  rsmd   = rs.getMetaData()
  for col in range( 1, rsmd.getColumnCount() + 1 ) :
    Type = rsmd.getColumnType( col )
    if Type in NumTypes :                   # Only numeric values have...
      precision = rsmd.getPrecision( col )  # precision, and
      scale     = rsmd.getScale( col )      # scale
    else :                                  #
      precision = scale = None              #
    row =  ( rsmd.getColumnLabel( col ),
             rsmd.getColumnDisplaySize( col ),
             Type,
             precision,
             scale,
             rsmd.getColumnTypeName( col )
           )
    result += ( row, )
  return result

#--------------------------------------------------------------------------
# printRSMD() - Routine used to display the ResultSet MetaData of interest
#--------------------------------------------------------------------------
def printRSMD( rsmd, TableName ) :
  truncated = ''
  print '\n Fields contained in:', TableName
  print '\n| Size  |  Label                 |Type |Type Name'
  print '+-------+------------------------+-----+--------------------'
  for col in range( len( rsmd ) ) :
    Label, Size, Type, precision, scale, TypeName = rsmd[ col ]
    if len( Label ) > 24 :
      Label = Label[:23] + '*'
      truncated = '*'
    print '|%7d|%-24s|%5d|%-20s' % ( Size, Label, Type, TypeName )
  print
  if truncated != '' : print ' * The specified field was truncated.'