# 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"))
}
# Functions
DoubleQuote puts double quotes around a string and escapes existing double quotes.
IsReservedWord returns true if the given string is a reserved word.
IsType returns true if the given string is a sqlite type.
Quote puts single quotes around a string and escapes existing single quotes.
QuoteDeclType returns a supported type or quotes type TEXT => TEXT TIMESTAMP => TIMESTAMP some other type => "some other type".
QuoteIdentifier returns a safe version of an identifier.
QuoteIdentifiers returns a safe version of a list of identifiers, separated by commas.
ReservedWords returns a list of reserved words.
Types returns a list of sqlite types.