merge v0.23.0-rc changes
This commit is contained in:
@@ -5,31 +5,31 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// JsonEach returns JSON_EACH SQLite string expression with
|
||||
// JSONEach returns JSON_EACH SQLite string expression with
|
||||
// some normalizations for non-json columns.
|
||||
func JsonEach(column string) string {
|
||||
func JSONEach(column string) string {
|
||||
return fmt.Sprintf(
|
||||
`json_each(CASE WHEN json_valid([[%s]]) THEN [[%s]] ELSE json_array([[%s]]) END)`,
|
||||
column, column, column,
|
||||
)
|
||||
}
|
||||
|
||||
// JsonArrayLength returns JSON_ARRAY_LENGTH SQLite string expression
|
||||
// JSONArrayLength returns JSON_ARRAY_LENGTH SQLite string expression
|
||||
// with some normalizations for non-json columns.
|
||||
//
|
||||
// It works with both json and non-json column values.
|
||||
//
|
||||
// Returns 0 for empty string or NULL column values.
|
||||
func JsonArrayLength(column string) string {
|
||||
func JSONArrayLength(column string) string {
|
||||
return fmt.Sprintf(
|
||||
`json_array_length(CASE WHEN json_valid([[%s]]) THEN [[%s]] ELSE (CASE WHEN [[%s]] = '' OR [[%s]] IS NULL THEN json_array() ELSE json_array([[%s]]) END) END)`,
|
||||
column, column, column, column, column,
|
||||
)
|
||||
}
|
||||
|
||||
// JsonExtract returns a JSON_EXTRACT SQLite string expression with
|
||||
// JSONExtract returns a JSON_EXTRACT SQLite string expression with
|
||||
// some normalizations for non-json columns.
|
||||
func JsonExtract(column string, path string) string {
|
||||
func JSONExtract(column string, path string) string {
|
||||
// prefix the path with dot if it is not starting with array notation
|
||||
if path != "" && !strings.HasPrefix(path, "[") {
|
||||
path = "." + path
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tools/dbutils"
|
||||
)
|
||||
|
||||
func TestJsonEach(t *testing.T) {
|
||||
result := dbutils.JsonEach("a.b")
|
||||
func TestJSONEach(t *testing.T) {
|
||||
result := dbutils.JSONEach("a.b")
|
||||
|
||||
expected := "json_each(CASE WHEN json_valid([[a.b]]) THEN [[a.b]] ELSE json_array([[a.b]]) END)"
|
||||
|
||||
@@ -16,8 +16,8 @@ func TestJsonEach(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestJsonArrayLength(t *testing.T) {
|
||||
result := dbutils.JsonArrayLength("a.b")
|
||||
func TestJSONArrayLength(t *testing.T) {
|
||||
result := dbutils.JSONArrayLength("a.b")
|
||||
|
||||
expected := "json_array_length(CASE WHEN json_valid([[a.b]]) THEN [[a.b]] ELSE (CASE WHEN [[a.b]] = '' OR [[a.b]] IS NULL THEN json_array() ELSE json_array([[a.b]]) END) END)"
|
||||
|
||||
@@ -26,7 +26,7 @@ func TestJsonArrayLength(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestJsonExtract(t *testing.T) {
|
||||
func TestJSONExtract(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
name string
|
||||
column string
|
||||
@@ -55,12 +55,11 @@ func TestJsonExtract(t *testing.T) {
|
||||
|
||||
for _, s := range scenarios {
|
||||
t.Run(s.name, func(t *testing.T) {
|
||||
result := dbutils.JsonExtract(s.column, s.path)
|
||||
result := dbutils.JSONExtract(s.column, s.path)
|
||||
|
||||
if result != s.expected {
|
||||
t.Fatalf("Expected\n%v\ngot\n%v", s.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user