xugu_parse.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. package xugu
  2. import (
  3. "database/sql/driver"
  4. "encoding/binary"
  5. "errors"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type ParseParam interface {
  12. // 转换参数数据类型
  13. assertParamType(driver.Value, int) error
  14. // 解析参数数量
  15. assertParamCount(string) int
  16. // 解析参数绑定类型(按参数名称绑定类型和按参数位置绑定类型)
  17. assertBindType(string) int
  18. // 解析参数名称
  19. assertParamName(string) error
  20. }
  21. // 辅助结构体
  22. type xuguValue struct {
  23. // 布尔值,如果值为 true,表示当前字段的数据类型是大对象数据类型
  24. islob bool
  25. plob []byte
  26. //字段名
  27. paramName []byte
  28. //paramNameLength [2]byte
  29. // 字段的实际值
  30. value []byte
  31. // 值的长度
  32. valueLength int
  33. // buff 通常指定要绑定的参数数据的内存缓冲区大小
  34. //buff int
  35. // 字段的类型
  36. types fieldType
  37. }
  38. type xuguParse struct {
  39. // bind_type 用于标识参数绑定的类型。
  40. // 参数绑定类型包括按参数名称绑定和按参数占位符绑定
  41. bind_type int
  42. // param_count 用于指定在 SQL 语句中需要绑定的参数数量
  43. param_count int
  44. // 当参数绑定类型为按参数名称绑定时,param_names 是参数名称的集合
  45. param_names []byte
  46. values []xuguValue
  47. // 当参数绑定类型为按参数占位符绑定时,position 标识参数的位置
  48. position int
  49. }
  50. // 判断参数个数
  51. func (Parse *xuguParse) assertParamCount(query string) int {
  52. fmt.Println("----assertParamCount func 判断参数个数")
  53. if Parse.bind_type == 0 {
  54. Parse.bind_type = Parse.assertBindType(query)
  55. }
  56. switch Parse.bind_type {
  57. case BIND_PARAM_BY_POS:
  58. Parse.param_count = strings.Count(query, "?")
  59. fmt.Println("Parse.param_count ", Parse.param_count)
  60. case BIND_PARAM_BY_NAME:
  61. Parse.param_count = 0
  62. pos := 0
  63. phead := -1
  64. for {
  65. pos = strings.IndexByte(query[phead+1:], ':')
  66. if pos == -1 {
  67. break
  68. }
  69. pos += phead + 1
  70. tmp := pos
  71. for tmp > phead {
  72. tmp--
  73. if query[tmp] == ' ' {
  74. continue
  75. }
  76. if query[tmp] == ',' || query[tmp] == '(' {
  77. Parse.param_count++
  78. }
  79. break
  80. }
  81. phead = pos
  82. }
  83. }
  84. return Parse.param_count
  85. }
  86. // 判断绑定类型 ? 或者 :name
  87. func (Parse *xuguParse) assertBindType(query string) int {
  88. fmt.Println("--assertBindType")
  89. Parse.bind_type = strings.IndexByte(query, '?')
  90. fmt.Println("Parse.bind_type", Parse.bind_type)
  91. if Parse.bind_type != -1 {
  92. Parse.bind_type = BIND_PARAM_BY_POS
  93. return BIND_PARAM_BY_POS
  94. }
  95. Parse.bind_type = BIND_PARAM_BY_NAME
  96. return BIND_PARAM_BY_NAME
  97. }
  98. // 判断参数变量类型
  99. func (param *xuguParse) assertParamType(dV driver.Value, pos int) error {
  100. fmt.Println("-----(param *xuguParse) assertParamType 判断参数类型")
  101. var dest xuguValue
  102. switch dV.(type) {
  103. case int64:
  104. srcv, ok := dV.(int64)
  105. if !ok {
  106. return errors.New("assertParamType int64 error")
  107. }
  108. S := strconv.FormatInt(srcv, 10)
  109. dest.value = []byte(S)
  110. dest.valueLength = strings.Count(S, "") - 1
  111. dest.islob = false
  112. dest.types = fieldType_I8
  113. case float32:
  114. srcv, ok := dV.(float64)
  115. if !ok {
  116. return errors.New("assertParamType float32 error")
  117. }
  118. S := strconv.FormatFloat(srcv, 'f', 6, 64)
  119. dest.value = []byte(S)
  120. dest.valueLength = strings.Count(S, "") - 1
  121. dest.islob = false
  122. dest.types = fieldType_R4
  123. case float64:
  124. srcv, ok := dV.(float64)
  125. if !ok {
  126. return errors.New("assertParamType float64 error")
  127. }
  128. S := strconv.FormatFloat(srcv, 'f', 15, 64)
  129. dest.value = []byte(S)
  130. dest.valueLength = strings.Count(S, "") - 1
  131. dest.islob = false
  132. dest.types = fieldType_R8
  133. case bool:
  134. srcv, ok := dV.(bool)
  135. if !ok {
  136. return errors.New("assertParamType bool error")
  137. }
  138. S := strconv.FormatBool(srcv)
  139. dest.value = []byte(S)
  140. dest.valueLength = strings.Count(S, "") - 1
  141. dest.islob = false
  142. dest.types = fieldType_BOOL
  143. case string:
  144. S, ok := dV.(string)
  145. if !ok {
  146. return errors.New("assertParamType string error")
  147. }
  148. fmt.Println("是string")
  149. dest.value = []byte(S)
  150. dest.valueLength = strings.Count(S, "") - 1
  151. dest.islob = false
  152. dest.types = fieldType_CHAR
  153. fmt.Printf("是string类型: %#v\n", dest)
  154. case time.Time:
  155. srcv, ok := dV.(time.Time)
  156. if !ok {
  157. return errors.New("assertParamType time error")
  158. }
  159. tm := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d",
  160. srcv.Year(), int(srcv.Month()), srcv.Day(),
  161. srcv.Hour(), srcv.Minute(), srcv.Second())
  162. dest.value = []byte(tm)
  163. dest.valueLength = strings.Count(tm, "") - 1
  164. dest.islob = false
  165. dest.types = fieldType_TIME
  166. case []byte:
  167. // re := cgo_xgc_new_lob(&dest.plob)
  168. // if re < 0 {
  169. // return errors.New("Cannot create new large object")
  170. // }
  171. srcv, ok := dV.([]byte)
  172. if !ok {
  173. return errors.New("assertParamType []byte error")
  174. }
  175. // cgo_xgc_put_lob_data(
  176. // &dest.plob,
  177. // unsafe.Pointer((*C.char)(unsafe.Pointer(&srcv[0]))),
  178. // len(srcv))
  179. // cgo_xgc_put_lob_data(&dest.plob, nil, -1)
  180. dest.value = srcv
  181. dest.valueLength = len(srcv)
  182. dest.islob = true
  183. dest.types = fieldType_BLOB
  184. fmt.Println("-----判断参数类型为 byte[]")
  185. case nil:
  186. dest.value = []byte("xugusql")
  187. dest.valueLength = 0
  188. dest.islob = false
  189. dest.types = fieldType_CHAR
  190. default:
  191. /* OTHER DATA TYPE */
  192. return errors.New("Unknown data type")
  193. }
  194. param.position = pos
  195. param.values = append(param.values, dest)
  196. return nil
  197. }
  198. // 判断参数名
  199. func (param *xuguParse) assertParamName(query string) error {
  200. fmt.Println("----(param *xuguParse) assertParamName 判断参数名")
  201. if param.param_count <= 0 {
  202. param.assertParamCount(query)
  203. }
  204. pos := 0
  205. phead := -1
  206. for {
  207. pos = strings.IndexByte(query[phead+1:], ':')
  208. if pos == -1 {
  209. break
  210. }
  211. pos += phead + 1
  212. tmp := pos
  213. for tmp > phead {
  214. tmp--
  215. if query[tmp] == ' ' {
  216. continue
  217. }
  218. // Parse parameter positions bound by parameter name
  219. if query[tmp] == ',' || query[tmp] == '(' {
  220. parg := pos
  221. for true {
  222. parg++
  223. if query[parg] == ',' || query[parg] == ')' || query[parg] == ' ' {
  224. param.param_names = append(param.param_names, query[pos+1:parg]...)
  225. break
  226. }
  227. }
  228. }
  229. break
  230. }
  231. phead = pos
  232. }
  233. fmt.Printf("param: %#v", param)
  234. fmt.Printf("param: %#v", query)
  235. fmt.Println("----(param *xuguParse) assertParamName 判断参数名结束")
  236. return nil
  237. }
  238. func (param *xuguParse) bindParamByPos(query string) string {
  239. fmt.Println("----(param *xuguParse) bindParamByPos ")
  240. fmt.Printf("param: %#v \n", param)
  241. for i := 0; i < param.param_count; i++ {
  242. //sql语句里替换夫的下标, 替换为
  243. query = strings.Replace(query, "?", string(param.values[i].value), 1)
  244. }
  245. return query
  246. }
  247. type SelectResult struct {
  248. Field_Num uint32
  249. Fields []FieldDescri
  250. Values [][]ValueData //[字段][字段所有值]
  251. rowIdx int
  252. }
  253. type FieldDescri struct {
  254. FieldNameLen int
  255. FieldName string
  256. FieldType fieldType
  257. FieldPreciScale int
  258. FieldFlag int
  259. }
  260. type ValueData struct {
  261. Col_len uint32
  262. Col_Data []byte
  263. }
  264. type InsertResult struct {
  265. RowidLen uint32
  266. RowidData []byte
  267. }
  268. type UpdateResult struct {
  269. UpdateNum uint32
  270. }
  271. type DeleteResult struct {
  272. DeleteNum uint32
  273. }
  274. type ProcRet struct {
  275. RetDType uint32
  276. RetDataLen uint32
  277. RetData []byte
  278. }
  279. type OutParamRet struct {
  280. OutParamNo uint32
  281. OutParamDType uint32
  282. OutParamLen uint32
  283. OutParamData []byte
  284. }
  285. type ErrInfo struct {
  286. ErrStrLen uint32
  287. ErrStr []byte
  288. }
  289. type WarnInfo struct {
  290. WarnStrLen uint32
  291. WarnStr []byte
  292. }
  293. type Message struct {
  294. MsgStrLen uint32
  295. MsgStr []byte
  296. }
  297. type FormArgDescri struct {
  298. ArgNum uint32
  299. Args []ArgDescri
  300. }
  301. type ArgDescri struct {
  302. ArgNameLen uint32
  303. ArgName []byte
  304. ArgNo uint32
  305. ArgDType uint32
  306. ArgPreciScale uint32
  307. }
  308. func parseSelectResult(readBuf *buffer) (*SelectResult, error) {
  309. fmt.Println("调用 parseSelectResult")
  310. data := &SelectResult{}
  311. char := readBuf.peekChar()
  312. fmt.Println("--=char: ", string(char))
  313. switch char {
  314. case 'A':
  315. readBuf.idx++
  316. //Field_Num
  317. Field_Num := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  318. data.Field_Num = Field_Num
  319. data.rowIdx = 0
  320. fmt.Println("Field_Num: ", data.Field_Num)
  321. //获取字段信息
  322. for i := 0; i < int(Field_Num); i++ {
  323. field := FieldDescri{}
  324. //Field_Name_Len
  325. field.FieldNameLen = int(binary.LittleEndian.Uint32(readBuf.readNext(4, true)))
  326. fmt.Println("field.FieldNameLen: ", field.FieldNameLen)
  327. //Field_Name:
  328. field.FieldName = string(readBuf.readNext(field.FieldNameLen, false))
  329. fmt.Println("field.Field_Name: ", field.FieldName)
  330. //Field_DType:
  331. field.FieldType = fieldType(binary.LittleEndian.Uint32(readBuf.readNext(4, true)))
  332. fmt.Println("field.FieldType: ", field.FieldType)
  333. //Field_Preci_Scale:
  334. field.FieldPreciScale = int(binary.LittleEndian.Uint32(readBuf.readNext(4, true)))
  335. fmt.Println("field.FieldPreciScale: ", field.FieldPreciScale)
  336. //Field_Flag:
  337. field.FieldFlag = int(binary.LittleEndian.Uint32(readBuf.readNext(4, true)))
  338. fmt.Println("field.FieldFlag: ", field.FieldFlag)
  339. data.Fields = append(data.Fields, field)
  340. }
  341. data.Values = make([][]ValueData, data.Field_Num)
  342. //获取字段的行值,并判断类型
  343. // 使用 Peek 方法检查下一个字节是否为'R'或'K'
  344. fmt.Println("\n\n=========开始获取行数据=================================")
  345. defer func() {
  346. fmt.Println("\n\n=========获取行数据结束=================================")
  347. }()
  348. char := readBuf.peekChar()
  349. fmt.Println(" --char: ", string(char))
  350. readBuf.idx++
  351. if char == 'K' {
  352. break
  353. } else if char == 'R' {
  354. colIdx := 0
  355. typeIdx := 0
  356. fmt.Println("开始循环 ")
  357. for {
  358. col := ValueData{}
  359. //获取数据的大小
  360. col.Col_len = binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  361. fmt.Println("数据大小为: ", col.Col_len)
  362. //获取数据
  363. //判断类型
  364. fmt.Println("查看类型", data.Fields[typeIdx].FieldType)
  365. switch data.Fields[typeIdx].FieldType {
  366. case fieldType_CHAR, fieldType_NCHAR:
  367. col.Col_Data = readBuf.readNext(int(col.Col_len), false)
  368. fmt.Println("fieldTypeVarChar data: ", col.Col_Data, string(col.Col_Data))
  369. data.Values[colIdx] = append(data.Values[colIdx], col)
  370. colIdx++
  371. fmt.Println("从查询返回 解析出的值 :", col.Col_Data, string(col.Col_Data))
  372. char := readBuf.peekChar()
  373. if char == 'R' {
  374. readBuf.idx++
  375. colIdx = 0
  376. if typeIdx >= int(data.Field_Num)-1 {
  377. typeIdx = 0
  378. } else {
  379. typeIdx++
  380. }
  381. continue
  382. } else if char == 'K' {
  383. return data, nil
  384. //break
  385. }
  386. typeIdx++
  387. //fieldTypeTinyint,
  388. case fieldType_I1, fieldType_I4, fieldType_I8:
  389. col.Col_Data = reverseBytes(readBuf.readNext(int(col.Col_len), false))
  390. data.Values[colIdx] = append(data.Values[colIdx], col)
  391. colIdx++
  392. char := readBuf.peekChar()
  393. fmt.Println("从查询返回 解析出的值 :", col.Col_Data, string(col.Col_Data))
  394. if char == 'R' {
  395. readBuf.idx++
  396. colIdx = 0
  397. if typeIdx >= int(data.Field_Num)-1 {
  398. fmt.Println("typeIdx <= int(data.Field_Num)-1: ", typeIdx, int(data.Field_Num)-1)
  399. typeIdx = 0
  400. } else {
  401. typeIdx++
  402. }
  403. continue
  404. } else if char == 'K' {
  405. return data, nil
  406. //break
  407. }
  408. typeIdx++
  409. } //swich end
  410. fmt.Println("循环结束")
  411. return data, nil
  412. } //for end
  413. } else {
  414. break
  415. }
  416. default:
  417. return nil, errors.New("parseQueryResult error")
  418. } //swich end
  419. return data, nil
  420. }
  421. func parseInsertResult(readBuf *buffer) (*InsertResult, error) {
  422. //Rowid_Len
  423. Rowid_Len := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  424. //Rowid_Data
  425. encoded := readBuf.readNext(int(Rowid_Len), false)
  426. //检测是否结束
  427. char := readBuf.peekChar()
  428. if char == 'K' {
  429. return &InsertResult{
  430. RowidLen: Rowid_Len,
  431. RowidData: encoded,
  432. }, nil
  433. }
  434. return nil, errors.New("parseInsertResult error")
  435. }
  436. func parseUpdateResult(readBuf *buffer) (*UpdateResult, error) {
  437. updateNum := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  438. return &UpdateResult{UpdateNum: updateNum}, nil
  439. }
  440. func parseDeleteResult(readBuf *buffer) (*DeleteResult, error) {
  441. deleteNum := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  442. return &DeleteResult{DeleteNum: deleteNum}, nil
  443. }
  444. func parseProcRet(readBuf *buffer) (*ProcRet, error) {
  445. retDType := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  446. retDataLen := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  447. retData := readBuf.readNext(int(retDataLen), false)
  448. return &ProcRet{RetDType: retDType, RetDataLen: retDataLen, RetData: retData}, nil
  449. }
  450. func parseOutParamRet(readBuf *buffer) (*OutParamRet, error) {
  451. outParamNo := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  452. outParamDType := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  453. outParamLen := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  454. outParamData := readBuf.readNext(int(outParamLen), false)
  455. return &OutParamRet{
  456. OutParamNo: outParamNo,
  457. OutParamDType: outParamDType,
  458. OutParamLen: outParamLen,
  459. OutParamData: outParamData,
  460. }, nil
  461. }
  462. func parseErrInfo(readBuf *buffer) (*ErrInfo, error) {
  463. errStrLen := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  464. errStr := readBuf.readNext(int(errStrLen), false)
  465. return &ErrInfo{ErrStrLen: errStrLen, ErrStr: errStr}, nil
  466. }
  467. func parseWarnInfo(readBuf *buffer) (*WarnInfo, error) {
  468. warnStrLen := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  469. warnStr := readBuf.readNext(int(warnStrLen), false)
  470. return &WarnInfo{WarnStrLen: warnStrLen, WarnStr: warnStr}, nil
  471. }
  472. func parseMessage(readBuf *buffer) (*Message, error) {
  473. msgStrLen := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  474. msgStr := readBuf.readNext(int(msgStrLen), false)
  475. return &Message{MsgStrLen: msgStrLen, MsgStr: msgStr}, nil
  476. }
  477. func parseFormArgDescri(readBuf *buffer) (*FormArgDescri, error) {
  478. // FormArgDescri: '$' Arg_Num { Arg_Name_Len Arg_Name Arg_No Arg_DType Arg_Preci_Scale }+
  479. Arg_Num := binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  480. formArgDescri := &FormArgDescri{ArgNum: Arg_Num}
  481. fmt.Println("-- parseFormArgDescri Arg_Num:", Arg_Num)
  482. for i := 0; i < int(Arg_Num); i++ {
  483. arg := ArgDescri{}
  484. //Arg_Name_Len
  485. arg.ArgNameLen = binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  486. //Arg_Name
  487. arg.ArgName = readBuf.readNext(int(arg.ArgNameLen), false)
  488. //Arg_No
  489. arg.ArgNo = binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  490. //Argg_DType
  491. arg.ArgDType = binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  492. //Arg_Preci_Scale
  493. arg.ArgPreciScale = binary.LittleEndian.Uint32(readBuf.readNext(4, true))
  494. formArgDescri.Args = append(formArgDescri.Args, arg)
  495. }
  496. fmt.Printf("formArgDescri %#v \n", formArgDescri)
  497. return formArgDescri, nil
  498. }