quote

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

Query Quoting

This package provides functions for quoting and unquoting strings for use in SQL queries, taking into account all sqlite quoting rules and reserved keywords.

Quoting Identifiers

In order to quote an identifier, use the QuoteIdentifier function:

    import "github.com/mutablelogic/go-accessory/pkg/sqlite/quote"

    func main() {
        // Quote an identifier
        fmt.Println(quote.QuoteIdentifier("foo"))
    }

This will ensure reserved words are quoted, and that the identifier is quoted if it contains spaces or special characters. Where several identifers need to be quoted as a list separated by commas, use the QuoteIdentifiers function:

    import "github.com/mutablelogic/go-accessory/pkg/sqlite/quote"

    func main() {
        // Quote a list of identifiers
        fmt.Println(quote.QuoteIdentifiers("foo", "bar")
    }

Quoting TEXT types

In order to quote a string for use in a TEXT type, use the Quote and DoubleQuote functions:

    import "github.com/mutablelogic/go-accessory/pkg/sqlite/quote"

    func main() {
        // Quote a string 'foo'
        fmt.Println(quote.Quote("foo"))

        // Quote a string "foo"
        fmt.Println(quote.DoubleQuote("foo"))

}

Reserved Words

You can use the IsReservedWord function to test if a string is a reserved word:

    import "github.com/mutablelogic/go-accessory/pkg/sqlite/quote"

    func main() {
        // Test if a string is a reserved word, SELECT => true
        fmt.Println(quote.IsReservedWord("SELECT"))
    }

Documentation

Overview

Package quote provides SQL quoting functions for strings and identifiers

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoubleQuote

func DoubleQuote(value string) string

DoubleQuote puts double quotes around a string and escapes existing double quotes

func IsReservedWord

func IsReservedWord(k string) bool

IsReservedWord returns true if the given string is a reserved word

func IsType

func IsType(k string) bool

IsType returns true if the given string is a sqlite type

func Quote

func Quote(value string) string

Quote puts single quotes around a string and escapes existing single quotes

func QuoteDeclType

func QuoteDeclType(v string) string

QuoteDeclType returns a supported type or quotes type TEXT => TEXT TIMESTAMP => TIMESTAMP some other type => "some other type"

func QuoteIdentifier

func QuoteIdentifier(v string) string

QuoteIdentifier returns a safe version of an identifier

func QuoteIdentifiers

func QuoteIdentifiers(v ...string) string

QuoteIdentifiers returns a safe version of a list of identifiers, separated by commas

func ReservedWords

func ReservedWords() []string

ReservedWords returns a list of reserved words

func Types

func Types() []string

Types returns a list of sqlite types

Types

This section is empty.

Jump to

Keyboard shortcuts

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