Skip to content

Commit 62ef9ff

Browse files
committed
add more langs
1 parent 4100988 commit 62ef9ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+3421
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ __pycache__/
44
**/**/__pycache__/
55
**/**/**/__pycache__/
66
**/**/**/**/__pycache__/
7+
target/

base/production.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ services:
1111
image: backend_prod
1212
container_name: backend_base_prod
1313
environment:
14-
- "DB_HOST=db"
14+
- "DB_HOST=db_base"
1515
- "DATABASE_SELECTION=postgresql"
1616
depends_on:
17-
- db
17+
- db_base
1818
ports:
1919
- "127.0.0.1:8000:8000"
2020
privileged: true

golang/cursor-simple/accounts.db

20 KB
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="WEB_MODULE" version="4">
3+
<component name="Go" enabled="true" />
4+
<component name="NewModuleRootManager" inherit-compiler-output="true">
5+
<exclude-output />
6+
<content url="file://$MODULE_DIR$" />
7+
<orderEntry type="sourceFolder" forTests="false" />
8+
</component>
9+
</module>

golang/cursor-simple/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module cursor-simple
2+
3+
go 1.19
4+
5+
require github.com/mattn/go-sqlite3 v1.14.17

golang/cursor-simple/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
2+
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=

golang/cursor-simple/main.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
_ "github.com/mattn/go-sqlite3"
7+
"log"
8+
)
9+
10+
func main() {
11+
db, err := sql.Open("sqlite3", "./accounts.db")
12+
if err != nil {
13+
log.Fatal(err)
14+
}
15+
defer db.Close()
16+
17+
sqlStmt := `
18+
CREATE TABLE IF NOT EXISTS accounts (
19+
user_id text PRIMARY KEY,
20+
username text UNIQUE NOT NULL,
21+
password text NOT NULL,
22+
email text UNIQUE NOT NULL,
23+
created_on text NOT NULL,
24+
last_login text
25+
);
26+
`
27+
_, err = db.Exec(sqlStmt)
28+
if err != nil {
29+
log.Printf("%q: %s\n", err, sqlStmt)
30+
return
31+
}
32+
33+
rows, err := db.Query("SELECT * FROM accounts")
34+
if err != nil {
35+
log.Fatal(err)
36+
}
37+
defer rows.Close()
38+
printRows(rows)
39+
40+
tx, err := db.Begin()
41+
if err != nil {
42+
log.Fatal(err)
43+
}
44+
stmt, err := tx.Prepare("INSERT INTO accounts(user_id, username, password, email, created_on) values(?, ?, ?, ?, ?)")
45+
if err != nil {
46+
log.Fatal(err)
47+
}
48+
defer stmt.Close()
49+
_, err = stmt.Exec("c996c60b-c617-4c05-8eca-e3391a9b495e", "test", "test", "[email protected]", "2022-10-10T13:10:11Z")
50+
if err != nil {
51+
log.Printf("db table entry [email protected] already exists and therefore ignored")
52+
} else {
53+
tx.Commit()
54+
}
55+
56+
rows, err = db.Query("SELECT * FROM accounts")
57+
if err != nil {
58+
log.Fatal(err)
59+
}
60+
defer rows.Close()
61+
printRows(rows)
62+
}
63+
64+
func printRows(rows *sql.Rows) {
65+
for rows.Next() {
66+
var user_id string
67+
var username string
68+
var password string
69+
var email string
70+
var created_on string
71+
var last_login sql.NullString
72+
err := rows.Scan(&user_id, &username, &password, &email, &created_on, &last_login)
73+
if err != nil {
74+
log.Fatal(err)
75+
}
76+
fmt.Println(user_id, username, password, email, created_on, last_login.String)
77+
}
78+
err := rows.Err()
79+
if err != nil {
80+
log.Fatal(err)
81+
}
82+
}

golang/repository-simple/accounts.db

12 KB
Binary file not shown.

golang/repository-simple/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module repository-simple
2+
3+
go 1.19
4+
5+
require github.com/mattn/go-sqlite3 v1.14.17

golang/repository-simple/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
2+
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=

golang/repository-simple/main.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
"log"
7+
"time"
8+
9+
_ "github.com/mattn/go-sqlite3"
10+
)
11+
12+
type Account struct {
13+
ID string
14+
Username string
15+
Password string
16+
Email string
17+
CreatedOn time.Time
18+
}
19+
20+
type Repository interface {
21+
All() ([]Account, error)
22+
Get(id string) (*Account, error)
23+
Create(account Account) error
24+
Update(account Account) error
25+
Delete(id string) error
26+
}
27+
28+
type AccountRepository struct {
29+
db *sql.DB
30+
}
31+
32+
func NewAccountRepository() (Repository, error) {
33+
db, err := sql.Open("sqlite3", "./accounts.db")
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS accounts (
39+
id TEXT PRIMARY KEY,
40+
username TEXT NOT NULL,
41+
password TEXT NOT NULL,
42+
email TEXT NOT NULL,
43+
created_on DATETIME NOT NULL
44+
)`)
45+
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
return &AccountRepository{db: db}, nil
51+
}
52+
53+
func (r *AccountRepository) All() ([]Account, error) {
54+
rows, err := r.db.Query("SELECT id, username, password, email, created_on FROM accounts")
55+
if err != nil {
56+
return nil, err
57+
}
58+
defer rows.Close()
59+
60+
var accounts []Account
61+
for rows.Next() {
62+
var a Account
63+
err = rows.Scan(&a.ID, &a.Username, &a.Password, &a.Email, &a.CreatedOn)
64+
if err != nil {
65+
return nil, err
66+
}
67+
accounts = append(accounts, a)
68+
}
69+
70+
return accounts, rows.Err()
71+
}
72+
73+
func (r *AccountRepository) Get(id string) (*Account, error) {
74+
var a Account
75+
err := r.db.QueryRow("SELECT id, username, password, email, created_on FROM accounts WHERE id = ?", id).Scan(&a.ID, &a.Username, &a.Password, &a.Email, &a.CreatedOn)
76+
if err == sql.ErrNoRows {
77+
return nil, nil
78+
} else if err != nil {
79+
return nil, err
80+
}
81+
return &a, nil
82+
}
83+
84+
func (r *AccountRepository) Create(account Account) error {
85+
_, err := r.db.Exec("INSERT INTO accounts (id, username, password, email, created_on) VALUES (?, ?, ?, ?, ?)", account.ID, account.Username, account.Password, account.Email, account.CreatedOn)
86+
return err
87+
}
88+
89+
func (r *AccountRepository) Update(account Account) error {
90+
_, err := r.db.Exec("UPDATE accounts SET username = ?, password = ?, email = ?, created_on = ? WHERE id = ?", account.Username, account.Password, account.Email, account.CreatedOn, account.ID)
91+
return err
92+
}
93+
94+
func (r *AccountRepository) Delete(id string) error {
95+
_, err := r.db.Exec("DELETE FROM accounts WHERE id = ?", id)
96+
return err
97+
}
98+
99+
func main() {
100+
repo, err := NewAccountRepository()
101+
if err != nil {
102+
log.Fatal(err)
103+
}
104+
105+
newAccount := Account{
106+
ID: "test2",
107+
Username: "test2",
108+
Password: "test2",
109+
110+
CreatedOn: time.Now(),
111+
}
112+
113+
err = repo.Create(newAccount)
114+
if err != nil {
115+
log.Fatal(err)
116+
}
117+
118+
accounts, err := repo.All()
119+
if err != nil {
120+
log.Fatal(err)
121+
}
122+
fmt.Println("All accounts:", accounts)
123+
124+
account, err := repo.Get("test2")
125+
if err != nil {
126+
log.Fatal(err)
127+
}
128+
fmt.Println("Single account:", account)
129+
130+
newAccount.Username = "test3"
131+
err = repo.Update(newAccount)
132+
if err != nil {
133+
log.Fatal(err)
134+
}
135+
136+
account, err = repo.Get("test2")
137+
if err != nil {
138+
log.Fatal(err)
139+
}
140+
fmt.Println("Updated account:", account)
141+
142+
err = repo.Delete("test2")
143+
if err != nil {
144+
log.Fatal(err)
145+
}
146+
147+
accounts, err = repo.All()
148+
if err != nil {
149+
log.Fatal(err)
150+
}
151+
fmt.Println("All accounts after deletion:", accounts)
152+
}

java/cursor-simple/.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.gradle
2+
build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
!**/src/main/**/build/
5+
!**/src/test/**/build/
6+
7+
### IntelliJ IDEA ###
8+
.idea/modules.xml
9+
.idea/jarRepositories.xml
10+
.idea/compiler.xml
11+
.idea/libraries/
12+
*.iws
13+
*.iml
14+
*.ipr
15+
out/
16+
!**/src/main/**/out/
17+
!**/src/test/**/out/
18+
19+
### Eclipse ###
20+
.apt_generated
21+
.classpath
22+
.factorypath
23+
.project
24+
.settings
25+
.springBeans
26+
.sts4-cache
27+
bin/
28+
!**/src/main/**/bin/
29+
!**/src/test/**/bin/
30+
31+
### NetBeans ###
32+
/nbproject/private/
33+
/nbbuild/
34+
/dist/
35+
/nbdist/
36+
/.nb-gradle/
37+
38+
### VS Code ###
39+
.vscode/
40+
41+
### Mac OS ###
42+
.DS_Store

java/cursor-simple/accounts.db

20 KB
Binary file not shown.

java/cursor-simple/build.gradle.kts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
plugins {
2+
id("java")
3+
}
4+
5+
group = "org.lecture"
6+
version = "1.0-SNAPSHOT"
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
implementation("org.xerial:sqlite-jdbc:3.42.0.0")
14+
testImplementation(platform("org.junit:junit-bom:5.9.1"))
15+
testImplementation("org.junit.jupiter:junit-jupiter")
16+
}
17+
18+
tasks.test {
19+
useJUnitPlatform()
20+
}
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Sun Jul 23 17:47:55 CEST 2023
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)