-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encapsulate observations query in sql client. (#1486)
- Loading branch information
Showing
11 changed files
with
230 additions
and
77 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
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,30 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
// Model objects related to the SQL database. | ||
package sqldb | ||
|
||
// Observation struct to represent a row in the observations table | ||
type Observation struct { | ||
Entity string `db:"entity"` | ||
Variable string `db:"variable"` | ||
Date string `db:"date"` | ||
Value float64 `db:"value"` | ||
Provenance string `db:"provenance"` | ||
Unit string `db:"unit"` | ||
ScalingFactor string `db:"scaling_factor"` | ||
MeasurementMethod string `db:"measurement_method"` | ||
ObservationPeriod string `db:"observation_period"` | ||
Properties string `db:"properties"` | ||
} |
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,96 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
// Queries executed by the SQLClient. | ||
package sqldb | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
const ( | ||
latestDate = "LATEST" | ||
) | ||
|
||
// GetObservations retrieves observations from SQL given a list of variables and entities and a date. | ||
func (sc *SQLClient) GetObservations(ctx context.Context, variables []string, entities []string, date string) ([]*Observation, error) { | ||
var observations []*Observation | ||
if len(variables) == 0 || len(entities) == 0 { | ||
return observations, nil | ||
} | ||
|
||
var stmt statement | ||
|
||
switch { | ||
case date != "" && date != latestDate: | ||
stmt = statement{ | ||
query: statements.getObsByVariableEntityAndDate, | ||
args: map[string]interface{}{ | ||
"variables": variables, | ||
"entities": entities, | ||
"date": date, | ||
}, | ||
} | ||
default: | ||
stmt = statement{ | ||
query: statements.getObsByVariableAndEntity, | ||
args: map[string]interface{}{ | ||
"variables": variables, | ||
"entities": entities, | ||
}, | ||
} | ||
} | ||
|
||
err := sc.queryAndCollect( | ||
ctx, | ||
stmt, | ||
&observations, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return observations, nil | ||
} | ||
|
||
func (sc *SQLClient) queryAndCollect( | ||
ctx context.Context, | ||
stmt statement, | ||
dest interface{}, | ||
) error { | ||
// Convert named query and maps of args to placeholder query and list of args. | ||
query, args, err := sqlx.Named(stmt.query, stmt.args) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Expand slice values. | ||
query, args, err = sqlx.In(query, args...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Transform query to the driver's placeholder type. | ||
query = sc.dbx.Rebind(query) | ||
|
||
return sc.dbx.SelectContext(ctx, dest, query, args...) | ||
} | ||
|
||
// statement struct includes the sql query and named args used to execute a sql query. | ||
type statement struct { | ||
query string | ||
args map[string]interface{} | ||
} |
Oops, something went wrong.