Documentation ¶
Overview ¶
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #
Index ¶
- Constants
- func CreateCommandSQLARR() rm.Command
- func CreateCommandSQLEXEC() rm.Command
- func CreateCommandSQLMAP() rm.Command
- func CreateCommandSQLPREP() rm.Command
- func CreateModule() *rm.Module
- func Exec(stmtOrNumber string, args []interface{}) (count int64, lastId int64, err error)
- func NextIndex(index uint32, cache map[uint32]*sql.Stmt, MAX uint32) uint32
- func Open() (err error)
- func Prep(queryOrNumber string) (uint32, error)
- func Query(queryOrNumber string, args []interface{}, asMap bool, count int64) (res []string, err error)
Examples ¶
Constants ¶
const PREP_MAX_INDEX = 2_000_000_000
const PREP_MAX_SIZE = 10000
prepared statement cache
Variables ¶
This section is empty.
Functions ¶
func CreateCommandSQLARR ¶
CreateCommandSQL execute a sqlquery
func CreateCommandSQLEXEC ¶
CreateCommandSQLEXEC is sql execute command
func CreateCommandSQLMAP ¶
CreateCommandSQL execute a sqlquery
func CreateCommandSQLPREP ¶
CreateCommandSQLPREP prepares a statement
func CreateModule ¶
func Exec ¶
Exec execute a statement applying an array of arguments, returns the number of affected rows and the last id modified, when applicable
func NextIndex ¶ added in v1.0.4
NextIndex generate a new index for a cache avoding conflicts the value should never be 0, must be <MAX and must not be present in the cache
Example ¶
MAX := uint32(4) cache := make(map[uint32]*sql.Stmt) stat, _ := db.Prepare("select 2+2") fmt.Println(NextIndex(1, cache, MAX)) cache[2] = stat fmt.Println(NextIndex(1, cache, MAX)) fmt.Println(NextIndex(3, cache, MAX)) stat.Close()
Output: 2 3 1
func Prep ¶
Prep accepts prepares a sql statement and stores it in a table returning a number. It also accepts a number, and if it corresponds to the number returned by a previous statement, it closes the prepared statement you can store up to one 10000 statements, if you go over the limit it will return an error using the special statement "clean_prep_cache" you can close all the opened statement returnend 0 means OK, any other number is the index in the cache
Example ¶
// prepare bad, err := Prep("blabla") fmt.Println(1, bad, err) bad, err = Prep("9999") fmt.Println(1.1, bad, err) crt, err := Prep("create table tttt(k string, i int)") fmt.Println(2, crt > 0, err) _, _, err = Exec(strconv.FormatUint(uint64(crt), 10), nil) fmt.Println(3, err) sel, err := Prep("select k from tttt where i=?") fmt.Println(4, sel > 0, err) ins, err := Prep("insert into tttt values(?,?)") fmt.Println(5, ins > 0, err) // insert _, _, err = Exec(strconv.FormatUint(uint64(ins), 10), nil) fmt.Println(6, err) count, lastId, err := Exec(strconv.FormatUint(uint64(ins), 10), []interface{}{"a", 1}) fmt.Println(7, count, lastId, err) count, lastId, err = Exec(strconv.FormatUint(uint64(ins), 10), []interface{}{"b", 2}) fmt.Println(8, count, lastId, err) // select _, err = Query(strconv.FormatUint(uint64(sel), 10), nil, true, 0) fmt.Println(9, err) _, err = Query(strconv.FormatUint(uint64(sel), 10), []interface{}{"b", 2}, true, 0) fmt.Println(10, err) res, err := Query(strconv.FormatUint(uint64(sel), 10), []interface{}{2}, true, 0) fmt.Println(11, err, res) // unprep ins1, err := Prep(strconv.FormatUint(uint64(sel), 10)) fmt.Println(12, ins1, res) sel1, err := Prep(strconv.FormatUint(uint64(ins), 10)) fmt.Println(13, sel1, res) // check no prepared statement _, _, err = Exec(strconv.FormatUint(uint64(sel), 10), nil) fmt.Println(14, err) _, _, err = Exec("999", nil) fmt.Println(15, err) _, err = Query(strconv.FormatUint(uint64(ins), 10), nil, true, 0) fmt.Println(16, err) _, err = Query("999", nil, true, 0) fmt.Println(17, err)
Output: 1 0 near "blabla": syntax error 1.1 0 invalid prepared statement index 2 true <nil> 3 <nil> 4 true <nil> 5 true <nil> 6 sql: expected 2 arguments, got 0 7 1 1 <nil> 8 2 1 <nil> 9 sql: expected 1 arguments, got 0 10 sql: expected 1 arguments, got 2 11 <nil> [{"k":"b"}] 12 0 [{"k":"b"}] 13 0 [{"k":"b"}] 14 no such prepared statement index 15 no such prepared statement index 16 no such prepared statement index 17 no such prepared statement index
func Query ¶
func Query(queryOrNumber string, args []interface{}, asMap bool, count int64) (res []string, err error)
Query execute a query applying an array of args query can be either an sql string or a number if it is a number then it will execute a prepared statement idenfied by the number returned by Prep returns an array of results, either as an array of maps or as an array of arrays according the `asMap` parameters, and returns up to `count` results (0 for everything)
Types ¶
This section is empty.