feat(go): qualify tdengine history writes by database
This commit is contained in:
@@ -22,8 +22,9 @@ type Execer interface {
|
||||
}
|
||||
|
||||
type Writer struct {
|
||||
exec Execer
|
||||
cache tableCache
|
||||
exec Execer
|
||||
database string
|
||||
cache tableCache
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -50,6 +51,12 @@ func NewWriter(exec Execer) *Writer {
|
||||
return &Writer{exec: exec, cache: tableCache{seen: map[string]struct{}{}}}
|
||||
}
|
||||
|
||||
func NewWriterWithDatabase(exec Execer, database string) *Writer {
|
||||
writer := NewWriter(exec)
|
||||
writer.database = normalizeIdentifier(database)
|
||||
return writer
|
||||
}
|
||||
|
||||
func (w *Writer) EnsureSchema(ctx context.Context, database string) error {
|
||||
for _, statement := range SchemaStatements(database) {
|
||||
if _, err := w.exec.ExecContext(ctx, statement); err != nil {
|
||||
@@ -59,6 +66,14 @@ func (w *Writer) EnsureSchema(ctx context.Context, database string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Writer) qualify(table string) string {
|
||||
table = normalizeIdentifier(table)
|
||||
if w.database == "" {
|
||||
return table
|
||||
}
|
||||
return w.database + "." + table
|
||||
}
|
||||
|
||||
func (w *Writer) AppendAll(ctx context.Context, env envelope.FrameEnvelope) error {
|
||||
if err := w.AppendRawFrame(ctx, env); err != nil {
|
||||
return err
|
||||
@@ -87,7 +102,7 @@ func (w *Writer) AppendRawFrame(ctx context.Context, env envelope.FrameEnvelope)
|
||||
_, err := w.exec.ExecContext(ctx, fmt.Sprintf(`INSERT INTO %s
|
||||
(ts, frame_id, event_id, message_id, event_time, received_at, raw_size_bytes,
|
||||
raw_hex, raw_text, parsed_json, parse_status, parse_error, source_endpoint)
|
||||
VALUES (%s)`, table, joinLiterals(rawValues(env, rawHex, rawText, parsedFields))))
|
||||
VALUES (%s)`, w.qualify(table), joinLiterals(rawValues(env, rawHex, rawText, parsedFields))))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -103,7 +118,7 @@ VALUES (%s)`, table, joinLiterals(rawValues(env, rawHex, rawText, parsedFields))
|
||||
for _, chunk := range chunks {
|
||||
if _, err := w.exec.ExecContext(ctx, fmt.Sprintf(`INSERT INTO %s
|
||||
(ts, event_id, frame_id, received_at, payload_kind, chunk_index, chunk_count, chunk_text)
|
||||
VALUES (%s)`, chunkTable, joinLiterals(chunkValues(env, chunk)))); err != nil {
|
||||
VALUES (%s)`, w.qualify(chunkTable), joinLiterals(chunkValues(env, chunk)))); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -144,7 +159,7 @@ func (w *Writer) AppendRawFrameBatch(ctx context.Context, envelopes []envelope.F
|
||||
if _, err := w.exec.ExecContext(ctx, fmt.Sprintf(`INSERT INTO %s
|
||||
(ts, frame_id, event_id, message_id, event_time, received_at, raw_size_bytes,
|
||||
raw_hex, raw_text, parsed_json, parse_status, parse_error, source_endpoint)
|
||||
VALUES %s`, table, strings.Join(rows, ","))); err != nil {
|
||||
VALUES %s`, w.qualify(table), strings.Join(rows, ","))); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -155,7 +170,7 @@ VALUES %s`, table, strings.Join(rows, ","))); err != nil {
|
||||
_ = chunkEnvByTable[table]
|
||||
if _, err := w.exec.ExecContext(ctx, fmt.Sprintf(`INSERT INTO %s
|
||||
(ts, event_id, frame_id, received_at, payload_kind, chunk_index, chunk_count, chunk_text)
|
||||
VALUES %s`, table, strings.Join(rows, ","))); err != nil {
|
||||
VALUES %s`, w.qualify(table), strings.Join(rows, ","))); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -178,7 +193,7 @@ func (w *Writer) AppendLocation(ctx context.Context, env envelope.FrameEnvelope)
|
||||
_, err := w.exec.ExecContext(ctx, fmt.Sprintf(`INSERT INTO %s
|
||||
(ts, event_id, received_at, longitude, latitude, altitude_m, speed_kmh,
|
||||
direction_deg, alarm_flag, status_flag, total_mileage_km)
|
||||
VALUES (%s)`, table, joinLiterals(locationValues(env, longitude, latitude))))
|
||||
VALUES (%s)`, w.qualify(table), joinLiterals(locationValues(env, longitude, latitude))))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -206,7 +221,7 @@ func (w *Writer) AppendLocationBatch(ctx context.Context, envelopes []envelope.F
|
||||
if _, err := w.exec.ExecContext(ctx, fmt.Sprintf(`INSERT INTO %s
|
||||
(ts, event_id, received_at, longitude, latitude, altitude_m, speed_kmh,
|
||||
direction_deg, alarm_flag, status_flag, total_mileage_km)
|
||||
VALUES %s`, table, strings.Join(rows, ","))); err != nil {
|
||||
VALUES %s`, w.qualify(table), strings.Join(rows, ","))); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -222,7 +237,7 @@ func (w *Writer) ensureRawChild(ctx context.Context, table string, stable string
|
||||
return nil
|
||||
}
|
||||
statement := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s USING %s TAGS ('%s', '%s', '%s', '%s', '%s')",
|
||||
table, stable,
|
||||
w.qualify(table), w.qualify(stable),
|
||||
quote(string(env.Protocol)),
|
||||
quote(env.VehicleKey()),
|
||||
quote(env.VIN),
|
||||
@@ -232,7 +247,7 @@ func (w *Writer) ensureRawChild(ctx context.Context, table string, stable string
|
||||
return err
|
||||
}
|
||||
if phone := normalizePhoneTag(env.Phone); phone != "" {
|
||||
if _, err := w.exec.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s SET TAG phone = '%s'", table, quote(phone))); err != nil {
|
||||
if _, err := w.exec.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s SET TAG phone = '%s'", w.qualify(table), quote(phone))); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -250,8 +265,9 @@ func (w *Writer) ensureLocationChild(ctx context.Context, table string, env enve
|
||||
if ok {
|
||||
return nil
|
||||
}
|
||||
statement := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s USING vehicle_locations TAGS ('%s', '%s')",
|
||||
table,
|
||||
statement := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s USING %s TAGS ('%s', '%s')",
|
||||
w.qualify(table),
|
||||
w.qualify("vehicle_locations"),
|
||||
quote(string(env.Protocol)),
|
||||
quote(strings.TrimSpace(env.VIN)))
|
||||
if _, err := w.exec.ExecContext(ctx, statement); err != nil {
|
||||
@@ -367,6 +383,12 @@ func locationTableName(env envelope.FrameEnvelope) string {
|
||||
return "loc_" + strings.ToLower(string(env.Protocol)) + "_" + hash16(strings.TrimSpace(env.VIN))
|
||||
}
|
||||
|
||||
func normalizeIdentifier(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
value = strings.Trim(value, "`")
|
||||
return value
|
||||
}
|
||||
|
||||
func normalizePhoneTag(phone string) string {
|
||||
trimmed := strings.TrimLeft(strings.TrimSpace(phone), "0")
|
||||
if trimmed == "" {
|
||||
|
||||
Reference in New Issue
Block a user