github.com/dolthub/go-mysql-server@v0.18.0/memory/external_stored_procedures.go (about) 1 // Copyright 2022 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package memory 16 17 import ( 18 "fmt" 19 "strings" 20 "time" 21 22 "github.com/shopspring/decimal" 23 24 "github.com/dolthub/go-mysql-server/sql" 25 "github.com/dolthub/go-mysql-server/sql/types" 26 ) 27 28 var ( 29 externalSPSchemaInt = sql.Schema{&sql.Column{ 30 Name: "a", 31 Type: types.Int64, 32 }} 33 externalSPSchemaUint = sql.Schema{&sql.Column{ 34 Name: "a", 35 Type: types.Uint64, 36 }} 37 externalSPSchemaText = sql.Schema{&sql.Column{ 38 Name: "a", 39 Type: types.LongText, 40 }} 41 ExternalStoredProcedures = []sql.ExternalStoredProcedureDetails{ 42 { 43 Name: "memory_inout_add", 44 Schema: nil, 45 Function: inout_add, 46 }, 47 { 48 Name: "memory_inout_set_unitialized", 49 Schema: nil, 50 Function: inout_set_unitialized, 51 }, 52 { 53 Name: "memory_overloaded_mult", 54 Schema: externalSPSchemaInt, 55 Function: overloaded_mult1, 56 }, 57 { 58 Name: "memory_overloaded_mult", 59 Schema: externalSPSchemaInt, 60 Function: overloaded_mult2, 61 }, 62 { 63 Name: "memory_overloaded_mult", 64 Schema: externalSPSchemaInt, 65 Function: overloaded_mult3, 66 }, 67 { 68 Name: "memory_overloaded_type_test", 69 Schema: externalSPSchemaInt, 70 Function: overloaded_type_test1, 71 }, 72 { 73 Name: "memory_overloaded_type_test", 74 Schema: externalSPSchemaText, 75 Function: overloaded_type_test2, 76 }, 77 { 78 Name: "memory_type_test3", 79 Schema: externalSPSchemaUint, 80 Function: type_test3, 81 }, 82 { 83 Name: "memory_inout_bool_byte", 84 Schema: nil, 85 Function: inout_bool_byte, 86 }, 87 { 88 Name: "memory_error_table_not_found", 89 Schema: nil, 90 Function: error_table_not_found, 91 }, 92 { 93 Name: "memory_variadic_add", 94 Schema: externalSPSchemaInt, 95 Function: variadic_add, 96 }, 97 { 98 Name: "memory_variadic_byte_slice", 99 Schema: externalSPSchemaText, 100 Function: variadic_byte_slice, 101 }, 102 { 103 Name: "memory_variadic_overload", 104 Schema: externalSPSchemaText, 105 Function: variadic_overload1, 106 }, 107 { 108 Name: "memory_variadic_overload", 109 Schema: externalSPSchemaText, 110 Function: variadic_overload2, 111 }, 112 { 113 Name: "memory_inout_add_readonly", 114 Schema: externalSPSchemaInt, 115 Function: variadic_add, 116 ReadOnly: true, 117 }, 118 { 119 Name: "memory_inout_add_readwrite", 120 Schema: externalSPSchemaInt, 121 Function: variadic_add, 122 ReadOnly: false, 123 }, 124 { 125 Name: "memory_admin_only", 126 Schema: externalSPSchemaInt, 127 Function: variadic_add, 128 ReadOnly: false, 129 AdminOnly: true, 130 }, 131 } 132 ) 133 134 func inout_add(_ *sql.Context, a *int64, b int64) (sql.RowIter, error) { 135 *a = *a + b 136 return sql.RowsToRowIter(), nil 137 } 138 139 func inout_set_unitialized(_ *sql.Context, a *int, b *uint, c *string, d *int) (sql.RowIter, error) { 140 *a = 5 141 *b = 5 142 *c = "5" 143 // We intentionally do not set `d` to verify that it is given the zero value 144 return nil, nil 145 } 146 147 func overloaded_mult1(_ *sql.Context, a int8) (sql.RowIter, error) { 148 return sql.RowsToRowIter(sql.Row{int64(a)}), nil 149 } 150 func overloaded_mult2(_ *sql.Context, a int16, b int32) (sql.RowIter, error) { 151 return sql.RowsToRowIter(sql.Row{int64(a) * int64(b)}), nil 152 } 153 func overloaded_mult3(_ *sql.Context, a int8, b int32, c int64) (sql.RowIter, error) { 154 return sql.RowsToRowIter(sql.Row{int64(a) * int64(b) * c}), nil 155 } 156 157 func overloaded_type_test1( 158 _ *sql.Context, 159 aa int8, ab int16, ac int, ad int32, ae int64, af float32, ag float64, 160 ba *int8, bb *int16, bc *int, bd *int32, be *int64, bf *float32, bg *float64, 161 ) (sql.RowIter, error) { 162 return sql.RowsToRowIter(sql.Row{ 163 int64(aa) + int64(ab) + int64(ac) + int64(ad) + int64(ae) + int64(af) + int64(ag) + 164 int64(*ba) + int64(*bb) + int64(*bc) + int64(*bd) + int64(*be) + int64(*bf) + int64(*bg), 165 }), nil 166 } 167 func overloaded_type_test2( 168 _ *sql.Context, 169 aa bool, ab string, ac []byte, ad time.Time, ae decimal.Decimal, 170 ba *bool, bb *string, bc *[]byte, bd *time.Time, be *decimal.Decimal, 171 ) (sql.RowIter, error) { 172 return sql.RowsToRowIter(sql.Row{ 173 fmt.Sprintf(`aa:%v,ba:%v,ab:"%s",bb:"%s",ac:%v,bc:%v,ad:%s,bd:%s,ae:%s,be:%s`, 174 aa, *ba, ab, *bb, ac, *bc, ad.Format("2006-01-02"), (*bd).Format("2006-01-02"), ae.String(), (*be).String()), 175 }), nil 176 } 177 178 func type_test3( 179 _ *sql.Context, 180 aa uint8, ab uint16, ac uint, ad uint32, ae uint64, af float32, ag float64, 181 ba *uint8, bb *uint16, bc *uint, bd *uint32, be *uint64, bf *float32, bg *float64, 182 ) (sql.RowIter, error) { 183 return sql.RowsToRowIter(sql.Row{ 184 uint64(aa) + uint64(ab) + uint64(ac) + uint64(ad) + uint64(ae) + uint64(af) + uint64(ag) + 185 uint64(*ba) + uint64(*bb) + uint64(*bc) + uint64(*bd) + uint64(*be) + uint64(*bf) + uint64(*bg), 186 }), nil 187 } 188 189 func inout_bool_byte(_ *sql.Context, a bool, b *bool, c []byte, d *[]byte) (sql.RowIter, error) { 190 a = !a 191 *b = !*b 192 for i := range c { 193 c[i] = c[i] + 1 194 } 195 for i := range *d { 196 (*d)[i] = (*d)[i] + 1 197 } 198 return nil, nil 199 } 200 201 func error_table_not_found(_ *sql.Context) (sql.RowIter, error) { 202 return nil, sql.ErrTableNotFound.New("non_existent_table") 203 } 204 205 func variadic_add(_ *sql.Context, vals ...int) (sql.RowIter, error) { 206 sum := int64(0) 207 for _, val := range vals { 208 sum += int64(val) 209 } 210 return sql.RowsToRowIter(sql.Row{sum}), nil 211 } 212 213 func variadic_byte_slice(_ *sql.Context, vals ...[]byte) (sql.RowIter, error) { 214 sb := strings.Builder{} 215 for _, val := range vals { 216 sb.Write(val) 217 } 218 return sql.RowsToRowIter(sql.Row{sb.String()}), nil 219 } 220 221 func variadic_overload1(_ *sql.Context, a string, b string) (sql.RowIter, error) { 222 return sql.RowsToRowIter(sql.Row{fmt.Sprintf("%s-%s", a, b)}), nil 223 } 224 225 func variadic_overload2(_ *sql.Context, a string, b string, vals ...uint8) (sql.RowIter, error) { 226 return sql.RowsToRowIter(sql.Row{fmt.Sprintf("%s,%s,%v", a, b, vals)}), nil 227 }