annotate semiconginev2/thirdparty/db_connector/db_common.nim @ 1254:b0f4c8ccd49a

did: stuff to test gltf importer
author sam <sam@basx.dev>
date Sat, 27 Jul 2024 20:47:54 +0700
parents 56781cc0fc7c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1191
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
1 #
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
2 #
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
3 # Nim's Runtime Library
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
4 # (c) Copyright 2015 Andreas Rumpf
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
5 #
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
6 # See the file "copying.txt", included in this
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
7 # distribution, for details about the copyright.
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
8 #
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
9
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
10 ## .. note:: In order to use this module, run `nimble install db_connector`.
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
11 ##
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
12 ## Common datatypes and definitions for all `db_*.nim` (
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
13 ## `db_mysql <db_mysql.html>`_, `db_postgres <db_postgres.html>`_,
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
14 ## and `db_sqlite <db_sqlite.html>`_) modules.
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
15
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
16 type
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
17 DbError* = object of IOError ## exception that is raised if a database error occurs
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
18
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
19 SqlQuery* = distinct string ## an SQL query string
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
20
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
21
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
22 DbEffect* = object of IOEffect ## effect that denotes a database operation
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
23 ReadDbEffect* = object of DbEffect ## effect that denotes a read operation
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
24 WriteDbEffect* = object of DbEffect ## effect that denotes a write operation
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
25
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
26 DbTypeKind* = enum ## a superset of datatypes that might be supported.
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
27 dbUnknown, ## unknown datatype
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
28 dbSerial, ## datatype used for primary auto-increment keys
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
29 dbNull, ## datatype used for the NULL value
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
30 dbBit, ## bit datatype
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
31 dbBool, ## boolean datatype
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
32 dbBlob, ## blob datatype
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
33 dbFixedChar, ## string of fixed length
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
34 dbVarchar, ## string datatype
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
35 dbJson, ## JSON datatype
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
36 dbXml, ## XML datatype
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
37 dbInt, ## some integer type
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
38 dbUInt, ## some unsigned integer type
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
39 dbDecimal, ## decimal numbers (fixed-point number)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
40 dbFloat, ## some floating point type
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
41 dbDate, ## a year-month-day description
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
42 dbTime, ## HH:MM:SS information
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
43 dbDatetime, ## year-month-day and HH:MM:SS information,
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
44 ## plus optional time or timezone information
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
45 dbTimestamp, ## Timestamp values are stored as the number of seconds
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
46 ## since the epoch ('1970-01-01 00:00:00' UTC).
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
47 dbTimeInterval, ## an interval [a,b] of times
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
48 dbEnum, ## some enum
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
49 dbSet, ## set of enum values
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
50 dbArray, ## an array of values
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
51 dbComposite, ## composite type (record, struct, etc)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
52 dbUrl, ## a URL
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
53 dbUuid, ## a UUID
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
54 dbInet, ## an IP address
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
55 dbMacAddress, ## a MAC address
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
56 dbGeometry, ## some geometric type
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
57 dbPoint, ## Point on a plane (x,y)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
58 dbLine, ## Infinite line ((x1,y1),(x2,y2))
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
59 dbLseg, ## Finite line segment ((x1,y1),(x2,y2))
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
60 dbBox, ## Rectangular box ((x1,y1),(x2,y2))
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
61 dbPath, ## Closed or open path (similar to polygon) ((x1,y1),...)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
62 dbPolygon, ## Polygon (similar to closed path) ((x1,y1),...)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
63 dbCircle, ## Circle <(x,y),r> (center point and radius)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
64 dbUser1, ## user definable datatype 1 (for unknown extensions)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
65 dbUser2, ## user definable datatype 2 (for unknown extensions)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
66 dbUser3, ## user definable datatype 3 (for unknown extensions)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
67 dbUser4, ## user definable datatype 4 (for unknown extensions)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
68 dbUser5 ## user definable datatype 5 (for unknown extensions)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
69
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
70 DbType* = object ## describes a database type
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
71 kind*: DbTypeKind ## the kind of the described type
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
72 notNull*: bool ## does the type contain NULL?
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
73 name*: string ## the name of the type
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
74 size*: Natural ## the size of the datatype; 0 if of variable size
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
75 maxReprLen*: Natural ## maximal length required for the representation
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
76 precision*, scale*: Natural ## precision and scale of the number
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
77 min*, max*: BiggestInt ## the minimum and maximum of allowed values
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
78 validValues*: seq[string] ## valid values of an enum or a set
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
79
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
80 DbColumn* = object ## information about a database column
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
81 name*: string ## name of the column
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
82 tableName*: string ## name of the table the column belongs to (optional)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
83 typ*: DbType ## type of the column
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
84 primaryKey*: bool ## is this a primary key?
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
85 foreignKey*: bool ## is this a foreign key?
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
86 DbColumns* = seq[DbColumn]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
87
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
88 template sql*(query: string): SqlQuery =
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
89 ## constructs a SqlQuery from the string `query`. This is supposed to be
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
90 ## used as a raw-string-literal modifier:
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
91 ## `sql"update user set counter = counter + 1"`
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
92 ##
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
93 ## If assertions are turned off, it does nothing. If assertions are turned
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
94 ## on, later versions will check the string for valid syntax.
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
95 SqlQuery(query)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
96
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
97 proc dbError*(msg: string) {.noreturn, noinline.} =
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
98 ## raises an DbError exception with message `msg`.
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
99 var e: ref DbError
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
100 new(e)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
101 e.msg = msg
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
102 raise e