feat: database.

This commit is contained in:
Bobo
2025-06-29 09:29:47 +08:00
parent d0e55cf372
commit 29a8782662
22 changed files with 1481 additions and 109 deletions

View File

@@ -3,11 +3,8 @@ package influxdb
import (
"context"
"github.com/go-kratos/kratos/v2/encoding"
_ "github.com/go-kratos/kratos/v2/encoding/json"
"github.com/go-kratos/kratos/v2/log"
"github.com/InfluxCommunity/influxdb3-go/v2/influxdb3"
"github.com/go-kratos/kratos/v2/log"
conf "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
)
@@ -15,14 +12,12 @@ import (
type Client struct {
cli *influxdb3.Client
log *log.Helper
codec encoding.Codec
log *log.Helper
}
func NewClient(logger log.Logger, cfg *conf.Bootstrap) (*Client, error) {
c := &Client{
log: log.NewHelper(log.With(logger, "module", "influxdb-client")),
codec: encoding.GetCodec("json"),
log: log.NewHelper(log.With(logger, "module", "influxdb-client")),
}
if err := c.createInfluxdbClient(cfg); err != nil {
@@ -87,6 +82,31 @@ func (c *Client) Query(ctx context.Context, query string) (*influxdb3.QueryItera
return result, nil
}
func (c *Client) QueryWithParams(
ctx context.Context,
table string,
filters map[string]interface{},
operators map[string]string,
fields []string,
) (*influxdb3.QueryIterator, error) {
if c.cli == nil {
return nil, ErrInfluxDBClientNotInitialized
}
query := BuildQueryWithParams(table, filters, operators, fields)
result, err := c.cli.Query(
ctx,
query,
influxdb3.WithQueryType(influxdb3.InfluxQL),
)
if err != nil {
c.log.Errorf("failed to query data: %v", err)
return nil, ErrInfluxDBQueryFailed
}
return result, nil
}
// Insert 插入数据
func (c *Client) Insert(ctx context.Context, point *influxdb3.Point) error {
if c.cli == nil {

View File

@@ -11,7 +11,7 @@ require (
github.com/go-kratos/kratos/v2 v2.8.4
github.com/stretchr/testify v1.10.0
github.com/tx7do/go-utils v1.1.29
github.com/tx7do/kratos-bootstrap/api v0.0.25
github.com/tx7do/kratos-bootstrap/api v0.0.27
google.golang.org/protobuf v1.36.6
)

View File

@@ -10,6 +10,43 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"
)
func BuildQuery(
table string,
filters map[string]interface{},
operators map[string]string,
fields []string,
) (string, []interface{}) {
var queryBuilder strings.Builder
args := make([]interface{}, 0)
// 构建 SELECT 语句
queryBuilder.WriteString("SELECT ")
if len(fields) > 0 {
queryBuilder.WriteString(strings.Join(fields, ", "))
} else {
queryBuilder.WriteString("*")
}
queryBuilder.WriteString(fmt.Sprintf(" FROM %s", table))
// 构建 WHERE 条件
if len(filters) > 0 {
queryBuilder.WriteString(" WHERE ")
var conditions []string
var operator string
for key, value := range filters {
operator = "=" // 默认操作符
if op, exists := operators[key]; exists {
operator = op
}
conditions = append(conditions, fmt.Sprintf("%s %s ?", key, operator))
args = append(args, value)
}
queryBuilder.WriteString(strings.Join(conditions, " AND "))
}
return queryBuilder.String(), args
}
func GetPointTag(point *influxdb3.Point, name string) *string {
if point == nil {
return nil

View File

@@ -1,9 +1,86 @@
package influxdb
import (
"reflect"
"testing"
)
func TestBuildQuery(t *testing.T) {
tests := []struct {
name string
table string
filters map[string]interface{}
operators map[string]string
fields []string
expectedQuery string
expectedArgs []interface{}
}{
{
name: "Basic query with filters and fields",
table: "candles",
filters: map[string]interface{}{"s": "AAPL", "o": 150.0},
fields: []string{"s", "o", "h", "l", "c", "v"},
expectedQuery: "SELECT s, o, h, l, c, v FROM candles WHERE s = ? AND o = ?",
expectedArgs: []interface{}{"AAPL", 150.0},
},
{
name: "Query with no filters",
table: "candles",
filters: map[string]interface{}{},
fields: []string{"s", "o", "h"},
expectedQuery: "SELECT s, o, h FROM candles",
expectedArgs: []interface{}{},
},
{
name: "Query with no fields",
table: "candles",
filters: map[string]interface{}{"s": "AAPL"},
fields: []string{},
expectedQuery: "SELECT * FROM candles WHERE s = ?",
expectedArgs: []interface{}{"AAPL"},
},
{
name: "Empty table name",
table: "",
filters: map[string]interface{}{"s": "AAPL"},
fields: []string{"s", "o"},
expectedQuery: "SELECT s, o FROM WHERE s = ?",
expectedArgs: []interface{}{"AAPL"},
},
{
name: "Special characters in filters",
table: "candles",
filters: map[string]interface{}{"name": "O'Reilly"},
fields: []string{"name"},
expectedQuery: "SELECT name FROM candles WHERE name = ?",
expectedArgs: []interface{}{"O'Reilly"},
},
{
name: "Query with interval filters",
table: "candles",
filters: map[string]interface{}{"time": "now() - interval '15 minutes'"},
fields: []string{"*"},
operators: map[string]string{"time": ">="},
expectedQuery: "SELECT * FROM candles WHERE time >= ?",
expectedArgs: []interface{}{"now() - interval '15 minutes'"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
query, args := BuildQuery(tt.table, tt.filters, tt.operators, tt.fields)
if query != tt.expectedQuery {
t.Errorf("expected query %s, got %s", tt.expectedQuery, query)
}
if !reflect.DeepEqual(args, tt.expectedArgs) {
t.Errorf("expected args %v, got %v", tt.expectedArgs, args)
}
})
}
}
func TestBuildQueryWithParams(t *testing.T) {
tests := []struct {
name string