test_codes.mod 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. MODULE test_codes ;
  2. (*
  3. m2SQLITE test: extended result codes and the get_table family.
  4. Triggers unique, not-null, primary-key and foreign-key violations
  5. and checks the exact extended codes, then reads a result table
  6. through the raw binding and the GetTable helpers.
  7. *)
  8. FROM SYSTEM IMPORT ADDRESS, ADR;
  9. FROM SQLite IMPORT DbHandle,
  10. SQLiteOk,
  11. SQLiteConstraintUnique, SQLiteConstraintNotNull,
  12. SQLiteConstraintPrimaryKey, SQLiteConstraintForeignKey,
  13. sqlite3_open, sqlite3_close,
  14. sqlite3_extended_result_codes, sqlite3_extended_errcode,
  15. sqlite3_get_table, sqlite3_free_table;
  16. FROM SQLiteUtils IMPORT ErrMsg, ExecSimple,
  17. GetTable, TableCell, FreeTable;
  18. FROM libc IMPORT printf;
  19. VAR
  20. db: DbHandle;
  21. tbl: ADDRESS; msg: ADDRESS;
  22. rows, cols, rc: INTEGER;
  23. buf: ARRAY [0..127] OF CHAR;
  24. PROCEDURE fail (what: ARRAY OF CHAR);
  25. VAR e: ARRAY [0..255] OF CHAR;
  26. BEGIN
  27. ErrMsg(db, e);
  28. printf("FAIL %s: %s\n", what, e);
  29. HALT(1)
  30. END fail;
  31. PROCEDURE check (rc: INTEGER; what: ARRAY OF CHAR);
  32. BEGIN
  33. IF rc # SQLiteOk THEN fail(what) END
  34. END check;
  35. PROCEDURE expect (sql: ARRAY OF CHAR; code: INTEGER; tag: ARRAY OF CHAR);
  36. VAR rc: INTEGER;
  37. BEGIN
  38. rc := ExecSimple(db, sql);
  39. printf("%s rc %d\n", tag, rc);
  40. IF rc # code THEN fail(tag) END;
  41. IF sqlite3_extended_errcode(db) # code THEN fail(tag) END
  42. END expect;
  43. BEGIN
  44. check(sqlite3_open(":memory:", db), "open");
  45. check(sqlite3_extended_result_codes(db, 1), "ext on");
  46. check(ExecSimple(db, "CREATE TABLE u(v TEXT UNIQUE);"), "uniq tab");
  47. check(ExecSimple(db, "INSERT INTO u VALUES('a');"), "uniq seed");
  48. expect("INSERT INTO u VALUES('a');",
  49. SQLiteConstraintUnique, "unique");
  50. check(ExecSimple(db, "CREATE TABLE n(v TEXT NOT NULL);"), "nn tab");
  51. expect("INSERT INTO n VALUES(NULL);",
  52. SQLiteConstraintNotNull, "notnull");
  53. check(ExecSimple(db, "CREATE TABLE p(id INTEGER PRIMARY KEY);"),
  54. "pk tab");
  55. check(ExecSimple(db, "INSERT INTO p VALUES(1);"), "pk seed");
  56. expect("INSERT INTO p VALUES(1);",
  57. SQLiteConstraintPrimaryKey, "primarykey");
  58. check(ExecSimple(db, "PRAGMA foreign_keys=ON;"), "fk pragma");
  59. check(ExecSimple(db, "CREATE TABLE ch(pid INTEGER"
  60. + " REFERENCES p(id));"), "fk tab");
  61. expect("INSERT INTO ch VALUES(2);",
  62. SQLiteConstraintForeignKey, "foreignkey");
  63. printf("extended codes ok\n");
  64. (* raw binding call *)
  65. check(ExecSimple(db, "CREATE TABLE t(a TEXT, b INTEGER);"), "t tab");
  66. check(ExecSimple(db, "INSERT INTO t VALUES('x', 1),"
  67. + "(NULL, 2), ('z', 3);"), "t seed");
  68. tbl := NIL; rows := 0; cols := 0; msg := NIL;
  69. rc := sqlite3_get_table(db, "SELECT a, b FROM t ORDER BY b;",
  70. tbl, rows, cols, ADR(msg));
  71. IF rc # SQLiteOk THEN fail("raw get_table") END;
  72. IF msg # NIL THEN fail("raw errmsg set") END;
  73. IF (rows # 3) OR (cols # 2) THEN fail("raw dims") END;
  74. TableCell(tbl, cols, -1, 0, buf);
  75. IF buf[0] # 'a' THEN fail("raw header") END;
  76. TableCell(tbl, cols, 1, 0, buf);
  77. IF buf[0] # 0C THEN fail("raw null cell") END;
  78. TableCell(tbl, cols, 2, 0, buf);
  79. IF buf[0] # 'z' THEN fail("raw cell") END;
  80. TableCell(tbl, cols, 0, 1, buf);
  81. IF buf[0] # '1' THEN fail("raw int cell") END;
  82. FreeTable(tbl);
  83. printf("raw table ok\n");
  84. (* helper path, including the failing-statement branch *)
  85. check(GetTable(db, "SELECT a FROM t WHERE b = 3;",
  86. tbl, rows, cols, buf), "helper table");
  87. IF (rows # 1) OR (cols # 1) THEN fail("helper dims") END;
  88. TableCell(tbl, cols, 0, 0, buf);
  89. printf("helper cell %s\n", buf);
  90. IF buf[0] # 'z' THEN fail("helper cell") END;
  91. FreeTable(tbl);
  92. rc := GetTable(db, "SELECT * FROM bogus;", tbl, rows, cols, buf);
  93. printf("helper bad rc %d err %s\n", rc, buf);
  94. IF rc = SQLiteOk THEN fail("helper bad ok") END;
  95. IF buf[0] = 0C THEN fail("helper bad msg") END;
  96. IF tbl # NIL THEN fail("helper bad table") END;
  97. check(sqlite3_close(db), "close");
  98. printf("PASS test_codes\n")
  99. END test_codes.