743885e6361509cf0fe7466c8f841a75a349206f.svn-base 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  1. package restful
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. _ "github.com/go-sql-driver/mysql"
  8. "../../pkg/setting"
  9. "github.com/patrickmn/go-cache"
  10. "github.com/tealeg/xlsx"
  11. "github.com/xormplus/xorm"
  12. "math"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "unsafe"
  17. )
  18. var (
  19. Engine *xorm.Engine
  20. Apisql_cache *cache.Cache
  21. dbs *sql.DB
  22. )
  23. func SQLT() *xorm.Session {
  24. tx := Engine.NewSession()
  25. return tx
  26. }
  27. // SQLInit 初始化数据库操作句柄,这里要提供:
  28. // driverName string: 数据库类型,例如mysql、sqlite等,参考github.com/go-sql-driver/mysql官方介绍
  29. // dataSourceName string: 数据库地址,参考github.com/go-sql-driver/mysql官方介绍
  30. // MaxOpenConns int: 最大缓存连接数,这个数值包含了MaxIdleConns
  31. // MaxIdleConns int:预备的最大空闲连接数
  32. func SQLInit(driverName, dataSourceName string, maxOpenConns, maxIdleConns int, showlog bool) error {
  33. if Engine == nil {
  34. var err error
  35. if Engine, err = xorm.NewEngine(driverName, dataSourceName); err != nil {
  36. return err
  37. }
  38. if showlog {
  39. Engine.ShowSQL(true)
  40. }
  41. Engine.SetMaxIdleConns(maxIdleConns)
  42. Engine.SetMaxOpenConns(maxOpenConns)
  43. }
  44. if Apisql_cache == nil {
  45. Apisql_cache = cache.New(600*time.Second, 10*time.Second)
  46. SetCacheSql()
  47. }
  48. err := SQLInitRow(driverName, dataSourceName, maxOpenConns, maxIdleConns)
  49. if err != nil {
  50. return err
  51. }
  52. return nil
  53. }
  54. func SQLInitRow(driverName, dataSourceName string, maxOpenConns, maxIdleConns int) error {
  55. if dbs == nil {
  56. var err error
  57. if dbs, err = sql.Open(driverName, dataSourceName); err != nil {
  58. return err
  59. }
  60. dbs.SetMaxOpenConns(maxOpenConns)
  61. dbs.SetMaxIdleConns(maxIdleConns)
  62. }
  63. return nil
  64. }
  65. func SetCacheSql() {
  66. valuesMap, err := Engine.SQL("SELECT sqlname, CONCAT(sqlstr,'|',IFNULL(params,'')) AS sqlstr FROM apisql WHERE ENABLE>0 ").QueryString()
  67. if err == nil && len(valuesMap) > 0 {
  68. for _, v := range valuesMap {
  69. Apisql_cache.Set(v["sqlname"], v["sqlstr"], cache.DefaultExpiration)
  70. }
  71. }
  72. }
  73. func sqlCheckParam1(param string) error {
  74. /*if strings.Contains(param, "where") {
  75. return errors.New("can not have where")
  76. }
  77. if strings.Contains(param, "and") {
  78. return errors.New("can not have and")
  79. }
  80. if strings.Contains(param, "or") {
  81. return errors.New("can not have or")
  82. }
  83. if strings.Contains(param, "=") {
  84. return errors.New("can not have =")
  85. }*/
  86. if strings.Contains(param, ";") {
  87. return errors.New("can not have ;")
  88. }
  89. return nil
  90. }
  91. func GetSqlByName(name string, tx *xorm.Session) (string, string) {
  92. sqls := ""
  93. param := ""
  94. if sql, ok := Apisql_cache.Get(name); ok {
  95. tempstrb := strings.IndexAny(sql.(string), "|")
  96. sqls = sql.(string)[0:tempstrb]
  97. param = sql.(string)[tempstrb+1:]
  98. } else {
  99. valuesMap, err := tx.SQL("SELECT sqlstr, params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", name).QueryString() //20200615 13:17
  100. if err == nil && len(valuesMap) > 0 {
  101. sqls = valuesMap[0]["sqlstr"]
  102. param = valuesMap[0]["params"]
  103. Apisql_cache.Set(name, (sqls + "|" + param), cache.DefaultExpiration)
  104. }
  105. }
  106. if setting.DatabaseSetting.ShowGetSqllog {
  107. fmt.Println("apisql:" + sqls)
  108. fmt.Println("param:" + param)
  109. }
  110. return sqls, param
  111. }
  112. func GetSqlByNameDB(name string) (string, string) {
  113. sqls := ""
  114. param := ""
  115. //if sql, ok := Apisql_cache.Get(name); ok && setting.DatabaseSetting.CacheApiSql {
  116. // tempstrb := strings.IndexAny(sql.(string), "|")
  117. // sqls = sql.(string)[0:tempstrb]
  118. // param = sql.(string)[tempstrb+1:]
  119. //} else {
  120. //valuesMap, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", name).QueryString()
  121. valuesMap, err := Engine.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", name).QueryString()
  122. if err == nil && len(valuesMap) > 0 {
  123. sqls = valuesMap[0]["sqlstr"]
  124. param = valuesMap[0]["params"]
  125. Apisql_cache.Set(name, (sqls + "|" + param), cache.DefaultExpiration)
  126. }
  127. //}
  128. if setting.DatabaseSetting.ShowGetSqllog {
  129. fmt.Println("apisql:" + sqls)
  130. fmt.Println("param:" + param)
  131. }
  132. return sqls, param
  133. }
  134. // sqlGetValues 根据结构体中指向实际数据的指针获取出数据,并存储到另一张表中返回
  135. func sqlGetValues1(pvs []interface{}, columnsType []*sql.ColumnType, columnsLen int) map[string]interface{} {
  136. result := make(map[string]interface{}, columnsLen)
  137. for a := 0; a < columnsLen; a++ {
  138. switch s := pvs[a].(type) {
  139. case *int8:
  140. result[columnsType[a].Name()] = *s
  141. case *int16:
  142. result[columnsType[a].Name()] = *s
  143. case *int32:
  144. result[columnsType[a].Name()] = *s
  145. case *int64:
  146. result[columnsType[a].Name()] = *s
  147. case *float32:
  148. result[columnsType[a].Name()] = *s
  149. case *float64:
  150. result[columnsType[a].Name()] = *s
  151. case *string:
  152. result[columnsType[a].Name()] = *s
  153. case *sql.NullInt64:
  154. result[columnsType[a].Name()] = *s
  155. case *sql.NullString:
  156. result[columnsType[a].Name()] = *s
  157. }
  158. }
  159. return result
  160. }
  161. // 这里返回的是原始数组的基础上加上了字段名标识
  162. func sqlQuery1(columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) ([]map[string]interface{}, error) {
  163. jsondata := make([]map[string]interface{}, queryCount)
  164. for k1, v1 := range queryData {
  165. if k1 >= queryCount {
  166. break
  167. }
  168. jsondata[k1] = sqlGetValues(v1, columnsType, columnsLen)
  169. }
  170. return jsondata, nil
  171. }
  172. func sqlQueryByTinyIntMap1(columnName string, columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) (map[int8]map[string]interface{}, error) {
  173. jsondata := make(map[int8]map[string]interface{}, queryCount)
  174. for k1, v1 := range queryData {
  175. if k1 >= queryCount {
  176. break
  177. }
  178. for a := 0; a < columnsLen; a++ {
  179. if columnsType[a].Name() == columnName {
  180. if value, ok := v1[a].(*int8); ok {
  181. jsondata[*value] = sqlGetValues(v1, columnsType, columnsLen)
  182. }
  183. break
  184. }
  185. }
  186. }
  187. return jsondata, nil
  188. }
  189. func sqlQueryBySmallIntMap1(columnName string, columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) (map[int16]map[string]interface{}, error) {
  190. jsondata := make(map[int16]map[string]interface{}, queryCount)
  191. for k1, v1 := range queryData {
  192. if k1 >= queryCount {
  193. break
  194. }
  195. for a := 0; a < columnsLen; a++ {
  196. if columnsType[a].Name() == columnName {
  197. if value, ok := v1[a].(*int16); ok {
  198. jsondata[*value] = sqlGetValues(v1, columnsType, columnsLen)
  199. }
  200. break
  201. }
  202. }
  203. }
  204. return jsondata, nil
  205. }
  206. func sqlQueryByIntMap1(columnName string, columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) (map[int32]map[string]interface{}, error) {
  207. jsondata := make(map[int32]map[string]interface{}, queryCount)
  208. for k1, v1 := range queryData {
  209. if k1 >= queryCount {
  210. break
  211. }
  212. for a := 0; a < columnsLen; a++ {
  213. if columnsType[a].Name() == columnName {
  214. if value, ok := v1[a].(*int32); ok {
  215. jsondata[*value] = sqlGetValues(v1, columnsType, columnsLen)
  216. }
  217. break
  218. }
  219. }
  220. }
  221. return jsondata, nil
  222. }
  223. func sqlQueryByBigIntMap1(columnName string, columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) (map[int64]map[string]interface{}, error) {
  224. jsondata := make(map[int64]map[string]interface{}, queryCount)
  225. for k1, v1 := range queryData {
  226. if k1 >= queryCount {
  227. break
  228. }
  229. for a := 0; a < columnsLen; a++ {
  230. if columnsType[a].Name() == columnName {
  231. if value, ok := v1[a].(*int64); ok {
  232. jsondata[*value] = sqlGetValues(v1, columnsType, columnsLen)
  233. }
  234. break
  235. }
  236. }
  237. }
  238. return jsondata, nil
  239. }
  240. func sqlQueryByFloatIntMap1(columnName string, columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) (map[float32]map[string]interface{}, error) {
  241. jsondata := make(map[float32]map[string]interface{}, queryCount)
  242. for k1, v1 := range queryData {
  243. if k1 >= queryCount {
  244. break
  245. }
  246. for a := 0; a < columnsLen; a++ {
  247. if columnsType[a].Name() == columnName {
  248. if value, ok := v1[a].(*float32); ok {
  249. jsondata[*value] = sqlGetValues(v1, columnsType, columnsLen)
  250. }
  251. break
  252. }
  253. }
  254. }
  255. return jsondata, nil
  256. }
  257. func sqlQueryByDoubleMap1(columnName string, columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) (map[float64]map[string]interface{}, error) {
  258. jsondata := make(map[float64]map[string]interface{}, queryCount)
  259. for k1, v1 := range queryData {
  260. if k1 >= queryCount {
  261. break
  262. }
  263. for a := 0; a < columnsLen; a++ {
  264. if columnsType[a].Name() == columnName {
  265. if value, ok := v1[a].(*float64); ok {
  266. jsondata[*value] = sqlGetValues(v1, columnsType, columnsLen)
  267. }
  268. break
  269. }
  270. }
  271. }
  272. return jsondata, nil
  273. }
  274. func sqlQueryByStringMap1(columnName string, columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) (map[string]map[string]interface{}, error) {
  275. jsondata := make(map[string]map[string]interface{}, queryCount)
  276. for k1, v1 := range queryData {
  277. if k1 >= queryCount {
  278. break
  279. }
  280. for a := 0; a < columnsLen; a++ {
  281. if columnsType[a].Name() == columnName {
  282. if value, ok := v1[a].(*string); ok {
  283. jsondata[*value] = sqlGetValues(v1, columnsType, columnsLen)
  284. }
  285. break
  286. }
  287. }
  288. }
  289. return jsondata, nil
  290. }
  291. func sqlGetColumnType1(columnsType []*sql.ColumnType, columnsLen int, valueName string) string {
  292. for a := 0; a < columnsLen; a++ {
  293. if columnsType[a].Name() == valueName {
  294. return columnsType[a].DatabaseTypeName()
  295. }
  296. }
  297. return ""
  298. }
  299. func round1(num float64) int {
  300. return int(num + math.Copysign(0.5, num))
  301. }
  302. func toFixed1(num float64, precision int) float64 {
  303. output := math.Pow(10, float64(precision))
  304. return float64(round(num*output)) / output
  305. }
  306. // QueryByMap 将查询到的数据,按照指定字段的值做为索引构建map并返回
  307. // columnName string: 作为索引的字段名称
  308. // sql string: 查询需要获取哪些字段的值,就是select后面跟着的部分,一般用"*"
  309. // where string: 过滤条件,就是where后面跟着的部分
  310. // order string: 排序条件,就是order by后面跟着的部分。默认是ASC排序,除非"-"开头则DESC排序
  311. // offset string: limit后面逗号相隔的两个数值,前者就是offset,后者就是count
  312. // count string: limit后面逗号相隔的两个数值,前者就是offset,后者就是count
  313. func QueryByMap1(sqlstr string, offset, count int, params []interface{}) (interface{}, error) {
  314. if err := sqlCheckParam(sqlstr); err != nil {
  315. return 0, err
  316. }
  317. recordcount := int64(0)
  318. if count > 1 {
  319. tt, err := Engine.SQL("SELECT COUNT(*) as count FROM ("+sqlstr+") a", params...).Query().List()
  320. if err != nil {
  321. fmt.Println("s1", err)
  322. return nil, err
  323. }
  324. if tt == nil {
  325. recordcount = 0
  326. } else {
  327. recordcount = tt[0]["count"].(int64)
  328. }
  329. }
  330. queryData := make([]map[string]interface{},1)
  331. var err error
  332. if(count==0){
  333. queryData, err = Engine.SQL(sqlstr, params...).Query().List()
  334. } else {
  335. if count > 0 && offset > 0 {
  336. offset = offset * count
  337. }
  338. queryData, err = Engine.SQL(sqlstr, params...).Limit(count, offset).Query().List()
  339. }
  340. if err != nil {
  341. return nil, err
  342. }
  343. if recordcount == 0 {
  344. recordcount = int64(len(queryData))
  345. }
  346. data := make(map[string]interface{})
  347. data["list"] = queryData
  348. data["total"] = recordcount
  349. data["code"] = 0
  350. data["message"] = "ok"
  351. if count > 0 {
  352. data["pages"] = math.Ceil(float64(recordcount) / float64(count))
  353. data["pageNum"] = math.Ceil(float64(offset+1) / float64(count))
  354. }
  355. data["pageSize"] = count
  356. return data, nil
  357. }
  358. // QueryByXlsx 将查询到的数据,按照指定字段的值做为索引构建map并返回
  359. // sql string: 查询需要获取哪些字段的值,就是select后面跟着的部分,一般用"*"
  360. // params string: 过滤条件,就是where后面跟着的部分
  361. func QueryByXlsx1(sqlstr string, params []interface{}) (*xlsx.File, error) {
  362. if err := sqlCheckParam(sqlstr); err != nil {
  363. return nil, err
  364. }
  365. rows, err := Engine.SQL(sqlstr, params...).QueryRows()
  366. if err != nil {
  367. return nil, err
  368. }
  369. defer rows.Close()
  370. columnsType, _ := rows.ColumnTypes()
  371. columnsLen := len(columnsType)
  372. columns, _ := rows.Columns()
  373. values := make([]interface{}, columnsLen)
  374. valuePtrs := make([]interface{}, columnsLen)
  375. var sheet *xlsx.Sheet
  376. var row *xlsx.Row
  377. var cell *xlsx.Cell
  378. file := xlsx.NewFile()
  379. sheet, err = file.AddSheet("Sheet1")
  380. if err != nil {
  381. fmt.Printf(err.Error())
  382. }
  383. row = sheet.AddRow()
  384. for _, columnname := range columns {
  385. cell = row.AddCell()
  386. cell.Value = columnname
  387. }
  388. for rows.Next() {
  389. for a := 0; a < columnsLen; a++ {
  390. valuePtrs[a] = &values[a]
  391. }
  392. rows.Scan(valuePtrs...)
  393. row = sheet.AddRow()
  394. for i, _ := range columns {
  395. var v interface{}
  396. val := values[i]
  397. b, ok := val.([]byte)
  398. cell = row.AddCell()
  399. if ok {
  400. v1 := string(b)
  401. switch columnsType[i].DatabaseTypeName() {
  402. case "TINYINT", "SMALLINT", "MEDIUMINT", "INT", "INTEGER", "BIGINT":
  403. {
  404. v, _ = strconv.ParseInt(v1, 10, 32)
  405. cell.Value = fmt.Sprintf("%v", v)
  406. }
  407. case "FLOAT", "DOUBLE", "DECIMAL":
  408. {
  409. cindex := len(val.([]byte)) - strings.Index(v1, ".") - 1
  410. v2, _ := strconv.ParseFloat(v1, 32)
  411. v = toFixed(v2, cindex)
  412. cell.Value = fmt.Sprintf("%v", v)
  413. }
  414. case "BOOL":
  415. {
  416. v, _ = strconv.ParseBool(v1)
  417. cell.Value = fmt.Sprintf("%v", v)
  418. }
  419. default:
  420. {
  421. v = v1
  422. cell.Value = fmt.Sprintf("%v", v)
  423. }
  424. }
  425. } else {
  426. v = val
  427. cell.Value = fmt.Sprintf("%v", v)
  428. }
  429. if cell.Value == "<nil>" {
  430. cell.Value = ""
  431. }
  432. }
  433. }
  434. return file, nil
  435. }
  436. // QueryByMap 将查询到的数据,按照指定字段的值做为索引构建map并返回
  437. // columnName string: 作为索引的字段名称
  438. // sql string: 查询需要获取哪些字段的值,就是select后面跟着的部分,一般用"*"
  439. // where string: 过滤条件,就是where后面跟着的部分
  440. // order string: 排序条件,就是order by后面跟着的部分。默认是ASC排序,除非"-"开头则DESC排序
  441. // offset string: limit后面逗号相隔的两个数值,前者就是offset,后者就是count
  442. // count string: limit后面逗号相隔的两个数值,前者就是offset,后者就是count
  443. func QueryByMapMenu1(sqlstr string, offset, count int, params []interface{}) ([]map[string]interface{}, error) {
  444. if err := sqlCheckParam(sqlstr); err != nil {
  445. return nil, err
  446. }
  447. if offset < 0 {
  448. offset = 0
  449. }
  450. if count <= 0 {
  451. count = 0
  452. }
  453. if count > 0 {
  454. sqlstr += " limit " + strconv.Itoa(offset) + "," + strconv.Itoa(count)
  455. }
  456. rows, err := Engine.SQL(sqlstr, params...).QueryRows()
  457. if err != nil {
  458. return nil, err
  459. }
  460. defer rows.Close()
  461. columnsType, _ := rows.ColumnTypes()
  462. columnsLen := len(columnsType)
  463. columns, _ := rows.Columns()
  464. queryData := make([]map[string]interface{}, 0)
  465. values := make([]interface{}, columnsLen)
  466. valuePtrs := make([]interface{}, columnsLen)
  467. for rows.Next() {
  468. for a := 0; a < columnsLen; a++ {
  469. valuePtrs[a] = &values[a]
  470. }
  471. rows.Scan(valuePtrs...)
  472. entry := make(map[string]interface{})
  473. for i, col := range columns {
  474. entry[col] = getValue(values[i], columnsType[i].DatabaseTypeName())
  475. }
  476. queryData = append(queryData, entry)
  477. }
  478. data := queryData
  479. return data, nil
  480. //return Engine.SQL(sqlstr, params...).Query().List()
  481. }
  482. func QueryByList1(sqlstr string, offset, count int, params []interface{}) (interface{}, error) {
  483. if err := sqlCheckParam(sqlstr); err != nil {
  484. return 0, err
  485. }
  486. if(count==0){
  487. count = 1000
  488. }
  489. recordcount := int64(0)
  490. if count > 1 {
  491. tt, err := Engine.SQL("SELECT COUNT(*) as count FROM ("+sqlstr+") a", params...).Query().List()
  492. if err != nil {
  493. return nil, err
  494. }
  495. recordcount = tt[0]["count"].(int64)
  496. }
  497. queryData := make([]map[string]interface{},1)
  498. var err error
  499. if(count==0){
  500. queryData, err = Engine.SQL(sqlstr, params...).Query().List()
  501. } else {
  502. if count > 0 && offset > 0 {
  503. offset = offset * count
  504. }
  505. queryData, err = Engine.SQL(sqlstr, params...).Limit(count, offset).Query().List()
  506. }
  507. if err != nil {
  508. return nil, err
  509. }
  510. columnsname := make([]string, 0)
  511. listdata := make(map[string][]interface{})
  512. if len(queryData) > 0 {
  513. for i, _ := range queryData {
  514. if i == 0 {
  515. for i, _ := range queryData[0] {
  516. columnsname = append(columnsname, i)
  517. }
  518. }
  519. for _, v := range columnsname {
  520. listdata[v] = append(listdata[v], queryData[i][v])
  521. }
  522. }
  523. recordcount = int64(len(queryData))
  524. }
  525. data := make(map[string]interface{})
  526. data["lists"] = listdata
  527. //data["columnsname"] = columnsname
  528. data["total"] = recordcount
  529. data["code"] = 0
  530. data["message"] = "ok"
  531. if count > 0 {
  532. data["pages"] = math.Ceil(float64(recordcount) / float64(count))
  533. data["pageNum"] = math.Ceil(float64(offset+1) / float64(count))
  534. }
  535. data["pageSize"] = count
  536. return data, nil
  537. }
  538. func QueryByListList1(sqlstr string, offset, count int, params []interface{}) (interface{}, error) {
  539. if err := sqlCheckParam(sqlstr); err != nil {
  540. return 0, err
  541. }
  542. if(count==0){
  543. count = 1000
  544. }
  545. recordcount := int64(0)
  546. if count > 1 {
  547. tt, err := Engine.SQL("SELECT COUNT(*) as count FROM ("+sqlstr+") a", params...).Query().List()
  548. if err != nil {
  549. return nil, err
  550. }
  551. recordcount = tt[0]["count"].(int64)
  552. }
  553. queryData := make([]map[string]interface{},1)
  554. var err error
  555. if(count==0){
  556. queryData, err = Engine.SQL(sqlstr, params...).Query().List()
  557. } else {
  558. if count > 0 && offset > 0 {
  559. offset = offset * count
  560. }
  561. queryData, err = Engine.SQL(sqlstr, params...).Limit(count, offset).Query().List()
  562. }
  563. if err != nil {
  564. return nil, err
  565. }
  566. columnsname := make([]string, 0)
  567. listdata := make([]interface{}, 0)
  568. if len(queryData) > 0 {
  569. for i, _ := range queryData {
  570. if i == 0 {
  571. for i, _ := range queryData[0] {
  572. columnsname = append(columnsname, i)
  573. }
  574. }
  575. listdatarow := make([]interface{}, 0)
  576. for _, v := range columnsname {
  577. listdatarow = append(listdatarow, queryData[i][v])
  578. }
  579. listdata = append(listdata, listdatarow)
  580. }
  581. recordcount = int64(len(queryData))
  582. }
  583. data := make(map[string]interface{})
  584. data["list"] = listdata
  585. data["columnsname"] = columnsname
  586. data["total"] = recordcount
  587. data["code"] = 0
  588. data["message"] = "ok"
  589. if count > 0 {
  590. data["pages"] = math.Ceil(float64(recordcount) / float64(count))
  591. data["pageNum"] = math.Ceil(float64(offset+1) / float64(count))
  592. }
  593. data["pageSize"] = count
  594. return data, nil
  595. }
  596. func ExecQuery1(sqlstr string, params []interface{}) (interface{}, error) {
  597. if err := sqlCheckParam(sqlstr); err != nil {
  598. return 0, err
  599. }
  600. rows, err := Engine.SQL(sqlstr, params...).Execute()
  601. if err != nil {
  602. return nil, err
  603. }
  604. data := make(map[string]interface{})
  605. LastInsertId, _ := rows.LastInsertId()
  606. idPointer := (*int)(unsafe.Pointer(&LastInsertId))
  607. idd16 := *idPointer
  608. RowsAffected, _ := rows.RowsAffected()
  609. _idPointer := (*int)(unsafe.Pointer(&RowsAffected))
  610. _idd16 := *_idPointer
  611. data["result"] = rows
  612. data["LastInsertId"] = idd16
  613. data["RowsAffected"] = _idd16
  614. return data, nil
  615. }
  616. func ExecQueryT(sqlstr string, params []interface{}, tx *xorm.Session) (interface{}, error) {
  617. if err := sqlCheckParam(sqlstr); err != nil {
  618. return 0, err
  619. }
  620. rows, err := tx.SQL(sqlstr, params...).Execute()
  621. if err != nil {
  622. fmt.Println("exe", err)
  623. return nil, err
  624. }
  625. LastInsertId, _ := rows.LastInsertId()
  626. idPointer := (*int)(unsafe.Pointer(&LastInsertId))
  627. idd16 := *idPointer
  628. data := make(map[string]interface{})
  629. RowsAffected, _ := rows.RowsAffected()
  630. _idPointer := (*int)(unsafe.Pointer(&RowsAffected))
  631. _idd16 := *_idPointer
  632. data["LastInsertId"] = idd16
  633. data["RowsAffected"] = _idd16
  634. data["result"] = rows
  635. return data, nil
  636. }
  637. func ExecQuerys(sqlstr string, params []interface{}) (interface{}, error) {
  638. if err := sqlCheckParam(sqlstr); err != nil {
  639. return 0, err
  640. }
  641. /*for i, v := range params{
  642. _, err := dbs.Exec(sqlstr, v[i]...)
  643. if err != nil {
  644. return nil, err
  645. }
  646. }
  647. */
  648. data := make(map[string]interface{})
  649. data["result"] = nil
  650. return data, nil
  651. }
  652. func getValue(value interface{}, CT string) interface{} {
  653. var v interface{}
  654. val := value
  655. b, ok := val.([]byte)
  656. if ok {
  657. v1 := string(b)
  658. switch CT {
  659. case "TINYINT", "SMALLINT", "MEDIUMINT", "INT", "INTEGER", "BIGINT":
  660. {
  661. v, _ = strconv.ParseInt(v1, 10, 32)
  662. }
  663. case "FLOAT", "DOUBLE", "DECIMAL":
  664. {
  665. cindex := len(val.([]byte)) - strings.Index(v1, ".") - 1
  666. v2, _ := strconv.ParseFloat(v1, 32)
  667. v = toFixed(v2, cindex)
  668. }
  669. case "BOOL":
  670. {
  671. v, _ = strconv.ParseBool(v1)
  672. }
  673. default:
  674. {
  675. v = v1
  676. }
  677. }
  678. } else {
  679. v = val
  680. }
  681. return v
  682. }
  683. func sqlCheckParam(param string) error {
  684. /*if strings.Contains(param, "where") {
  685. return errors.New("can not have where")
  686. }
  687. if strings.Contains(param, "and") {
  688. return errors.New("can not have and")
  689. }
  690. if strings.Contains(param, "or") {
  691. return errors.New("can not have or")
  692. }
  693. if strings.Contains(param, "=") {
  694. return errors.New("can not have =")
  695. }*/
  696. if strings.Contains(param, ";") {
  697. return errors.New("can not have ;")
  698. }
  699. return nil
  700. }
  701. // SQLInsert 增加一条数据
  702. // tableName string: 操作的表名
  703. // data []byte: 需要更新的内容,用string转换后是json格式
  704. func SQLInsert(tableName string, data []byte) (int64, error) {
  705. if err := sqlCheckParam(tableName); err != nil {
  706. return 0, err
  707. }
  708. var f []map[string]interface{}
  709. err := json.Unmarshal(data, &f)
  710. if err != nil {
  711. return 0, err
  712. }
  713. var res sql.Result
  714. for _, data := range f {
  715. var sqlset string
  716. for k, v := range data {
  717. if sqlset != "" {
  718. sqlset += ","
  719. }
  720. switch vv := v.(type) {
  721. case string:
  722. sqlset += k + "='" + vv + "'"
  723. case int:
  724. sqlset += k + "=" + strconv.Itoa(vv)
  725. case float64:
  726. sqlset += k + "=" + strconv.FormatFloat(vv, 'f', -1, 64)
  727. default:
  728. fmt.Println(k, "is of a type I don't know how to handle")
  729. }
  730. }
  731. stmt, err := dbs.Prepare("INSERT " + tableName + " set " + sqlset)
  732. if err != nil {
  733. return 0, err
  734. }
  735. res, err = stmt.Exec()
  736. if err != nil {
  737. return 0, err
  738. }
  739. }
  740. return res.LastInsertId()
  741. }
  742. // SQLUpdate 更新一条数据
  743. // tableName string: 操作的表名
  744. // where string: 过滤条件,就是where后面跟着的部分
  745. // data []byte: 需要更新的内容,用string转换后是json格式
  746. func SQLUpdate(tableName, where string, data []byte) (int64, error) {
  747. if err := sqlCheckParam(tableName + where); err != nil {
  748. return 0, err
  749. }
  750. var f map[string]interface{}
  751. err := json.Unmarshal(data, &f)
  752. var sqlset string
  753. for k, v := range f {
  754. if sqlset != "" {
  755. sqlset += ","
  756. }
  757. switch vv := v.(type) {
  758. case string:
  759. sqlset += k + "='" + vv + "'"
  760. case int:
  761. sqlset += k + "=" + strconv.Itoa(vv)
  762. case float64:
  763. sqlset += k + "=" + strconv.FormatFloat(vv, 'f', -1, 64)
  764. default:
  765. fmt.Println(k, "is of a type I don't know how to handle")
  766. }
  767. }
  768. stmt, err := dbs.Prepare("UPDATE " + tableName + " set " + sqlset + " where " + where)
  769. if err != nil {
  770. return 0, err
  771. }
  772. res, err := stmt.Exec()
  773. if err != nil {
  774. return 0, err
  775. }
  776. return res.RowsAffected()
  777. }
  778. // SQLDelete 根据where条件删除数据
  779. // tableName string: 操作的表名
  780. // where string: 过滤条件,就是where后面跟着的部分
  781. func SQLDelete(tableName, where string) (int64, error) {
  782. if err := sqlCheckParam(tableName + where); err != nil {
  783. return 0, err
  784. }
  785. if dbs == nil {
  786. return 0, errors.New("gogo sql not init")
  787. }
  788. //删除数据
  789. stmt, err := dbs.Prepare("DELETE from " + tableName + " where " + where)
  790. if err != nil {
  791. return 0, err
  792. }
  793. res, err := stmt.Exec()
  794. if err != nil {
  795. return 0, err
  796. }
  797. return res.RowsAffected()
  798. }
  799. // sqlQueryTable 从数据库中查询到的数据,这里是以数组方式存储的,需要做二次转换
  800. func sqlQueryTable(feilds, tableName, where, order string, offset, count int) ([]*sql.ColumnType, int, [][]interface{}, int, error) {
  801. if dbs == nil {
  802. return nil, 0, nil, 0, errors.New("gogo sql not init")
  803. }
  804. if feilds == "" {
  805. feilds = "*"
  806. }
  807. sqlstr := "select " + feilds + " from " + tableName
  808. if where != "" {
  809. sqlstr += " where " + where
  810. }
  811. if order != "" {
  812. sqlstr += " order by "
  813. if strings.HasPrefix(order, "-") {
  814. sqlstr += string([]byte(order)[1:]) + " desc"
  815. } else {
  816. sqlstr += order + " asc"
  817. }
  818. }
  819. if offset < 0 {
  820. offset = 0
  821. }
  822. if count <= 0 {
  823. count = 20
  824. }
  825. sqlstr += " limit " + strconv.Itoa(offset) + "," + strconv.Itoa(count)
  826. rows, err := dbs.Query(sqlstr)
  827. if err != nil {
  828. return nil, 0, nil, 0, err
  829. }
  830. columnsType, _ := rows.ColumnTypes()
  831. columnsLen := len(columnsType)
  832. queryData := make([][]interface{}, count)
  833. queryCount := 0
  834. for rows.Next() {
  835. queryData[queryCount] = make([]interface{}, columnsLen)
  836. for a := 0; a < columnsLen; a++ {
  837. switch columnsType[a].DatabaseTypeName() {
  838. case "TINYINT":
  839. {
  840. queryData[queryCount][a] = new(int8)
  841. }
  842. case "SMALLINT":
  843. {
  844. queryData[queryCount][a] = new(int16)
  845. }
  846. case "MEDIUMINT":
  847. {
  848. queryData[queryCount][a] = new(int32)
  849. }
  850. case "INT":
  851. {
  852. queryData[queryCount][a] = new(sql.NullInt64)
  853. }
  854. case "INTEGER":
  855. {
  856. queryData[queryCount][a] = new(int32)
  857. }
  858. case "BIGINT":
  859. {
  860. queryData[queryCount][a] = new(int64)
  861. }
  862. case "FLOAT":
  863. {
  864. queryData[queryCount][a] = new(float32)
  865. }
  866. case "DOUBLE":
  867. {
  868. queryData[queryCount][a] = new(float64)
  869. }
  870. default:
  871. {
  872. queryData[queryCount][a] = new(sql.NullString)
  873. }
  874. }
  875. }
  876. if err = rows.Scan(queryData[queryCount]...); err != nil {
  877. return nil, 0, nil, 0, err
  878. }
  879. queryCount = queryCount + 1
  880. }
  881. return columnsType, columnsLen, queryData, queryCount, nil
  882. }
  883. // sqlQueryTable 从数据库中查询到的数据,这里是以数组方式存储的,需要做二次转换
  884. func sqlQuerySql(sqlstr, order string, offset, count int) ([]*sql.ColumnType, int, [][]interface{}, int, error) {
  885. if dbs == nil {
  886. return nil, 0, nil, 0, errors.New("gogo sql not init")
  887. }
  888. if sqlstr == "" {
  889. return nil, 0, nil, 0, errors.New("sql not set")
  890. }
  891. if order != "" {
  892. sqlstr += " order by "
  893. if strings.HasPrefix(order, "-") {
  894. sqlstr += string([]byte(order)[1:]) + " desc"
  895. } else {
  896. sqlstr += order + " asc"
  897. }
  898. }
  899. if offset < 0 {
  900. offset = 0
  901. }
  902. if count <= 0 {
  903. count = 20
  904. }
  905. sqlstr += " limit " + strconv.Itoa(offset) + "," + strconv.Itoa(count)
  906. rows, err := dbs.Query(sqlstr)
  907. if err != nil {
  908. return nil, 0, nil, 0, err
  909. }
  910. columnsType, _ := rows.ColumnTypes()
  911. columnsLen := len(columnsType)
  912. queryData := make([][]interface{}, count)
  913. queryCount := 0
  914. for rows.Next() {
  915. queryData[queryCount] = make([]interface{}, columnsLen)
  916. for a := 0; a < columnsLen; a++ {
  917. switch columnsType[a].DatabaseTypeName() {
  918. case "TINYINT":
  919. {
  920. queryData[queryCount][a] = new(int8)
  921. }
  922. case "SMALLINT":
  923. {
  924. queryData[queryCount][a] = new(int16)
  925. }
  926. case "MEDIUMINT":
  927. {
  928. queryData[queryCount][a] = new(int32)
  929. }
  930. case "INT":
  931. {
  932. queryData[queryCount][a] = new(sql.NullInt64)
  933. }
  934. case "INTEGER":
  935. {
  936. queryData[queryCount][a] = new(int32)
  937. }
  938. case "BIGINT":
  939. {
  940. queryData[queryCount][a] = new(int64)
  941. }
  942. case "FLOAT":
  943. {
  944. queryData[queryCount][a] = new(float32)
  945. }
  946. case "DOUBLE":
  947. {
  948. queryData[queryCount][a] = new(float64)
  949. }
  950. default:
  951. {
  952. queryData[queryCount][a] = new(sql.NullString)
  953. }
  954. }
  955. }
  956. if err = rows.Scan(queryData[queryCount]...); err != nil {
  957. return nil, 0, nil, 0, err
  958. }
  959. queryCount = queryCount + 1
  960. }
  961. return columnsType, columnsLen, queryData, queryCount, nil
  962. }
  963. // sqlGetValues 根据结构体中指向实际数据的指针获取出数据,并存储到另一张表中返回
  964. func sqlGetValues(pvs []interface{}, columnsType []*sql.ColumnType, columnsLen int) map[string]interface{} {
  965. result := make(map[string]interface{}, columnsLen)
  966. for a := 0; a < columnsLen; a++ {
  967. switch s := pvs[a].(type) {
  968. case *int8:
  969. result[columnsType[a].Name()] = *s
  970. case *int16:
  971. result[columnsType[a].Name()] = *s
  972. case *int32:
  973. result[columnsType[a].Name()] = *s
  974. case *int64:
  975. result[columnsType[a].Name()] = *s
  976. case *float32:
  977. result[columnsType[a].Name()] = *s
  978. case *float64:
  979. result[columnsType[a].Name()] = *s
  980. case *string:
  981. result[columnsType[a].Name()] = *s
  982. case *sql.NullInt64:
  983. result[columnsType[a].Name()] = *s
  984. case *sql.NullString:
  985. result[columnsType[a].Name()] = *s
  986. }
  987. }
  988. return result
  989. }
  990. // 这里返回的是原始数组的基础上加上了字段名标识
  991. func sqlQuery(columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) ([]map[string]interface{}, error) {
  992. jsondata := make([]map[string]interface{}, queryCount)
  993. for k1, v1 := range queryData {
  994. if k1 >= queryCount {
  995. break
  996. }
  997. jsondata[k1] = sqlGetValues(v1, columnsType, columnsLen)
  998. }
  999. return jsondata, nil
  1000. }
  1001. func sqlQueryByTinyIntMap(columnName string, columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) (map[int8]map[string]interface{}, error) {
  1002. jsondata := make(map[int8]map[string]interface{}, queryCount)
  1003. for k1, v1 := range queryData {
  1004. if k1 >= queryCount {
  1005. break
  1006. }
  1007. for a := 0; a < columnsLen; a++ {
  1008. if columnsType[a].Name() == columnName {
  1009. if value, ok := v1[a].(*int8); ok {
  1010. jsondata[*value] = sqlGetValues(v1, columnsType, columnsLen)
  1011. }
  1012. break
  1013. }
  1014. }
  1015. }
  1016. return jsondata, nil
  1017. }
  1018. func sqlQueryBySmallIntMap(columnName string, columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) (map[int16]map[string]interface{}, error) {
  1019. jsondata := make(map[int16]map[string]interface{}, queryCount)
  1020. for k1, v1 := range queryData {
  1021. if k1 >= queryCount {
  1022. break
  1023. }
  1024. for a := 0; a < columnsLen; a++ {
  1025. if columnsType[a].Name() == columnName {
  1026. if value, ok := v1[a].(*int16); ok {
  1027. jsondata[*value] = sqlGetValues(v1, columnsType, columnsLen)
  1028. }
  1029. break
  1030. }
  1031. }
  1032. }
  1033. return jsondata, nil
  1034. }
  1035. func sqlQueryByIntMap(columnName string, columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) (map[int32]map[string]interface{}, error) {
  1036. jsondata := make(map[int32]map[string]interface{}, queryCount)
  1037. for k1, v1 := range queryData {
  1038. if k1 >= queryCount {
  1039. break
  1040. }
  1041. for a := 0; a < columnsLen; a++ {
  1042. if columnsType[a].Name() == columnName {
  1043. if value, ok := v1[a].(*int32); ok {
  1044. jsondata[*value] = sqlGetValues(v1, columnsType, columnsLen)
  1045. }
  1046. break
  1047. }
  1048. }
  1049. }
  1050. return jsondata, nil
  1051. }
  1052. func sqlQueryByBigIntMap(columnName string, columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) (map[int64]map[string]interface{}, error) {
  1053. jsondata := make(map[int64]map[string]interface{}, queryCount)
  1054. for k1, v1 := range queryData {
  1055. if k1 >= queryCount {
  1056. break
  1057. }
  1058. for a := 0; a < columnsLen; a++ {
  1059. if columnsType[a].Name() == columnName {
  1060. if value, ok := v1[a].(*int64); ok {
  1061. jsondata[*value] = sqlGetValues(v1, columnsType, columnsLen)
  1062. }
  1063. break
  1064. }
  1065. }
  1066. }
  1067. return jsondata, nil
  1068. }
  1069. func sqlQueryByFloatIntMap(columnName string, columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) (map[float32]map[string]interface{}, error) {
  1070. jsondata := make(map[float32]map[string]interface{}, queryCount)
  1071. for k1, v1 := range queryData {
  1072. if k1 >= queryCount {
  1073. break
  1074. }
  1075. for a := 0; a < columnsLen; a++ {
  1076. if columnsType[a].Name() == columnName {
  1077. if value, ok := v1[a].(*float32); ok {
  1078. jsondata[*value] = sqlGetValues(v1, columnsType, columnsLen)
  1079. }
  1080. break
  1081. }
  1082. }
  1083. }
  1084. return jsondata, nil
  1085. }
  1086. func sqlQueryByDoubleMap(columnName string, columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) (map[float64]map[string]interface{}, error) {
  1087. jsondata := make(map[float64]map[string]interface{}, queryCount)
  1088. for k1, v1 := range queryData {
  1089. if k1 >= queryCount {
  1090. break
  1091. }
  1092. for a := 0; a < columnsLen; a++ {
  1093. if columnsType[a].Name() == columnName {
  1094. if value, ok := v1[a].(*float64); ok {
  1095. jsondata[*value] = sqlGetValues(v1, columnsType, columnsLen)
  1096. }
  1097. break
  1098. }
  1099. }
  1100. }
  1101. return jsondata, nil
  1102. }
  1103. func sqlQueryByStringMap(columnName string, columnsType []*sql.ColumnType, columnsLen int, queryData [][]interface{}, queryCount int) (map[string]map[string]interface{}, error) {
  1104. jsondata := make(map[string]map[string]interface{}, queryCount)
  1105. for k1, v1 := range queryData {
  1106. if k1 >= queryCount {
  1107. break
  1108. }
  1109. for a := 0; a < columnsLen; a++ {
  1110. if columnsType[a].Name() == columnName {
  1111. if value, ok := v1[a].(*string); ok {
  1112. jsondata[*value] = sqlGetValues(v1, columnsType, columnsLen)
  1113. }
  1114. break
  1115. }
  1116. }
  1117. }
  1118. return jsondata, nil
  1119. }
  1120. func sqlGetColumnType(columnsType []*sql.ColumnType, columnsLen int, valueName string) string {
  1121. for a := 0; a < columnsLen; a++ {
  1122. if columnsType[a].Name() == valueName {
  1123. return columnsType[a].DatabaseTypeName()
  1124. }
  1125. }
  1126. return ""
  1127. }
  1128. // SQLQueryByMap 将查询到的数据,按照指定字段的值做为索引构建map并返回
  1129. // columnName string: 作为索引的字段名称
  1130. // feilds string: 查询需要获取哪些字段的值,就是select后面跟着的部分,一般用"*"
  1131. // tableName string: 查询的表名
  1132. // where string: 过滤条件,就是where后面跟着的部分
  1133. // order string: 排序条件,就是order by后面跟着的部分。默认是ASC排序,除非"-"开头则DESC排序
  1134. // offset string: limit后面逗号相隔的两个数值,前者就是offset,后者就是count
  1135. // count string: limit后面逗号相隔的两个数值,前者就是offset,后者就是count
  1136. func SQLQueryByMap(columnName, feilds, tableName, where, order string, offset, count int) (interface{}, error) {
  1137. if err := sqlCheckParam(columnName + feilds + tableName + where + order); err != nil {
  1138. return 0, err
  1139. }
  1140. columnsType, columnsLen, queryData, queryCount, err := sqlQueryTable(feilds, tableName, where, order, offset, count)
  1141. if err != nil {
  1142. return nil, err
  1143. }
  1144. if queryCount == 0 {
  1145. return "", errors.New("0")
  1146. }
  1147. if columnName == "" {
  1148. return sqlQuery(columnsType, columnsLen, queryData, queryCount)
  1149. }
  1150. switch sqlGetColumnType(columnsType, columnsLen, columnName) {
  1151. case "TINYINT":
  1152. return sqlQueryByTinyIntMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1153. case "SMALLINT":
  1154. return sqlQueryBySmallIntMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1155. case "MEDIUMINT":
  1156. return sqlQueryByIntMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1157. case "INT":
  1158. return sqlQueryByIntMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1159. case "INTEGER":
  1160. return sqlQueryByIntMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1161. case "BIGINT":
  1162. return sqlQueryByBigIntMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1163. case "FLOAT":
  1164. return sqlQueryByFloatIntMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1165. case "DOUBLE":
  1166. return sqlQueryByDoubleMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1167. }
  1168. return sqlQueryByStringMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1169. }
  1170. func SQLQuerySqlByMap(sqlstr, order string, offset, count int) (interface{}, error) {
  1171. if err := sqlCheckParam(sqlstr + order); err != nil {
  1172. return 0, err
  1173. }
  1174. columnsType, columnsLen, queryData, queryCount, err := sqlQuerySql(sqlstr, order, offset, count)
  1175. if err != nil {
  1176. return nil, err
  1177. }
  1178. if queryCount == 0 {
  1179. return "", errors.New("0")
  1180. }
  1181. columnName:=""
  1182. if columnName == "" {
  1183. return sqlQuery(columnsType, columnsLen, queryData, queryCount)
  1184. }
  1185. switch sqlGetColumnType(columnsType, columnsLen, columnName) {
  1186. case "TINYINT":
  1187. return sqlQueryByTinyIntMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1188. case "SMALLINT":
  1189. return sqlQueryBySmallIntMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1190. case "MEDIUMINT":
  1191. return sqlQueryByIntMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1192. case "INT":
  1193. return sqlQueryByIntMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1194. case "INTEGER":
  1195. return sqlQueryByIntMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1196. case "BIGINT":
  1197. return sqlQueryByBigIntMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1198. case "FLOAT":
  1199. return sqlQueryByFloatIntMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1200. case "DOUBLE":
  1201. return sqlQueryByDoubleMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1202. }
  1203. return sqlQueryByStringMap(columnName, columnsType, columnsLen, queryData, queryCount)
  1204. }
  1205. func round(num float64) int {
  1206. return int(num + math.Copysign(0.5, num))
  1207. }
  1208. func toFixed(num float64, precision int) float64 {
  1209. output := math.Pow(10, float64(precision))
  1210. return float64(round(num * output)) / output
  1211. }
  1212. // QueryByMap 将查询到的数据,按照指定字段的值做为索引构建map并返回
  1213. // columnName string: 作为索引的字段名称
  1214. // sql string: 查询需要获取哪些字段的值,就是select后面跟着的部分,一般用"*"
  1215. // where string: 过滤条件,就是where后面跟着的部分
  1216. // order string: 排序条件,就是order by后面跟着的部分。默认是ASC排序,除非"-"开头则DESC排序
  1217. // offset string: limit后面逗号相隔的两个数值,前者就是offset,后者就是count
  1218. // count string: limit后面逗号相隔的两个数值,前者就是offset,后者就是count
  1219. func QueryByMap(sqlstr string, offset, count int, params []interface{}) (interface{}, error) {
  1220. if err := sqlCheckParam(sqlstr); err != nil {
  1221. return 0, err
  1222. }
  1223. if offset <= 0 {
  1224. offset = 1
  1225. }
  1226. if count <= 0 {
  1227. count = 0
  1228. }
  1229. //if count > 0 {
  1230. // sqlstr += " limit " + strconv.Itoa(count*(offset-1)) + "," + strconv.Itoa(count)
  1231. //}
  1232. rows, err := dbs.Query(sqlstr, params...)
  1233. if err != nil {
  1234. return nil, err
  1235. }
  1236. defer rows.Close()
  1237. columnsType, _ := rows.ColumnTypes()
  1238. columnsLen := len(columnsType)
  1239. columns, _ := rows.Columns()
  1240. queryData := make([]map[string]interface{}, 0)
  1241. values := make([]interface{}, columnsLen)
  1242. valuePtrs := make([]interface{}, columnsLen)
  1243. curoffset := 0
  1244. beginoffset := count*(offset-1)
  1245. for rows.Next() {
  1246. if (curoffset>=beginoffset && curoffset-beginoffset< count) || count ==0 {
  1247. for a := 0; a < columnsLen; a++ {
  1248. valuePtrs[a] = &values[a]
  1249. }
  1250. rows.Scan(valuePtrs...)
  1251. entry := make(map[string]interface{})
  1252. for i, col := range columns {
  1253. var v interface{}
  1254. val := values[i]
  1255. b, ok := val.([]byte)
  1256. if ok {
  1257. v1 := string(b)
  1258. switch columnsType[i].DatabaseTypeName() {
  1259. case "TINYINT", "SMALLINT", "MEDIUMINT", "INT", "INTEGER", "BIGINT":
  1260. {
  1261. v,_ = strconv.ParseInt(v1, 10, 32)
  1262. }
  1263. case "FLOAT", "DOUBLE", "DECIMAL":
  1264. {
  1265. cindex := len(val.([]byte))-strings.Index(v1, ".")-1
  1266. v2 , _ := strconv.ParseFloat(v1, 32)
  1267. v = toFixed(v2 ,cindex)
  1268. }
  1269. case "BOOL":
  1270. {
  1271. v, _ = strconv.ParseBool(v1)
  1272. }
  1273. default:
  1274. {
  1275. v = v1
  1276. }
  1277. }
  1278. } else {
  1279. v = val
  1280. }
  1281. entry[col] = v
  1282. }
  1283. queryData = append(queryData, entry)
  1284. }
  1285. curoffset++
  1286. }
  1287. //results, _ := engine.Sql(sqlstr).Query().List()
  1288. data := make(map[string]interface{})
  1289. data["list"] = queryData
  1290. data["total"] = curoffset
  1291. data["pages"] = math.Ceil(float64(curoffset)/float64(count))
  1292. data["pageNum"] = math.Ceil(float64(offset)/float64(count))
  1293. data["pageSize"] = count
  1294. data["code"] = 0
  1295. data["message"] = "ok"
  1296. return data,nil
  1297. }
  1298. // QueryByMap 将查询到的数据,按照指定字段的值做为索引构建map并返回
  1299. // columnName string: 作为索引的字段名称
  1300. // sql string: 查询需要获取哪些字段的值,就是select后面跟着的部分,一般用"*"
  1301. // where string: 过滤条件,就是where后面跟着的部分
  1302. // order string: 排序条件,就是order by后面跟着的部分。默认是ASC排序,除非"-"开头则DESC排序
  1303. // offset string: limit后面逗号相隔的两个数值,前者就是offset,后者就是count
  1304. // count string: limit后面逗号相隔的两个数值,前者就是offset,后者就是count
  1305. func QueryByMapEQ(sqlstr string, offset, count int, returntype string,params []interface{}) (interface{}, error) {
  1306. if err := sqlCheckParam(sqlstr); err != nil {
  1307. return 0, err
  1308. }
  1309. if offset <= 0 {
  1310. offset = 1
  1311. }
  1312. if count <= 0 {
  1313. count = 0
  1314. }
  1315. //if count > 0 {
  1316. // sqlstr += " limit " + strconv.Itoa(count*(offset-1)) + "," + strconv.Itoa(count)
  1317. //}
  1318. rows, err := dbs.Query(sqlstr, params...)
  1319. if err != nil {
  1320. return nil, err
  1321. }
  1322. defer rows.Close()
  1323. columnsType, _ := rows.ColumnTypes()
  1324. columnsLen := len(columnsType)
  1325. columns, _ := rows.Columns()
  1326. queryData := make([]map[string]interface{}, 0)
  1327. values := make([]interface{}, columnsLen)
  1328. valuePtrs := make([]interface{}, columnsLen)
  1329. curoffset := 0
  1330. beginoffset := count*(offset-1)
  1331. if beginoffset < 0 {
  1332. beginoffset=0
  1333. }
  1334. for rows.Next() {
  1335. if (curoffset>=beginoffset && curoffset-beginoffset< count) || count ==0 {
  1336. for a := 0; a < columnsLen; a++ {
  1337. valuePtrs[a] = &values[a]
  1338. }
  1339. rows.Scan(valuePtrs...)
  1340. entry := make(map[string]interface{})
  1341. for i, col := range columns {
  1342. var v interface{}
  1343. val := values[i]
  1344. b, ok := val.([]byte)
  1345. if ok {
  1346. v1 := string(b)
  1347. switch columnsType[i].DatabaseTypeName() {
  1348. case "TINYINT", "SMALLINT", "MEDIUMINT", "INT", "INTEGER", "BIGINT":
  1349. {
  1350. v,_ = strconv.ParseInt(v1, 10, 32)
  1351. }
  1352. case "FLOAT", "DOUBLE", "DECIMAL":
  1353. {
  1354. cindex := len(val.([]byte))-strings.Index(v1, ".")-1
  1355. v2 , _ := strconv.ParseFloat(v1, 32)
  1356. v = toFixed(v2 ,cindex)
  1357. }
  1358. case "BOOL":
  1359. {
  1360. v, _ = strconv.ParseBool(v1)
  1361. }
  1362. default:
  1363. {
  1364. v = v1
  1365. }
  1366. }
  1367. } else {
  1368. v = val
  1369. }
  1370. entry[col] = v
  1371. }
  1372. queryData = append(queryData, entry)
  1373. }
  1374. curoffset++
  1375. }
  1376. //results, _ := engine.Sql(sqlstr).Query().List()
  1377. data := make(map[string]interface{})
  1378. data1 := make(map[string]interface{})
  1379. if returntype == "0" {
  1380. data["data"] = queryData
  1381. data["count"] = len(queryData)
  1382. }else if returntype == "2" {
  1383. data["data"] = queryData[0]
  1384. data["count"] = len(queryData)
  1385. }else if returntype == "3" {
  1386. data["data"] = queryData[0]["fsion"]
  1387. data["count"] = len(queryData)
  1388. }else {
  1389. data1["list"] = queryData
  1390. data1["total"] = curoffset
  1391. data1["pages"] = math.Ceil(float64(curoffset)/float64(count))
  1392. data1["pageNum"] = math.Ceil(float64(offset)/float64(count))
  1393. data1["pageSize"] = count
  1394. data1["count"] = len(queryData)
  1395. data["data"]= data1
  1396. }
  1397. return data,nil
  1398. }
  1399. // QueryByMap 将查询到的数据,按照指定字段的值做为索引构建map并返回
  1400. // columnName string: 作为索引的字段名称
  1401. // sql string: 查询需要获取哪些字段的值,就是select后面跟着的部分,一般用"*"
  1402. // where string: 过滤条件,就是where后面跟着的部分
  1403. // order string: 排序条件,就是order by后面跟着的部分。默认是ASC排序,除非"-"开头则DESC排序
  1404. // offset string: limit后面逗号相隔的两个数值,前者就是offset,后者就是count
  1405. // count string: limit后面逗号相隔的两个数值,前者就是offset,后者就是count
  1406. func QueryByMapMenu(sqlstr string, offset, count int, params []interface{}) ([]map[string]interface{}, error) {
  1407. if err := sqlCheckParam(sqlstr); err != nil {
  1408. return nil, err
  1409. }
  1410. if offset < 0 {
  1411. offset = 0
  1412. }
  1413. if count <= 0 {
  1414. count = 0
  1415. }
  1416. if count > 0 {
  1417. sqlstr += " limit " + strconv.Itoa(offset) + "," + strconv.Itoa(count)
  1418. }
  1419. rows, err := dbs.Query(sqlstr, params...)
  1420. if err != nil {
  1421. return nil, err
  1422. }
  1423. defer rows.Close()
  1424. columnsType, _ := rows.ColumnTypes()
  1425. columnsLen := len(columnsType)
  1426. columns, _ := rows.Columns()
  1427. queryData := make([]map[string]interface{}, 0)
  1428. values := make([]interface{}, columnsLen)
  1429. valuePtrs := make([]interface{}, columnsLen)
  1430. for rows.Next() {
  1431. for a := 0; a < columnsLen; a++ {
  1432. valuePtrs[a] = &values[a]
  1433. }
  1434. rows.Scan(valuePtrs...)
  1435. entry := make(map[string]interface{})
  1436. for i, col := range columns {
  1437. var v interface{}
  1438. val := values[i]
  1439. b, ok := val.([]byte)
  1440. if ok {
  1441. v1 := string(b)
  1442. switch columnsType[i].DatabaseTypeName() {
  1443. case "TINYINT", "SMALLINT", "MEDIUMINT", "INT", "INTEGER", "BIGINT":
  1444. {
  1445. v,_ = strconv.ParseInt(v1, 10, 32)
  1446. }
  1447. case "FLOAT", "DOUBLE", "DECIMAL":
  1448. {
  1449. cindex := len(val.([]byte))-strings.Index(v1, ".")-1
  1450. v2 , _ := strconv.ParseFloat(v1, 32)
  1451. v = toFixed(v2 ,cindex)
  1452. }
  1453. case "BOOL":
  1454. {
  1455. v, _ = strconv.ParseBool(v1)
  1456. }
  1457. default:
  1458. {
  1459. v = v1
  1460. }
  1461. }
  1462. } else {
  1463. v = val
  1464. }
  1465. entry[col] = v
  1466. }
  1467. queryData = append(queryData, entry)
  1468. }
  1469. //results, _ := engine.Sql(sqlstr).Query().List()
  1470. data := queryData
  1471. return data,nil
  1472. }
  1473. func QueryByList(sqlstr string, offset, count int, params []interface{}) (interface{}, error) {
  1474. if err := sqlCheckParam(sqlstr); err != nil {
  1475. return 0, err
  1476. }
  1477. if offset <= 0 {
  1478. offset = 1
  1479. }
  1480. if count <= 0 {
  1481. count = 0
  1482. }
  1483. // if count > 0 {
  1484. // sqlstr += " limit " + strconv.Itoa(count*(offset-1)) + "," + strconv.Itoa(count)
  1485. // }
  1486. rows, err := dbs.Query(sqlstr, params...)
  1487. if err != nil {
  1488. return nil, err
  1489. }
  1490. defer rows.Close()
  1491. columnsType, _ := rows.ColumnTypes()
  1492. columnsLen := len(columnsType)
  1493. columns, _ := rows.Columns()
  1494. queryData := make(map[string][]interface{})
  1495. values := make([]interface{}, columnsLen)
  1496. valuePtrs := make([]interface{}, columnsLen)
  1497. curoffset := 0
  1498. beginoffset := count*(offset-1)
  1499. for rows.Next() {
  1500. if (curoffset>=beginoffset && curoffset-beginoffset< count) || count ==0 {
  1501. for a := 0; a < columnsLen; a++ {
  1502. valuePtrs[a] = &values[a]
  1503. }
  1504. rows.Scan(valuePtrs...)
  1505. for i, col := range columns {
  1506. var v interface{}
  1507. val := values[i]
  1508. b, ok := val.([]byte)
  1509. if ok {
  1510. v1 := string(b)
  1511. switch columnsType[i].DatabaseTypeName() {
  1512. case "TINYINT", "SMALLINT", "MEDIUMINT", "INT", "INTEGER", "BIGINT":
  1513. {
  1514. v,_ = strconv.ParseInt(v1, 10, 32)
  1515. }
  1516. case "FLOAT", "DOUBLE", "DECIMAL":
  1517. {
  1518. cindex := len(val.([]byte))-strings.Index(v1, ".")-1
  1519. v2 , _ := strconv.ParseFloat(v1, 32)
  1520. v = toFixed(v2 ,cindex)
  1521. }
  1522. case "BOOL":
  1523. {
  1524. v, _ = strconv.ParseBool(v1)
  1525. }
  1526. default:
  1527. {
  1528. v = v1
  1529. }
  1530. }
  1531. } else {
  1532. v = val
  1533. }
  1534. queryData[col] = append(queryData[col], v)
  1535. }
  1536. }
  1537. curoffset++
  1538. }
  1539. data := make(map[string]interface{})
  1540. data["lists"] = queryData
  1541. data["total"] = curoffset
  1542. data["offset"] = offset
  1543. data["count"] = count
  1544. data["code"] = 0
  1545. data["message"] = "ok"
  1546. return data,nil
  1547. }
  1548. // QueryByXlsx 将查询到的数据,按照指定字段的值做为索引构建map并返回
  1549. // sql string: 查询需要获取哪些字段的值,就是select后面跟着的部分,一般用"*"
  1550. // params string: 过滤条件,就是where后面跟着的部分
  1551. func QueryByXlsx(sqlstr string, params []interface{}) (*xlsx.File, error) {
  1552. if err := sqlCheckParam(sqlstr); err != nil {
  1553. return nil, err
  1554. }
  1555. rows, err := dbs.Query(sqlstr, params...)
  1556. if err != nil {
  1557. return nil, err
  1558. }
  1559. defer rows.Close()
  1560. columnsType, _ := rows.ColumnTypes()
  1561. columnsLen := len(columnsType)
  1562. columns, _ := rows.Columns()
  1563. values := make([]interface{}, columnsLen)
  1564. valuePtrs := make([]interface{}, columnsLen)
  1565. var sheet *xlsx.Sheet
  1566. var row *xlsx.Row
  1567. var cell *xlsx.Cell
  1568. file := xlsx.NewFile()
  1569. sheet, err = file.AddSheet("Sheet1")
  1570. if err != nil {
  1571. fmt.Printf(err.Error())
  1572. }
  1573. row = sheet.AddRow()
  1574. for _, columnname := range columns{
  1575. cell = row.AddCell()
  1576. cell.Value = columnname
  1577. }
  1578. for rows.Next() {
  1579. for a := 0; a < columnsLen; a++ {
  1580. valuePtrs[a] = &values[a]
  1581. }
  1582. rows.Scan(valuePtrs...)
  1583. row = sheet.AddRow()
  1584. for i, _ := range columns {
  1585. var v interface{}
  1586. val := values[i]
  1587. b, ok := val.([]byte)
  1588. cell = row.AddCell()
  1589. if ok {
  1590. v1 := string(b)
  1591. switch columnsType[i].DatabaseTypeName() {
  1592. case "TINYINT", "SMALLINT", "MEDIUMINT", "INT", "INTEGER", "BIGINT":
  1593. {
  1594. v,_ = strconv.ParseInt(v1, 10, 32)
  1595. cell.Value = fmt.Sprintf("%v", v)
  1596. }
  1597. case "FLOAT", "DOUBLE", "DECIMAL":
  1598. {
  1599. cindex := len(val.([]byte))-strings.Index(v1, ".")-1
  1600. v2 , _ := strconv.ParseFloat(v1, 32)
  1601. v = toFixed(v2 ,cindex)
  1602. cell.Value = fmt.Sprintf("%v", v)
  1603. }
  1604. case "BOOL":
  1605. {
  1606. v, _ = strconv.ParseBool(v1)
  1607. cell.Value = fmt.Sprintf("%v", v)
  1608. }
  1609. default:
  1610. {
  1611. v = v1
  1612. cell.Value = fmt.Sprintf("%v", v)
  1613. }
  1614. }
  1615. } else {
  1616. v = val
  1617. cell.Value = fmt.Sprintf("%v", v)
  1618. }
  1619. if (cell.Value == "<nil>") {
  1620. cell.Value=""
  1621. }
  1622. }
  1623. }
  1624. return file,nil
  1625. }
  1626. func ExecQuery(sqlstr string, params []interface{}) (interface{}, error) {
  1627. if err := sqlCheckParam(sqlstr); err != nil {
  1628. return 0, err
  1629. }
  1630. rows, err := dbs.Exec(sqlstr, params...)
  1631. if err != nil {
  1632. return nil, err
  1633. }
  1634. data := make(map[string]interface{})
  1635. data["result"] = rows
  1636. return data,nil
  1637. }
  1638. func ExecQuerys1(sqlstr string, params []interface{}) (interface{}, error) {
  1639. if err := sqlCheckParam(sqlstr); err != nil {
  1640. return 0, err
  1641. }
  1642. /*for i, v := range params{
  1643. _, err := dbs.Exec(sqlstr, v[i]...)
  1644. if err != nil {
  1645. return nil, err
  1646. }
  1647. }
  1648. */
  1649. data := make(map[string]interface{})
  1650. data["result"] = nil
  1651. return data,nil
  1652. }
  1653. func QueryByListList(sqlstr string, offset, count int, params []interface{}) (interface{}, error) {
  1654. if err := sqlCheckParam(sqlstr); err != nil {
  1655. return 0, err
  1656. }
  1657. if offset <= 0 {
  1658. offset = 1
  1659. }
  1660. if count <= 0 {
  1661. count = 0
  1662. }
  1663. //if count > 0 {
  1664. // sqlstr += " limit " + strconv.Itoa(count*(offset-1)) + "," + strconv.Itoa(count)
  1665. //}
  1666. rows, err := dbs.Query(sqlstr, params...)
  1667. if err != nil {
  1668. return nil, err
  1669. }
  1670. defer rows.Close()
  1671. columnsType, _ := rows.ColumnTypes()
  1672. columnsLen := len(columnsType)
  1673. columns, _ := rows.Columns()
  1674. columnsname := make([]interface{}, columnsLen)
  1675. for i, col := range columns {
  1676. columnsname[i] = col
  1677. }
  1678. queryData := make([]interface{}, 0)
  1679. values := make([]interface{}, columnsLen)
  1680. valuePtrs := make([]interface{}, columnsLen)
  1681. curoffset := 0
  1682. beginoffset := count * (offset - 1)
  1683. for rows.Next() {
  1684. if (curoffset >= beginoffset && curoffset-beginoffset < count) || count == 0 {
  1685. for a := 0; a < columnsLen; a++ {
  1686. valuePtrs[a] = &values[a]
  1687. }
  1688. rows.Scan(valuePtrs...)
  1689. entry := make([]interface{}, 0)
  1690. for i, _ := range columns {
  1691. var v interface{}
  1692. val := values[i]
  1693. b, ok := val.([]byte)
  1694. if ok {
  1695. v1 := string(b)
  1696. switch columnsType[i].DatabaseTypeName() {
  1697. case "TINYINT", "SMALLINT", "MEDIUMINT", "INT", "INTEGER", "BIGINT":
  1698. {
  1699. v, _ = strconv.ParseInt(v1, 10, 32)
  1700. }
  1701. case "FLOAT", "DOUBLE", "DECIMAL":
  1702. {
  1703. cindex := len(val.([]byte)) - strings.Index(v1, ".") - 1
  1704. v2, _ := strconv.ParseFloat(v1, 32)
  1705. v = toFixed(v2, cindex)
  1706. }
  1707. case "BOOL":
  1708. {
  1709. v, _ = strconv.ParseBool(v1)
  1710. }
  1711. default:
  1712. {
  1713. v = v1
  1714. }
  1715. }
  1716. } else {
  1717. v = val
  1718. }
  1719. entry = append(entry, v)
  1720. }
  1721. queryData = append(queryData, entry)
  1722. }
  1723. curoffset++
  1724. }
  1725. data := make(map[string]interface{})
  1726. data["lists"] = queryData
  1727. data["columnsname"] = columnsname
  1728. data["total"] = curoffset
  1729. data["offset"] = offset
  1730. data["count"] = count
  1731. data["code"] = 0
  1732. data["message"] = "ok"
  1733. return data, nil
  1734. }