Exemple #1
0
func handleBType(temp string, msg proxy.Pkg) msgStruct {
	msgs <- fmt.Sprintf("3 received ---->%#v\n", msg)
	msgs <- fmt.Sprintf("3 temp ---->%#v\n", temp)
	msgs <- fmt.Sprintf("3 len(msg.Content) ---->%#v\n", len(msg.Content))
	// if msg.Type == byte('B') && temp != "" && len(msg.Content) > 23 {
	msgs <- fmt.Sprintf("4 received ---->%#v\n", msg)
	var newMsg proxy.ReadBuf
	newMsg = msg.Content

	msgs <- fmt.Sprintf("5 received ---->%#v\n", msg)
	// The name of the destination portal (an empty string selects the unnamed portal).
	p := bytes.Index(newMsg, []byte{0})
	// remove first string
	msgs <- fmt.Sprintf("msg ---->%#v\n", newMsg)
	msgs <- fmt.Sprintf("first string ---->%#v\n", newMsg[:p+1])
	newMsg = newMsg[p+1:]
	msgs <- fmt.Sprintf("0 newMsg   ----->%#v\n", newMsg)

	if newMsg[0] == 0 {
		p := bytes.Index(newMsg, []byte{0})
		// remove first string
		msgs <- fmt.Sprintf("msg ---->%#v\n", newMsg)
		msgs <- fmt.Sprintf("1.1 first string ---->%#v\n", newMsg[:p+1])
		newMsg = newMsg[p:]
		msgs <- fmt.Sprintf("0.1 newMsg   ----->%#v\n", newMsg)
	}
	// The name of the source prepared statement (an empty string selects the unnamed prepared statement).
	p = bytes.Index(newMsg, []byte{0})
	// remove second string
	msgs <- fmt.Sprintf("second string ---->%#v\n", newMsg[:p+1])
	newMsg = newMsg[p+1:]
	msgs <- fmt.Sprintf("1 newMsg   ----->%#v\n", newMsg)

	totalVar := newMsg.Int16()
	msgs <- fmt.Sprintf("vars types numbers ---->%#v\n", totalVar)
	// read variables' types
	for t1 := 0; t1 < totalVar; t1++ {
		varType := newMsg.Int16()
		msgs <- fmt.Sprintf("var #%d typeId----->%#v\n", t1+1, varType)
	}

	msgs <- fmt.Sprintf("23 newMsg   ----->%#v\n", newMsg)
	msgs <- fmt.Sprintf("23 t        ----->%#v\n", totalVar)
	// var total
	newMsg.Int16()
	vars := make(map[int]string)
	var varsIdx []int
	// if (totalVar == 0 && len(newMsg) > 4) || totalVar > len(newMsg) {
	// 	msgs <- fmt.Sprintf("23.1 newMsg   ----->%#v\n", newMsg)
	// 	msgs <- fmt.Sprintf("0 totalVar  ----->%d\n", totalVar)
	// 	for totalVar := 0; totalVar != 0 && totalVar < len(newMsg); totalVar = newMsg.Int16() {
	// 		msgs <- fmt.Sprintf("24 newMsg   ----->%#v\n", newMsg)
	// 		msgs <- fmt.Sprintf("1 totalVar  ----->%d\n", totalVar)
	// 	}
	// }
	//
	// if totalVar == 0 && len(newMsg) > 4 {
	// 	totalVar = newMsg.Int16()
	// }
	// if totalVar == 0 && len(newMsg) > 4 {
	// 	totalVar = newMsg.Int32()
	// }
	msgs <- fmt.Sprintf("totalVar   ----->%d\n", totalVar)
	for i := 0; i < totalVar; i++ {
		msgs <- fmt.Sprintf("2 newMsg   ----->%#v\n", newMsg)
		varLen := newMsg.Int32()
		// var1 := newMsg.Next(4)
		// // log.Debug("aa   -----> %#v\n", aa)
		// // log.Debug("aa bits ----->%8b\n", aa[len(aa)-1])
		// varLen := int(binary.BigEndian.Uint32(var1))
		// if varLen > len(newMsg) {
		// 	varLen = int(binary.BigEndian.Uint16(var1[:2]))
		// }
		if varLen > 0 {
			msgs <- fmt.Sprintf("varLen ----->%v\n", varLen)
			msgs <- fmt.Sprintf("newMsg   ----->%#v\n", newMsg)
			if varLen > len(newMsg) {
				varLen = len(newMsg) - 4
				msgs <- fmt.Sprintf("1 varLen ----->%v\n", varLen)
				msgs <- fmt.Sprintf("1 newMsg   ----->%#v\n", newMsg)
			}
			vars[i] = string(newMsg.Next(varLen))
			msgs <- fmt.Sprintf("vars   ----->%#v\n", vars)
			varsIdx = append(varsIdx, i)
			msgs <- fmt.Sprintf("varIdx  ----->%#v\n", varsIdx)
		}
	}

	sort.Sort(sort.Reverse(sort.IntSlice(varsIdx)))
	for _, k := range varsIdx {
		// messages = append(messages, strings.Replace(temp, fmt.Sprintf("$%d", k+1), fmt.Sprintf("'%s'", string(newMsg[k+1])), -1))
		temp = strings.Replace(temp, fmt.Sprintf("$%d", k+1), fmt.Sprintf("'%s'", string(vars[k])), -1)
		msgs <- fmt.Sprintf("message subst k ----->%v\n", k)
		msgs <- fmt.Sprintf("message subst newMsg ----->%#v\n", newMsg)
		msgs <- fmt.Sprintf("message subst msg ----->%v\n", vars[k+1])
		msgs <- fmt.Sprintf("message subst temp ----->%v\n", temp)
		msgs <- fmt.Sprintf("message subst param %s ----->%v\n", fmt.Sprintf("$%d", k+1), fmt.Sprintf("'%s'", vars[k]))
	}
	msgs <- fmt.Sprintf("end message  ----->%v\n", temp)
	// msgOut <- msgStruct{Type: "M", Content: temp}
	// } else {
	// }

	return msgStruct{Type: "M", Content: temp, Time: msg.Time}
}
Exemple #2
0
func handlePType(msg proxy.Pkg) (msgStruct, string) {
	newMsg := ""
	myMsgStruct := msgStruct{}

	if strings.Contains(string(msg.Content), "$1") {
		// msgs <- fmt.Sprintf("1 received ---->%#v\n", msg)
		// msgs <- fmt.Sprintf("1 received ---->%d bytes\n", len(msg.Content))
		var newM proxy.ReadBuf
		newM = msg.Content
		newM.Int32()

		// The name of the destination portal (an empty string selects the unnamed portal).
		p := bytes.Index(newM, []byte{112, 100, 111, 95, 115, 116, 109, 116, 95})
		// remove first string
		stringSize := 14
		// msgs <- fmt.Sprintf("msg ---->%#v\n", newM)
		// msgs <- fmt.Sprintf("msg ---->%s\n", string(newM))
		// msgs <- fmt.Sprintf("first string ---->%#v\n", newM[:stringSize+14])
		// msgs <- fmt.Sprintf("first string ---->%s\n", string(newM[:stringSize+14]))
		newM = newM[p+stringSize+1:]
		p = bytes.Index(newM, []byte{0})
		newMsg = string(newM[:p])
		// log.Debug("0 newMsg   ----->%s\n", newMsg)

		// temp = string(newMsg)
		// msgs <- fmt.Sprintf("1 temp ---->%#v\n", temp)
		// log.Notice("Returning: pkg %s -- temp %s", myMsgStruct.Content, newMsg)
		return myMsgStruct, newMsg
	} else {
		// msgs <- fmt.Sprintf("2 received ---->%#v\n", msg)
		selectIdx := strings.Index(string(msg.Content), string([]byte{83, 69, 76, 69, 67, 84, 32}))
		if selectIdx == -1 {
			selectIdx = 0
		}
		sepIdx := strings.Index(string(msg.Content), string([]byte{0, 0, 1, 0, 0}))
		if sepIdx == -1 || sepIdx+5 > len(msg.Content) {
			sepIdx := strings.Index(string(msg.Content), string([]byte{0, 1, 0, 0}))
			if sepIdx == -1 || sepIdx+4 > len(msg.Content) {
				sepIdx = len(msg.Content) - 4
			} else {
				sepIdx = len(msg.Content) - 1
			}
		}
		if sepIdx == -1 {
			sepIdx = len(msg.Content)
		}
		if selectIdx == -1 {
			selectIdx = 0
		}

		// log.Debug("SEP index ----->%v\n", sepIdx)
		// log.Debug("SEP len   ----->%v\n", len(msg.Content))
		// log.Debug("SEP CONT  ----->%v\n", msg.Content)
		// messages = append(messages, string(bytes.Trim(msg.Content[selectIdx:sepIdx], "\x00")))
		myMsgStruct = msgStruct{Type: "M", Content: string(bytes.Trim(msg.Content[selectIdx:sepIdx], "\x00"))}
		newMsg = ""
		// log.Notice("Returning: pkg %s -- temp %s", myMsgStruct.Content, newMsg)
		return myMsgStruct, newMsg
	}
	// log.Notice("Returning: pkg %s -- temp %s", myMsgStruct.Content, newMsg)
	// return myMsgStruct, newMsg
}