数据库操作
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.

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