数据库操作
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

401 lines
7.8 KiB

  1. package dbquery
  2. import (
  3. "database/sql"
  4. "errors"
  5. "strconv"
  6. "strings"
  7. "git.tetele.net/tgo/helper"
  8. )
  9. var stmt *sql.Stmt
  10. var err error
  11. type Query struct {
  12. dbname string
  13. table string
  14. alias string
  15. title string
  16. where []string
  17. where_or []string
  18. join [][]string //[["tablea as a","a.id=b.id","left"]]
  19. data []string
  20. value []interface{}
  21. orderby string
  22. page int
  23. page_size int
  24. stmt *sql.Stmt
  25. }
  26. func (this *Query) Db(dbname string) *Query {
  27. this.dbname = dbname
  28. return this
  29. }
  30. func (this *Query) Table(tablename string) *Query {
  31. this.table = tablename
  32. return this
  33. }
  34. func (this *Query) Alias(tablename string) *Query {
  35. this.alias = tablename
  36. return this
  37. }
  38. func (this *Query) Title(title string) *Query {
  39. this.title = title
  40. return this
  41. }
  42. func (this *Query) Page(page int) *Query {
  43. this.page = page
  44. return this
  45. }
  46. func (this *Query) PageSize(page_num int) *Query {
  47. this.page_size = page_num
  48. return this
  49. }
  50. func (this *Query) Orderby(orderby string) *Query {
  51. this.orderby = orderby
  52. return this
  53. }
  54. func (this *Query) Where(where string) *Query {
  55. this.where = append(this.where, where)
  56. return this
  57. }
  58. func (this *Query) Wheres(wheres []string) *Query {
  59. if len(wheres) > 0 {
  60. this.where = append(this.where, wheres...)
  61. }
  62. return this
  63. }
  64. func (this *Query) WhereOr(where string) *Query {
  65. this.where_or = append(this.where_or, where)
  66. return this
  67. }
  68. func (this *Query) Value(value interface{}) *Query {
  69. this.value = append(this.value, value)
  70. return this
  71. }
  72. func (this *Query) Values(values []interface{}) *Query {
  73. this.value = append(this.value, values...)
  74. return this
  75. }
  76. func (this *Query) Join(join []string) *Query {
  77. this.join = append(this.join, join)
  78. return this
  79. }
  80. func (this *Query) Data(data string) *Query {
  81. this.data = append(this.data, data)
  82. return this
  83. }
  84. func (this *Query) Datas(datas []string) *Query {
  85. this.data = append(this.data, datas...)
  86. return this
  87. }
  88. // func (this *Query) Insert(where string) *Query {
  89. // this.insert = append(this.insert, where)
  90. // return this
  91. // }
  92. // 拼查询sql
  93. func (this *Query) QueryStmt() error {
  94. if this.dbname == "" && this.table == "" {
  95. return errors.New("参数错误,没有数据表")
  96. }
  97. // if len(this.where)+len(this.where_or) < len(this.value) {
  98. // return errors.New("参数错误,条件值错误")
  99. // }
  100. table := getTableName(this.dbname, this.table)
  101. // var err error
  102. var sql, title string
  103. if this.title != "" {
  104. title = this.title
  105. } else {
  106. title = "*"
  107. }
  108. sql = helper.StringJoin("select ", title)
  109. if this.alias != "" {
  110. table = helper.StringJoin(table, " as ", this.alias)
  111. }
  112. sql = helper.StringJoin(sql, " from ", table)
  113. if len(this.join) > 0 {
  114. for _, joinitem := range this.join {
  115. if len(joinitem) < 2 {
  116. continue
  117. }
  118. if len(joinitem) == 3 {
  119. sql = helper.StringJoin(sql, " ", joinitem[2], " join ", getTableName(this.dbname, joinitem[0]), " on ", joinitem[1])
  120. } else { //默认左连接
  121. sql = helper.StringJoin(sql, " left join ", getTableName(this.dbname, joinitem[0]), " on ", joinitem[1])
  122. }
  123. }
  124. }
  125. if len(this.where) > 0 || len(this.where_or) > 0 {
  126. sql = helper.StringJoin(sql, " where ")
  127. }
  128. if len(this.where) > 0 {
  129. sql = helper.StringJoin(sql, " (", strings.Join(this.where, " and "), " ) ")
  130. }
  131. if len(this.where_or) > 0 {
  132. if len(this.where) > 0 {
  133. sql = helper.StringJoin(sql, " or ", strings.Join(this.where_or, " or "))
  134. } else {
  135. sql = helper.StringJoin(sql, strings.Join(this.where_or, " or "))
  136. }
  137. }
  138. if this.orderby != "" {
  139. sql = helper.StringJoin(sql, " order by ", this.orderby)
  140. }
  141. if this.page > 0 || this.page_size > 0 {
  142. if this.page < 1 {
  143. this.page = 1
  144. }
  145. if this.page_size < 1 {
  146. this.page_size = 10
  147. }
  148. from := strconv.Itoa((this.page - 1) * this.page_size)
  149. offset := strconv.Itoa(this.page_size)
  150. if from != "" && offset != "" {
  151. sql = helper.StringJoin(sql, " limit ", from, " , ", offset)
  152. }
  153. }
  154. // log.Println(sql)
  155. condition_len := 0 //所有条件数
  156. for _, ch2 := range sql {
  157. if string(ch2) == "?" {
  158. condition_len++
  159. }
  160. }
  161. if condition_len != len(this.value) {
  162. return errors.New("参数错误,条件值错误")
  163. }
  164. stmt, err = DB.Prepare(sql)
  165. if err != nil {
  166. return err
  167. }
  168. this.stmt = stmt
  169. return nil
  170. }
  171. // 拼更新sql
  172. func (this *Query) UpdateStmt() error {
  173. if this.dbname == "" && this.table == "" {
  174. return errors.New("参数错误,没有数据表")
  175. }
  176. if len(this.where) < 1 {
  177. return errors.New("参数错误,缺少条件")
  178. }
  179. dbName := getTableName(this.dbname, this.table)
  180. // var stmt *sql.Stmt
  181. // var err error
  182. var sql string
  183. sql = helper.StringJoin("update ", dbName, " set ", strings.Join(this.data, " , "))
  184. sql = helper.StringJoin(sql, " where ", strings.Join(this.where, " and "))
  185. condition_len := 0 //所有条件数
  186. for _, ch2 := range sql {
  187. if string(ch2) == "?" {
  188. condition_len++
  189. }
  190. }
  191. if condition_len != len(this.value) {
  192. return errors.New("参数错误,条件值错误")
  193. }
  194. stmt, err = DB.Prepare(sql)
  195. if err != nil {
  196. return err
  197. }
  198. this.stmt = stmt
  199. return nil
  200. }
  201. // 拼插入sql
  202. func (this *Query) CreateStmt() error {
  203. if this.dbname == "" && this.table == "" {
  204. return errors.New("参数错误,没有数据表")
  205. }
  206. dbName := getTableName(this.dbname, this.table)
  207. // var stmt *sql.Stmt
  208. // var err error
  209. var sql string
  210. sql = helper.StringJoin("insert into ", dbName, " set ", strings.Join(this.data, " , "))
  211. condition_len := 0 //所有条件数
  212. for _, ch2 := range sql {
  213. if string(ch2) == "?" {
  214. condition_len++
  215. }
  216. }
  217. if condition_len != len(this.value) {
  218. return errors.New("参数错误,条件值错误")
  219. }
  220. stmt, err = DB.Prepare(sql)
  221. if err != nil {
  222. return err
  223. }
  224. this.stmt = stmt
  225. return nil
  226. }
  227. // 拼删除sql
  228. func (this *Query) DeleteStmt() error {
  229. if this.dbname == "" && this.table == "" {
  230. return errors.New("参数错误,没有数据表")
  231. }
  232. if len(this.where) < 1 {
  233. return errors.New("参数错误,缺少条件")
  234. }
  235. // if len(this.where) != len(this.value) {
  236. // return errors.New("参数错误,条件值错误")
  237. // }
  238. dbName := getTableName(this.dbname, this.table)
  239. // var stmt *sql.Stmt
  240. // var err error
  241. var sql string
  242. sql = helper.StringJoin("delete from ", dbName, " where ", strings.Join(this.where, " and "))
  243. if this.page_size > 0 {
  244. sql = helper.StringJoin(sql, " limit ", strconv.Itoa(this.page_size))
  245. }
  246. condition_len := 0 //所有条件数
  247. for _, ch2 := range sql {
  248. if string(ch2) == "?" {
  249. condition_len++
  250. }
  251. }
  252. if condition_len != len(this.value) {
  253. return errors.New("参数错误,条件值错误")
  254. }
  255. stmt, err = DB.Prepare(sql)
  256. if err != nil {
  257. return err
  258. }
  259. this.stmt = stmt
  260. return nil
  261. }
  262. /**
  263. * 执行查询列表
  264. * return list error
  265. */
  266. func (this *Query) Select() ([]map[string]string, error) {
  267. err := this.QueryStmt()
  268. if err != nil {
  269. return []map[string]string{}, err
  270. }
  271. if this.stmt == nil {
  272. return []map[string]string{}, errors.New("缺少必要参数")
  273. }
  274. return StmtForQueryList(this.stmt, this.value)
  275. }
  276. /**
  277. * 执行查询一条数据
  278. * return row error
  279. */
  280. func (this *Query) Find() (map[string]string, error) {
  281. err := this.QueryStmt()
  282. if err != nil {
  283. return map[string]string{}, err
  284. }
  285. if this.stmt == nil {
  286. return nil, errors.New("缺少必要参数")
  287. }
  288. return StmtForQueryRow(this.stmt, this.value)
  289. }
  290. /**
  291. * 执行更新
  292. * return is_updated error
  293. */
  294. func (this *Query) Update() (int64, error) {
  295. err := this.UpdateStmt()
  296. if err != nil {
  297. return 0, err
  298. }
  299. return StmtForUpdateExec(this.stmt, this.value)
  300. }
  301. /**
  302. * 执行删除
  303. * return is_delete error
  304. */
  305. func (this *Query) Delete() (int64, error) {
  306. err := this.DeleteStmt()
  307. if err != nil {
  308. return 0, err
  309. }
  310. return StmtForUpdateExec(this.stmt, this.value)
  311. }
  312. /**
  313. * 执行写入
  314. * return is_insert error
  315. */
  316. func (this *Query) Create() (int64, error) {
  317. err := this.CreateStmt()
  318. if err != nil {
  319. return 0, err
  320. }
  321. return StmtForInsertExec(this.stmt, this.value)
  322. }