Browse Source

interfacing

Soe Thu 5 years ago
parent
commit
98deba7284
8 changed files with 360 additions and 382 deletions
  1. 0 0
      README.md
  2. 0 216
      crud.go
  3. 20 18
      db.go
  4. 1 7
      go.mod
  5. 0 126
      go.sum
  6. 188 0
      postgres.go
  7. 151 0
      sqlite.go
  8. 0 15
      vo.go

+ 0 - 0
README.md


+ 0 - 216
crud.go

@@ -1,216 +0,0 @@
-package db
-
-import "fmt"
-
-func (db DB) Insert(obj interface{}) error {
-	tx := db.Orm.Begin()
-
-	err := tx.Create(obj).Error
-	if err != nil {
-		tx.Rollback()
-		return err
-	}
-
-	err = tx.Commit().Error
-	if err != nil {
-		tx.Rollback()
-		return err
-	}
-	return nil
-}
-func (db DB) Get(objs interface{}, where map[string]interface{}, order string, limit uint64, offset uint64, deleted bool) error {
-	con := db.Orm
-	if deleted {
-		con = con.Unscoped()
-	}
-
-	keys, values := whereExactKeyValues(where)
-	con = con.Where(keys, values)
-	if deleted {
-		con = con.Where("deleted_at is not null")
-	}
-
-	if order != "" {
-		con = con.Order(order)
-	}
-	if limit != 0 {
-		con = con.Limit(limit)
-	}
-	if offset != 0 {
-		con = con.Offset(offset)
-	}
-	err := con.Find(objs).Error
-	if err != nil {
-		return err
-	}
-	return nil
-}
-
-// get by id
-func (db DB) GetById(obj interface{}, id uint64) error {
-	err := db.Orm.First(obj, id).Error
-	if err != nil {
-		return err
-	}
-	return nil
-}
-func (db DB) Update(obj interface{}, para map[string]interface{}) error {
-	tx := db.Orm.Begin()
-
-	err := tx.Model(obj).Updates(para).Error
-	if err != nil {
-		tx.Rollback()
-		return err
-	}
-
-	err = tx.Commit().Error
-	if err != nil {
-		tx.Rollback()
-		return err
-	}
-	return nil
-}
-func (db DB) Delete(obj interface{}, para map[string]interface{}) error {
-	tx := db.Orm.Begin()
-
-	err := tx.Where(para).Delete(obj).Error
-	if err != nil {
-		tx.Rollback()
-		return err
-	}
-
-	err = tx.Commit().Error
-	if err != nil {
-		tx.Rollback()
-		return err
-	}
-	return nil
-}
-
-// get tables
-func (db DB) GetTables() ([]interface{}, error) {
-	var tables []interface{}
-	if db.Driver == SQLITE_DRIVER {
-		ts, err := db.sqliteTables()
-		if err != nil {
-			return ts, err
-		}
-		tables = ts
-	}
-	if db.Driver == POSTGRES_DRIVER {
-		tp, err := db.Select("select table_name from information_schema.tables", Filter{Field: "table_type", Compare: "=", Value: "BASE TABLE"})
-		if err != nil {
-			return tp, err
-		}
-		tables = tp
-	}
-	return tables, nil
-}
-func (db DB) GetMeta(tableName interface{}) (interface{}, error) {
-	var meta interface{}
-	if db.Driver == SQLITE_DRIVER {
-		tm, err := db.sqliteMeta(tableName)
-		if err != nil {
-			return meta, err
-		}
-		meta = tm
-	}
-	if db.Driver == POSTGRES_DRIVER {
-		pm, err := db.postgresMeta(fmt.Sprint(tableName))
-		if err != nil {
-			return pm, err
-		}
-		meta = pm
-	}
-	return meta, nil
-}
-func (db DB) postgresMeta(tableName string) ([]interface{}, error) {
-	q := "SELECT columns.table_name,columns.column_name,columns.data_type,columns.column_default,columns.is_nullable FROM information_schema.columns"
-	mp, err := db.Select(q, Filter{Field: "columns.table_name", Compare: "=", Value: tableName})
-	if err != nil {
-		return nil, err
-	}
-	return mp, nil
-}
-
-// get table meta data
-func (db DB) sqliteMeta(tableName interface{}) ([]TableInfo, error) {
-	var info []TableInfo
-	str := fmt.Sprint(tableName)
-	q := "pragma table_info(" + str + ")"
-	row, err := db.Orm.DB().Query(q)
-	if err != nil {
-		return info, err
-	}
-	var cid uint
-	var t string
-	var name string
-	var notnull string
-	var pk int
-	var dfltValue interface{}
-	for row.Next() {
-		err = row.Scan(&cid, &name, &t, &notnull, &dfltValue, &pk)
-		if err != nil {
-			return info, err
-		}
-		table_info := TableInfo{
-			Cid:       cid,
-			Name:      name,
-			Type:      t,
-			Notnull:   notnull,
-			DfltValue: dfltValue,
-			Pk:        pk,
-		}
-		info = append(info, table_info)
-	}
-	return info, nil
-}
-func (db DB) Select(para string, filter Filter) ([]interface{}, error) {
-	var tables []interface{}
-	row, err := db.Orm.Select(para).Where(filter.Field+" "+filter.Compare+" ?", filter.Value).Rows()
-	if err != nil {
-		return tables, err
-	}
-	columns, err := row.Columns()
-	if err != nil {
-		return tables, err
-	}
-	for row.Next() {
-		values := make([]interface{}, len(columns))
-		record := make([]interface{}, len(columns))
-		for i, _ := range columns {
-			values[i] = &record[i]
-		}
-		err = row.Scan(values...)
-		if err != nil {
-			return tables, err
-		}
-		for _, v := range record {
-			var str string
-			switch value := v.(type) {
-			case int64:
-				str = fmt.Sprint(value)
-			case float64:
-				str = fmt.Sprintf("%f", value)
-			default:
-				str = fmt.Sprintf("%s", value)
-			}
-			tables = append(tables, str)
-		}
-	}
-	return tables, nil
-}
-func (db DB) sqliteTables() ([]interface{}, error) {
-	var tables []interface{}
-	var sqlMaster []SqliteMaster
-	err := db.Get(&sqlMaster, nil, "", 0, 0, false)
-	if err != nil {
-		return tables, err
-	}
-	for _, t := range sqlMaster {
-		if t.Type == "table" {
-			tables = append(tables, t.TblName)
-		}
-	}
-	return tables, nil
-}

+ 20 - 18
db.go

@@ -14,15 +14,20 @@ const (
 	POSTGRES_DRIVER = "postgres"
 )
 
-type DB struct {
-	Orm    *gorm.DB
-	Mutex  *sync.Mutex
-	Driver string
+type DB interface {
+	Hello() string
+	Orm() *gorm.DB
+	Mutex() *sync.Mutex
+	Insert(obj interface{}) error
+	Get(objs interface{}, where map[string]interface{}, order string, limit uint64, offset uint64, deleted bool) error
+	Update(obj interface{}, para map[string]interface{}) error
+	Delete(obj interface{}, para map[string]interface{}) error
+	GetMeta(name interface{}) (interface{}, error)
+	GetTables() (interface{}, error)
 }
 
-// databe driver name
 //support databes 'sqlite3','postgres'
-func Open(driver string, args ...interface{}) (*DB, error) {
+func Open(driver string, args ...interface{}) (DB, error) {
 	if driver == "" {
 		return nil, errors.New("Invalid driver name.")
 	}
@@ -30,17 +35,14 @@ func Open(driver string, args ...interface{}) (*DB, error) {
 	if err != nil {
 		return nil, err
 	}
-	return &DB{Orm: d, Mutex: &sync.Mutex{}, Driver: driver}, nil
-}
-func (db DB) isSqlite() bool {
-	if db.Driver == SQLITE_DRIVER {
-		return true
-	}
-	return false
-}
-func (db DB) isPostgres() bool {
-	if db.Driver == POSTGRES_DRIVER {
-		return true
+	var db DB
+	switch driver {
+	case SQLITE_DRIVER:
+		db = Sqlite{orm: d, mutex: &sync.Mutex{}}
+	case POSTGRES_DRIVER:
+		db = Postgres{orm: d, mutex: &sync.Mutex{}}
+	default:
+		return nil, errors.New("Unknown database driver")
 	}
-	return false
+	return db, nil
 }

+ 1 - 7
go.mod

@@ -1,9 +1,3 @@
-module git.mokkon.com/soethu/godb
+module git.mokkon.com/soeth/godb
 
 go 1.12
-
-require (
-	github.com/jinzhu/gorm v1.9.11
-	github.com/lib/pq v1.2.0
-	github.com/mattn/go-sqlite3 v1.11.0
-)

+ 0 - 126
go.sum

@@ -1,126 +0,0 @@
-cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
-cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
-cloud.google.com/go v0.37.4/go.mod h1:NHPJ89PdicEuT9hdPXMROBD91xc5uRDxsMtSB16k7hw=
-github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
-github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
-github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
-github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
-github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
-github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
-github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
-github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
-github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM=
-github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
-github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
-github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
-github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
-github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
-github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
-github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
-github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
-github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
-github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
-github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
-github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
-github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
-github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
-github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
-github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
-github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
-github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
-github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
-github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
-github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
-github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
-github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
-github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
-github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
-github.com/jinzhu/gorm v1.9.11 h1:gaHGvE+UnWGlbWG4Y3FUwY1EcZ5n6S9WtqBA/uySMLE=
-github.com/jinzhu/gorm v1.9.11/go.mod h1:bu/pK8szGZ2puuErfU0RwyeNdsf3e6nCX/noXaVxkfw=
-github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
-github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
-github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
-github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
-github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
-github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
-github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
-github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
-github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
-github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
-github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
-github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q=
-github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
-github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
-github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
-github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
-github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
-github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
-github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
-github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
-github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
-github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
-github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
-github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=
-github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
-github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
-github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
-github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
-github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
-github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
-github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
-github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
-github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
-go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
-golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
-golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
-golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
-golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
-golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
-golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
-golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
-golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
-golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
-golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
-golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
-golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
-golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
-golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
-golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
-golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
-golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
-golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
-golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
-golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
-golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
-golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
-golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
-golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
-golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
-golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
-golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
-golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
-golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
-golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
-golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
-golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
-golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
-golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
-google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
-google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
-google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
-google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
-google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
-google.golang.org/genproto v0.0.0-20190404172233-64821d5d2107/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
-google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
-google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
-gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
-gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
-gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
-gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
-gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
-gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
-gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
-honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
-honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
-honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

+ 188 - 0
postgres.go

@@ -0,0 +1,188 @@
+package db
+
+import (
+	"fmt"
+	"sync"
+
+	"github.com/jinzhu/gorm"
+)
+
+type PostgresMaster struct {
+	MyTable
+	TableCatalog              string
+	TableSchema               string
+	TableType                 string
+	SelfReferencingColumnName string
+	ReferenceGeneration       string
+	UserDefinedTypeName       string
+	IsInsertableInto          string
+	IsTyped                   string
+	CommitAction              string
+}
+type PostgresTableInfo struct {
+	MyTable
+	TableCatalog    string
+	TableSchema     string
+	ColumnName      string
+	OrdinalPosition int
+	ColumnDefault   string
+	IsNullable      string
+	DataType        interface{}
+}
+
+func (PostgresTableInfo) TableName() string {
+	return "information_schema.columns"
+}
+func (PostgresMaster) TableName() string {
+	return "information_schema.tables"
+}
+
+type MyTable struct {
+	TableName string
+}
+type Postgres struct {
+	orm   *gorm.DB
+	mutex *sync.Mutex
+}
+
+func (Postgres) Hello() string {
+	return "Postgres"
+}
+func (p Postgres) Orm() *gorm.DB {
+	return p.orm
+}
+func (p Postgres) Mutex() *sync.Mutex {
+	return p.mutex
+}
+func (db Postgres) Insert(obj interface{}) error {
+	tx := db.orm.Begin()
+
+	err := tx.Create(obj).Error
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+
+	err = tx.Commit().Error
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+	return nil
+}
+func (db Postgres) Delete(obj interface{}, para map[string]interface{}) error {
+	tx := db.orm.Begin()
+
+	err := tx.Where(para).Delete(obj).Error
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+
+	err = tx.Commit().Error
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+	return nil
+}
+func (db Postgres) Get(objs interface{}, where map[string]interface{}, order string, limit uint64, offset uint64, deleted bool) error {
+	con := db.orm
+	if deleted {
+		con = con.Unscoped()
+	}
+
+	keys, values := whereExactKeyValues(where)
+	con = con.Where(keys, values)
+	if deleted {
+		con = con.Where("deleted_at is not null")
+	}
+
+	if order != "" {
+		con = con.Order(order)
+	}
+	if limit != 0 {
+		con = con.Limit(limit)
+	}
+	if offset != 0 {
+		con = con.Offset(offset)
+	}
+	err := con.Find(objs).Error
+	if err != nil {
+		return err
+	}
+	return nil
+}
+func (db Postgres) Update(obj interface{}, para map[string]interface{}) error {
+	tx := db.orm.Begin()
+
+	err := tx.Model(obj).Updates(para).Error
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+
+	err = tx.Commit().Error
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+	return nil
+}
+func (db Postgres) GetMeta(name interface{}) (interface{}, error) {
+	tableName := fmt.Sprint(name)
+	var tabInfo []PostgresTableInfo
+	err := db.Get(&tabInfo, map[string]interface{}{"table_name": tableName}, "", 0, 0, false)
+	if err != nil {
+		return tabInfo, err
+	}
+	return tabInfo, nil
+}
+func (db Postgres) GetTables() (interface{}, error) {
+	var tables []PostgresMaster
+	if err := db.Get(&tables, map[string]interface{}{"table_type": "BASE TABLE"}, "", 0, 0, false); err != nil {
+		return nil, err
+	}
+	return tables, nil
+}
+func (db Postgres) Select(obj interface{}, para string, filter []Filter) ([]interface{}, error) {
+	var tables []interface{}
+	con := db.orm
+	con = con.Table(fmt.Sprint(obj))
+	con = con.Select(para)
+	for _, f := range filter {
+		con = con.Where(f.Field+" "+f.Compare+" ?", f.Value)
+	}
+	row, err := con.Rows()
+	if err != nil {
+		return tables, err
+	}
+	columns, err := row.Columns()
+	if err != nil {
+		return tables, err
+	}
+	for row.Next() {
+		values := make([]interface{}, len(columns))
+		record := make([]interface{}, len(columns))
+		for i, _ := range columns {
+			values[i] = &record[i]
+		}
+		err = row.Scan(values...)
+		if err != nil {
+			return tables, err
+		}
+		for _, v := range record {
+			var str string
+			switch value := v.(type) {
+			case int64:
+				str = fmt.Sprint(value)
+			case float64:
+				str = fmt.Sprintf("%f", value)
+			default:
+				str = fmt.Sprintf("%s", value)
+			}
+			tables = append(tables, str)
+		}
+	}
+	return tables, nil
+}

+ 151 - 0
sqlite.go

@@ -0,0 +1,151 @@
+package db
+
+import (
+	"fmt"
+	"sync"
+
+	"github.com/jinzhu/gorm"
+)
+
+type SqliteMaster struct {
+	Type     string `json:"type"`
+	TblName  string `json:"tbl_name"`
+	RootPage string `json:"root_page"`
+	Name     string `json:"name"`
+	Sql      string `json:"sql"`
+}
+type TableInfo struct {
+	Cid       uint        `json:"cid"`
+	Type      string      `json:"type"`
+	Name      string      `json:"name"`
+	Notnull   string      `json:"not_null"`
+	Pk        int         `json:"pk"`
+	DfltValue interface{} `json:"deflt_value"`
+}
+type Sqlite struct {
+	orm   *gorm.DB
+	mutex *sync.Mutex
+}
+
+func (Sqlite) Hello() string {
+	return "Sqlite"
+}
+func (s Sqlite) Orm() *gorm.DB {
+	return s.orm
+}
+func (s Sqlite) Mutex() *sync.Mutex {
+	return s.mutex
+}
+func (db Sqlite) Insert(obj interface{}) error {
+	tx := db.orm.Begin()
+
+	err := tx.Create(obj).Error
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+
+	err = tx.Commit().Error
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+	return nil
+}
+func (db Sqlite) Delete(obj interface{}, para map[string]interface{}) error {
+	tx := db.orm.Begin()
+
+	err := tx.Where(para).Delete(obj).Error
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+
+	err = tx.Commit().Error
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+	return nil
+}
+func (db Sqlite) Get(objs interface{}, where map[string]interface{}, order string, limit uint64, offset uint64, deleted bool) error {
+	con := db.orm
+	if deleted {
+		con = con.Unscoped()
+	}
+
+	keys, values := whereExactKeyValues(where)
+	con = con.Where(keys, values)
+	if deleted {
+		con = con.Where("deleted_at is not null")
+	}
+
+	if order != "" {
+		con = con.Order(order)
+	}
+	if limit != 0 {
+		con = con.Limit(limit)
+	}
+	if offset != 0 {
+		con = con.Offset(offset)
+	}
+	err := con.Find(objs).Error
+	if err != nil {
+		return err
+	}
+	return nil
+}
+func (db Sqlite) Update(obj interface{}, para map[string]interface{}) error {
+	tx := db.orm.Begin()
+
+	err := tx.Model(obj).Updates(para).Error
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+
+	err = tx.Commit().Error
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+	return nil
+}
+func (db Sqlite) GetTables() (interface{}, error) {
+	var sqlMaster []SqliteMaster
+	if err := db.Get(&sqlMaster, map[string]interface{}{"type": "table"}, "", 0, 0, false); err != nil {
+		return nil, err
+	}
+	return sqlMaster, nil
+}
+func (db Sqlite) GetMeta(tableName interface{}) (interface{}, error) {
+	var info []TableInfo
+	str := fmt.Sprint(tableName)
+	q := "pragma table_info(" + str + ")"
+	row, err := db.orm.DB().Query(q)
+	if err != nil {
+		return info, err
+	}
+	var cid uint
+	var t string
+	var name string
+	var notnull string
+	var pk int
+	var dfltValue interface{}
+	for row.Next() {
+		err = row.Scan(&cid, &name, &t, &notnull, &dfltValue, &pk)
+		if err != nil {
+			return info, err
+		}
+		table_info := TableInfo{
+			Cid:       cid,
+			Name:      name,
+			Type:      t,
+			Notnull:   notnull,
+			DfltValue: dfltValue,
+			Pk:        pk,
+		}
+		info = append(info, table_info)
+	}
+	return info, nil
+}

+ 0 - 15
vo.go

@@ -8,21 +8,6 @@ type Model struct {
 	UpdatedAt time.Time
 	DeletedAt *time.Time `sql:"index"`
 }
-type SqliteMaster struct {
-	Type     string `json:"type"`
-	TblName  string `json:"tbl_name"`
-	RootPage string `json:"root_page"`
-	Name     string `json:"name"`
-	Sql      string `json:"sql"`
-}
-type TableInfo struct {
-	Cid       uint        `json:"cid"`
-	Type      string      `json:"type"`
-	Name      string      `json:"name"`
-	Notnull   string      `json:"not_null"`
-	Pk        int         `json:"pk"`
-	DfltValue interface{} `json:"deflt_value"`
-}
 type Filter struct {
 	Field   string      `json:"field"`
 	Compare string      `json:"compare"`