Model Query Reconstruction of Blog Editing Function in Web Development of Go Basic Learning Records

Keywords: Go github git SQL PHP

Every time I put my practice code on github and typed a tag, which is convenient for the students to use later. Here I share my practice with the code of the following branches.

https://github.com/durban89/typescript_demo.git
tag: 1.1.4

In the previous article, [Go Basic Learning Record - Writing Web Application - Model Refactoring of Blog Editing Function], we only updated the Updaet method. This time, we share the implementation of query-related.
To tell you the truth, this query was supposed to be very simple. Just like writing PHP or Python before, directly returning the query results may actually have something to do with the MySQL library I use. But this is not the point, the point is that we find a way to solve the problem.
Before we start, let's talk about golang's interface knowledge point... interface {} to understand what this means.

Let's first look at... String, which represents string parameters, an example of a specific function.

func music(m ...string) {
    // other implementation
}

When we call it, we call it this way.

music("a", "b", "c")

Now I'm just passing in three parameters, and I can also pass in more parameters, and of course there's another way to call it.

var args = []string{
    "a",
    "b",
    "c",
}
music(args...)

The above is a well-understood example, so the use of... interface {} is similar to that of... string. Let's start with how I optimize my query method.

Step 1, Model Layer Modification

Here we optimize two Queries and QueryOne, first look at QueryOne, the code is as follows

// QueryOne Gets a Data
func (p *ModelProperty) QueryOne(s SelectValues, where WhereValues) error {
    var selectString = s.MergeSelect()

    var whereString = where.MergeWhere()

    sql := fmt.Sprintf("SELECT %s FROM %s WHERE %s LIMIT 0, 1", selectString, tableName, whereString)

    rows, err := Conn.Query(sql)

    if err != nil {
        return err
    }

    selectField := make([]interface{}, len(s))

    var i = 0
    for _, v := range s {
        selectField[i] = v
        i++
    }

    for rows.Next() {
        err = rows.Scan(selectField...)

        if err != nil {
            return err
        }

        var i = 0
        for _, v := range s {
            ref := reflect.ValueOf(v)
            fmt.Println(ref.Elem())
            i++
        }
    }

    return nil
}

At the same time, the structure of SelectValues was modified as follows

// SelectValues select condition value
type SelectValues map[string]interface{}

The key change is here.

selectField := make([]interface{}, len(s))

var i = 0
for _, v := range s {
    selectField[i] = v
    i++
}

And function MergeSelect

// MergeSelect Merge select Conditions
func (s SelectValues) MergeSelect() string {
    value := []string{}
    for k := range s {
        v := fmt.Sprintf("`%s`", k)
        value = append(value, v)
    }

    return strings.Join(value, ", ")
}

The first part of the modification is to conform to the function call of rows.Scan(selectField...), so it needs to be transformed. At the same time, in order to facilitate access to data in the control layer, it needs to pass the data back to the control layer by pointer.
The second part of the modification is to cooperate with the generation of query statements, so the second modification is to deal with the key value, and finally the reason for modifying SelectValues. After the modification, in fact, it is convenient for query as a whole, and many logics feel that the implementation is also smooth.

Step 2, Control Layer Call

The QueryOne query is called as follows

var autokid int64
var title string
selectField := models.SelectValues{
    "autokid": &autokid,
    "title":   &title,
}

err := blogModel.QueryOne(selectField, where)

if err != nil {
    http.NotFound(w, r)
    return
}

p := helpers.Page{
    Title: title,
    ID:    autokid,
}

Query query invocation

var blogModel models.BlogModel

var autokid int64
var title string
selectField := models.SelectValues{
    "autokid": &autokid,
    "title":   &title,
}

where := models.WhereValues{}

qr, err := blogModel.Query(selectField, where, 1, 10)

if err != nil {
    http.NotFound(w, r)
    return
}

fmt.Println(qr)
//As can be seen from the difference, in fact, QueryOne directly finds out the data needed, while Query queries directly return specific result sets.

//This is all for today's sharing. If you have any questions, you can leave a message or share with others.

//Project Update Address

https://github.com/durban89/t...
tag: 1.1.5

Posted by beebum on Tue, 29 Jan 2019 16:06:16 -0800