xugu_utils.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package xugu
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func parseDSN(dsn string) dsnConfig {
  7. // Initialize a dsnConfig struct
  8. var config dsnConfig
  9. // Split the string by semicolons
  10. pairs := strings.Split(dsn, ";")
  11. // Iterate over the pairs and map them to the struct fields
  12. for _, pair := range pairs {
  13. // Split each pair by the equals sign
  14. kv := strings.SplitN(pair, "=", 2)
  15. if len(kv) != 2 {
  16. continue
  17. }
  18. key, value := strings.TrimSpace(kv[0]), strings.Trim(strings.TrimSpace(kv[1]), "'")
  19. keyL := strings.ToLower(key)
  20. // Map the key to the appropriate struct field
  21. switch keyL {
  22. case "ip":
  23. config.IP = value
  24. case "port":
  25. config.Port = value
  26. case "db":
  27. config.Database = value
  28. case "user":
  29. config.User = value
  30. case "pwd":
  31. config.Password = value
  32. case "encryptor":
  33. config.Encryptor = value
  34. case "char_set":
  35. config.CharSet = value
  36. case "time_zone":
  37. config.TimeZone = value
  38. case "iso_level":
  39. config.IsoLevel = value
  40. case "lock_timeout":
  41. config.LockTimeout = value
  42. case "auto_commit":
  43. config.AutoCommit = value
  44. case "strict_commit":
  45. config.StrictCommit = value
  46. case "result":
  47. config.Result = value
  48. case "return_schema":
  49. config.ReturnSchema = value
  50. case "return_cursor_id":
  51. config.ReturnCursorID = value
  52. case "lob_ret":
  53. config.LobRet = value
  54. case "return_rowid":
  55. config.ReturnRowid = value
  56. case "version":
  57. config.Version = value
  58. }
  59. }
  60. return config
  61. }
  62. func generateLoginString(config dsnConfig) string {
  63. baseString := "login database = '%s' user = '%s' password = '%s' "
  64. additionalParams := ""
  65. if config.Encryptor != "" {
  66. additionalParams += fmt.Sprintf(" encryptor='%s'", config.Encryptor)
  67. }
  68. if config.CharSet != "" {
  69. additionalParams += fmt.Sprintf(" char_set='%s'", config.CharSet)
  70. }
  71. if config.TimeZone != "" {
  72. additionalParams += fmt.Sprintf(" time_zone='%s'", config.TimeZone)
  73. }
  74. if config.IsoLevel != "" {
  75. additionalParams += fmt.Sprintf(" iso_level='%s'", config.IsoLevel)
  76. }
  77. if config.LockTimeout != "" {
  78. additionalParams += fmt.Sprintf(" lock_timeout='%s'", config.LockTimeout)
  79. }
  80. if config.AutoCommit != "" {
  81. additionalParams += fmt.Sprintf(" auto_commit='%s'", config.AutoCommit)
  82. }
  83. if config.StrictCommit != "" {
  84. additionalParams += fmt.Sprintf(" strict_commit='%s'", config.StrictCommit)
  85. }
  86. if config.Result != "" {
  87. additionalParams += fmt.Sprintf(" result='%s'", config.Result)
  88. }
  89. if config.ReturnSchema != "" {
  90. additionalParams += fmt.Sprintf(" return_schema='%s'", config.ReturnSchema)
  91. }
  92. if config.ReturnCursorID != "" {
  93. additionalParams += fmt.Sprintf(" return_cursor_id='%s'", config.ReturnCursorID)
  94. }
  95. if config.LobRet != "" {
  96. additionalParams += fmt.Sprintf(" lob_ret='%s'", config.LobRet)
  97. }
  98. if config.ReturnRowid != "" {
  99. additionalParams += fmt.Sprintf(" return_rowid='%s'", config.ReturnRowid)
  100. }
  101. if config.Version != "" {
  102. additionalParams += fmt.Sprintf(" version='%s'", config.Version)
  103. } else {
  104. additionalParams += " version='201'"
  105. }
  106. finalString := fmt.Sprintf(baseString, config.Database, config.User, config.Password)
  107. if additionalParams != "" {
  108. finalString += additionalParams
  109. }
  110. //finalString += " version='201'"
  111. return finalString
  112. }
  113. // reverseBytes 反转 byte slice 的顺序
  114. func reverseBytes(b []byte) []byte {
  115. reversed := make([]byte, len(b))
  116. for i := range b {
  117. reversed[i] = b[len(b)-1-i]
  118. }
  119. return reversed
  120. }
  121. func gt(name string) {
  122. fmt.Printf("\n=============%s================\n", name)
  123. }
  124. func switchSQLType(sql string) int {
  125. // 去掉首尾的空格、换行符和回车符
  126. sql = strings.TrimSpace(sql)
  127. if len(sql) < 6 {
  128. return SQL_OTHER
  129. }
  130. // 取前6个字符并转为大写
  131. kstr := strings.ToUpper(sql[:6])
  132. // 根据SQL语句前缀判断类型
  133. switch {
  134. case strings.HasPrefix(kstr, "SELECT"):
  135. if strings.Contains(sql, ";") && len(sql[strings.Index(sql, ";"):]) > 5 {
  136. return SQL_OTHER // 多结果集
  137. }
  138. return SQL_SELECT
  139. case strings.HasPrefix(kstr, "INSERT"):
  140. return SQL_INSERT
  141. case strings.HasPrefix(kstr, "UPDATE"):
  142. return SQL_UPDATE
  143. case strings.HasPrefix(kstr, "DELETE"):
  144. return SQL_DELETE
  145. case strings.HasPrefix(kstr, "CREATE"):
  146. return SQL_CREATE
  147. case strings.HasPrefix(kstr, "ALTER "):
  148. return SQL_ALTER
  149. case strings.HasPrefix(kstr, "EXEC "):
  150. return SQL_PROCEDURE
  151. case strings.HasPrefix(kstr, "EXECUT"):
  152. return SQL_PROCEDURE
  153. case strings.HasPrefix(kstr, "STC"):
  154. return SQL_SELECT
  155. default:
  156. return SQL_OTHER
  157. }
  158. }