Best Venom code snippet using sql.New
mssql.go
Source:mssql.go
...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 ")...
resource_aws_waf_sql_injection_match_set.go
Source:resource_aws_waf_sql_injection_match_set.go
...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,...
equal_test.go
Source:equal_test.go
...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{...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!