GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — sqlparser/parsed_query.go at master · supercxx/sqlparser Expand file tree/ Copy pathparsed_query.goMore file actions 180 lines (166 loc) · 3.93 KB / Copy pathparsed_query.goFile metadata and controls180 lines (166 loc) · 3.93 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 // Copyright 2012, Google Inc. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package sqlparser import ( "bytes" "encoding/json" "errors" "fmt" "github.com/xwb1989/sqlparser/dependency/sqltypes" ) type bindLocation struct { offset, length int } type ParsedQuery struct { Query string bindLocations []bindLocation } type EncoderFunc func(value interface{}) ([]byte, error) func (pq *ParsedQuery) GenerateQuery(bindVariables map[string]interface{}) ([]byte, error) { if len(pq.bindLocations) == 0 { return []byte(pq.Query), nil } buf := bytes.NewBuffer(make([]byte, 0, len(pq.Query))) current := 0 for _, loc := range pq.bindLocations { buf.WriteString(pq.Query[current:loc.offset]) name := pq.Query[loc.offset : loc.offset+loc.length] supplied, _, err := FetchBindVar(name, bindVariables) if err != nil { return nil, err } if err := EncodeValue(buf, supplied); err != nil { return nil, err } current = loc.offset + loc.length } buf.WriteString(pq.Query[current:]) return buf.Bytes(), nil } func (pq *ParsedQuery) MarshalJSON() ([]byte, error) { return json.Marshal(pq.Query) } func EncodeValue(buf *bytes.Buffer, value interface{}) error { switch bindVal := value.(type) { case nil: buf.WriteString("null") case []sqltypes.Value: for i := 0; i < len(bindVal); i++ { if i != 0 { buf.WriteString(", ") } if err := EncodeValue(buf, bindVal[i]); err != nil { return err } } case [][]sqltypes.Value: for i := 0; i < len(bindVal); i++ { if i != 0 { buf.WriteString(", ") } buf.WriteByte('(') if err := EncodeValue(buf, bindVal[i]); err != nil { return err } buf.WriteByte(')') } case []interface{}: buf.WriteByte('(') for i, v := range bindVal { if i != 0 { buf.WriteString(", ") } if err := EncodeValue(buf, v); err != nil { return err } } buf.WriteByte(')') case TupleEqualityList: if err := bindVal.Encode(buf); err != nil { return err } default: v, err := sqltypes.BuildValue(bindVal) if err != nil { return err } v.EncodeSql(buf) } return nil } type TupleEqualityList struct { Columns []string Rows [][]sqltypes.Value } func (tpl *TupleEqualityList) Encode(buf *bytes.Buffer) error { if len(tpl.Rows) == 0 { return errors.New("cannot encode with 0 rows") } if len(tpl.Columns) == 1 { return tpl.encodeAsIN(buf) } return tpl.encodeAsEquality(buf) } func (tpl *TupleEqualityList) encodeAsIN(buf *bytes.Buffer) error { buf.WriteString(tpl.Columns[0]) buf.WriteString(" in (") for i, r := range tpl.Rows { if len(r) != 1 { return errors.New("values don't match column count") } if i != 0 { buf.WriteString(", ") } if err := EncodeValue(buf, r); err != nil { return err } } buf.WriteByte(')') return nil } func (tpl *TupleEqualityList) encodeAsEquality(buf *bytes.Buffer) error { for i, r := range tpl.Rows { if i != 0 { buf.WriteString(" or ") } buf.WriteString("(") for j, c := range tpl.Columns { if j != 0 { buf.WriteString(" and ") } buf.WriteString(c) buf.WriteString(" = ") if err := EncodeValue(buf, r[j]); err != nil { return err } } buf.WriteByte(')') } return nil } func FetchBindVar(name string, bindVariables map[string]interface{}) (val interface{}, isList bool, err error) { name = name[1:] if name[0] == ':' { name = name[1:] isList = true } supplied, ok := bindVariables[name] if !ok { return nil, false, fmt.Errorf("missing bind var %s", name) } list, gotList := supplied.([]interface{}) if isList { if !gotList { return nil, false, fmt.Errorf("unexpected list arg type %T for key %s", supplied, name) } if len(list) == 0 { return nil, false, fmt.Errorf("empty list supplied for %s", name) } return list, true, nil } if gotList { return nil, false, fmt.Errorf("unexpected arg type %T for key %s", supplied, name) } return supplied, false, nil } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |