Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kropotov - go2hw07 #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions codegen/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module hw07/codegen

go 1.16

require github.com/mailru/easyjson v0.7.7 // indirect
4 changes: 4 additions & 0 deletions codegen/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
6 changes: 6 additions & 0 deletions codegen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main


func main() {

}
11 changes: 11 additions & 0 deletions codegen/struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

//easyjson:json

type Product struct {
Id int64
Name string
Code string
Price float64
Status int
}
16 changes: 16 additions & 0 deletions codegen/struct_easyjson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// TEMPORARY AUTOGENERATED FILE: easyjson stub code to make the package
// compilable during generation.

package main

import (
"github.com/mailru/easyjson/jwriter"
"github.com/mailru/easyjson/jlexer"
)

func ( Product ) MarshalJSON() ([]byte, error) { return nil, nil }
func (* Product ) UnmarshalJSON([]byte) error { return nil }
func ( Product ) MarshalEasyJSON(w *jwriter.Writer) {}
func (* Product ) UnmarshalEasyJSON(l *jlexer.Lexer) {}

type EasyJSON_exporter_Product *Product
Empty file added codegen/struct_easyjson.go.tmp
Empty file.
3 changes: 0 additions & 3 deletions go.mod

This file was deleted.

3 changes: 3 additions & 0 deletions ref/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module hw07/ref

go 1.16
42 changes: 42 additions & 0 deletions ref/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"fmt"
"reflect"
"strings"
)

func sqlConvert(sqlText string, params ...interface{}) (sqlTextResult string, paramsResult []interface{}) {

paramsResult = []interface{}(nil)
sqlTextParts := strings.Split(sqlText, "?")
sqlTextResult = sqlTextParts[0]
for i, val := range params {
rv := reflect.ValueOf(val)
if rv.Kind() == reflect.Slice {
blank := ""
for j := 0; j < rv.Len(); j++ {
paramsResult = append(paramsResult, rv.Index(j).Interface())
if j < rv.Len() -1 {
blank += "?,"
} else {
blank += "?"
}
}
sqlTextResult = sqlTextResult + blank + sqlTextParts[i+1]
} else {
paramsResult = append(paramsResult, interface{}(val))
sqlTextResult = sqlTextResult + "?" + sqlTextParts[i+1]
}
}
return sqlTextResult, paramsResult
}

func main() {
a, b := sqlConvert("SELECT * FROM table WHERE deleted = ? AND id IN(?) AND count < ?",
false, []int{1, 6, 234}, 555 )
fmt.Println(a, b)
//
// SELECT * FROM table WHERE deleted = ? AND id IN(?,?,?) AND count < ? [false 1 6 234 555]
//
}