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

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