github.com/dshekhar95/sub_dgraph@v0.0.0-20230424164411-6be28e40bbf1/dgraph/cmd/migrate/datatype.go (about)

     1  /*
     2   * Copyright 2022 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package migrate
    18  
    19  const (
    20  	unknownType dataType = iota
    21  	intType
    22  	stringType
    23  	floatType
    24  	doubleType
    25  	datetimeType
    26  	uidType // foreign key reference, which would corrspond to uid type in Dgraph
    27  )
    28  
    29  // the typeToString map is used to generate the Dgraph schema file
    30  var typeToString map[dataType]string
    31  
    32  // the sqlTypeToInternal map is used to parse date types in SQL schema
    33  var sqlTypeToInternal map[string]dataType
    34  
    35  func initDataTypes() {
    36  	typeToString = make(map[dataType]string)
    37  	typeToString[unknownType] = "unknown"
    38  	typeToString[intType] = "int"
    39  	typeToString[stringType] = "string"
    40  	typeToString[floatType] = "float"
    41  	typeToString[doubleType] = "double"
    42  	typeToString[datetimeType] = "datetime"
    43  	typeToString[uidType] = "uid"
    44  
    45  	sqlTypeToInternal = make(map[string]dataType)
    46  	sqlTypeToInternal["int"] = intType
    47  	sqlTypeToInternal["tinyint"] = intType
    48  	sqlTypeToInternal["varchar"] = stringType
    49  	sqlTypeToInternal["text"] = stringType
    50  	sqlTypeToInternal["date"] = datetimeType
    51  	sqlTypeToInternal["time"] = datetimeType
    52  	sqlTypeToInternal["datetime"] = datetimeType
    53  	sqlTypeToInternal["float"] = floatType
    54  	sqlTypeToInternal["double"] = doubleType
    55  	sqlTypeToInternal["decimal"] = floatType
    56  }
    57  
    58  func (t dataType) String() string {
    59  	return typeToString[t]
    60  }