-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit add better support to odatasql for SQL variants, as well as updating the gorm database driver to connect to postgres when configured to do so. The cloudformation installer has been updated with a new database configuration section which allows the user to choose the DB driver (postgres or sqlite), and also configure options to connect to an external database.
- Loading branch information
Showing
12 changed files
with
542 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright © 2023 Cisco Systems, Inc. and its affiliates. | ||
// All rights reserved. | ||
// | ||
// Licensed 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. | ||
|
||
package jsonsql | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type postgres struct{} | ||
|
||
var Postgres Variant = postgres{} | ||
|
||
func (postgres) JSONObject(parts []string) string { | ||
return fmt.Sprintf("JSONB_BUILD_OBJECT(%s)", strings.Join(parts, ", ")) | ||
} | ||
|
||
func (postgres) JSONArrayAggregate(value string) string { | ||
return fmt.Sprintf("JSONB_AGG(%s)", value) | ||
} | ||
|
||
func (postgres) CastToDateTime(strTime string) string { | ||
return fmt.Sprintf("(%s)::timestamptz", strTime) | ||
} | ||
|
||
func (postgres) JSONEach(source string) string { | ||
return fmt.Sprintf("JSONB_ARRAY_ELEMENTS((%s))", source) | ||
} | ||
|
||
func (postgres) JSONArray(items []string) string { | ||
return fmt.Sprintf("JSONB_BUILD_ARRAY(%s)", strings.Join(items, ", ")) | ||
} | ||
|
||
func convertJSONPathToPostgresPath(jsonPath string) string { | ||
parts := strings.Split(jsonPath, ".") | ||
newParts := []string{} | ||
for _, part := range parts { | ||
if part == "$" { | ||
continue | ||
} | ||
newParts = append(newParts, part) | ||
} | ||
return fmt.Sprintf("{%s}", strings.Join(newParts, ",")) | ||
} | ||
|
||
func (postgres) JSONExtract(source, path string) string { | ||
path = convertJSONPathToPostgresPath(path) | ||
return fmt.Sprintf("%s#>'%s'", source, path) | ||
} | ||
|
||
func (postgres) JSONExtractText(source, path string) string { | ||
path = convertJSONPathToPostgresPath(path) | ||
return fmt.Sprintf("%s#>>'%s'", source, path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright © 2023 Cisco Systems, Inc. and its affiliates. | ||
// All rights reserved. | ||
// | ||
// Licensed 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. | ||
|
||
package jsonsql | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type sqlite struct{} | ||
|
||
var SQLite Variant = sqlite{} | ||
|
||
func (sqlite) JSONObject(parts []string) string { | ||
return fmt.Sprintf("JSON_OBJECT(%s)", strings.Join(parts, ", ")) | ||
} | ||
|
||
func (sqlite) JSONArrayAggregate(value string) string { | ||
return fmt.Sprintf("JSON_GROUP_ARRAY(%s)", value) | ||
} | ||
|
||
func (sqlite) CastToDateTime(strTime string) string { | ||
return fmt.Sprintf("datetime(%s)", strTime) | ||
} | ||
|
||
func (sqlite) JSONEach(source string) string { | ||
return fmt.Sprintf("JSON_EACH(%s)", source) | ||
} | ||
|
||
func (sqlite) JSONArray(items []string) string { | ||
return fmt.Sprintf("JSON_ARRAY(%s)", strings.Join(items, ", ")) | ||
} | ||
|
||
func (sqlite) JSONExtract(source, path string) string { | ||
return fmt.Sprintf("%s->'%s'", source, path) | ||
} | ||
|
||
func (sqlite) JSONExtractText(source, path string) string { | ||
return fmt.Sprintf("%s->>'%s'", source, path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright © 2023 Cisco Systems, Inc. and its affiliates. | ||
// All rights reserved. | ||
// | ||
// Licensed 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. | ||
|
||
package jsonsql | ||
|
||
type Variant interface { | ||
JSONObject(parts []string) string | ||
JSONArrayAggregate(string) string | ||
CastToDateTime(string) string | ||
JSONEach(source string) string | ||
JSONArray(items []string) string | ||
JSONExtract(source string, path string) string | ||
JSONExtractText(source string, path string) string | ||
} |
Oops, something went wrong.