From 44cd5275d59e536cf1712d467aeb642d764a08c3 Mon Sep 17 00:00:00 2001 From: john Date: Wed, 8 Aug 2018 20:09:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Esqlite=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/database/gdb/gdb.go | 28 ++++++++++++++-------------- g/database/gdb/gdb_pgsql.go | 4 +++- g/database/gdb/gdb_sqlite.go | 10 ++++------ g/database/gdb/gdb_transaction.go | 7 +++---- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/g/database/gdb/gdb.go b/g/database/gdb/gdb.go index 5f8e6d10b..22b899add 100644 --- a/g/database/gdb/gdb.go +++ b/g/database/gdb/gdb.go @@ -5,20 +5,20 @@ // You can obtain one at https://gitee.com/johng/gf. // 数据库ORM. +// 默认内置支持MySQL, 其他数据库需要手动import对应的数据库引擎第三方包. package gdb import ( - "database/sql" - "errors" "fmt" + "time" + "errors" + "database/sql" "gitee.com/johng/gf/g/container/gmap" "gitee.com/johng/gf/g/container/gring" "gitee.com/johng/gf/g/container/gtype" "gitee.com/johng/gf/g/os/gcache" "gitee.com/johng/gf/g/util/grand" _ "github.com/go-sql-driver/mysql" - _ "github.com/lib/pq" - "time" ) const ( @@ -127,8 +127,8 @@ var linkMysql = &dbmysql{} // PostgreSQL接口对象 var linkPgsql = &dbpgsql{} -//Sqlite接口对象 -//@author wxkj +// Sqlite接口对象 +// @author wxkj var linkSqlite = &dbsqlite{} // 数据库查询缓存对象map,使用数据库连接名称作为键名,键值为查询缓存对象 @@ -209,14 +209,14 @@ func getConfigNodeByPriority(cg ConfigGroup) *ConfigNode { func newDb(masterNode *ConfigNode, slaveNode *ConfigNode, groupName string) (*Db, error) { var link Link switch masterNode.Type { - case "mysql": - link = linkMysql - case "pgsql": - link = linkPgsql - case "sqlite": - link = linkSqlite - default: - return nil, errors.New(fmt.Sprintf("unsupported db type '%s'", masterNode.Type)) + case "mysql": + link = linkMysql + case "pgsql": + link = linkPgsql + case "sqlite": + link = linkSqlite + default: + return nil, errors.New(fmt.Sprintf("unsupported db type '%s'", masterNode.Type)) } master, err := link.Open(masterNode) if err != nil { diff --git a/g/database/gdb/gdb_pgsql.go b/g/database/gdb/gdb_pgsql.go index bf4bf903b..7164ff513 100644 --- a/g/database/gdb/gdb_pgsql.go +++ b/g/database/gdb/gdb_pgsql.go @@ -13,7 +13,9 @@ import ( "database/sql" ) -// postgresql的适配 +// PostgreSQL的适配. +// 使用时需要import: +// _ "github.com/lib/pq" // @todo 需要完善replace和save的操作覆盖 // 数据库链接对象 diff --git a/g/database/gdb/gdb_sqlite.go b/g/database/gdb/gdb_sqlite.go index 443f2ea27..6c67da878 100644 --- a/g/database/gdb/gdb_sqlite.go +++ b/g/database/gdb/gdb_sqlite.go @@ -3,7 +3,7 @@ // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, // You can obtain one at https://gitee.com/johng/gf. -//@author wxkj +// @author wxkj package gdb @@ -11,6 +11,9 @@ import ( "database/sql" ) +// 使用时需要import: +// _ "github.com/mattn/go-sqlite3" + // 数据库链接对象 type dbsqlite struct { Db @@ -21,11 +24,6 @@ func (db *dbsqlite) Open(c *ConfigNode) (*sql.DB, error) { if c.Linkinfo != "" { source = c.Linkinfo } else { - //path, err := os.Getwd() - //if err != nil { - // return nil, err - //} - //先这样吧 source = c.Name } if db, err := sql.Open("sqlite3", source); err == nil { diff --git a/g/database/gdb/gdb_transaction.go b/g/database/gdb/gdb_transaction.go index 09d0cc94f..33be6cf73 100644 --- a/g/database/gdb/gdb_transaction.go +++ b/g/database/gdb/gdb_transaction.go @@ -10,12 +10,11 @@ import ( "fmt" "errors" "strings" - "database/sql" - _ "github.com/lib/pq" - _ "github.com/go-sql-driver/mysql" - "gitee.com/johng/gf/g/util/gconv" "reflect" + "database/sql" "gitee.com/johng/gf/g/os/gtime" + "gitee.com/johng/gf/g/util/gconv" + _ "github.com/go-sql-driver/mysql" ) // 数据库事务对象