tpostgres.nim 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import db_postgres, strutils
  2. let db = open("localhost", "dom", "", "test")
  3. db.exec(sql"DROP TABLE IF EXISTS myTable")
  4. db.exec(sql("""CREATE TABLE myTable (
  5. id integer PRIMARY KEY,
  6. name varchar(50) not null)"""))
  7. let name = "Dom"
  8. db.exec(sql"INSERT INTO myTable (id, name) VALUES (0, ?)",
  9. name)
  10. doAssert db.getValue(sql"SELECT name FROM myTable") == name
  11. # Check issue #3513
  12. doAssert db.getValue(sql"SELECT name FROM myTable") == name
  13. # issue #3560
  14. proc addToDb(conn: DbConn, fileId: int, fileName: string): int64 =
  15. result = conn.insertId(sql("INSERT into files (id, filename) VALUES (?, ?)"), fileId, fileName)
  16. db.exec(sql"DROP TABLE IF EXISTS files")
  17. db.exec(sql"DROP TABLE IF EXISTS fileobjects")
  18. db.exec(sql("""CREATE TABLE FILEOBJECTS(
  19. ID SERIAL PRIMARY KEY,
  20. FILE_SIZE INT,
  21. MD5 CHAR(32) NOT NULL UNIQUE
  22. );"""))
  23. db.exec(sql("""CREATE TABLE FILES(
  24. ID SERIAL PRIMARY KEY,
  25. OBJECT_ID INT,
  26. FILENAME TEXT NOT NULL,
  27. URI TEXT,
  28. SCHEME CHAR(10),
  29. PUBLIC BOOLEAN DEFAULT FALSE,
  30. CONSTRAINT fk1_fileobjs FOREIGN KEY (object_id)
  31. REFERENCES fileobjects (id) MATCH SIMPLE
  32. ON DELETE CASCADE
  33. );"""))
  34. let f1 = db.addToDb(1, "hello.tmp")
  35. doAssert f1 == 1
  36. let f2 = db.addToDb(2, "hello2.tmp")
  37. doAssert f2 == 2
  38. # PreparedStmt vs. normal query
  39. try:
  40. echo db.getValue(sql("select * from files where id = $1"), 1)
  41. doAssert false, "Exception expected"
  42. except DbError:
  43. let msg = getCurrentExceptionMsg().normalize
  44. doAssert "expects" in msg
  45. doAssert "?" in msg
  46. doAssert "parameter substitution" in msg
  47. doAssert db.getValue(sql("select filename from files where id = ?"), 1) == "hello.tmp"
  48. var first = prepare(db, "one", sql"select filename from files where id = $1", 1)
  49. doAssert db.getValue(first, 1) == "hello.tmp"
  50. try:
  51. var second = prepare(db, "two", sql"select filename from files where id = ?", 1)
  52. doAssert false, "Exception expected"
  53. except:
  54. let msg = getCurrentExceptionMsg().normalize
  55. doAssert "expects" in msg
  56. doAssert "$1" in msg
  57. doAssert "parameter substitution" in msg
  58. # issue #3569
  59. db.exec(SqlQuery("DROP TABLE IF EXISTS tags"))
  60. db.exec(SqlQuery("CREATE TABLE tags(id serial UNIQUE, name varchar(255))"))
  61. for i in 1..10:
  62. var name = "t" & $i
  63. echo(name)
  64. discard db.getRow(
  65. SqlQuery("INSERT INTO tags(name) VALUES(\'$1\') RETURNING id" % [name]))
  66. # get column details
  67. db.exec(SqlQuery("DROP TABLE IF EXISTS dbtypes;"))
  68. db.exec(SqlQuery("DROP TYPE IF EXISTS custom_enum;"))
  69. db.exec(SqlQuery("CREATE TYPE custom_enum AS ENUM ('1', '2', '3');"))
  70. db.exec(SqlQuery("DROP TYPE IF EXISTS custom_composite;"))
  71. db.exec(SqlQuery("CREATE TYPE custom_composite AS (r double precision, i double precision);"))
  72. db.exec(SqlQuery("""CREATE TABLE dbtypes(
  73. id serial UNIQUE,
  74. bytea_col bytea,
  75. smallint_col smallint,
  76. integer_col integer,
  77. bigint_col bigint,
  78. decimal_col decimal,
  79. numeric_col numeric,
  80. real_col real,
  81. double_precision_col double precision,
  82. smallserial_col smallserial,
  83. serial_col serial,
  84. bigserial_col bigserial,
  85. money_col money,
  86. varchar_col varchar(10),
  87. character_col character(1),
  88. text_col text,
  89. timestamp_col timestamp,
  90. date_col date,
  91. time_col time,
  92. interval_col interval,
  93. bool_col boolean,
  94. custom_enum_col custom_enum,
  95. point_col point,
  96. line_col line,
  97. lseg_col lseg,
  98. box_col box,
  99. path_col path,
  100. polygon_col polygon,
  101. circle_col circle,
  102. cidr_col cidr,
  103. inet_col inet,
  104. macaddr_col macaddr,
  105. bit_col bit,
  106. varbit_col bit(3),
  107. tsvector_col tsvector,
  108. tsquery_col tsquery,
  109. uuid_col uuid,
  110. xml_col xml,
  111. json_col json,
  112. array_col integer[],
  113. custom_composite_col custom_composite,
  114. range_col int4range
  115. );"""))
  116. db.exec(SqlQuery("INSERT INTO dbtypes (id) VALUES(0);"))
  117. var dbCols : DbColumns = @[]
  118. for row in db.instantRows(dbCols, sql"SELECT * FROM dbtypes"):
  119. doAssert len(dbCols) == 42
  120. doAssert dbCols[0].name == "id"
  121. doAssert dbCols[0].typ.kind == DbTypeKind.dbInt
  122. doAssert dbCols[0].typ.name == "int4"
  123. doAssert dbCols[0].typ.size == 4
  124. doAssert dbCols[1].name == "bytea_col"
  125. doAssert dbCols[1].typ.kind == DbTypeKind.dbBlob
  126. doAssert dbCols[1].typ.name == "bytea"
  127. doAssert dbCols[2].name == "smallint_col"
  128. doAssert dbCols[2].typ.kind == DbTypeKind.dbInt
  129. doAssert dbCols[2].typ.name == "int2"
  130. doAssert dbCols[2].typ.size == 2
  131. doAssert dbCols[3].name == "integer_col"
  132. doAssert dbCols[3].typ.kind == DbTypeKind.dbInt
  133. doAssert dbCols[3].typ.name == "int4"
  134. doAssert dbCols[3].typ.size == 4
  135. doAssert dbCols[4].name == "bigint_col"
  136. doAssert dbCols[4].typ.kind == DbTypeKind.dbInt
  137. doAssert dbCols[4].typ.name == "int8"
  138. doAssert dbCols[4].typ.size == 8
  139. doAssert dbCols[5].name == "decimal_col"
  140. doAssert dbCols[5].typ.kind == DbTypeKind.dbDecimal
  141. doAssert dbCols[5].typ.name == "numeric"
  142. doAssert dbCols[6].name == "numeric_col"
  143. doAssert dbCols[6].typ.kind == DbTypeKind.dbDecimal
  144. doAssert dbCols[6].typ.name == "numeric"
  145. doAssert dbCols[7].name == "real_col"
  146. doAssert dbCols[7].typ.kind == DbTypeKind.dbFloat
  147. doAssert dbCols[7].typ.name == "float4"
  148. doAssert dbCols[8].name == "double_precision_col"
  149. doAssert dbCols[8].typ.kind == DbTypeKind.dbFloat
  150. doAssert dbCols[8].typ.name == "float8"
  151. doAssert dbCols[9].name == "smallserial_col"
  152. doAssert dbCols[9].typ.kind == DbTypeKind.dbInt
  153. doAssert dbCols[9].typ.name == "int2"
  154. doAssert dbCols[10].name == "serial_col"
  155. doAssert dbCols[10].typ.kind == DbTypeKind.dbInt
  156. doAssert dbCols[10].typ.name == "int4"
  157. doAssert dbCols[11].name == "bigserial_col"
  158. doAssert dbCols[11].typ.kind == DbTypeKind.dbInt
  159. doAssert dbCols[11].typ.name == "int8"
  160. doAssert dbCols[12].name == "money_col"
  161. doAssert dbCols[12].typ.kind == DbTypeKind.dbDecimal
  162. doAssert dbCols[12].typ.name == "money"
  163. doAssert dbCols[13].name == "varchar_col"
  164. doAssert dbCols[13].typ.kind == DbTypeKind.dbVarchar
  165. doAssert dbCols[13].typ.name == "varchar"
  166. doAssert dbCols[14].name == "character_col"
  167. doAssert dbCols[14].typ.kind == DbTypeKind.dbFixedChar
  168. doAssert dbCols[14].typ.name == "bpchar"
  169. doAssert dbCols[15].name == "text_col"
  170. doAssert dbCols[15].typ.kind == DbTypeKind.dbVarchar
  171. doAssert dbCols[15].typ.name == "text"
  172. doAssert dbCols[16].name == "timestamp_col"
  173. doAssert dbCols[16].typ.kind == DbTypeKind.dbTimestamp
  174. doAssert dbCols[16].typ.name == "timestamp"
  175. doAssert dbCols[17].name == "date_col"
  176. doAssert dbCols[17].typ.kind == DbTypeKind.dbDate
  177. doAssert dbCols[17].typ.name == "date"
  178. doAssert dbCols[18].name == "time_col"
  179. doAssert dbCols[18].typ.kind == DbTypeKind.dbTime
  180. doAssert dbCols[18].typ.name == "time"
  181. doAssert dbCols[19].name == "interval_col"
  182. doAssert dbCols[19].typ.kind == DbTypeKind.dbTimeInterval
  183. doAssert dbCols[19].typ.name == "interval"
  184. doAssert dbCols[20].name == "bool_col"
  185. doAssert dbCols[20].typ.kind == DbTypeKind.dbBool
  186. doAssert dbCols[20].typ.name == "bool"
  187. doAssert dbCols[21].name == "custom_enum_col"
  188. doAssert dbCols[21].typ.kind == DbTypeKind.dbUnknown
  189. doAssert parseInt(dbCols[21].typ.name) > 0
  190. doAssert dbCols[22].name == "point_col"
  191. doAssert dbCols[22].typ.kind == DbTypeKind.dbPoint
  192. doAssert dbCols[22].typ.name == "point"
  193. doAssert dbCols[23].name == "line_col"
  194. doAssert dbCols[23].typ.kind == DbTypeKind.dbLine
  195. doAssert dbCols[23].typ.name == "line"
  196. doAssert dbCols[24].name == "lseg_col"
  197. doAssert dbCols[24].typ.kind == DbTypeKind.dbLseg
  198. doAssert dbCols[24].typ.name == "lseg"
  199. doAssert dbCols[25].name == "box_col"
  200. doAssert dbCols[25].typ.kind == DbTypeKind.dbBox
  201. doAssert dbCols[25].typ.name == "box"
  202. doAssert dbCols[26].name == "path_col"
  203. doAssert dbCols[26].typ.kind == DbTypeKind.dbPath
  204. doAssert dbCols[26].typ.name == "path"
  205. doAssert dbCols[27].name == "polygon_col"
  206. doAssert dbCols[27].typ.kind == DbTypeKind.dbPolygon
  207. doAssert dbCols[27].typ.name == "polygon"
  208. doAssert dbCols[28].name == "circle_col"
  209. doAssert dbCols[28].typ.kind == DbTypeKind.dbCircle
  210. doAssert dbCols[28].typ.name == "circle"
  211. doAssert dbCols[29].name == "cidr_col"
  212. doAssert dbCols[29].typ.kind == DbTypeKind.dbInet
  213. doAssert dbCols[29].typ.name == "cidr"
  214. doAssert dbCols[30].name == "inet_col"
  215. doAssert dbCols[30].typ.kind == DbTypeKind.dbInet
  216. doAssert dbCols[30].typ.name == "inet"
  217. doAssert dbCols[31].name == "macaddr_col"
  218. doAssert dbCols[31].typ.kind == DbTypeKind.dbMacAddress
  219. doAssert dbCols[31].typ.name == "macaddr"
  220. doAssert dbCols[32].name == "bit_col"
  221. doAssert dbCols[32].typ.kind == DbTypeKind.dbBit
  222. doAssert dbCols[32].typ.name == "bit"
  223. doAssert dbCols[33].name == "varbit_col"
  224. doAssert dbCols[33].typ.kind == DbTypeKind.dbBit
  225. doAssert dbCols[33].typ.name == "bit"
  226. doAssert dbCols[34].name == "tsvector_col"
  227. doAssert dbCols[34].typ.kind == DbTypeKind.dbVarchar
  228. doAssert dbCols[34].typ.name == "tsvector"
  229. doAssert dbCols[35].name == "tsquery_col"
  230. doAssert dbCols[35].typ.kind == DbTypeKind.dbVarchar
  231. doAssert dbCols[35].typ.name == "tsquery"
  232. doAssert dbCols[36].name == "uuid_col"
  233. doAssert dbCols[36].typ.kind == DbTypeKind.dbVarchar
  234. doAssert dbCols[36].typ.name == "uuid"
  235. doAssert dbCols[37].name == "xml_col"
  236. doAssert dbCols[37].typ.kind == DbTypeKind.dbXml
  237. doAssert dbCols[37].typ.name == "xml"
  238. doAssert dbCols[38].name == "json_col"
  239. doAssert dbCols[38].typ.kind == DbTypeKind.dbJson
  240. doAssert dbCols[38].typ.name == "json"
  241. doAssert dbCols[39].name == "array_col"
  242. doAssert dbCols[39].typ.kind == DbTypeKind.dbArray
  243. doAssert dbCols[39].typ.name == "int4[]"
  244. doAssert dbCols[40].name == "custom_composite_col"
  245. doAssert dbCols[40].typ.kind == DbTypeKind.dbUnknown
  246. doAssert parseInt(dbCols[40].typ.name) > 0
  247. doAssert dbCols[41].name == "range_col"
  248. doAssert dbCols[41].typ.kind == DbTypeKind.dbComposite
  249. doAssert dbCols[41].typ.name == "int4range"
  250. # issue 6571
  251. db.exec(sql"DROP TABLE IF EXISTS DICTIONARY")
  252. db.exec(sql("""CREATE TABLE DICTIONARY(
  253. id SERIAL PRIMARY KEY,
  254. entry VARCHAR(1000) NOT NULL,
  255. definition VARCHAR(4000) NOT NULL
  256. );"""))
  257. var entry = "あっそ"
  258. var definition = "(int) (See ああそうそう) oh, really (uninterested)/oh yeah?/hmmmmm"
  259. discard db.getRow(
  260. SqlQuery("INSERT INTO DICTIONARY(entry, definition) VALUES(\'$1\', \'$2\') RETURNING id" % [entry, definition]))
  261. doAssert db.getValue(sql"SELECT definition FROM DICTIONARY WHERE entry = ?", entry) == definition
  262. entry = "Format string entry"
  263. definition = "Format string definition"
  264. db.exec(sql"INSERT INTO DICTIONARY(entry, definition) VALUES (?, ?)", entry, definition)
  265. doAssert db.getValue(sql"SELECT definition FROM DICTIONARY WHERE entry = ?", entry) == definition
  266. echo("All tests succeeded!")
  267. db.close()