FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/tutorials/sql/02-insert_data.das at master · NicSavichev/daScript · GitHub
NicSavichev
/
daScript
Public
forked from
GaijinEntertainment/daScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
daScript
/
tutorials
/
sql
/
02-insert_data.das
Copy path
More file actions
More file actions
Latest commit
History
History
History
51 lines (48 loc) · 2.26 KB
Breadcrumbs
daScript
/
tutorials
/
sql
/
02-insert_data.das
Copy path
File metadata and controls
51 lines (48 loc) · 2.26 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
options
gen2
require
daslib
/
sql
require
sqlite
/
sqlite_boost
// [sql_table] is a structure macro. At compile time it reads the field
// metadata (@sql_primary_key etc.) and generates, attached to type Car:
// - per-type CREATE / DROP / INSERT SQL emitters
// - a row-binder that binds each field to the right ? placeholder
// The user-facing `create_table`, `drop_table_if_exists`, `insert` generics
// in sqlite_boost forward to those helpers.
// Optional `name="Cars"` keeps the on-disk table name explicit when the
// struct name (Car) doesn't match the table name you want (Cars).
[
sql_table
(
name
=
"Cars"
)]
struct
Car
{
@
sql_primary_key Id : int
Name : string
Price : int
}
[
export
]
def
main
() {
// `with_sqlite` panics if the file can't be opened. For user-supplied
// paths the future error-handling chunk introduces a non-panic
// Result<T, string>-returning sibling.
with_sqlite
(
"test.db"
)
$
(db) {
db
|
>
drop_table_if_exists
(
type
<
Car
>
)
db
|
>
create_table
(
type
<
Car
>
)
// `check_schema` verifies the open DB's columns match the [sql_table]
// struct on (name, type, NOT NULL, PRIMARY KEY). Recommended startup
// pattern for code that opens a DB it didn't just create — fails loud
// instead of letting a column rename surface as a silent wrong-result
// later. Use `try_check_schema` for the non-panicking Option form
// (returns SqlError = Option<string>: none on match, some(errmsg) on mismatch).
db
|
>
check_schema
(
type
<
Car
>
)
// `insert` is overloaded on Car vs array<Car>. Both overloads are
// generated by [sql_table]; the array form runs inside a single
// BEGIN/COMMIT transaction so 8 rows = 1 fsync, not 8.
// Panics on failure (constraint violation, BUSY, IO).
db
|
>
insert
([
Car
(
Id
=
1
,
Name
=
"Audi"
,
Price
=
52642
),
Car
(
Id
=
2
,
Name
=
"Mercedes"
,
Price
=
57127
),
Car
(
Id
=
3
,
Name
=
"Skoda"
,
Price
=
9000
),
Car
(
Id
=
4
,
Name
=
"Volvo"
,
Price
=
29000
),
Car
(
Id
=
5
,
Name
=
"Bentley"
,
Price
=
350000
),
Car
(
Id
=
6
,
Name
=
"Citroen"
,
Price
=
21000
),
Car
(
Id
=
7
,
Name
=
"Hummer"
,
Price
=
41400
),
Car
(
Id
=
8
,
Name
=
"Volkswagen"
,
Price
=
21600
)
])
}
}
Back
|
FazBrowse Home
|
New Git URL