redisqlite

package module
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2021 License: Apache-2.0 Imports: 7 Imported by: 1

README

Redisqlite

A fresh implementation of the sqlite module for redis.

Truly open source, no string attached, no phone-home requirements, no licensing per instance/hour and so on.

Installation

How to build and run locally

Instructions for OSX, it should work on any other unix-like environment with Go and Redis.

You need go 1.15, install it (OSX: brew install go)

You need redis of course, install it (OSX: brew install redis)

Then run make start

On another terminal connect to redis with redis-cli and try:

$ redis-cli
127.0.0.1:6379> sqlexec "create table t(i int)"
OK
127.0.0.1:6379> sqlexec "insert into t(i) values(1),(2),(3)"
OK
127.0.0.1:6379> sqlmap 0 "select * from t"
[{"i":1},{"i":2},{"i":3}]
127.0.0.1:6379> sqlmap 1 "select * from t"
[{"i":1}]
127.0.0.1:6379>

Build and start a docker image

You need docker up and running, of course.

Use:

make image
make imagestart

It will build and start a local image called redisqlite with redis including the module.

Command Reference

  • SQLPREP (<query>|<id>):
    if the argument is not numeric, it is sql to prepare a statement. Returns a numeric <id> that identifies the statement in queryes. If the argument is numeric it is assumed to be a previously prepared statement that is then closed. You can create up to 10000 prepared statements, more will return an error. You can use SQLPREP clean_prep_cache to close all the prepared statements.

  • SQLEXEC (<statement>|<id>) [<args> ...]:
    execute a sql statement, either a prepared one or an sql statement. If the argument is numeric it is assumed to be an <id> of a prepared statement, otherwise it assumed to be an SQL statements. It returns the number of rows affected when relevant or -1, and the last id generated when relevant or -1.

  • SQLMAP <limit> (<query>|<id>) [<args> ...]:
    execute a SQL query, either a prepared one or a sql query. If the argument is number it is assumed to be an <id> of a prepared statement, otherwiser it is assumed to be an SQL qyery. It returns an array of string representing a JSON maps. It limits the number of elements in the returned arrays by <limit>; 0 means unlimited. Each object corresponds to a record, where each key corresponds to field names and each value is the corresponding field value.

  • SQLARR <limit> <query>|<number>) [<args> ...]:
    execute a SQL query, either a prepared one or a sql query. If the argument is number it is assumed to be an <id> of a prepared statement, otherwiser it is assumed to be an SQL qyery. It returns an array of string representing JSON arrays. It limits the number of elements in the returned arrays by <limit>; 0 means unlimited. Each array represents a record, with the values of the fields in the array.

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

Examples

Constants

View Source
const PREP_MAX_INDEX = 2_000_000_000
View Source
const PREP_MAX_SIZE = 10000

prepared statement cache

Variables

This section is empty.

Functions

func CreateCommandSQLARR

func CreateCommandSQLARR() rm.Command

CreateCommandSQL execute a sqlquery

func CreateCommandSQLEXEC

func CreateCommandSQLEXEC() rm.Command

CreateCommandSQLEXEC is sql execute command

func CreateCommandSQLMAP

func CreateCommandSQLMAP() rm.Command

CreateCommandSQL execute a sqlquery

func CreateCommandSQLPREP

func CreateCommandSQLPREP() rm.Command

CreateCommandSQLPREP prepares a statement

func CreateModule

func CreateModule() *rm.Module

func Exec

func Exec(stmtOrNumber string, args []interface{}) (count int64, lastId int64, err error)

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

func NextIndex(index uint32, cache map[uint32]*sql.Stmt, MAX uint32) uint32

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 Open

func Open() (err error)

Open opens the sqlite database

func Prep

func Prep(queryOrNumber string) (uint32, error)

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.

Directories

Path Synopsis
main module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL