Skip to content

Commit

Permalink
refactored examples
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Sep 4, 2024
1 parent 2587b07 commit 903a6df
Showing 1 changed file with 131 additions and 118 deletions.
249 changes: 131 additions & 118 deletions examples/basic/native/query/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Struct<
series_id: Bytes,
title: Text,
series_info: Text,
release_date: Date,
comment: Optional<Text>>>;
DECLARE $seasonsData AS List<Struct<
series_id: Bytes,
season_id: Bytes,
title: Text,
first_aired: Date,
last_aired: Date>>;
DECLARE $episodesData AS List<Struct<
series_id: Bytes,
season_id: Bytes,
episode_id: Bytes,
title: Text,
air_date: Date>>;
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<Struct<
series_id: Bytes,
title: Text,
series_info: Text,
release_date: Date,
comment: Optional<Text>>>;
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<Struct<
series_id: Bytes,
season_id: Bytes,
title: Text,
first_aired: Date,
last_aired: Date>>;
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<Struct<
series_id: Bytes,
season_id: Bytes,
episode_id: Bytes,
title: Text,
air_date: Date>>;
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
}

0 comments on commit 903a6df

Please sign in to comment.