db_common.nim 4.5 KB

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