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

474 lines
9.0 KiB

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