ydb_types.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //go:build ydb
  2. // +build ydb
  3. package ydb
  4. import (
  5. "fmt"
  6. "github.com/ydb-platform/ydb-go-sdk/v3/table"
  7. "github.com/ydb-platform/ydb-go-sdk/v3/table/options"
  8. "github.com/ydb-platform/ydb-go-sdk/v3/table/types"
  9. )
  10. type FileMeta struct {
  11. DirHash int64 `ydb:"type:int64"`
  12. Name string `ydb:"type:utf8"`
  13. Directory string `ydb:"type:utf8"`
  14. Meta []byte `ydb:"type:string"`
  15. }
  16. type FileMetas []FileMeta
  17. func (fm *FileMeta) queryParameters(ttlSec int32) *table.QueryParameters {
  18. var expireAtValue types.Value
  19. if ttlSec > 0 {
  20. expireAtValue = types.Uint32Value(uint32(ttlSec))
  21. } else {
  22. expireAtValue = types.NullValue(types.TypeUint32)
  23. }
  24. return table.NewQueryParameters(
  25. table.ValueParam("$dir_hash", types.Int64Value(fm.DirHash)),
  26. table.ValueParam("$directory", types.UTF8Value(fm.Directory)),
  27. table.ValueParam("$name", types.UTF8Value(fm.Name)),
  28. table.ValueParam("$meta", types.StringValue(fm.Meta)),
  29. table.ValueParam("$expire_at", expireAtValue))
  30. }
  31. func createTableOptions() []options.CreateTableOption {
  32. columnUnit := options.TimeToLiveUnitSeconds
  33. return []options.CreateTableOption{
  34. options.WithColumn("dir_hash", types.Optional(types.TypeInt64)),
  35. options.WithColumn("directory", types.Optional(types.TypeUTF8)),
  36. options.WithColumn("name", types.Optional(types.TypeUTF8)),
  37. options.WithColumn("meta", types.Optional(types.TypeString)),
  38. options.WithColumn("expire_at", types.Optional(types.TypeUint32)),
  39. options.WithPrimaryKeyColumn("dir_hash", "name"),
  40. options.WithTimeToLiveSettings(options.TimeToLiveSettings{
  41. ColumnName: "expire_at",
  42. ColumnUnit: &columnUnit,
  43. Mode: options.TimeToLiveModeValueSinceUnixEpoch},
  44. ),
  45. }
  46. }
  47. func withPragma(prefix *string, query string) *string {
  48. queryWithPragma := fmt.Sprintf(query, *prefix)
  49. return &queryWithPragma
  50. }