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