github.com/aldelo/common@v1.5.1/wrapper/mysql/querybuilder.go (about) 1 package mysql 2 3 /* 4 * Copyright 2020-2023 Aldelo, LP 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 import ( 20 "bytes" 21 util "github.com/aldelo/common" 22 ) 23 24 // QueryBuilder struct to help build sql queries (use named parameters instead of ordinal) 25 type QueryBuilder struct { 26 buf bytes.Buffer // internal buffer holder 27 output string // internal output data based on buffer string() 28 paramsNamed map[string]interface{} // internal map of named parameters 29 paramsOrdinal []interface{} // internal slice of ordinal parameters 30 } 31 32 // ClearAll will reset the query builder internal fields to init status 33 func (q *QueryBuilder) ClearAll() { 34 q.buf.Reset() 35 q.output = "" 36 q.paramsNamed = make(map[string]interface{}) 37 q.paramsOrdinal = make([]interface{}, 0) 38 } 39 40 // ClearSQL will clear the sql buffer and output only, leaving named map params intact 41 func (q *QueryBuilder) ClearSQL() { 42 q.buf.Reset() 43 q.output = "" 44 } 45 46 // ClearParams will clear the parameters map in reset state 47 func (q *QueryBuilder) ClearParams() { 48 q.paramsNamed = make(map[string]interface{}) 49 q.paramsOrdinal = make([]interface{}, 0) 50 } 51 52 // Set will append a sqlPart to the query builder buffer 53 // [ notes ] 54 // 1. Ordinal Params 55 // a) MySql, SQLite 56 // Params = ? 57 // b) SQLServer 58 // Params = @p1, @p2, ...@pN 59 // 2. Named Params 60 // a) MySql, SQLite, SQLServer 61 // Params = :xyz1, :xyz2, ...:xyzN 62 func (q *QueryBuilder) Set(sqlPart string) { 63 q.buf.WriteString(sqlPart) 64 } 65 66 // Named will add or update a named parameter and its value into named params map 67 func (q *QueryBuilder) Named(paramName string, paramValue interface{}) { 68 if util.LenTrim(paramName) == 0 { 69 return 70 } 71 72 if q.paramsNamed == nil { 73 q.paramsNamed = make(map[string]interface{}) 74 } 75 76 q.paramsNamed[paramName] = paramValue 77 } 78 79 // Ordinal will add an ordinal parameter value into ordinal params slice 80 func (q *QueryBuilder) Ordinal(ordinalParamValue interface{}) { 81 q.paramsOrdinal = append(q.paramsOrdinal, ordinalParamValue) 82 } 83 84 // Build will create the output query string based on the bytes buffer appends 85 func (q *QueryBuilder) Build() { 86 q.output = q.buf.String() 87 } 88 89 // SQL will return the built query string 90 func (q *QueryBuilder) SQL() string { 91 if util.LenTrim(q.output) == 0 { 92 q.output = q.buf.String() 93 } 94 95 return q.output 96 } 97 98 // ParamsMap returns the parameters map for use as input argument to the appropriate mysql query or exec actions 99 func (q *QueryBuilder) ParamsMap() map[string]interface{} { 100 return q.paramsNamed 101 } 102 103 // ParamsSlice returns parameters slice for use as input argument to appropriate mysql, sqlite, sqlserver as its ordinal parameters 104 func (q *QueryBuilder) ParamsSlice() []interface{} { 105 return q.paramsOrdinal 106 }