Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support setting default values ​​for header #4090

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions contrib/drivers/mssql/mssql_do_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (d *Driver) parseSql(toBeCommittedSql string) (string, error) {

func (d *Driver) handleSelectSqlReplacement(toBeCommittedSql string) (newSql string, err error) {
// SELECT * FROM USER WHERE ID=1 LIMIT 1
match, err := gregex.MatchString(`^SELECT(.+?)LIMIT\s+1$`, toBeCommittedSql)
match, err := gregex.MatchString(`^SELECT([\s\S]+?)LIMIT\s+1$`, toBeCommittedSql)
if err != nil {
return "", err
}
Expand All @@ -98,7 +98,7 @@ func (d *Driver) handleSelectSqlReplacement(toBeCommittedSql string) (newSql str
}

// SELECT * FROM USER WHERE AGE>18 ORDER BY ID DESC LIMIT 100, 200
pattern := `(?i)SELECT(.+?)(ORDER BY.+?)?\s*LIMIT\s*(\d+)(?:\s*,\s*(\d+))?`
pattern := `(?i)SELECT([\s\S]+?)(ORDER BY[\s\S]+?)?\s*LIMIT\s*(\d+)(?:\s*,\s*(\d+))?`
if !gregex.IsMatchString(pattern, toBeCommittedSql) {
return toBeCommittedSql, nil
}
Expand Down
33 changes: 29 additions & 4 deletions contrib/drivers/mssql/mssql_do_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,21 @@ func TestDriver_handleSelectSqlReplacement(t *testing.T) {
d := &Driver{}

// LIMIT 1
inputSql := "SELECT * FROM User WHERE ID = 1 LIMIT 1"
expectedSql := "SELECT TOP 1 * FROM User WHERE ID = 1"
inputSql := `SELECT * FROM User WHERE ID = 1 LIMIT 1`
expectedSql := `SELECT TOP 1 * FROM User WHERE ID = 1`
resultSql, err := d.handleSelectSqlReplacement(inputSql)
t.AssertNil(err)
t.Assert(resultSql, expectedSql)

// MultiLine LIMIT 1
inputSql = `SELECT *
FROM User WHERE ID = 1 LIMIT 1`
expectedSql = `SELECT TOP 1 *
FROM User WHERE ID = 1`
resultSql, err = d.handleSelectSqlReplacement(inputSql)
t.AssertNil(err)
t.Assert(resultSql, expectedSql)

// LIMIT query with offset and number of rows
inputSql = "SELECT * FROM User ORDER BY ID DESC LIMIT 100, 200"
expectedSql = "SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY ID DESC) as ROW_NUMBER__, * FROM (SELECT * FROM User) as InnerQuery ) as TMP_ WHERE TMP_.ROW_NUMBER__ > 100 AND TMP_.ROW_NUMBER__ <= 300"
Expand Down Expand Up @@ -100,8 +109,10 @@ func TestDriver_handleSelectSqlReplacement(t *testing.T) {
t.Assert(resultSql, expectedSql)

// Complex query with ORDER BY and LIMIT
inputSql = "SELECT name, age FROM User WHERE age > 18 ORDER BY age ASC LIMIT 10, 5"
expectedSql = "SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY age ASC) as ROW_NUMBER__, * FROM (SELECT name, age FROM User WHERE age > 18) as InnerQuery ) as TMP_ WHERE TMP_.ROW_NUMBER__ > 10 AND TMP_.ROW_NUMBER__ <= 15"
inputSql = `SELECT name,
age FROM User WHERE age > 18 ORDER BY age ASC LIMIT 10, 5`
expectedSql = `SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY age ASC) as ROW_NUMBER__, * FROM (SELECT name,
age FROM User WHERE age > 18) as InnerQuery ) as TMP_ WHERE TMP_.ROW_NUMBER__ > 10 AND TMP_.ROW_NUMBER__ <= 15`
resultSql, err = d.handleSelectSqlReplacement(inputSql)
t.AssertNil(err)
t.Assert(resultSql, expectedSql)
Expand All @@ -127,5 +138,19 @@ func TestDriver_handleSelectSqlReplacement(t *testing.T) {
t.AssertNil(err)
t.Assert(resultSql, expectedSql)

// MultiLine Queries with comment and complex ORDER BY and LIMIT
inputSql = `SELECT name, -- 名称
age -- 年龄
FROM User WHERE age > 18
ORDER BY age DESC,
name ASC LIMIT 20, 10`
expectedSql = `SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY age DESC,
name ASC) as ROW_NUMBER__, * FROM (SELECT name, -- 名称
age -- 年龄
FROM User WHERE age > 18) as InnerQuery ) as TMP_ WHERE TMP_.ROW_NUMBER__ > 20 AND TMP_.ROW_NUMBER__ <= 30`
resultSql, err = d.handleSelectSqlReplacement(inputSql)
t.AssertNil(err)
t.Assert(resultSql, expectedSql)

})
}
11 changes: 11 additions & 0 deletions contrib/drivers/mssql/mssql_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"github.com/gogf/gf/v2/text/gstr"
)

var (
version string
)

// Open creates and returns an underlying sql.DB object for mssql.
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
source, err := configNodeToSource(config)
Expand All @@ -30,6 +34,13 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
)
return nil, err
}
if err = db.QueryRow("SELECT @@VERSION").Scan(&version); err != nil {
err = gerror.WrapCodef(
gcode.CodeDbOperationError, err,
`Error querying version: SELECT @@VERSION`,
)
return nil, err
}
return
}

Expand Down
10 changes: 8 additions & 2 deletions net/ghttp/ghttp_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,14 @@ func (r *Request) IsExited() bool {
}

// GetHeader retrieves and returns the header value with given `key`.
func (r *Request) GetHeader(key string) string {
return r.Header.Get(key)
func (r *Request) GetHeader(key string, def ...string) string {
header := r.Header.Get(key)
if header == "" {
if len(def) > 0 {
return def[0]
}
}
return header
}

// GetHost returns current request host name, which might be a domain or an IP without port.
Expand Down
Loading