SetDefaultValue parses the default value string if present and sets to TableSchema. Schema lock should be acquired and release by caller and enum dict should already be created/update before this function.
(columnID int)
| 117 | // Schema lock should be acquired and release by caller and enum dict should already be |
| 118 | // created/update before this function. |
| 119 | func (t *TableSchema) SetDefaultValue(columnID int) { |
| 120 | // Default values are already set. |
| 121 | if t.DefaultValues[columnID] != nil { |
| 122 | return |
| 123 | } |
| 124 | |
| 125 | column := t.Schema.Columns[columnID] |
| 126 | defStrVal := column.DefaultValue |
| 127 | if defStrVal == nil || column.Deleted { |
| 128 | t.DefaultValues[columnID] = &NullDataValue |
| 129 | return |
| 130 | } |
| 131 | |
| 132 | dataType := t.ValueTypeByColumn[columnID] |
| 133 | dataTypeName := DataTypeName[dataType] |
| 134 | val := DataValue{ |
| 135 | Valid: true, |
| 136 | DataType: dataType, |
| 137 | } |
| 138 | |
| 139 | if dataType == SmallEnum || dataType == BigEnum { |
| 140 | enumDict, ok := t.EnumDicts[column.Name] |
| 141 | if !ok { |
| 142 | // Should no happen since the enum dict should already be created. |
| 143 | utils.GetLogger().With( |
| 144 | "data_type", dataTypeName, |
| 145 | "default_value", *defStrVal, |
| 146 | "column", t.Schema.Columns[columnID].Name, |
| 147 | ).Panic("Cannot find EnumDict for column") |
| 148 | } |
| 149 | enumVal, ok := enumDict.Dict[*defStrVal] |
| 150 | if !ok { |
| 151 | // Should no happen since the enum value should already be created. |
| 152 | utils.GetLogger().With( |
| 153 | "data_type", dataTypeName, |
| 154 | "default_value", *defStrVal, |
| 155 | "column", t.Schema.Columns[columnID].Name, |
| 156 | ).Panic("Cannot find enum value for column") |
| 157 | } |
| 158 | |
| 159 | if dataType == SmallEnum { |
| 160 | enumValUint8 := uint8(enumVal) |
| 161 | val.OtherVal = unsafe.Pointer(&enumValUint8) |
| 162 | } else { |
| 163 | enumValUint16 := uint16(enumVal) |
| 164 | val.OtherVal = unsafe.Pointer(&enumValUint16) |
| 165 | } |
| 166 | } else { |
| 167 | dataValue, err := ValueFromString(*defStrVal, dataType) |
| 168 | if err != nil { |
| 169 | // Should not happen since the string value is already validated by schema handler. |
| 170 | utils.GetLogger().With( |
| 171 | "data_type", dataTypeName, |
| 172 | "default_value", *defStrVal, |
| 173 | "column", t.Schema.Columns[columnID].Name, |
| 174 | ).Panic("Cannot parse default value") |
| 175 | } |
| 176 |