-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move SQL driver conversion helpers to raftlogs package
- Loading branch information
1 parent
e26aa2f
commit 9cd12a1
Showing
5 changed files
with
323 additions
and
224 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
Copyright 2023 Avi Zimmerman <[email protected]> | ||
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 raftlogs | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"fmt" | ||
"time" | ||
|
||
v1 "github.com/webmeshproj/api/v1" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
// ValuesToNamedValues converts a slice of values to a slice of NamedValues. | ||
func ValuesToNamedValues(args []driver.Value) []driver.NamedValue { | ||
named := make([]driver.NamedValue, len(args)) | ||
for i, arg := range args { | ||
named[i] = driver.NamedValue{ | ||
Ordinal: i + 1, | ||
Value: arg, | ||
} | ||
} | ||
return named | ||
} | ||
|
||
// NamedValuesToSQLParameters converts a slice of NamedValues to a slice of SQLParameters. | ||
func NamedValuesToSQLParameters(values []driver.NamedValue) ([]*v1.SQLParameter, error) { | ||
params := make([]*v1.SQLParameter, len(values)) | ||
for i, argz := range values { | ||
arg := argz | ||
sqlParam := &v1.SQLParameter{Name: arg.Name} | ||
switch v := arg.Value.(type) { | ||
case nil: | ||
sqlParam.Type = v1.SQLParameterType_SQL_PARAM_NULL | ||
case bool: | ||
sqlParam.Type = v1.SQLParameterType_SQL_PARAM_BOOL | ||
sqlParam.Bool = v | ||
case int: | ||
sqlParam.Type = v1.SQLParameterType_SQL_PARAM_INT64 | ||
sqlParam.Int64 = int64(v) | ||
case int64: | ||
sqlParam.Type = v1.SQLParameterType_SQL_PARAM_INT64 | ||
sqlParam.Int64 = v | ||
case float64: | ||
sqlParam.Type = v1.SQLParameterType_SQL_PARAM_DOUBLE | ||
sqlParam.Double = v | ||
case string: | ||
sqlParam.Type = v1.SQLParameterType_SQL_PARAM_STRING | ||
sqlParam.Str = v | ||
case []byte: | ||
sqlParam.Type = v1.SQLParameterType_SQL_PARAM_BYTES | ||
sqlParam.Bytes = v | ||
case time.Time: | ||
sqlParam.Type = v1.SQLParameterType_SQL_PARAM_TIME | ||
sqlParam.Time = timestamppb.New(v) | ||
default: | ||
return nil, fmt.Errorf("unsupported parameter type: %T", v) | ||
} | ||
params[i] = sqlParam | ||
} | ||
return params, nil | ||
} | ||
|
||
// SQLParametersToNamedArgs converts a slice of SQLParameters to a slice of NamedArgs. | ||
func SQLParametersToNamedArgs(params []*v1.SQLParameter) ([]any, error) { | ||
if params == nil { | ||
return nil, nil | ||
} | ||
values := make([]any, len(params)) | ||
for idx, param := range params { | ||
i := idx | ||
switch param.GetType() { | ||
case v1.SQLParameterType_SQL_PARAM_INT64: | ||
values[i] = sql.Named(param.GetName(), param.Int64) | ||
case v1.SQLParameterType_SQL_PARAM_DOUBLE: | ||
values[i] = sql.Named(param.GetName(), param.Double) | ||
case v1.SQLParameterType_SQL_PARAM_BOOL: | ||
values[i] = sql.Named(param.GetName(), param.Bool) | ||
case v1.SQLParameterType_SQL_PARAM_BYTES: | ||
values[i] = sql.Named(param.GetName(), param.Bytes) | ||
case v1.SQLParameterType_SQL_PARAM_STRING: | ||
values[i] = sql.Named(param.GetName(), param.Str) | ||
case v1.SQLParameterType_SQL_PARAM_TIME: | ||
values[i] = sql.Named(param.GetName(), param.Time.AsTime()) | ||
case v1.SQLParameterType_SQL_PARAM_NULL: | ||
values[i] = sql.Named(param.GetName(), nil) | ||
default: | ||
return nil, fmt.Errorf("unsupported type: %T", param.GetType()) | ||
} | ||
} | ||
return values, nil | ||
} |
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,89 @@ | ||
/* | ||
Copyright 2023 Avi Zimmerman <[email protected]> | ||
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 raftlogs | ||
|
||
import ( | ||
"database/sql/driver" | ||
"fmt" | ||
"io" | ||
|
||
v1 "github.com/webmeshproj/api/v1" | ||
) | ||
|
||
// NewResult returns a new driver.Result from the given RaftApplyResponse. | ||
func NewResult(res *v1.SQLExecResult) driver.Result { | ||
return &execResult{res} | ||
} | ||
|
||
type execResult struct { | ||
res *v1.SQLExecResult | ||
} | ||
|
||
// LastInsertId returns the database's auto-generated ID | ||
// after, for example, an INSERT into a table with primary | ||
// key. | ||
func (r *execResult) LastInsertId() (int64, error) { | ||
return r.res.GetLastInsertId(), nil | ||
} | ||
|
||
// RowsAffected returns the number of rows affected by the | ||
// query. | ||
func (r *execResult) RowsAffected() (int64, error) { | ||
return r.res.GetRowsAffected(), nil | ||
} | ||
|
||
// NewRows returns a new driver.Rows from the given RaftApplyResponse. | ||
func NewRows(res *v1.SQLQueryResult) driver.Rows { | ||
return &queryResult{res, 0} | ||
} | ||
|
||
type queryResult struct { | ||
res *v1.SQLQueryResult | ||
index int64 | ||
} | ||
|
||
// Columns returns the names of the columns. | ||
func (q *queryResult) Columns() []string { | ||
return q.res.GetColumns() | ||
} | ||
|
||
// Next is called to populate the next row of data into | ||
// the provided slice. | ||
func (q *queryResult) Next(dest []driver.Value) error { | ||
if q.index >= int64(len(q.res.GetValues())) { | ||
return io.EOF | ||
} | ||
var err error | ||
for i, v := range q.res.GetValues()[q.index].Values { | ||
dest[i], err = SQLParameterToDriverValue(v) | ||
if err != nil { | ||
return fmt.Errorf("sql parameter to driver value: %w", err) | ||
} | ||
} | ||
q.index++ | ||
return nil | ||
} | ||
|
||
// ColumnTypeDatabaseTypeName returns the database system type. | ||
func (q *queryResult) ColumnTypeDatabaseTypeName(index int) string { | ||
return q.res.GetTypes()[index] | ||
} | ||
|
||
// Close closes the rows iterator. | ||
func (q *queryResult) Close() error { | ||
return nil | ||
} |
Oops, something went wrong.