How to use New method of sql Package

Best Venom code snippet using sql.New

mssql.go

Source:mssql.go Github

copy

Full Screen

...93 for index, s := range pts {94 if v, ok := args[s[1]]; ok { //存在参数95 result[index] = v96 } else {97 return sql, nil, errors.New("缺少参数[" + s[0] + "]的值")98 }99 }100 return cp.ReplaceAllString(sql, "?"), result, nil101 }102 return sql, nil, nil103}104//RowsPage 分页查询105func (m *mssql) QueryWithPage(sql string, page *db.PageObj, args ...interface{}) db.QueryResult {106 if page == nil {107 return m.QueryRows(sql, args...)108 }109 sql = strings.ReplaceAll(sql, "?", "@")110 stmt, err := sqlparser.Parse(sql)111 if err != nil {112 return db.ErrQueryResult(fmt.Errorf("sql语句解析错误:%w", err), sql, args)113 }114 selectColumn := ""115 from := ""116 where := ""117 orderBy := ""118 switch stmt := stmt.(type) {119 case *sqlparser.Select:120 buf := sqlparser.NewTrackedBuffer(nil)121 stmt.SelectExprs.Format(buf)122 selectColumn = buf.String()123 buf.Reset()124 stmt.From.Format(buf)125 from = buf.String()126 buf.Reset()127 stmt.Where.Format(buf)128 where = buf.String()129 buf.Reset()130 stmt.OrderBy.Format(buf)131 orderBy = buf.String()132 default:133 return db.ErrQueryResult(errors.New("只支持select语句"), sql, args)134 }135 where = strings.ReplaceAll(where, "@", "?")136 sqlBuilder := strings.Builder{}137 sqlBuilder.WriteString("SELECT count(0) num FROM ")138 sqlBuilder.WriteString(from)139 sqlBuilder.WriteString(where)140 result := m.QueryRows(sqlBuilder.String(), args...)141 count := conv.ToInt64(result.Get("num"))142 page.SetTotal(count)143 currentpage := 0144 if page.Page-1 > 0 {145 currentpage = page.Page - 1146 }147 if count < 1 {148 return db.NewQueryResult(nil, sql, args)149 }150 sqlBuilder.Reset()151 sqlBuilder.WriteString("SELECT TOP ")152 sqlBuilder.WriteString(conv.ToString(page.Rows))153 sqlBuilder.WriteString(" * FROM (SELECT ROW_NUMBER() OVER (")154 sqlBuilder.WriteString(orderBy)155 sqlBuilder.WriteString(") as RowNumber,")156 sqlBuilder.WriteString(selectColumn)157 sqlBuilder.WriteString(" FROM ")158 sqlBuilder.WriteString(from)159 sqlBuilder.WriteString(where)160 sqlBuilder.WriteString(") as tmp WHERE RowNumber > ")161 sqlBuilder.WriteString(conv.ToString(page.Rows * currentpage))162 sqlBuilder.WriteString(" ORDER BY RowNumber ASC ")163 sql = sqlBuilder.String()164 return m.QueryRows(sql, args...)165}166//Exec 执行一条SQL167//@param sql string SQL168//@param args... interface{} SQL参数169func (m *mssql) Exec(sql string, args ...interface{}) db.ExecResult {170 i := 0171 sql = regexp.MustCompile("(\\?)").ReplaceAllStringFunc(sql, func(s string) string {172 i++173 return fmt.Sprintf("@p%d", i)174 })175 if len(args) < i {176 return db.ErrExecResult(fmt.Errorf("参数缺少,目标参数%d个,实际参数%d个", i, len(args)), sql, args)177 }178 return m.Conn.Exec(sql, args...)179}180//Transaction 事务处理181//@param t TransactionFunc 事务处理函数182func (m *mssql) Transaction(t db.TransactionFunc, option ...*db.TxOption) error {183 f := func(tx db.TxSQL) error {184 return t(&mssqlTx{185 TxSQL: tx,186 db: m,187 })188 }189 return m.Conn.Transaction(f, option...)190}191type mssqlTx struct {192 db.TxSQL193 db *mssql194}195//Transaction 事务处理196//@param t TransactionFunc 事务处理函数197func (m *mssqlTx) Transaction(t db.TransactionFunc, options ...*db.TxOption) error {198 if t != nil {199 if len(options) > 0 && options[0] != nil && options[0].New {200 options[0].New = false201 //要求新事物返回新事务202 return m.db.Transaction(t, options...)203 }204 //本身就是事务了,直接调用即可205 return t(m)206 }207 return nil208}209//RowsCallbackResult 查询多条数据,结果以回调函数处理210//211//@param sql string SQL212//213//@param callback func(*sql.Rows) 回调函数指针214//215//@param args... interface{} SQL参数216func (m *mssqlTx) QueryRows(sql string, args ...interface{}) db.QueryResult {217 i := 0218 sql = regexp.MustCompile("(\\?)").ReplaceAllStringFunc(sql, func(s string) string {219 i++220 return fmt.Sprintf("@p%d", i)221 })222 if len(args) < i {223 return db.ErrQueryResult(fmt.Errorf("参数缺少,目标参数%d个,实际参数%d个", i, len(args)), sql, args)224 }225 return m.TxSQL.QueryRows(sql, args...)226}227//Row 查询单条语句,返回结果228//@param sql string SQL229//@param args... interface{} SQL参数230func (m *mssqlTx) QueryRow(sql string, args ...interface{}) db.QueryResult {231 if ok, _ := regexp.MatchString("(?i)(.*?) TOP (.*?)\\s?(.*)?", sql); ok {232 sql = regexp.MustCompile("(?i)(.*?) TOP (.*?)\\s?(.*)?").ReplaceAllString(sql, "$1")233 } else {234 sql = strings.Replace(sql, "SELECT ", "SELECT TOP 1 ", 1)235 }236 return m.QueryRows(sql, args...)237}238//ParseSQL 解析SQL239//@param sql string SQL240//@param args map[string]interface{} 参数映射241func (m *mssqlTx) ParseSQL(sql string, args map[string]interface{}) (string, []interface{}, error) {242 cp, err := regexp.Compile("@([^\\s|,|\\)]*)")243 if err != nil {244 return sql, nil, nil245 }246 pts := cp.FindAllStringSubmatch(sql, -1)247 if pts != nil && args != nil { //匹配到数据248 result := make([]interface{}, len(pts))249 for index, s := range pts {250 if v, ok := args[s[1]]; ok { //存在参数251 result[index] = v252 } else {253 return sql, nil, errors.New("缺少参数[" + s[0] + "]的值")254 }255 }256 return cp.ReplaceAllString(sql, "?"), result, nil257 }258 return sql, nil, nil259}260//格式化表名称,不做处理直接返回261func (m *mssqlTx) Table(tbname string) string {262 return tbname263}264//RowsPage 分页查询265func (m *mssqlTx) QueryWithPage(sql string, page *db.PageObj, args ...interface{}) db.QueryResult {266 if page == nil {267 return m.QueryRows(sql, args...)268 }269 stmt, err := sqlparser.Parse(sql)270 if err != nil {271 return db.ErrQueryResult(fmt.Errorf("sql语句解析错误:%w", err), sql, args)272 }273 selectColumn := ""274 from := ""275 where := ""276 orderBy := ""277 switch stmt := stmt.(type) {278 case *sqlparser.Select:279 buf := sqlparser.NewTrackedBuffer(nil)280 stmt.SelectExprs.Format(buf)281 selectColumn = buf.String()282 buf.Reset()283 stmt.From.Format(buf)284 from = buf.String()285 buf.Reset()286 stmt.Where.Format(buf)287 where = buf.String()288 buf.Reset()289 stmt.OrderBy.Format(buf)290 orderBy = buf.String()291 default:292 return db.ErrQueryResult(errors.New("只支持select语句"), sql, args)293 }294 sqlBuilder := strings.Builder{}295 sqlBuilder.WriteString("SELECT count(0) num FROM ")296 sqlBuilder.WriteString(from)297 sqlBuilder.WriteString(where)298 result := m.QueryRows(sqlBuilder.String(), args...)299 count := conv.ToInt64(result.Get("num"))300 page.SetTotal(count)301 currentpage := 0302 if page.Page-1 > 0 {303 currentpage = page.Page - 1304 }305 if count < 1 {306 return db.NewQueryResult(nil, sql, args)307 }308 sqlBuilder.Reset()309 sqlBuilder.WriteString("SELECT TOP ")310 sqlBuilder.WriteString(conv.ToString(page.Rows))311 sqlBuilder.WriteString(" * FROM (SELECT ROW_NUMBER() OVER (")312 sqlBuilder.WriteString(orderBy)313 sqlBuilder.WriteString(") as RowNumber,")314 sqlBuilder.WriteString(selectColumn)315 sqlBuilder.WriteString(" FROM ")316 sqlBuilder.WriteString(from)317 sqlBuilder.WriteString(where)318 sqlBuilder.WriteString(") as tmp WHERE RowNumber > ")319 sqlBuilder.WriteString(conv.ToString(page.Rows * currentpage))320 sqlBuilder.WriteString(" ORDER BY RowNumber ASC ")...

Full Screen

Full Screen

resource_aws_waf_sql_injection_match_set.go

Source:resource_aws_waf_sql_injection_match_set.go Github

copy

Full Screen

...16 Schema: map[string]*schema.Schema{17 "name": &schema.Schema{18 Type: schema.TypeString,19 Required: true,20 ForceNew: true,21 },22 "sql_injection_match_tuples": &schema.Schema{23 Type: schema.TypeSet,24 Optional: true,25 Elem: &schema.Resource{26 Schema: map[string]*schema.Schema{27 "field_to_match": {28 Type: schema.TypeSet,29 Required: true,30 MaxItems: 1,31 Elem: &schema.Resource{32 Schema: map[string]*schema.Schema{33 "data": {34 Type: schema.TypeString,...

Full Screen

Full Screen

equal_test.go

Source:equal_test.go Github

copy

Full Screen

...14 tw.Runp("fuzz identity", testEqualFuzzIdentity)15 tw.Runp("normal form", testEqualNormalForm)16}17func testEqualFuzzIdentity(tw *testutil.T) {18 g := sqltest.NewGenerator(tw)19 for i := 0; i < 1000; i++ {20 sql := g.Gen()21 if !sqlEqual(sql, sql) {22 tw.Logf("sql: %#v", sql)23 tw.Error()24 }25 }26}27func testEqualNormalForm(tw *testutil.T) {28 type normalFormTestCase struct {29 in sqlgen.SQL30 normal bool31 }32 tests := []normalFormTestCase{...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Venom automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful