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

345 lines
6.6 KiB

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