My golib:db query Result

Source: Internet
Author: User
Tags stmt
This is a creation in Article, where the information may have evolved or changed.

Go provides a unified SQL interface for database operations, and any third party can access the databases of interest by implementing the appropriate driver. For example, the go-mysql-driver used in our project.

Go provides a good set of mechanisms to handle database query operations, such as the official example:

age := 27rows, err := db.Query("SELECT name FROM users WHERE age=?", age)if err != nil {        log.Fatal(err)}for rows.Next() {        var name string        if err := rows.Scan(&name); err != nil {                log.Fatal(err)        }        fmt.Printf("%s is %d\n", name, age)}if err := rows.Err(); err != nil {        log.Fatal(err)}

In the example above, query returns a row, we get the next row of data through next, and then we use scan to set the data on each line to the corresponding variable.

Although it is convenient to operate the query result set in this way, we find that when we use the SQL query statement too much, the amount of code is too large if every query follows this notation. So, we naturally think of encapsulation.

The main task of DB result is to provide a set of unified interfaces for external convenience using the query result set, which mainly provides the following interfaces:

func (*Result ) GetInt (row, col int) (int64, error) func (*result) getintbyname (row int, colname string) (Int64, error) func (*result) Get Float (row, col int) (float64, error) func (*result) getfloatbyname (row int, colname string) (float64, error) func (*result) Getbool (row, col int) (bool, error) func (*result) getboolbyname (row int, colname string) (bool, error) func (*result) getst Ring (row, col int) (string, error) func (*result) getstringbyname (row int, colname string) (String, error) func (*result) Ge Tbuffer (row, col int) ([]byte, Error) func (*result) getbufferbyname (row int, colname string) ([]byte, error)  

As you can see, result's "get*" interface accesses data through the index of the result set's row and Col, while the "Get*byname" interface accesses the data through the name of row and Col.

Because go supports a lot of data types, for the sake of simplicity, all of the int types we use int64 instead, the external is doing the corresponding data conversion. Similarly the float type is also replaced with float64. If a field is empty for the result of the query, result simply returns the default value for that field. In general, I would require that the fields in the database be not NULL, so there is not much to consider in the case where the query field is null.

A very simple example:

//msg_id bigint//msg varchar(256)res, err := Query("is", "select msg_id, msg from msg_table where msg_id in (?, ?)", 1, 2)var id1 int64id1, err = res.GetInt(0, 0)var msg2 stringmsg2, err = res.GetStringByName(1, "msg")

In the example above, we returned a result at the time of the query and obtained the corresponding information through the related function. What we need to pay special attention here is the first parameter "is"after query.

in Go-mysql-driver, there are two result set patterns, one is text rows and the other is binary rows (the result set using stmt query). In text rows, all the data is in []byte format, and in binary rows, it is converted to the corresponding data according to the stmt corresponding result type. To unify, we create the corresponding type data directly at the row scan by manually specifying column types at query time for the scan setting. as follows:

func (res *Result) NewValue ( column int) (interface{}, error) {t: = Res. Columntypes[column] Switch T {case Column_string:return new (String), nil case Column_int:return New (Int64), Nil case Column_bool:return New (Bool), nil case Column_float:return New (float64), nil Case Column_buffer:return New ([]byte), nil Default:return nil, errcolumntypes}}//when traversing the result set, we are based on Colu MN types generates the specified value and sets the for rows through the scan. Next () {dest: = make ([]interface{}, Len (res. ColumnNames))) for I, _: = Range dest {Dest[i], err = Res.newvalue (i) if err! = Nil {return E RR}} Err = rows. Scan (dest ...) If err! = Nil {return err} 

If you have used Mysql_stmt_bind_result, you can find that the concept of column types is actually similar to the settings mysql_bind, only the result for the sake of simplicity, only support Int64,float64,bool, String, and []byte] these types.

The specific code is HTTPS://GITHUB.COM/SIDDONTANG/QPUSH/BLOB/MASTER/GO/SRC/LIB/DB/RESULT.GO here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.