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

463 lines
8.8 KiB

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