myql操作
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.

451 lines
8.5 KiB

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