Skip to content

Commit 3b18bc6

Browse files
authored
Merge pull request #211 from swordqiu/hotfix/qj-inet6-aton-support
feature: support INET6_ATON function
2 parents e37f519 + 33f7107 commit 3b18bc6

5 files changed

Lines changed: 59 additions & 6 deletions

File tree

backends.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ type IBackend interface {
123123
DATE_FORMAT(name string, field IQueryField, format string) IQueryField
124124
// INET_ATON
125125
INET_ATON(field IQueryField) IQueryField
126+
// INET6_ATON
127+
INET6_ATON(field IQueryField) IQueryField
126128
// AND_Val
127129
AND_Val(name string, field IQueryField, v interface{}) IQueryField
128130
// OR_Val

backends/dameng/functions.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (dameng *SDamengBackend) GROUP_CONCAT2(name string, sep string, field sqlch
3131

3232
// INET_ATON represents the SQL function INET_ATON
3333
func (dameng *SDamengBackend) INET_ATON(field sqlchemy.IQueryField) sqlchemy.IQueryField {
34-
expr := ""
34+
/*expr := ""
3535
vars := make([]sqlchemy.IQueryField, 0)
3636
expr += `TO_NUMBER(SUBSTR(%s,1,INSTR(%s,'.')-1))*POWER(256,3)+`
3737
vars = append(vars, field, field)
@@ -41,7 +41,8 @@ func (dameng *SDamengBackend) INET_ATON(field sqlchemy.IQueryField) sqlchemy.IQu
4141
vars = append(vars, field, field, field, field)
4242
expr += `TO_NUMBER(SUBSTR(%s,INSTR(%s,'.',1,3)+1))`
4343
vars = append(vars, field, field)
44-
return sqlchemy.NewFunctionField("", false, expr, vars...)
44+
return sqlchemy.NewFunctionField("", false, expr, vars...)*/
45+
return sqlchemy.NewFunctionField("", false, `HEX(SF_INET_SORT(%s))`, field)
4546
}
4647

4748
// cast field to string
@@ -58,3 +59,8 @@ func (dameng *SDamengBackend) CASTInt(field sqlchemy.IQueryField, fieldname stri
5859
func (dameng *SDamengBackend) CASTFloat(field sqlchemy.IQueryField, fieldname string) sqlchemy.IQueryField {
5960
return sqlchemy.NewFunctionField(fieldname, false, `CAST(%s AS REAL)`, field)
6061
}
62+
63+
// INET6_ATON represents a SQL function INET6_ATON
64+
func (dameng *SDamengBackend) INET6_ATON(field sqlchemy.IQueryField) sqlchemy.IQueryField {
65+
return sqlchemy.NewFunctionField("", false, `HEX(SF_INET_SORT(%s))`, field)
66+
}

backends/dameng/query_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,21 @@ func TestQuery(t *testing.T) {
103103
t.Run("query INET_ATON func", func(t *testing.T) {
104104
testReset()
105105
q := testTable.Query(testTable.Field("col1"), sqlchemy.INET_ATON(testTable.Field("col0")).Label("ipaddr"))
106-
want := `SELECT "t1"."col1" AS "col1", TO_NUMBER(SUBSTR("t1"."col0",1,INSTR("t1"."col0",'.')-1))*POWER(256,3)+TO_NUMBER(SUBSTR("t1"."col0",INSTR("t1"."col0",'.')+1,INSTR("t1"."col0",'.',1,2)-INSTR("t1"."col0",'.')-1))*POWER(256,2)+TO_NUMBER(SUBSTR("t1"."col0",INSTR("t1"."col0",'.',1,2)+1,INSTR("t1"."col0",'.',1,3)-INSTR("t1"."col0",'.',1,2)-1))*256+TO_NUMBER(SUBSTR("t1"."col0",INSTR("t1"."col0",'.',1,3)+1)) AS "ipaddr" FROM "test" AS "t1"`
106+
want := `SELECT "t1"."col1" AS "col1", HEX(SF_INET_SORT("t1"."col0")) AS "ipaddr" FROM "test" AS "t1"`
107107
testGotWant(t, q.String(), want)
108108
})
109109

110110
t.Run("query INET_ATON func by group", func(t *testing.T) {
111111
testReset()
112112
q := testTable.Query(testTable.Field("col1").Label("number"), sqlchemy.INET_ATON(testTable.Field("col0")).Label("ipaddr"), sqlchemy.NewConstField(123456).Label("gateway")).GroupBy(testTable.Field("col0"))
113-
want := `SELECT MAX("t1"."col1") AS "number", MAX(TO_NUMBER(SUBSTR("t1"."col0",1,INSTR("t1"."col0",'.')-1))*POWER(256,3)+TO_NUMBER(SUBSTR("t1"."col0",INSTR("t1"."col0",'.')+1,INSTR("t1"."col0",'.',1,2)-INSTR("t1"."col0",'.')-1))*POWER(256,2)+TO_NUMBER(SUBSTR("t1"."col0",INSTR("t1"."col0",'.',1,2)+1,INSTR("t1"."col0",'.',1,3)-INSTR("t1"."col0",'.',1,2)-1))*256+TO_NUMBER(SUBSTR("t1"."col0",INSTR("t1"."col0",'.',1,3)+1))) AS "ipaddr", 123456 AS "gateway" FROM "test" AS "t1" GROUP BY "t1"."col0"`
113+
want := `SELECT MAX("t1"."col1") AS "number", MAX(HEX(SF_INET_SORT("t1"."col0"))) AS "ipaddr", 123456 AS "gateway" FROM "test" AS "t1" GROUP BY "t1"."col0"`
114+
testGotWant(t, q.String(), want)
115+
})
116+
117+
t.Run("query INET6_ATON func", func(t *testing.T) {
118+
testReset()
119+
q := testTable.Query(testTable.Field("col1"), sqlchemy.INET6_ATON(testTable.Field("col0")).Label("ipaddr"))
120+
want := `SELECT "t1"."col1" AS "col1", HEX(SF_INET_SORT("t1"."col0")) AS "ipaddr" FROM "test" AS "t1"`
114121
testGotWant(t, q.String(), want)
115122
})
116123
}

backends_base.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ func (bb *SBaseBackend) INET_ATON(field IQueryField) IQueryField {
154154
return NewFunctionField("", false, `INET_ATON(%s)`, field)
155155
}
156156

157+
// INET6_ATON represents a SQL function INET6_ATON
158+
func (bb *SBaseBackend) INET6_ATON(field IQueryField) IQueryField {
159+
return NewFunctionField("", false, `INET6_ATON(%s)`, field)
160+
}
161+
157162
// SubStr represents a SQL function SUBSTR
158163
func (bb *SBaseBackend) SUBSTR(name string, field IQueryField, pos, length int) IQueryField {
159164
var rightStr string

functions.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ func REPLACE(name string, field IQueryField, old string, new string) IQueryField
228228
type SConstField struct {
229229
constVar interface{}
230230
alias string
231+
232+
db *SDatabase
231233
}
232234

233235
// Expression implementation of SConstField for IQueryField
@@ -260,7 +262,7 @@ func (s *SConstField) ConvertFromValue(val interface{}) interface{} {
260262

261263
// database implementation of SConstField for IQueryField
262264
func (s *SConstField) database() *SDatabase {
263-
return nil
265+
return s.db
264266
}
265267

266268
// Variables implementation of SConstField for IQueryField
@@ -278,10 +280,24 @@ func NewConstField(variable interface{}) *SConstField {
278280
return &SConstField{constVar: variable}
279281
}
280282

283+
// database implementation of SStringField for IQueryField
284+
func (s *SConstField) WithField(f IQueryField) *SConstField {
285+
s.db = f.database()
286+
return s
287+
}
288+
289+
func (q *SQuery) ConstField(variable interface{}) *SConstField {
290+
f := NewConstField(variable)
291+
f.db = q.db
292+
return f
293+
}
294+
281295
// SStringField is a query field of a string constant
282296
type SStringField struct {
283297
strConst string
284298
alias string
299+
300+
db *SDatabase
285301
}
286302

287303
// Expression implementation of SStringField for IQueryField
@@ -314,7 +330,7 @@ func (s *SStringField) ConvertFromValue(val interface{}) interface{} {
314330

315331
// database implementation of SStringField for IQueryField
316332
func (s *SStringField) database() *SDatabase {
317-
return nil
333+
return s.db
318334
}
319335

320336
// Variables implementation of SStringField for IQueryField
@@ -327,11 +343,23 @@ func (s *SStringField) IsAggregate() bool {
327343
return true
328344
}
329345

346+
// database implementation of SStringField for IQueryField
347+
func (s *SStringField) WithField(f IQueryField) *SStringField {
348+
s.db = f.database()
349+
return s
350+
}
351+
330352
// NewStringField returns an instance of SStringField
331353
func NewStringField(strConst string) *SStringField {
332354
return &SStringField{strConst: strConst}
333355
}
334356

357+
func (q *SQuery) StringField(strConst string) *SStringField {
358+
f := NewStringField(strConst)
359+
f.db = q.db
360+
return f
361+
}
362+
335363
// CONCAT represents a SQL function CONCAT
336364
func CONCAT(name string, fields ...IQueryField) IQueryField {
337365
return getFieldBackend(fields...).CONCAT(name, fields...)
@@ -363,6 +391,11 @@ func INET_ATON(field IQueryField) IQueryField {
363391
return getFieldBackend(field).INET_ATON(field)
364392
}
365393

394+
// INET6_ATON represents a SQL function INET_ATON
395+
func INET6_ATON(field IQueryField) IQueryField {
396+
return getFieldBackend(field).INET6_ATON(field)
397+
}
398+
366399
// TimestampAdd represents a SQL function TimestampAdd
367400
func TimestampAdd(name string, field IQueryField, offsetSeconds int) IQueryField {
368401
return TIMESTAMPADD(name, field, offsetSeconds)

0 commit comments

Comments
 (0)