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

498 lines
9.6 KiB

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