/ydb-go-qb

query builder for ydb-go-sdk, not ORM

Primary LanguageGoMIT LicenseMIT

ydb-go-qb - query builder for ydb-go-sdk, not ORM

License tests PRs Welcome

Installation

go get -u github.com/flymedllva/ydb-go-qb

Packages

  • yscan is a scanner the result.Result into Golang struct
    • built on the latest version of scany
  • yqb is a query builder that supports data types for working with the YDB
    • it's a fork of squirrel with additional functionality specifically for YDB
  • yqe this is query executor connecting yqb & yscan into a convenient API for work with YDB
  • ysg this is schema generator connects to YDB, collects circuit information by specified parameters, generates auxiliary go code

Example

yqb & yscan & yqe

connect using a native API to the YDB

db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
    log.Fatal(err)
}

execute example SELECT query

type AwesomeStruct struct {
   ID      string    `db:"id"`
   Pointer *string   `db:"pointer"`
   Int     int       `db:"int"`
   Pi      float64   `db:"pi"`
   Time    time.Time `db:"time"`
   Json    []byte    `db:"json"`
}
var as AwesomeStruct
queryErr := db.Table().Do(ctx, func(ctx context.Context, s table.Session) (err error) {
   query := yqb.Select(
       "\"Hey!\" as id",
       "\"*Hey!\" as pointer",
       "123 as int",
       "Math::Pi() as pi",
       "AddTimezone(CurrentUtcDatetime(), \"Europe/Moscow\") as time",
       "Json(\"{\\\"1\\\": \\\"2\\\"}\") as json",
   )

   // default
   txc := table.DefaultTxControl()
   tx, err := yqe.UseSession(s).WithTxControl(txc).Get(ctx, &as, query)
   if err != nil {
       return err
   }
   log.Printf("use tx.Rollback or tx.CommitTx if needs from %v\n", tx)
   // or (if txc not commit)
   // tx, r, err := yqe.UseTransaction(tx).Exec(ctx, yqb.Select("1 as test"))
   // if err != nil {
   // return err
   // }
   // log.Println(r.ResultSetCount())
   
   return nil
})
if queryErr != nil {
   log.Fatal(queryErr)
}
log.Printf("yo %v\n", as)

Features

  • Use yqb as you are used to, but with support for YDB variables
yqb.Select("id").
    From("cities").
    Where(yqb.Eq{"city": types.TextValue("Moscow")}).
    OrderBy("city", "number").
    Limit(10)

equal

yqb.Select("id").
    From("cities").
    Where(yqb.Eq{"city": "Moscow"}).
    OrderBy("city", "number").
    Limit(10)

=>

DECLARE $p1 AS Utf8;
SELECT id FROM cities WHERE city = $p1 ORDER BY city, number LIMIT 10;
  • Use complex data types from YQL
yqb.Select().
  Column("?", types.StructValue(
        types.StructFieldValue("city", types.TextValue("Moscow")),
        types.StructFieldValue("number", types.Uint32Value(1)), 
      )
  )

=>

DECLARE $p1 AS Struct<'city':Utf8,'number':Uint32>;
SELECT $p1;
  • Use additional YDB features in your queries
yqb.Select("id").
    From("cities").
    Index("cities_city_idx").
    With("SCHEMA Struct<id:String>").
    Where(yqb.Eq{"city": types.TextValue("Moscow")})

=>

DECLARE $p1 AS Utf8;
SELECT id FROM cities VIEW cities_city_idx WITH SCHEMA Struct<id:String> WHERE city = $p1

ysg

SERVICE_NAME:=auth

ysh --endpoint grpc://localhost:2136 --database /local --service $(SERVICE_NAME)

=>

internal/pkg/storage/schema.go

package storage

var (
  // usersTable is the table `users`
  usersTable = "`auth/users`"
  // usersTableColumns all columns of the table `users`
  usersTableColumns = []string{"id", "created_at"}
  
  // usersTableIDColumn column `id`
  usersTableIDColumn = "id"
  // usersTableCreatedAtColumn column `created_at`
  usersTableCreatedAtColumn = "created_at"
)

License

Released under the MIT License. See the bundled LICENSE file for details.

Contributors

All Contributions

Created by Dmitry Gridnev (@FlymeDllVa)