-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64400e4
commit 4539e9e
Showing
6 changed files
with
236 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2020 io.xream.sqlxb | ||
// | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package sqlxb | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
) | ||
|
||
type InsertBuilder struct { | ||
bbs []Bb | ||
} | ||
|
||
func (b *InsertBuilder) Set(k string, v interface{}) *InsertBuilder { | ||
|
||
defer func() *InsertBuilder { | ||
if s := recover(); s != nil { | ||
bytes, _ := json.Marshal(v) | ||
b.bbs = append(b.bbs, Bb{ | ||
op: "SET", | ||
key: k, | ||
value: string(bytes), | ||
}) | ||
} | ||
return b | ||
}() | ||
|
||
switch v.(type) { | ||
case string: | ||
if v.(string) == "" { | ||
return b | ||
} | ||
break | ||
case uint64, uint, int64, int, int32, int16, int8, bool, byte, float64, float32: | ||
if v == 0 { | ||
return b | ||
} | ||
break | ||
case *uint64, *uint, *int64, *int, *int32, *int16, *int8, *bool, *byte, *float64, *float32: | ||
isNil, n := NilOrNumber(v) | ||
if isNil { | ||
return b | ||
} | ||
v = n | ||
break | ||
case time.Time: | ||
ts := v.(time.Time).Format("2006-01-02 15:04:05") | ||
v = ts | ||
break | ||
case interface{}: | ||
bytes, _ := json.Marshal(v) | ||
v = string(bytes) | ||
break | ||
default: | ||
if v == nil { | ||
return b | ||
} | ||
} | ||
|
||
b.bbs = append(b.bbs, Bb{ | ||
op: "SET", | ||
key: k, | ||
value: v, | ||
}) | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package sqlxb | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
type Cat struct { | ||
Id int64 | ||
M map[string]string | ||
} | ||
|
||
type Pet struct { | ||
One Cat | ||
Id int64 | ||
} | ||
|
||
func (*Pet) TableName() string { | ||
return "t_pet" | ||
} | ||
|
||
func TestInsert(t *testing.T) { | ||
|
||
t.Run("insert", func(t *testing.T) { | ||
|
||
mm := make(map[string]string) | ||
mm["xxxx"] = "zzzzz" | ||
|
||
var po Pet | ||
sql, vs := Of(&po). | ||
Insert(func(b *InsertBuilder) { | ||
b.Set("id", 1).Set("one", Cat{ | ||
Id: 2, | ||
M: mm, | ||
}) | ||
}). | ||
Build(). | ||
SqlOfInsert() | ||
|
||
if !strings.Contains(sql, "one") { | ||
t.Error("sql erro") | ||
} | ||
|
||
fmt.Println(vs) | ||
fmt.Println(sql) | ||
}) | ||
} | ||
|
||
func TestUpdate(t *testing.T) { | ||
|
||
t.Run("update", func(t *testing.T) { | ||
mm := make(map[string]string) | ||
mm["xxxx"] = "zzzzz" | ||
|
||
var po Pet | ||
sql, vs := Of(&po). | ||
Update(func(b *UpdateBuilder) { | ||
|
||
b.Set("one", Cat{ | ||
Id: 2, | ||
M: mm, | ||
}) | ||
}). | ||
Eq("id", 2). | ||
Build(). | ||
SqlOfUpdate() | ||
|
||
if !strings.Contains(sql, "one") { | ||
t.Error("sql erro") | ||
} | ||
|
||
fmt.Println(vs) | ||
fmt.Println(sql) | ||
|
||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters