github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/sink/codec/java.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License")
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.orglicensesLICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package codec
    15  
    16  import "github.com/pingcap/parser/mysql"
    17  
    18  // JavaSQLType is the sql type defined in class java.sql.Types in java sdk
    19  type JavaSQLType int32
    20  
    21  // jdk 1.8
    22  const (
    23  	JavaSQLTypeBIT           JavaSQLType = -7
    24  	JavaSQLTypeTINYINT       JavaSQLType = -6
    25  	JavaSQLTypeSMALLINT      JavaSQLType = 5
    26  	JavaSQLTypeINTEGER       JavaSQLType = 4
    27  	JavaSQLTypeBIGINT        JavaSQLType = -5
    28  	JavaSQLTypeREAL          JavaSQLType = 7
    29  	JavaSQLTypeDOUBLE        JavaSQLType = 8
    30  	JavaSQLTypeDECIMAL       JavaSQLType = 3
    31  	JavaSQLTypeCHAR          JavaSQLType = 1
    32  	JavaSQLTypeVARCHAR       JavaSQLType = 12
    33  	JavaSQLTypeLONGVARCHAR   JavaSQLType = -1
    34  	JavaSQLTypeDATE          JavaSQLType = 91
    35  	JavaSQLTypeTIME          JavaSQLType = 92
    36  	JavaSQLTypeTIMESTAMP     JavaSQLType = 93
    37  	JavaSQLTypeBINARY        JavaSQLType = -2
    38  	JavaSQLTypeVARBINARY     JavaSQLType = -3
    39  	JavaSQLTypeLONGVARBINARY JavaSQLType = -4
    40  	JavaSQLTypeNULL          JavaSQLType = 0
    41  	JavaSQLTypeBLOB          JavaSQLType = 2004
    42  
    43  	// unused
    44  	// JavaSQLTypeFLOAT                   JavaSQLType = 6
    45  	// JavaSQLTypeNUMERIC                 JavaSQLType = 2
    46  	// JavaSQLTypeOTHER                   JavaSQLType = 1111
    47  	// JavaSQLTypeJAVA_OBJECT             JavaSQLType = 2000
    48  	// JavaSQLTypeDISTINCT                JavaSQLType = 2001
    49  	// JavaSQLTypeSTRUCT                  JavaSQLType = 2002
    50  	// JavaSQLTypeARRAY                   JavaSQLType = 2003
    51  	// JavaSQLTypeCLOB                    JavaSQLType = 2005
    52  	// JavaSQLTypeREF                     JavaSQLType = 2006
    53  	// JavaSQLTypeDATALINK                JavaSQLType = 70
    54  	// JavaSQLTypeBOOLEAN                 JavaSQLType = 16
    55  	// JavaSQLTypeROWID                   JavaSQLType = -8
    56  	// JavaSQLTypeNCHAR                   JavaSQLType = -15
    57  	// JavaSQLTypeNVARCHAR                JavaSQLType = -9
    58  	// JavaSQLTypeLONGNVARCHAR            JavaSQLType = -16
    59  	// JavaSQLTypeNCLOB                   JavaSQLType = 2011
    60  	// JavaSQLTypeSQLXML                  JavaSQLType = 2009
    61  	// JavaSQLTypeREF_CURSOR              JavaSQLType = 2012
    62  	// JavaSQLTypeTIME_WITH_TIMEZONE      JavaSQLType = 2013
    63  	// JavaSQLTypeTIMESTAMP_WITH_TIMEZONE JavaSQLType = 2014
    64  )
    65  
    66  // MysqlToJavaType converts the mysql protocol types to java sql types
    67  func MysqlToJavaType(mysqlType byte) JavaSQLType {
    68  	// see https://github.com/mysql/mysql-connector-j/blob/5.1.49/src/com/mysql/jdbc/MysqlDefs.java
    69  	switch mysqlType {
    70  	case mysql.TypeNewDecimal:
    71  		return JavaSQLTypeDECIMAL
    72  
    73  	case mysql.TypeTiny:
    74  		return JavaSQLTypeTINYINT
    75  
    76  	case mysql.TypeShort:
    77  		return JavaSQLTypeSMALLINT
    78  
    79  	case mysql.TypeLong:
    80  		return JavaSQLTypeINTEGER
    81  
    82  	case mysql.TypeFloat:
    83  		return JavaSQLTypeREAL
    84  
    85  	case mysql.TypeDouble:
    86  		return JavaSQLTypeDOUBLE
    87  
    88  	case mysql.TypeNull:
    89  		return JavaSQLTypeNULL
    90  
    91  	case mysql.TypeTimestamp:
    92  		return JavaSQLTypeTIMESTAMP
    93  
    94  	case mysql.TypeLonglong:
    95  		return JavaSQLTypeBIGINT
    96  
    97  	case mysql.TypeInt24:
    98  		return JavaSQLTypeINTEGER
    99  
   100  	case mysql.TypeDate:
   101  		return JavaSQLTypeDATE
   102  
   103  	case mysql.TypeDuration:
   104  		return JavaSQLTypeTIME
   105  
   106  	case mysql.TypeDatetime:
   107  		return JavaSQLTypeTIMESTAMP
   108  
   109  	case mysql.TypeYear:
   110  		return JavaSQLTypeDATE
   111  
   112  	case mysql.TypeNewDate:
   113  		return JavaSQLTypeDATE
   114  
   115  	case mysql.TypeEnum:
   116  		return JavaSQLTypeCHAR
   117  
   118  	case mysql.TypeSet:
   119  		return JavaSQLTypeCHAR
   120  
   121  	case mysql.TypeTinyBlob:
   122  		return JavaSQLTypeVARBINARY
   123  
   124  	case mysql.TypeMediumBlob:
   125  		return JavaSQLTypeLONGVARBINARY
   126  
   127  	case mysql.TypeLongBlob:
   128  		return JavaSQLTypeLONGVARBINARY
   129  
   130  	case mysql.TypeBlob:
   131  		return JavaSQLTypeLONGVARBINARY
   132  
   133  	case mysql.TypeVarString, mysql.TypeVarchar:
   134  		return JavaSQLTypeVARCHAR
   135  
   136  	case mysql.TypeJSON:
   137  		// json: see jdbc 8.0, https://github.com/mysql/mysql-connector-j/blob/8.0.20/src/main/core-api/java/com/mysql/cj/MysqlType.java
   138  		return JavaSQLTypeLONGVARCHAR
   139  
   140  	case mysql.TypeString:
   141  		return JavaSQLTypeCHAR
   142  
   143  	case mysql.TypeGeometry:
   144  		return JavaSQLTypeBINARY
   145  
   146  	case mysql.TypeBit:
   147  		return JavaSQLTypeBIT
   148  
   149  	default:
   150  		return JavaSQLTypeVARCHAR
   151  	}
   152  }