123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- package xugu
- import (
- "bufio"
- "database/sql/driver"
- "encoding/binary"
- "errors"
- "fmt"
- "strconv"
- "strings"
- )
- type ParseParam interface {
-
- assertParamType(driver.Value, int) error
-
- assertParamCount(string) int
-
- assertBindType(string) int
-
- assertParamName(string) error
- }
- type xuguValue struct {
-
- islob bool
- plob []byte
-
- value string
-
- length int
-
- buff int
-
- types int
-
- rcode int
- }
- type xuguParse struct {
-
-
- bind_type int
-
- param_count int
-
- param_names []byte
- Val []xuguValue
-
- position int
- }
- func (Parse *xuguParse) assertParamCount(query string) int {
- fmt.Println("----assertParamCount func 判断参数个数")
- if Parse.bind_type == 0 {
- Parse.bind_type = Parse.assertBindType(query)
- }
- switch Parse.bind_type {
- case BIND_PARAM_BY_POS:
- Parse.param_count = strings.Count(query, "?")
- case BIND_PARAM_BY_NAME:
- Parse.param_count = 0
- pos := 0
- phead := -1
- for {
- pos = strings.IndexByte(query[phead+1:], ':')
- if pos == -1 {
- break
- }
- pos += phead + 1
- tmp := pos
- for tmp > phead {
- tmp--
- if query[tmp] == ' ' {
- continue
- }
- if query[tmp] == ',' || query[tmp] == '(' {
- Parse.param_count++
- }
- break
- }
- phead = pos
- }
- }
- return Parse.param_count
- }
- func (Parse *xuguParse) assertBindType(query string) int {
- fmt.Println("--assertBindType")
- Parse.bind_type = strings.IndexByte(query, '?')
- fmt.Println("Parse.bind_type", Parse.bind_type)
- if Parse.bind_type != -1 {
- return BIND_PARAM_BY_POS
- }
- return BIND_PARAM_BY_NAME
- }
- func (param *xuguParse) assertParamType(dV driver.Value, pos int) error {
- fmt.Println("-----(param *xuguParse) assertParamType 判断参数类型")
- var dest xuguValue
- switch dV.(type) {
- case int64:
- srcv, ok := dV.(int64)
- if !ok {
- return errors.New("assertParamType int64 error")
- }
- S := strconv.FormatInt(srcv, 10)
- dest.value = S
- dest.length = strings.Count(S, "") - 1
- dest.buff = dest.length + 1
- dest.islob = false
- dest.types = SQL_XG_C_CHAR
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- case string:
- srcv, ok := dV.(string)
- if !ok {
- return errors.New("assertParamType string error")
- }
- dest.value = srcv
- dest.length = strings.Count(srcv, "") - 1
- dest.buff = dest.length + 1
- dest.islob = false
- dest.types = SQL_XG_C_CHAR
- if dest.length == 0 {
- dest.length = 1
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- case nil:
- dest.value = "xugusql"
- dest.length = 0
- dest.buff = strings.Count("xugusql", "")
- dest.islob = false
- dest.types = SQL_XG_C_CHAR
- default:
-
- return errors.New("Unknown data type")
- }
- param.position = pos
- param.Val = append(param.Val, dest)
- return nil
- }
- func (param *xuguParse) assertParamName(query string) error {
- fmt.Println("----(param *xuguParse) assertParamName 判断参数名")
- if param.param_count <= 0 {
- param.assertParamCount(query)
- }
- pos := 0
- phead := -1
- for {
- pos = strings.IndexByte(query[phead+1:], ':')
- if pos == -1 {
- break
- }
- pos += phead + 1
- tmp := pos
- for tmp > phead {
- tmp--
- if query[tmp] == ' ' {
- continue
- }
-
- if query[tmp] == ',' || query[tmp] == '(' {
- parg := pos
- for true {
- parg++
- if query[parg] == ',' || query[parg] == ')' || query[parg] == ' ' {
- param.param_names = append(param.param_names, query[pos+1:parg]...)
- break
- }
- }
- }
- break
- }
- phead = pos
- }
- fmt.Printf("param: %#v", param)
- fmt.Println("----(param *xuguParse) assertParamName 判断参数名结束")
- return nil
- }
- func (param *xuguParse) bindParamByPos(query string) string {
- fmt.Printf("param: %#v \n", param)
- for i := 0; i < param.param_count; i++ {
-
- query = strings.Replace(query, "?", param.Val[i].value, 1)
- }
- return query
- }
- type SelectResult struct {
- Field_Num uint32
- Fields []FieldDescri
- Values [][]ValueData
- rowIdx int
- }
- type FieldDescri struct {
- FieldNameLen int
- FieldName string
- FieldType fieldType
- FieldPreciScale int
- FieldFlag int
- }
- type ValueData struct {
- Col_len uint32
- Col_Data []byte
- }
- type InsertResult struct {
- RowidLen uint32
- RowidData []byte
- }
- type UpdateResult struct {
- UpdateNum uint32
- }
- type DeleteResult struct {
- DeleteNum uint32
- }
- type ProcRet struct {
- RetDType uint32
- RetDataLen uint32
- RetData []byte
- }
- type OutParamRet struct {
- OutParamNo uint32
- OutParamDType uint32
- OutParamLen uint32
- OutParamData []byte
- }
- type ErrInfo struct {
- ErrStrLen uint32
- ErrStr []byte
- }
- type WarnInfo struct {
- WarnStrLen uint32
- WarnStr []byte
- }
- type Message struct {
- MsgStrLen uint32
- MsgStr []byte
- }
- type FormArgDescri struct {
- ArgNum int
- Args []ArgDescri
- }
- type ArgDescri struct {
- ArgNameLen int
- ArgName string
- ArgNo int
- ArgDType int
- ArgPreciScale int
- }
- func parseSelectResult(readBuf buffer) (*SelectResult, error) {
- fmt.Println("调用 parseSelectResult")
- data := &SelectResult{}
- char := readBuf.peekChar()
- fmt.Println("--=char: ", string(char))
- switch char {
- case 'A':
- readBuf.idx++
-
- Field_Num := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
- data.Field_Num = Field_Num
- data.rowIdx = 0
- fmt.Println("Field_Num: ", data.Field_Num)
-
- for i := 0; i < int(Field_Num); i++ {
- field := FieldDescri{}
-
- field.FieldNameLen = int(binary.LittleEndian.Uint32(readBuf.readNext(4, true)))
- fmt.Println("field.FieldNameLen: ", field.FieldNameLen)
-
- field.FieldName = string(readBuf.readNext(field.FieldNameLen, false))
- fmt.Println("field.Field_Name: ", field.FieldName)
-
- field.FieldType = fieldType(binary.LittleEndian.Uint32(readBuf.readNext(4, true)))
- fmt.Println("field.FieldType: ", field.FieldType)
-
- field.FieldPreciScale = int(binary.LittleEndian.Uint32(readBuf.readNext(4, true)))
- fmt.Println("field.FieldPreciScale: ", field.FieldPreciScale)
-
- field.FieldFlag = int(binary.LittleEndian.Uint32(readBuf.readNext(4, true)))
- fmt.Println("field.FieldFlag: ", field.FieldFlag)
- data.Fields = append(data.Fields, field)
- }
- data.Values = make([][]ValueData, data.Field_Num)
-
-
- fmt.Println("\n\n=========开始获取行数据=================================")
- char := readBuf.peekChar()
- fmt.Println(" --char: ", string(char))
- readBuf.idx++
- if char == 'K' {
- break
- } else if char == 'R' {
- colIdx := 0
- typeIdx := 0
- fmt.Println("开始循环 ")
- for {
- col := ValueData{}
-
- col.Col_len = binary.LittleEndian.Uint32(readBuf.readNext(4, true))
- fmt.Println("数据大小为: ", col.Col_len)
-
-
- fmt.Println("查看类型", data.Fields[typeIdx].FieldType)
- switch data.Fields[typeIdx].FieldType {
- case fieldType_CHAR, fieldType_NCHAR:
- col.Col_Data = readBuf.readNext(int(col.Col_len), false)
- fmt.Println("fieldTypeVarChar data: ", col.Col_Data, string(col.Col_Data))
- data.Values[colIdx] = append(data.Values[colIdx], col)
- colIdx++
- fmt.Println("从查询返回 解析出的值 :", col.Col_Data, string(col.Col_Data))
- char := readBuf.peekChar()
- if char == 'R' {
- readBuf.idx++
- colIdx = 0
- if typeIdx >= int(data.Field_Num)-1 {
- typeIdx = 0
- } else {
- typeIdx++
- }
- continue
- } else if char == 'K' {
- return data, nil
-
- }
- typeIdx++
-
- case fieldType_I1, fieldType_I4, fieldType_I8:
- col.Col_Data = reverseBytes(readBuf.readNext(int(col.Col_len), false))
- data.Values[colIdx] = append(data.Values[colIdx], col)
- colIdx++
- char := readBuf.peekChar()
- fmt.Println("从查询返回 解析出的值 :", col.Col_Data, string(col.Col_Data))
- if char == 'R' {
- readBuf.idx++
- colIdx = 0
- if typeIdx >= int(data.Field_Num)-1 {
- fmt.Println("typeIdx <= int(data.Field_Num)-1: ", typeIdx, int(data.Field_Num)-1)
- typeIdx = 0
- } else {
- typeIdx++
- }
- continue
- } else if char == 'K' {
- return data, nil
-
- }
- typeIdx++
- }
- fmt.Println("循环结束")
- return data, nil
- }
- } else {
- break
- }
- default:
- return nil, errors.New("parseQueryResult error")
- }
- return data, nil
- }
- func parseInsertResult(readBuf buffer) (*InsertResult, error) {
-
- Rowid_Len := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
-
- encoded := readBuf.readNext(int(Rowid_Len), false)
-
- char := readBuf.peekChar()
- if char == 'K' {
- return &InsertResult{
- RowidLen: Rowid_Len,
- RowidData: encoded,
- }, nil
- }
- return nil, errors.New("parseInsertResult error")
- }
- func parseUpdateResult(readBuf buffer) (*UpdateResult, error) {
- updateNum := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
- return &UpdateResult{UpdateNum: updateNum}, nil
- }
- func parseDeleteResult(readBuf buffer) (*DeleteResult, error) {
- deleteNum := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
- return &DeleteResult{DeleteNum: deleteNum}, nil
- }
- func parseProcRet(readBuf buffer) (*ProcRet, error) {
- retDType := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
- retDataLen := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
- retData := readBuf.readNext(int(retDataLen), false)
- return &ProcRet{RetDType: retDType, RetDataLen: retDataLen, RetData: retData}, nil
- }
- func parseOutParamRet(readBuf buffer) (*OutParamRet, error) {
- outParamNo := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
- outParamDType := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
- outParamLen := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
- outParamData := readBuf.readNext(int(outParamLen), false)
- return &OutParamRet{
- OutParamNo: outParamNo,
- OutParamDType: outParamDType,
- OutParamLen: outParamLen,
- OutParamData: outParamData,
- }, nil
- }
- func parseErrInfo(readBuf buffer) (*ErrInfo, error) {
- errStrLen := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
- errStr := readBuf.readNext(int(errStrLen), false)
- return &ErrInfo{ErrStrLen: errStrLen, ErrStr: errStr}, nil
- }
- func parseWarnInfo(readBuf buffer) (*WarnInfo, error) {
- warnStrLen := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
- warnStr := readBuf.readNext(int(warnStrLen), false)
- return &WarnInfo{WarnStrLen: warnStrLen, WarnStr: warnStr}, nil
- }
- func parseMessage(readBuf buffer) (*Message, error) {
- msgStrLen := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
- msgStr := readBuf.readNext(int(msgStrLen), false)
- return &Message{MsgStrLen: msgStrLen, MsgStr: msgStr}, nil
- }
- func parseFormArgDescri(reader *bufio.Reader) (*FormArgDescri, error) {
- argNum, err := readInt32(reader)
- if err != nil {
- return nil, err
- }
- formArgDescri := &FormArgDescri{ArgNum: argNum}
- for i := 0; i < argNum; i++ {
- argDescri := ArgDescri{}
- argDescri.ArgNameLen, err = readInt32(reader)
- if err != nil {
- return nil, err
- }
- argDescri.ArgName, err = readString(reader, argDescri.ArgNameLen)
- if err != nil {
- return nil, err
- }
- argDescri.ArgNo, err = readInt32(reader)
- if err != nil {
- return nil, err
- }
- argDescri.ArgDType, err = readInt32(reader)
- if err != nil {
- return nil, err
- }
- argDescri.ArgPreciScale, err = readInt32(reader)
- if err != nil {
- return nil, err
- }
- formArgDescri.Args = append(formArgDescri.Args, argDescri)
- }
- return formArgDescri, nil
- }
|