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

456 lines
8.5 KiB

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