Skip to content

Commit 903a6df

Browse files
committed
refactored examples
1 parent 2587b07 commit 903a6df

File tree

1 file changed

+131
-118
lines changed

1 file changed

+131
-118
lines changed

examples/basic/native/query/series.go

Lines changed: 131 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ import (
1616
func read(ctx context.Context, c query.Client, prefix string) error {
1717
return c.Do(ctx,
1818
func(ctx context.Context, s query.Session) (err error) {
19-
result, err := s.Query(ctx, `
19+
result, err := s.Query(ctx, fmt.Sprintf(`
2020
SELECT
2121
series_id,
2222
title,
2323
release_date
2424
FROM
25-
series
26-
`, query.WithTxControl(query.TxControl(query.BeginTx(query.WithSnapshotReadOnly()))),
25+
%s
26+
`, "`"+path.Join(prefix, "series")+"`"),
27+
query.WithTxControl(query.TxControl(query.BeginTx(query.WithSnapshotReadOnly()))),
2728
)
2829
if err != nil {
2930
return err
@@ -73,126 +74,138 @@ func read(ctx context.Context, c query.Client, prefix string) error {
7374
func fillTablesWithData(ctx context.Context, c query.Client, prefix string) error {
7475
series, seasons, episodes := getData()
7576

76-
return c.Do(ctx,
77-
func(ctx context.Context, s query.Session) (err error) {
78-
return s.Exec(ctx,
79-
fmt.Sprintf(`
80-
PRAGMA TablePathPrefix("%s");
81-
82-
DECLARE $seriesData AS List<Struct<
83-
series_id: Bytes,
84-
title: Text,
85-
series_info: Text,
86-
release_date: Date,
87-
comment: Optional<Text>>>;
88-
89-
DECLARE $seasonsData AS List<Struct<
90-
series_id: Bytes,
91-
season_id: Bytes,
92-
title: Text,
93-
first_aired: Date,
94-
last_aired: Date>>;
95-
96-
DECLARE $episodesData AS List<Struct<
97-
series_id: Bytes,
98-
season_id: Bytes,
99-
episode_id: Bytes,
100-
title: Text,
101-
air_date: Date>>;
102-
103-
REPLACE INTO series
104-
SELECT
105-
series_id,
106-
title,
107-
series_info,
108-
release_date,
109-
comment
110-
FROM AS_TABLE($seriesData);
111-
112-
REPLACE INTO seasons
113-
SELECT
114-
series_id,
115-
season_id,
116-
title,
117-
first_aired,
118-
last_aired
119-
FROM AS_TABLE($seasonsData);
120-
121-
REPLACE INTO episodes
122-
SELECT
123-
series_id,
124-
season_id,
125-
episode_id,
126-
title,
127-
air_date
128-
FROM AS_TABLE($episodesData);
129-
`, prefix),
130-
query.WithParameters(ydb.ParamsBuilder().
131-
Param("$seriesData").BeginList().AddItems(series...).EndList().
132-
Param("$seasonsData").BeginList().AddItems(seasons...).EndList().
133-
Param("$episodesData").BeginList().AddItems(episodes...).EndList().
134-
Build(),
135-
),
136-
)
137-
},
77+
err := c.Exec(ctx, fmt.Sprintf(`
78+
DECLARE $seriesData AS List<Struct<
79+
series_id: Bytes,
80+
title: Text,
81+
series_info: Text,
82+
release_date: Date,
83+
comment: Optional<Text>>>;
84+
85+
REPLACE INTO %s
86+
SELECT
87+
series_id,
88+
title,
89+
series_info,
90+
release_date,
91+
comment
92+
FROM AS_TABLE($seriesData);`,
93+
"`"+path.Join(prefix, "series")+"`"),
94+
query.WithParameters(ydb.ParamsBuilder().
95+
Param("$seriesData").
96+
BeginList().AddItems(series...).EndList().
97+
Build(),
98+
),
99+
)
100+
if err != nil {
101+
return err
102+
}
103+
104+
err = c.Exec(ctx, fmt.Sprintf(`
105+
DECLARE $seasonsData AS List<Struct<
106+
series_id: Bytes,
107+
season_id: Bytes,
108+
title: Text,
109+
first_aired: Date,
110+
last_aired: Date>>;
111+
112+
REPLACE INTO %s
113+
SELECT
114+
series_id,
115+
season_id,
116+
title,
117+
first_aired,
118+
last_aired
119+
FROM AS_TABLE($seasonsData);`,
120+
"`"+path.Join(prefix, "seasons")+"`"),
121+
query.WithParameters(ydb.ParamsBuilder().
122+
Param("$seasonsData").
123+
BeginList().AddItems(seasons...).EndList().
124+
Build(),
125+
),
138126
)
127+
if err != nil {
128+
return err
129+
}
130+
131+
err = c.Exec(ctx, fmt.Sprintf(`
132+
DECLARE $episodesData AS List<Struct<
133+
series_id: Bytes,
134+
season_id: Bytes,
135+
episode_id: Bytes,
136+
title: Text,
137+
air_date: Date>>;
138+
139+
REPLACE INTO %s
140+
SELECT
141+
series_id,
142+
season_id,
143+
episode_id,
144+
title,
145+
air_date
146+
FROM AS_TABLE($episodesData);`,
147+
"`"+path.Join(prefix, "episodes")+"`"),
148+
query.WithParameters(ydb.ParamsBuilder().
149+
Param("$episodesData").
150+
BeginList().AddItems(episodes...).EndList().
151+
Build(),
152+
),
153+
)
154+
if err != nil {
155+
return err
156+
}
157+
158+
return nil
139159
}
140160

141161
func createTables(ctx context.Context, c query.Client, prefix string) error {
142-
return c.Do(ctx,
143-
func(ctx context.Context, s query.Session) error {
144-
err := s.Exec(ctx, fmt.Sprintf(`
145-
CREATE TABLE IF NOT EXISTS %s (
146-
series_id Bytes,
147-
title Text,
148-
series_info Text,
149-
release_date Date,
150-
comment Text,
151-
152-
PRIMARY KEY(series_id)
153-
)
154-
`, "`"+path.Join(prefix, "series")+"`"),
155-
query.WithTxControl(query.NoTx()),
156-
)
157-
if err != nil {
158-
return err
159-
}
160-
161-
err = s.Exec(ctx, fmt.Sprintf(`
162-
CREATE TABLE IF NOT EXISTS %s (
163-
series_id Bytes,
164-
season_id Bytes,
165-
title Text,
166-
first_aired Date,
167-
last_aired Date,
168-
169-
PRIMARY KEY(series_id,season_id)
170-
)
171-
`, "`"+path.Join(prefix, "seasons")+"`"),
172-
query.WithTxControl(query.NoTx()),
173-
)
174-
if err != nil {
175-
return err
176-
}
162+
err := c.Exec(ctx, fmt.Sprintf(`
163+
CREATE TABLE IF NOT EXISTS %s (
164+
series_id Bytes,
165+
title Text,
166+
series_info Text,
167+
release_date Date,
168+
comment Text,
169+
170+
PRIMARY KEY(series_id)
171+
)`, "`"+path.Join(prefix, "series")+"`"),
172+
query.WithTxControl(query.NoTx()),
173+
)
174+
if err != nil {
175+
return err
176+
}
177177

178-
err = s.Exec(ctx, fmt.Sprintf(`
179-
CREATE TABLE IF NOT EXISTS %s (
180-
series_id Bytes,
181-
season_id Bytes,
182-
episode_id Bytes,
183-
title Text,
184-
air_date Date,
185-
186-
PRIMARY KEY(series_id,season_id,episode_id)
187-
)
188-
`, "`"+path.Join(prefix, "episodes")+"`"),
189-
query.WithTxControl(query.NoTx()),
190-
)
191-
if err != nil {
192-
return err
193-
}
178+
err = c.Exec(ctx, fmt.Sprintf(`
179+
CREATE TABLE IF NOT EXISTS %s (
180+
series_id Bytes,
181+
season_id Bytes,
182+
title Text,
183+
first_aired Date,
184+
last_aired Date,
185+
186+
PRIMARY KEY(series_id,season_id)
187+
)`, "`"+path.Join(prefix, "seasons")+"`"),
188+
query.WithTxControl(query.NoTx()),
189+
)
190+
if err != nil {
191+
return err
192+
}
194193

195-
return nil
196-
},
194+
err = c.Exec(ctx, fmt.Sprintf(`
195+
CREATE TABLE IF NOT EXISTS %s (
196+
series_id Bytes,
197+
season_id Bytes,
198+
episode_id Bytes,
199+
title Text,
200+
air_date Date,
201+
202+
PRIMARY KEY(series_id,season_id,episode_id)
203+
)`, "`"+path.Join(prefix, "episodes")+"`"),
204+
query.WithTxControl(query.NoTx()),
197205
)
206+
if err != nil {
207+
return err
208+
}
209+
210+
return nil
198211
}

0 commit comments

Comments
 (0)