postgres.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package db
  2. import (
  3. "fmt"
  4. "sync"
  5. "github.com/jinzhu/gorm"
  6. )
  7. type PostgresMaster struct {
  8. MyTable
  9. TableCatalog string
  10. TableSchema string
  11. TableType string
  12. SelfReferencingColumnName string
  13. ReferenceGeneration string
  14. UserDefinedTypeName string
  15. IsInsertableInto string
  16. IsTyped string
  17. CommitAction string
  18. }
  19. type PostgresTableInfo struct {
  20. MyTable
  21. TableCatalog string
  22. TableSchema string
  23. ColumnName string
  24. OrdinalPosition int
  25. ColumnDefault string
  26. IsNullable string
  27. DataType interface{}
  28. }
  29. func (PostgresTableInfo) TableName() string {
  30. return "information_schema.columns"
  31. }
  32. func (PostgresMaster) TableName() string {
  33. return "information_schema.tables"
  34. }
  35. type MyTable struct {
  36. TableName string
  37. }
  38. type Postgres struct {
  39. orm *gorm.DB
  40. mutex *sync.Mutex
  41. }
  42. func (Postgres) Hello() string {
  43. return "Postgres"
  44. }
  45. func (p Postgres) Orm() *gorm.DB {
  46. return p.orm
  47. }
  48. func (p Postgres) Mutex() *sync.Mutex {
  49. return p.mutex
  50. }
  51. func (db Postgres) Insert(obj interface{}) error {
  52. tx := db.orm.Begin()
  53. err := tx.Create(obj).Error
  54. if err != nil {
  55. tx.Rollback()
  56. return err
  57. }
  58. err = tx.Commit().Error
  59. if err != nil {
  60. tx.Rollback()
  61. return err
  62. }
  63. return nil
  64. }
  65. func (db Postgres) Delete(obj interface{}, para map[string]interface{}) error {
  66. tx := db.orm.Begin()
  67. err := tx.Where(para).Delete(obj).Error
  68. if err != nil {
  69. tx.Rollback()
  70. return err
  71. }
  72. err = tx.Commit().Error
  73. if err != nil {
  74. tx.Rollback()
  75. return err
  76. }
  77. return nil
  78. }
  79. func (db Postgres) Get(objs interface{}, where map[string]interface{}, order string, limit uint64, offset uint64, deleted bool) error {
  80. con := db.orm
  81. if deleted {
  82. con = con.Unscoped()
  83. }
  84. keys, values := whereExactKeyValues(where)
  85. con = con.Where(keys, values)
  86. if deleted {
  87. con = con.Where("deleted_at is not null")
  88. }
  89. if order != "" {
  90. con = con.Order(order)
  91. }
  92. if limit != 0 {
  93. con = con.Limit(limit)
  94. }
  95. if offset != 0 {
  96. con = con.Offset(offset)
  97. }
  98. err := con.Find(objs).Error
  99. if err != nil {
  100. return err
  101. }
  102. return nil
  103. }
  104. func (db Postgres) Update(obj interface{}, para map[string]interface{}) error {
  105. tx := db.orm.Begin()
  106. err := tx.Model(obj).Updates(para).Error
  107. if err != nil {
  108. tx.Rollback()
  109. return err
  110. }
  111. err = tx.Commit().Error
  112. if err != nil {
  113. tx.Rollback()
  114. return err
  115. }
  116. return nil
  117. }
  118. func (db Postgres) GetMeta(name interface{}) (interface{}, error) {
  119. tableName := fmt.Sprint(name)
  120. var tabInfo []PostgresTableInfo
  121. err := db.Get(&tabInfo, map[string]interface{}{"table_name": tableName}, "", 0, 0, false)
  122. if err != nil {
  123. return tabInfo, err
  124. }
  125. return tabInfo, nil
  126. }
  127. func (db Postgres) GetTables() (interface{}, error) {
  128. var tables []PostgresMaster
  129. if err := db.Get(&tables, map[string]interface{}{"table_type": "BASE TABLE"}, "", 0, 0, false); err != nil {
  130. return nil, err
  131. }
  132. return tables, nil
  133. }
  134. func (db Postgres) Select(obj interface{}, para string, filter []Filter) ([]interface{}, error) {
  135. var tables []interface{}
  136. con := db.orm
  137. con = con.Table(fmt.Sprint(obj))
  138. con = con.Select(para)
  139. for _, f := range filter {
  140. con = con.Where(f.Field+" "+f.Compare+" ?", f.Value)
  141. }
  142. row, err := con.Rows()
  143. if err != nil {
  144. return tables, err
  145. }
  146. columns, err := row.Columns()
  147. if err != nil {
  148. return tables, err
  149. }
  150. for row.Next() {
  151. values := make([]interface{}, len(columns))
  152. record := make([]interface{}, len(columns))
  153. for i, _ := range columns {
  154. values[i] = &record[i]
  155. }
  156. err = row.Scan(values...)
  157. if err != nil {
  158. return tables, err
  159. }
  160. for _, v := range record {
  161. var str string
  162. switch value := v.(type) {
  163. case int64:
  164. str = fmt.Sprint(value)
  165. case float64:
  166. str = fmt.Sprintf("%f", value)
  167. default:
  168. str = fmt.Sprintf("%s", value)
  169. }
  170. tables = append(tables, str)
  171. }
  172. }
  173. return tables, nil
  174. }