Confusion with DatabaseMetadata.getTypeInfo.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Confusion with DatabaseMetadata.getTypeInfo.

    I am copying from a Database to another Database.
    I am generalizing this program.
    Actually to reduce the overhead of Data Type mapping between the source Database and target Database.
    What I am doing ..... I am getting the Table column info using DatabaseMetadat a.getColumns and there I getting the Column type of java.sql.Types.
    Now to map these types with the target Database types I am retrieving the SQL types using DatabaseMetadat a.getTypeInfo.
    Now my confusion is some constant values are missing with a particular Database(here Postgres)
    If my Source Database's Column Type matches with a Constant Value which is missing in my Target Database then what should I be doing?
    Should I map this manually?
    Please Help!

    Debasis Jana
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by dmjpro
    I am copying from a Database to another Database.
    I am generalizing this program.
    Actually to reduce the overhead of Data Type mapping between the source Database and target Database.
    What I am doing ..... I am getting the Table column info using DatabaseMetadat a.getColumns and there I getting the Column type of java.sql.Types.
    Now to map these types with the target Database types I am retrieving the SQL types using DatabaseMetadat a.getTypeInfo.
    Now my confusion is some constant values are missing with a particular Database(here Postgres)
    If my Source Database's Column Type matches with a Constant Value which is missing in my Target Database then what should I be doing?
    Should I map this manually?
    Please Help!

    Debasis Jana
    Anybody can help me out of this?

    Debasis Jana

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by dmjpro
      Now my confusion is some constant values are missing with a particular Database(here Postgres)
      ..
      Could you post an example of the missing constant values?

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by r035198x
        Could you post an example of the missing constant values?
        Thanks r035198x!
        Actually the does vendor take all the constant values and put a SQL Type Name against name?

        Right now i don't remember
        OK let me check .. I have a code to run then i ll check manually what constant values are missing.

        Debasis Jana

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by dmjpro
          Thanks r035198x!
          Actually the does vendor take all the constant values and put a SQL Type Name against name?

          Right now i don't remember
          OK let me check .. I have a code to run then i ll check manually what constant values are missing.

          Debasis Jana
          I know the answer already without 'running any code'. Every compliant JDBC
          interface has to supply a complete Type interface. That doesn't imply that all
          those are actually implemented by the database engine. That's where the
          DatabaseMetaDat a comes in.

          I'm afraid this turns out to be 'one of those questions' ...

          kind regards,

          Jos

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by JosAH
            I know the answer already without 'running any code'. Every compliant JDBC
            interface has to supply a complete Type interface. That doesn't imply that all
            those are actually implemented by the database engine. That's where the
            DatabaseMetaDat a comes in.

            I'm afraid this turns out to be 'one of those questions' ...

            kind regards,

            Jos
            hahahahahaah!
            yeah my behaviour maid you make a such comment!
            Actually i run this code.
            What you think that also I tried but I got surprised some constant values are missing.
            Neways I am now ...trying to run this code.

            Debasis Jana.

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Originally posted by JosAH
              I know the answer already without 'running any code'. Every compliant JDBC
              interface has to supply a complete Type interface. That doesn't imply that all
              those are actually implemented by the database engine. That's where the
              DatabaseMetaDat a comes in.

              I'm afraid this turns out to be 'one of those questions' ...

              kind regards,

              Jos

              Josh here is my Code.
              [code=java]
              ResultSet data_types = db_metadata.get TypeInfo();
              while(data_type s.next()){
              int type = data_types.getI nt("DATA_TYPE") ;
              String type_name = data_types.getS tring("TYPE_NAM E");

              System.out.prin tln("Type: " + type + "\tType Name: " + type_name);
              }
              [/code]

              and the missing constants are ....

              BLOB 2004
              BOOLEAN 16
              CLOB 2005
              DECIMAL 3

              Now I didn't check all the constants.
              But If i get any missing then what should i be doing?
              What I am trying to Josh to map a source SQL Type to a target SQL type.
              Source and target database may not be same.

              Debasis Jana.

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                See this link guys.

                postgressql bugs
                Now one more problem ....
                I am now converting from Sybase to Postgres.
                Now suppose Sybase implements java.sql.Types. XXX of size 10 but postgres may not implement this type of size more than 10.

                Actually what my code does first i am extracting the column types as java.sql.Types. XXX and the column size
                Now to convert it into the target database i am calling this method

                [code=java]
                /**
                * This method returns a Database specific column type for a java.sql.Types. XXXX.
                * This also checks that whether the found datatype supports the size given by the source.
                * @param sourceType The java.sql.Types. XXX value.
                * @param sourceSize The source database cloumn size.
                * @param targetConn The target connection for which the column name to be obtained.
                * @return It returns column name for java.sql.Types. XXXX.
                * If not found for java.sql.Types. XXX then returns null;
                * @exception It throws SQLException if any database operation fails.
                */
                public String getColumnType(i nt sourceType,int sourceSize,Conn ection targetConn) throws SQLException{
                System.out.prin tln("Source Type: " + sourceType + "\tSource Size: " + sourceSize);
                DatabaseMetaDat a db_metadadta = targetConn.getM etaData();
                ResultSet rs_columns = db_metadadta.ge tTypeInfo();
                while(rs_column s.next()){
                int type = rs_columns.getI nt("DATA_TYPE") ;
                String name = rs_columns.getS tring("TYPE_NAM E");
                short min_scale = rs_columns.getS hort("MINIMUM_S CALE"),
                max_scale = rs_columns.getS hort("MAXIMUM_S CALE");
                if(sourceType== type){
                System.out.prin tln("Column Type Name: " + name + "\tMinimum Scale: " + min_scale + "\tMaximum Sclae: " + max_scale);
                if(sourceSize>= min_scale && sourceSize<=max _scale){
                rs_columns.getS tatement().clos e();
                return name;
                }
                }
                }
                rs_columns.getS tatement().clos e();
                return null;
                }
                [/code]

                Hope all the experts can get my point.

                Debasis Jana.

                Comment

                • dmjpro
                  Top Contributor
                  • Jan 2007
                  • 2476

                  #9
                  Originally posted by dmjpro
                  See this link guys.

                  postgressql bugs
                  Now one more problem ....
                  I am now converting from Sybase to Postgres.
                  Now suppose Sybase implements java.sql.Types. XXX of size 10 but postgres may not implement this type of size more than 10.

                  Actually what my code does first i am extracting the column types as java.sql.Types. XXX and the column size
                  Now to convert it into the target database i am calling this method

                  [code=java]
                  /**
                  * This method returns a Database specific column type for a java.sql.Types. XXXX.
                  * This also checks that whether the found datatype supports the size given by the source.
                  * @param sourceType The java.sql.Types. XXX value.
                  * @param sourceSize The source database cloumn size.
                  * @param targetConn The target connection for which the column name to be obtained.
                  * @return It returns column name for java.sql.Types. XXXX.
                  * If not found for java.sql.Types. XXX then returns null;
                  * @exception It throws SQLException if any database operation fails.
                  */
                  public String getColumnType(i nt sourceType,int sourceSize,Conn ection targetConn) throws SQLException{
                  System.out.prin tln("Source Type: " + sourceType + "\tSource Size: " + sourceSize);
                  DatabaseMetaDat a db_metadadta = targetConn.getM etaData();
                  ResultSet rs_columns = db_metadadta.ge tTypeInfo();
                  while(rs_column s.next()){
                  int type = rs_columns.getI nt("DATA_TYPE") ;
                  String name = rs_columns.getS tring("TYPE_NAM E");
                  short min_scale = rs_columns.getS hort("MINIMUM_S CALE"),
                  max_scale = rs_columns.getS hort("MAXIMUM_S CALE");
                  if(sourceType== type){
                  System.out.prin tln("Column Type Name: " + name + "\tMinimum Scale: " + min_scale + "\tMaximum Sclae: " + max_scale);
                  if(sourceSize>= min_scale && sourceSize<=max _scale){
                  rs_columns.getS tatement().clos e();
                  return name;
                  }
                  }
                  }
                  rs_columns.getS tatement().clos e();
                  return null;
                  }
                  [/code]

                  Hope all the experts can get my point.

                  Debasis Jana.

                  Guys I solved that problem. What I am doing for those missing values I am doing it hard-code. One more thing which has minimum-scale and maximum scale zero that can't be of any size.

                  Debasis Jana

                  Comment

                  Working...