@@ -24,33 +24,86 @@ import (
2424const (
2525 SqlModeSessionVar = "SQL_MODE"
2626
27+ RealAsFloat = "REAL_AS_FLOAT"
28+ PipesAsConcat = "PIPES_AS_CONCAT"
29+ ANSIQuotes = "ANSI_QUOTES"
30+ IgnoreSpace = "IGNORE_SPACE"
31+ OnlyFullGroupBy = "ONLY_FULL_GROUP_BY"
32+ NoUnsignedSubtraction = "NO_UNSIGNED_SUBTRACTION"
33+ NoDirInCreate = "NO_DIR_IN_CREATE"
34+ // ANSI mode includes REAL_AS_FLOAT, PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, and ONLY_FULL_GROUP_BY
35+ ANSI = "ANSI"
36+ NoAutoValueOnZero = "NO_AUTO_VALUE_ON_ZERO"
37+ NoBackslashEscapes = "NO_BACKSLASH_ESCAPES"
38+ StrictTransTables = "STRICT_TRANS_TABLES"
39+ StrictAllTables = "STRICT_ALL_TABLES"
40+ NoZeroInDate = "NO_ZERO_IN_DATE"
2741 AllowInvalidDates = "ALLOW_INVALID_DATES"
28- ANSIQuotes = "ANSI_QUOTES"
2942 ErrorForDivisionByZero = "ERROR_FOR_DIVISION_BY_ZERO"
43+ // Traditional mode includes STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_ZERO_IN_DATE, ERROR_FOR_DIVISION_BY_ZERO,
44+ // and NO_ENGINE_SUBSTITUTION
45+ Traditional = "TRADITIONAL"
3046 HighNotPrecedence = "HIGH_NOT_PRECEDENCE"
31- IgnoreSpaces = "IGNORE_SPACE"
32- NoAutoValueOnZero = "NO_AUTO_VALUE_ON_ZERO"
33- NoBackslashEscapes = "NO_BACKSLASH_ESCAPES"
34- NoDirInCreate = "NO_DIR_IN_CREATE"
3547 NoEngineSubstitution = "NO_ENGINE_SUBSTITUTION"
36- NoUnsignedSubtraction = "NO_UNSIGNED_SUBTRACTION"
37- NoZeroInDate = "NO_ZERO_IN_DATE"
38- OnlyFullGroupBy = "ONLY_FULL_GROUP_BY"
3948 PadCharToFullLength = "PAD_CHAR_TO_FULL_LENGTH"
40- PipesAsConcat = "PIPES_AS_CONCAT"
41- RealAsFloat = "REAL_AS_FLOAT"
42- StrictTransTables = "STRICT_TRANS_TABLES"
43- StrictAllTables = "STRICT_ALL_TABLES"
4449 TimeTruncateFractional = "TIME_TRUNCATE_FRACTIONAL"
50+ )
4551
46- // ANSI mode includes REAL_AS_FLOAT, PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, and ONLY_FULL_GROUP_BY
47- ANSI = "ANSI"
48- // Traditional mode includes STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_ZERO_IN_DATE, ERROR_FOR_DIVISION_BY_ZERO,
49- // and NO_ENGINE_SUBSTITUTION
50- Traditional = "TRADITIONAL"
51- DefaultSqlMode = "NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES"
52+ // Bits for different SQL modes (from mysql-server/sql/system_variables.h)
53+ const (
54+ modeRealAsFloat = 1
55+ modePipesAsConcat = 2
56+ modeAnsiQuotes = 4
57+ modeIgnoreSpace = 8
58+ modeOnlyFullGroupBy = 32
59+ modeNoUnsignedSubtraction = 64
60+ modeNoDirInCreate = 128
61+ modeAnsi = 0x40000
62+ modeNoAutoValueOnZero = modeAnsi * 2
63+ modeNoBackslashEscapes = modeNoAutoValueOnZero * 2
64+ modeStrictTransTables = modeNoBackslashEscapes * 2
65+ modeStrictAllTables = modeStrictTransTables * 2
66+ modeNoZeroInDate = modeStrictAllTables * 2
67+ modeNoZeroDate = modeNoZeroInDate * 2
68+ modeAllowInvalidDates = modeNoZeroDate * 2
69+ modeErrorForDivisionByZero = modeAllowInvalidDates * 2
70+ modeTraditional = modeErrorForDivisionByZero * 2
71+ modeHighNotPrecedence = 1 << 29
72+ modeNoEngineSubstitution = modeHighNotPrecedence * 2
73+ modePadCharToFullLength = 1 << 31
74+ modeTimeTruncateFractional = 1 << 32
5275)
5376
77+ // sqlModeBitMap maps SQL mode bit flags to their string names.
78+ var sqlModeBitMap = map [uint64 ]string {
79+ modeRealAsFloat : RealAsFloat ,
80+ modePipesAsConcat : PipesAsConcat ,
81+ modeAnsiQuotes : ANSIQuotes ,
82+ modeIgnoreSpace : IgnoreSpace ,
83+ modeOnlyFullGroupBy : OnlyFullGroupBy ,
84+ modeNoUnsignedSubtraction : NoUnsignedSubtraction ,
85+ modeNoDirInCreate : NoDirInCreate ,
86+ modeAnsi : ANSI ,
87+ modeNoAutoValueOnZero : NoAutoValueOnZero ,
88+ modeNoBackslashEscapes : NoBackslashEscapes ,
89+ modeStrictTransTables : StrictTransTables ,
90+ modeStrictAllTables : StrictAllTables ,
91+ modeNoZeroInDate : NoZeroInDate ,
92+ modeAllowInvalidDates : AllowInvalidDates ,
93+ modeErrorForDivisionByZero : ErrorForDivisionByZero ,
94+ modeTraditional : Traditional ,
95+ modeHighNotPrecedence : HighNotPrecedence ,
96+ modeNoEngineSubstitution : NoEngineSubstitution ,
97+ modePadCharToFullLength : PadCharToFullLength ,
98+ modeTimeTruncateFractional : TimeTruncateFractional ,
99+ }
100+
101+ var DefaultSqlMode = strings .Join ([]string {
102+ NoEngineSubstitution ,
103+ OnlyFullGroupBy ,
104+ StrictTransTables ,
105+ }, "," )
106+
54107var defaultMode * SqlMode
55108
56109func init () {
@@ -170,3 +223,49 @@ func (s *SqlMode) ParserOptions() sqlparser.ParserOptions {
170223func (s * SqlMode ) String () string {
171224 return s .modeString
172225}
226+
227+ // ConvertSqlModeBitmask converts sql_mode values to their string representation.
228+ func ConvertSqlModeBitmask (val any ) (any , error ) {
229+ if _ , ok := val .(string ); ok {
230+ return val , nil
231+ }
232+
233+ var bitmask uint64
234+ switch v := val .(type ) {
235+ case int8 :
236+ bitmask = uint64 (v )
237+ case int16 :
238+ bitmask = uint64 (v )
239+ case int :
240+ bitmask = uint64 (v )
241+ case int32 :
242+ bitmask = uint64 (v )
243+ case int64 :
244+ bitmask = uint64 (v )
245+ case uint8 :
246+ bitmask = uint64 (v )
247+ case uint16 :
248+ bitmask = uint64 (v )
249+ case uint :
250+ bitmask = uint64 (v )
251+ case uint32 :
252+ bitmask = uint64 (v )
253+ case uint64 :
254+ bitmask = v
255+ default :
256+ return val , nil
257+ }
258+
259+ var modes []string
260+ for bit , modeName := range sqlModeBitMap {
261+ if bitmask & bit != 0 {
262+ modes = append (modes , modeName )
263+ }
264+ }
265+
266+ if len (modes ) == 0 {
267+ return "" , nil
268+ }
269+
270+ return strings .Join (modes , "," ), nil
271+ }
0 commit comments