From 903a6df7363a43862cb31f46c8259515bb6b8425 Mon Sep 17 00:00:00 2001 From: Aleksey Myasnikov Date: Wed, 4 Sep 2024 12:56:58 +0300 Subject: [PATCH] refactored examples --- examples/basic/native/query/series.go | 249 ++++++++++++++------------ 1 file changed, 131 insertions(+), 118 deletions(-) diff --git a/examples/basic/native/query/series.go b/examples/basic/native/query/series.go index 6d2e711cf..a461306ad 100644 --- a/examples/basic/native/query/series.go +++ b/examples/basic/native/query/series.go @@ -16,14 +16,15 @@ import ( func read(ctx context.Context, c query.Client, prefix string) error { return c.Do(ctx, func(ctx context.Context, s query.Session) (err error) { - result, err := s.Query(ctx, ` + result, err := s.Query(ctx, fmt.Sprintf(` SELECT series_id, title, release_date FROM - series - `, query.WithTxControl(query.TxControl(query.BeginTx(query.WithSnapshotReadOnly()))), + %s + `, "`"+path.Join(prefix, "series")+"`"), + query.WithTxControl(query.TxControl(query.BeginTx(query.WithSnapshotReadOnly()))), ) if err != nil { return err @@ -73,126 +74,138 @@ func read(ctx context.Context, c query.Client, prefix string) error { func fillTablesWithData(ctx context.Context, c query.Client, prefix string) error { series, seasons, episodes := getData() - return c.Do(ctx, - func(ctx context.Context, s query.Session) (err error) { - return s.Exec(ctx, - fmt.Sprintf(` - PRAGMA TablePathPrefix("%s"); - - DECLARE $seriesData AS List>>; - - DECLARE $seasonsData AS List>; - - DECLARE $episodesData AS List>; - - REPLACE INTO series - SELECT - series_id, - title, - series_info, - release_date, - comment - FROM AS_TABLE($seriesData); - - REPLACE INTO seasons - SELECT - series_id, - season_id, - title, - first_aired, - last_aired - FROM AS_TABLE($seasonsData); - - REPLACE INTO episodes - SELECT - series_id, - season_id, - episode_id, - title, - air_date - FROM AS_TABLE($episodesData); - `, prefix), - query.WithParameters(ydb.ParamsBuilder(). - Param("$seriesData").BeginList().AddItems(series...).EndList(). - Param("$seasonsData").BeginList().AddItems(seasons...).EndList(). - Param("$episodesData").BeginList().AddItems(episodes...).EndList(). - Build(), - ), - ) - }, + err := c.Exec(ctx, fmt.Sprintf(` + DECLARE $seriesData AS List>>; + + REPLACE INTO %s + SELECT + series_id, + title, + series_info, + release_date, + comment + FROM AS_TABLE($seriesData);`, + "`"+path.Join(prefix, "series")+"`"), + query.WithParameters(ydb.ParamsBuilder(). + Param("$seriesData"). + BeginList().AddItems(series...).EndList(). + Build(), + ), + ) + if err != nil { + return err + } + + err = c.Exec(ctx, fmt.Sprintf(` + DECLARE $seasonsData AS List>; + + REPLACE INTO %s + SELECT + series_id, + season_id, + title, + first_aired, + last_aired + FROM AS_TABLE($seasonsData);`, + "`"+path.Join(prefix, "seasons")+"`"), + query.WithParameters(ydb.ParamsBuilder(). + Param("$seasonsData"). + BeginList().AddItems(seasons...).EndList(). + Build(), + ), ) + if err != nil { + return err + } + + err = c.Exec(ctx, fmt.Sprintf(` + DECLARE $episodesData AS List>; + + REPLACE INTO %s + SELECT + series_id, + season_id, + episode_id, + title, + air_date + FROM AS_TABLE($episodesData);`, + "`"+path.Join(prefix, "episodes")+"`"), + query.WithParameters(ydb.ParamsBuilder(). + Param("$episodesData"). + BeginList().AddItems(episodes...).EndList(). + Build(), + ), + ) + if err != nil { + return err + } + + return nil } func createTables(ctx context.Context, c query.Client, prefix string) error { - return c.Do(ctx, - func(ctx context.Context, s query.Session) error { - err := s.Exec(ctx, fmt.Sprintf(` - CREATE TABLE IF NOT EXISTS %s ( - series_id Bytes, - title Text, - series_info Text, - release_date Date, - comment Text, - - PRIMARY KEY(series_id) - ) - `, "`"+path.Join(prefix, "series")+"`"), - query.WithTxControl(query.NoTx()), - ) - if err != nil { - return err - } - - err = s.Exec(ctx, fmt.Sprintf(` - CREATE TABLE IF NOT EXISTS %s ( - series_id Bytes, - season_id Bytes, - title Text, - first_aired Date, - last_aired Date, - - PRIMARY KEY(series_id,season_id) - ) - `, "`"+path.Join(prefix, "seasons")+"`"), - query.WithTxControl(query.NoTx()), - ) - if err != nil { - return err - } + err := c.Exec(ctx, fmt.Sprintf(` + CREATE TABLE IF NOT EXISTS %s ( + series_id Bytes, + title Text, + series_info Text, + release_date Date, + comment Text, + + PRIMARY KEY(series_id) + )`, "`"+path.Join(prefix, "series")+"`"), + query.WithTxControl(query.NoTx()), + ) + if err != nil { + return err + } - err = s.Exec(ctx, fmt.Sprintf(` - CREATE TABLE IF NOT EXISTS %s ( - series_id Bytes, - season_id Bytes, - episode_id Bytes, - title Text, - air_date Date, - - PRIMARY KEY(series_id,season_id,episode_id) - ) - `, "`"+path.Join(prefix, "episodes")+"`"), - query.WithTxControl(query.NoTx()), - ) - if err != nil { - return err - } + err = c.Exec(ctx, fmt.Sprintf(` + CREATE TABLE IF NOT EXISTS %s ( + series_id Bytes, + season_id Bytes, + title Text, + first_aired Date, + last_aired Date, + + PRIMARY KEY(series_id,season_id) + )`, "`"+path.Join(prefix, "seasons")+"`"), + query.WithTxControl(query.NoTx()), + ) + if err != nil { + return err + } - return nil - }, + err = c.Exec(ctx, fmt.Sprintf(` + CREATE TABLE IF NOT EXISTS %s ( + series_id Bytes, + season_id Bytes, + episode_id Bytes, + title Text, + air_date Date, + + PRIMARY KEY(series_id,season_id,episode_id) + )`, "`"+path.Join(prefix, "episodes")+"`"), + query.WithTxControl(query.NoTx()), ) + if err != nil { + return err + } + + return nil }