b0a8e2c10fac3cb77b75cf56cb70868211295144.svn-base 51 KB

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