Skip to content

Commit 85f7487

Browse files
authored
Merge pull request #752 from datbeohbbh/datbeohbbh/xorm-example
(example): basic example via Xorm
2 parents a02a055 + af64419 commit 85f7487

File tree

7 files changed

+1563
-19
lines changed

7 files changed

+1563
-19
lines changed

.github/workflows/examples.yml

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
matrix:
1818
go-version: [ 1.17.x, 1.18.x, 1.19.x, 1.20.x ]
1919
ydb-version: [ 22.5, 23.1, 23.2 ]
20-
driver: [ native, database_sql, gorm ]
20+
driver: [ native, database_sql, gorm, xorm ]
2121
exclude:
2222
- driver: database_sql
2323
go-version: 1.17.x
@@ -52,11 +52,15 @@ jobs:
5252
- name: Run examples for ${{ matrix.driver }}
5353
working-directory: ./examples/basic/${{ matrix.driver }}
5454
run: go run .
55-
examples-gorm-postgres:
55+
examples-postgres:
5656
concurrency:
57-
group: examples-gorm-postgres-${{ github.ref }}
57+
group: examples-${{ matrix.driver }}-postgres-${{ github.ref }}
5858
cancel-in-progress: true
5959
runs-on: ubuntu-latest
60+
strategy:
61+
fail-fast: false
62+
matrix:
63+
driver: [ gorm, xorm ]
6064
services:
6165
postgres:
6266
image: postgres
@@ -82,18 +86,22 @@ jobs:
8286
with:
8387
go-version: 1.20.x
8488
cache: true
85-
- name: Run examples for gorm with postgres
86-
working-directory: ./examples/basic/gorm
89+
- name: Run examples for ${{ matrix.driver }} with postgres
90+
working-directory: ./examples/basic/${{ matrix.driver }}
8791
run: go run .
88-
examples-gorm-sqlite:
92+
examples-sqlite:
8993
concurrency:
90-
group: examples-gorm-sqlite-${{ github.ref }}
94+
group: examples-${{ matrix.driver }}-sqlite-${{ github.ref }}
9195
cancel-in-progress: true
9296
runs-on: ubuntu-latest
97+
strategy:
98+
fail-fast: false
99+
matrix:
100+
driver: [ gorm, xorm ]
93101
env:
94102
OS: ubuntu-latest
95103
GO: 1.20.x
96-
SQLITE_CONNECTION_STRING: gorm.db
104+
SQLITE_CONNECTION_STRING: ${{ matrix.driver }}.db
97105
steps:
98106
- name: Checkout code
99107
uses: actions/checkout@v3
@@ -102,6 +110,6 @@ jobs:
102110
with:
103111
go-version: 1.20.x
104112
cache: true
105-
- name: Run examples for gorm with sqlite
106-
working-directory: ./examples/basic/gorm
113+
- name: Run examples for ${{ matrix.driver }} with sqlite
114+
working-directory: ./examples/basic/${{ matrix.driver }}
107115
run: go run .

examples/basic/xorm/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Basic example via YDB Xorm
2+
3+
Basic example demonstrates the possibilities of `YDB` Xorm:
4+
- define scheme of tables
5+
- insert data
6+
- select all data from database

examples/basic/xorm/data.go

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
package main
2+
3+
import (
4+
"time"
5+
6+
"github.com/google/uuid"
7+
)
8+
9+
func seriesData(id string, released time.Time, title, info, comment string) *Series {
10+
return &Series{
11+
ID: id,
12+
Title: title,
13+
Info: info,
14+
ReleaseDate: released,
15+
Comment: comment,
16+
}
17+
}
18+
19+
func seasonData(seriesID, seasonID, title string, first, last time.Time) *Seasons {
20+
return &Seasons{
21+
ID: seasonID,
22+
SeriesID: seriesID,
23+
Title: title,
24+
FirstAired: first,
25+
LastAired: last,
26+
}
27+
}
28+
29+
func episodeData(seasonID, episodeID, title string, date time.Time) *Episodes {
30+
return &Episodes{
31+
ID: episodeID,
32+
SeasonID: seasonID,
33+
Title: title,
34+
AirDate: date,
35+
}
36+
}
37+
38+
func getData() (series []*Series, seasons []*Seasons, episodes []*Episodes) {
39+
for seriesID, fill := range map[string]func(seriesID string) (
40+
seriesData *Series,
41+
seasons []*Seasons,
42+
episodes []*Episodes,
43+
){
44+
uuid.New().String(): getDataForITCrowd,
45+
uuid.New().String(): getDataForSiliconValley,
46+
} {
47+
seriesData, seasonsData, episodesData := fill(seriesID)
48+
series = append(series, seriesData)
49+
seasons = append(seasons, seasonsData...)
50+
episodes = append(episodes, episodesData...)
51+
}
52+
return
53+
}
54+
55+
func getDataForITCrowd(seriesID string) (series *Series, seasons []*Seasons, episodes []*Episodes) {
56+
series = seriesData(
57+
seriesID, date("2006-02-03"), "IT Crowd", ""+
58+
"The IT Crowd is a British sitcom produced by Channel 4, written by Graham Linehan, produced by "+
59+
"Ash Atalla and starring Chris O'Dowd, Richard Ayoade, Katherine Parkinson, and Matt Berry.",
60+
"", // NULL comment.
61+
)
62+
for _, season := range []*struct {
63+
title string
64+
first time.Time
65+
last time.Time
66+
episodes map[string]time.Time
67+
}{
68+
{
69+
"Season 1", date("2006-02-03"), date("2006-03-03"), map[string]time.Time{
70+
"Yesterday's Jam": date("2006-02-03"),
71+
"Calamity Jen": date("2006-02-03"),
72+
"Fifty-Fifty": date("2006-02-10"),
73+
"The Red Door": date("2006-02-17"),
74+
"The Haunting of Bill Crouse": date("2006-02-24"),
75+
"Aunt Irma Visits": date("2006-03-03"),
76+
},
77+
},
78+
{
79+
"Season 2", date("2007-08-24"), date("2007-09-28"), map[string]time.Time{
80+
"The Work Outing": date("2006-08-24"),
81+
"Return of the Golden Child": date("2007-08-31"),
82+
"Moss and the German": date("2007-09-07"),
83+
"The Dinner Party": date("2007-09-14"),
84+
"Smoke and Mirrors": date("2007-09-21"),
85+
"Men Without Women": date("2007-09-28"),
86+
},
87+
},
88+
{
89+
"Season 3", date("2008-11-21"), date("2008-12-26"), map[string]time.Time{
90+
"From Hell": date("2008-11-21"),
91+
"Are We Not Men?": date("2008-11-28"),
92+
"Tramps Like Us": date("2008-12-05"),
93+
"The Speech": date("2008-12-12"),
94+
"Friendface": date("2008-12-19"),
95+
"Calendar Geeks": date("2008-12-26"),
96+
},
97+
},
98+
{
99+
"Season 4", date("2010-06-25"), date("2010-07-30"), map[string]time.Time{
100+
"Jen The Fredo": date("2010-06-25"),
101+
"The Final Countdown": date("2010-07-02"),
102+
"Something Happened": date("2010-07-09"),
103+
"Italian For Beginners": date("2010-07-16"),
104+
"Bad Boys": date("2010-07-23"),
105+
"Reynholm vs Reynholm": date("2010-07-30"),
106+
},
107+
},
108+
} {
109+
seasonID := uuid.New().String()
110+
seasons = append(seasons, seasonData(seriesID, seasonID, season.title, season.first, season.last))
111+
for title, date := range season.episodes {
112+
episodes = append(episodes, episodeData(seasonID, uuid.New().String(), title, date))
113+
}
114+
}
115+
return series, seasons, episodes
116+
}
117+
118+
func getDataForSiliconValley(seriesID string) (series *Series, seasons []*Seasons, episodes []*Episodes) {
119+
series = seriesData(
120+
seriesID, date("2014-04-06"), "Silicon Valley", ""+
121+
"Silicon Valley is an American comedy television series created by Mike Judge, John Altschuler and "+
122+
"Dave Krinsky. The series focuses on five young men who founded a startup company in Silicon Valley.",
123+
"Some comment here",
124+
)
125+
for _, season := range []*struct {
126+
title string
127+
first time.Time
128+
last time.Time
129+
episodes map[string]time.Time
130+
}{
131+
{
132+
"Season 1", date("2014-04-06"), date("2014-06-01"), map[string]time.Time{
133+
"Minimum Viable Product": date("2014-04-06"),
134+
"The Cap Table": date("2014-04-13"),
135+
"Articles of Incorporation": date("2014-04-20"),
136+
"Fiduciary Duties": date("2014-04-27"),
137+
"Signaling Risk": date("2014-05-04"),
138+
"Third Party Insourcing": date("2014-05-11"),
139+
"Proof of Concept": date("2014-05-18"),
140+
"Optimal Tip-to-Tip Efficiency": date("2014-06-01"),
141+
},
142+
},
143+
{
144+
"Season 2", date("2015-04-12"), date("2015-06-14"), map[string]time.Time{
145+
"Sand Hill Shuffle": date("2015-04-12"),
146+
"Runaway Devaluation": date("2015-04-19"),
147+
"Bad Money": date("2015-04-26"),
148+
"The Lady": date("2015-05-03"),
149+
"Server Space": date("2015-05-10"),
150+
"Homicide": date("2015-05-17"),
151+
"Adult Content": date("2015-05-24"),
152+
"White Hat/Black Hat": date("2015-05-31"),
153+
"Binding Arbitration": date("2015-06-07"),
154+
"Two Days of the Condor": date("2015-06-14"),
155+
},
156+
},
157+
{
158+
"Season 3", date("2016-04-24"), date("2016-06-26"), map[string]time.Time{
159+
"Founder Friendly": date("2016-04-24"),
160+
"Two in the Box": date("2016-05-01"),
161+
"Meinertzhagen's Haversack": date("2016-05-08"),
162+
"Maleant Data Systems Solutions": date("2016-05-15"),
163+
"The Empty Chair": date("2016-05-22"),
164+
"Bachmanity Insanity": date("2016-05-29"),
165+
"To Build a Better Beta": date("2016-06-05"),
166+
"Bachman's Earnings Over-Ride": date("2016-06-12"),
167+
"Daily Active Users": date("2016-06-19"),
168+
"The Uptick": date("2016-06-26"),
169+
},
170+
},
171+
{
172+
"Season 4", date("2017-04-23"), date("2017-06-25"), map[string]time.Time{
173+
"Success Failure": date("2017-04-23"),
174+
"Terms of Service": date("2017-04-30"),
175+
"Intellectual Property": date("2017-05-07"),
176+
"Teambuilding Exercise": date("2017-05-14"),
177+
"The Blood Boy": date("2017-05-21"),
178+
"Customer Service": date("2017-05-28"),
179+
"The Patent Troll": date("2017-06-04"),
180+
"The Keenan Vortex": date("2017-06-11"),
181+
"Hooli-Con": date("2017-06-18"),
182+
"Server Error": date("2017-06-25"),
183+
},
184+
},
185+
{
186+
"Season 5", date("2018-03-25"), date("2018-05-13"), map[string]time.Time{
187+
"Grow Fast or Die Slow": date("2018-03-25"),
188+
"Reorientation": date("2018-04-01"),
189+
"Chief Operating Officer": date("2018-04-08"),
190+
"Tech Evangelist": date("2018-04-15"),
191+
"Facial Recognition": date("2018-04-22"),
192+
"Artificial Emotional Intelligence": date("2018-04-29"),
193+
"Initial Coin Offering": date("2018-05-06"),
194+
"Fifty-One Percent": date("2018-05-13"),
195+
},
196+
},
197+
} {
198+
seasonID := uuid.New().String()
199+
seasons = append(seasons, seasonData(seriesID, seasonID, season.title, season.first, season.last))
200+
for title, date := range season.episodes {
201+
episodes = append(episodes, episodeData(seasonID, uuid.New().String(), title, date))
202+
}
203+
}
204+
return series, seasons, episodes
205+
}
206+
207+
const dateISO8601 = "2006-01-02"
208+
209+
func date(date string) time.Time {
210+
t, err := time.Parse(dateISO8601, date)
211+
if err != nil {
212+
panic(err)
213+
}
214+
return t
215+
}

0 commit comments

Comments
 (0)