This article describes the principles and usage of the sql package of the Go language. Share it for your reference, as follows:
Go's SQL package is in pkg/database. The two packages in SQL and SQL/driver can be viewed together. It is recommended that you can check out the SQL folder before looking at these two packages. This document says a few more important points:
1 These two bags are truly Go style bags.
2 This requires no concurrency processing when using these two packages, nor does it require maintenance of its own database connection pool. Once a connection is established, this connection can be shared between various goroutines.
3 sql/driver provides a database interface, and the specific implementation needs to be implemented by yourself.
First look at the database/driver package
The first method used is Register
This method registers the implemented drivers into the variable drivers. After writing a driver, you need to register the drivers in SQL to use these interfaces in the SQL package. This implementation must implement the Open method.
Returns, its three methods
Prepare: Parameter binding
Close: Close the connection
Begin: Support transactions
Let’s look at Prepare first, this is the same usage as php mysql pdo
("select * from test where a=?")
The returned Stmt structure:
Close: Close this statement
NumInput: Returns how many parameters can be bound
Exec: Use without return, such as Insert or update
Query: select and other query operations
Exec is a binding variable and then returns the Result structure
Query is a binding variable and then returns the Rows result set
Look at the method in Result:
LastInsertId(): The primary key id obtained after the Insert operation
RowsAffect(): The number of rows affected
Rows:
Columns(): What columns are returned? It is actually the returned table column name
Close(): Close Rows, no more operations can be performed after the call
Next(): Fetch the data from the next row into the des[] Value. The Value interface here can be int64, float64, bool, []byte, string,
Next, return to Begin, return to Tx
After starting a transaction, there are two behaviors: Commit and Rollback. Both behaviors are methods of Tx interface.
The structure in drvier is all interface-based, and it requires you to implement and register in the Register.
The specific use of the driver is in database/sql
First, read the structure of several SQL
First of all, the structures in SQL are encapsulated in a layer of structures in drivers. For example, Rows, an internal property is rowsi.
The actual operation of SQL is done using the interface inside, which is actually using the driver you implement.
Driver and SQL are like plugs and a car full of plugs. If you implement driver, that is, if you implement these plug configurations, you can use SQL.
Result: Consistent with the Result in driver, that is, if you implement it, you will naturally implement it. It is an interface, but it has no special use. If all the results in the SQL package are replaced with rows, it is estimated that the author hopes that the return value will not be referenced to other packages, so he uses this method.
Rows: Based on it, we have extended several other methods on it. How to own:
Close
Cloumns
Err
Next
Scan
Stmt: Based on. How to own
Close
Exec
Query
QueryRow
Tx: Based on. How to own:
Commit
Exec
Prepare
Query
QueryRow
Rollback
Stmt
From the beginning
Returns the structure, which implements the structure. In addition to Conn's existing Prepare, Begin, and Close, there are also several additional query methods:
Driver(): Return to the current driver
Exec(): Perform directly
Query(): Conduct a query and return Rows
QueryRow(): It is expected to return a single row, return Row
Whether Rows or Row, there is a very useful method to put data into the formulated variables.
For example, the following is a typical Rows usage
...
for () {
var id int
var name string
err = (&id, &name)
...
}
Prepare returns Stmt structure
Exec returns the Result structure
As for these structures, there are their own methods.
I hope this article will be helpful to everyone's Go language programming.