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

442 lines
8.4 KiB

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