<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                Go與PHP不同的地方是Go官方沒有提供數據庫驅動,而是為開發數據庫驅動定義了一些標準接口,開發者可以根據定義的接口來開發相應的數據庫驅動,這樣做有一個好處,只要是按照標準接口開發的代碼, 以后需要遷移數據庫時,不需要任何修改。那么Go都定義了哪些標準接口呢?讓我們來詳細的分析一下 ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/05.1.md#sqlregister)sql.Register 這個存在于database/sql的函數是用來注冊數據庫驅動的,當第三方開發者開發數據庫驅動時,都會實現init函數,在init里面會調用這個`Register(name string, driver driver.Driver)`完成本驅動的注冊。 我們來看一下mymysql、sqlite3的驅動里面都是怎么調用的: ~~~ //https://github.com/mattn/go-sqlite3驅動 func init() { sql.Register("sqlite3", &SQLiteDriver{}) } //https://github.com/mikespook/mymysql驅動 // Driver automatically registered in database/sql var d = Driver{proto: "tcp", raddr: "127.0.0.1:3306"} func init() { Register("SET NAMES utf8") sql.Register("mymysql", &d) } ~~~ 我們看到第三方數據庫驅動都是通過調用這個函數來注冊自己的數據庫驅動名稱以及相應的driver實現。在database/sql內部通過一個map來存儲用戶定義的相應驅動。 ~~~ var drivers = make(map[string]driver.Driver) drivers[name] = driver ~~~ 因此通過database/sql的注冊函數可以同時注冊多個數據庫驅動,只要不重復。 > 在我們使用database/sql接口和第三方庫的時候經常看到如下: > > ~~~ > import ( > "database/sql" > _ "github.com/mattn/go-sqlite3" > ) > ~~~ > > 新手都會被這個`_`所迷惑,其實這個就是Go設計的巧妙之處,我們在變量賦值的時候經常看到這個符號,它是用來忽略變量賦值的占位符,那么包引入用到這個符號也是相似的作用,這兒使用`_`的意思是引入后面的包名而不直接使用這個包中定義的函數,變量等資源。 > > 我們在2.3節流程和函數一節中介紹過init函數的初始化過程,包在引入的時候會自動調用包的init函數以完成對包的初始化。因此,我們引入上面的數據庫驅動包之后會自動去調用init函數,然后在init函數里面注冊這個數據庫驅動,這樣我們就可以在接下來的代碼中直接使用這個數據庫驅動了。 ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/05.1.md#driverdriver)driver.Driver Driver是一個數據庫驅動的接口,他定義了一個method: Open(name string),這個方法返回一個數據庫的Conn接口。 ~~~ type Driver interface { Open(name string) (Conn, error) } ~~~ 返回的Conn只能用來進行一次goroutine的操作,也就是說不能把這個Conn應用于Go的多個goroutine里面。如下代碼會出現錯誤 ~~~ ... go goroutineA (Conn) //執行查詢操作 go goroutineB (Conn) //執行插入操作 ... ~~~ 上面這樣的代碼可能會使Go不知道某個操作究竟是由哪個goroutine發起的,從而導致數據混亂,比如可能會把goroutineA里面執行的查詢操作的結果返回給goroutineB從而使B錯誤地把此結果當成自己執行的插入數據。 第三方驅動都會定義這個函數,它會解析name參數來獲取相關數據庫的連接信息,解析完成后,它將使用此信息來初始化一個Conn并返回它。 ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/05.1.md#driverconn)driver.Conn Conn是一個數據庫連接的接口定義,他定義了一系列方法,這個Conn只能應用在一個goroutine里面,不能使用在多個goroutine里面,詳情請參考上面的說明。 ~~~ type Conn interface { Prepare(query string) (Stmt, error) Close() error Begin() (Tx, error) } ~~~ Prepare函數返回與當前連接相關的執行Sql語句的準備狀態,可以進行查詢、刪除等操作。 Close函數關閉當前的連接,執行釋放連接擁有的資源等清理工作。因為驅動實現了database/sql里面建議的conn pool,所以你不用再去實現緩存conn之類的,這樣會容易引起問題。 Begin函數返回一個代表事務處理的Tx,通過它你可以進行查詢,更新等操作,或者對事務進行回滾、遞交。 ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/05.1.md#driverstmt)driver.Stmt Stmt是一種準備好的狀態,和Conn相關聯,而且只能應用于一個goroutine中,不能應用于多個goroutine。 ~~~ type Stmt interface { Close() error NumInput() int Exec(args []Value) (Result, error) Query(args []Value) (Rows, error) } ~~~ Close函數關閉當前的鏈接狀態,但是如果當前正在執行query,query還是有效返回rows數據。 NumInput函數返回當前預留參數的個數,當返回>=0時數據庫驅動就會智能檢查調用者的參數。當數據庫驅動包不知道預留參數的時候,返回-1。 Exec函數執行Prepare準備好的sql,傳入參數執行update/insert等操作,返回Result數據 Query函數執行Prepare準備好的sql,傳入需要的參數執行select操作,返回Rows結果集 ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/05.1.md#drivertx)driver.Tx 事務處理一般就兩個過程,遞交或者回滾。數據庫驅動里面也只需要實現這兩個函數就可以 ~~~ type Tx interface { Commit() error Rollback() error } ~~~ 這兩個函數一個用來遞交一個事務,一個用來回滾事務。 ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/05.1.md#driverexecer)driver.Execer 這是一個Conn可選擇實現的接口 ~~~ type Execer interface { Exec(query string, args []Value) (Result, error) } ~~~ 如果這個接口沒有定義,那么在調用DB.Exec,就會首先調用Prepare返回Stmt,然后執行Stmt的Exec,然后關閉Stmt。 ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/05.1.md#driverresult)driver.Result 這個是執行Update/Insert等操作返回的結果接口定義 ~~~ type Result interface { LastInsertId() (int64, error) RowsAffected() (int64, error) } ~~~ LastInsertId函數返回由數據庫執行插入操作得到的自增ID號。 RowsAffected函數返回query操作影響的數據條目數。 ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/05.1.md#driverrows)driver.Rows Rows是執行查詢返回的結果集接口定義 ~~~ type Rows interface { Columns() []string Close() error Next(dest []Value) error } ~~~ Columns函數返回查詢數據庫表的字段信息,這個返回的slice和sql查詢的字段一一對應,而不是返回整個表的所有字段。 Close函數用來關閉Rows迭代器。 Next函數用來返回下一條數據,把數據賦值給dest。dest里面的元素必須是driver.Value的值除了string,返回的數據里面所有的string都必須要轉換成[]byte。如果最后沒數據了,Next函數最后返回io.EOF。 ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/05.1.md#driverrowsaffected)driver.RowsAffected RowsAffected其實就是一個int64的別名,但是他實現了Result接口,用來底層實現Result的表示方式 ~~~ type RowsAffected int64 func (RowsAffected) LastInsertId() (int64, error) func (v RowsAffected) RowsAffected() (int64, error) ~~~ ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/05.1.md#drivervalue)driver.Value Value其實就是一個空接口,他可以容納任何的數據 ~~~ type Value interface{} ~~~ drive的Value是驅動必須能夠操作的Value,Value要么是nil,要么是下面的任意一種 ~~~ int64 float64 bool []byte string [*]除了Rows.Next返回的不能是string. time.Time ~~~ ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/05.1.md#drivervalueconverter)driver.ValueConverter ValueConverter接口定義了如何把一個普通的值轉化成driver.Value的接口 ~~~ type ValueConverter interface { ConvertValue(v interface{}) (Value, error) } ~~~ 在開發的數據庫驅動包里面實現這個接口的函數在很多地方會使用到,這個ValueConverter有很多好處: * 轉化driver.value到數據庫表相應的字段,例如int64的數據如何轉化成數據庫表uint16字段 * 把數據庫查詢結果轉化成driver.Value值 * 在scan函數里面如何把driver.Value值轉化成用戶定義的值 ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/05.1.md#drivervaluer)driver.Valuer Valuer接口定義了返回一個driver.Value的方式 ~~~ type Valuer interface { Value() (Value, error) } ~~~ 很多類型都實現了這個Value方法,用來自身與driver.Value的轉化。 通過上面的講解,你應該對于驅動的開發有了一個基本的了解,一個驅動只要實現了這些接口就能完成增刪查改等基本操作了,剩下的就是與相應的數據庫進行數據交互等細節問題了,在此不再贅述。 ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/05.1.md#databasesql)database/sql database/sql在database/sql/driver提供的接口基礎上定義了一些更高階的方法,用以簡化數據庫操作,同時內部還建議性地實現一個conn pool。 ~~~ type DB struct { driver driver.Driver dsn string mu sync.Mutex // protects freeConn and closed freeConn []driver.Conn closed bool } ~~~ 我們可以看到Open函數返回的是DB對象,里面有一個freeConn,它就是那個簡易的連接池。它的實現相當簡單或者說簡陋,就是當執行Db.prepare的時候會`defer db.putConn(ci, err)`,也就是把這個連接放入連接池,每次調用conn的時候會先判斷freeConn的長度是否大于0,大于0說明有可以復用的conn,直接拿出來用就是了,如果不大于0,則創建一個conn,然后再返回之。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看