test_exec.mod 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. MODULE test_exec ;
  2. (*
  3. m2SQLITE smoke test: one-step exec interface (CREATE/INSERT),
  4. error capture via ExecWithErr, and change counters.
  5. *)
  6. FROM SQLite IMPORT DbHandle, SQLiteOk, SQLiteDone, SQLiteRow,
  7. sqlite3_open, sqlite3_close,
  8. sqlite3_changes, sqlite3_total_changes, sqlite3_last_insert_rowid;
  9. FROM SQLiteUtils IMPORT ExecSimple, ExecWithErr;
  10. FROM libc IMPORT printf;
  11. VAR db: DbHandle; rc: INTEGER; buf: ARRAY [0..255] OF CHAR;
  12. rowid: LONGINT;
  13. BEGIN
  14. IF sqlite3_open(":memory:", db) # SQLiteOk THEN
  15. printf("FAIL test_exec: open\n");
  16. HALT(1)
  17. END;
  18. rc := ExecSimple(db, "CREATE TABLE t(id INTEGER PRIMARY KEY, name TEXT);");
  19. IF rc # SQLiteOk THEN
  20. printf("FAIL test_exec: create rc=%d\n", rc);
  21. HALT(1)
  22. END;
  23. rc := ExecSimple(db, "INSERT INTO t(name) VALUES('alice');");
  24. IF rc # SQLiteOk THEN
  25. printf("FAIL test_exec: insert rc=%d\n", rc);
  26. HALT(1)
  27. END;
  28. IF sqlite3_changes(db) # 1 THEN
  29. printf("FAIL test_exec: changes=%d want 1\n", sqlite3_changes(db));
  30. HALT(1)
  31. END;
  32. rowid := sqlite3_last_insert_rowid(db);
  33. printf("last_rowid=%ld\n", rowid);
  34. IF rowid # 1 THEN
  35. printf("FAIL test_exec: rowid=%ld want 1\n", rowid);
  36. HALT(1)
  37. END;
  38. (* failing statement must report its message *)
  39. rc := ExecWithErr(db, "INSERT INTO bogus(x) VALUES(1);", buf);
  40. printf("bad exec rc=%d err=%s\n", rc, buf);
  41. IF rc = SQLiteOk THEN
  42. printf("FAIL test_exec: bad SQL succeeded\n");
  43. HALT(1)
  44. END;
  45. IF buf[0] = 0C THEN
  46. printf("FAIL test_exec: empty error message\n");
  47. HALT(1)
  48. END;
  49. printf("total_changes=%d\n", sqlite3_total_changes(db));
  50. rc := sqlite3_close(db);
  51. IF rc # SQLiteOk THEN
  52. printf("FAIL test_exec: close rc=%d\n", rc);
  53. HALT(1)
  54. END;
  55. printf("PASS test_exec\n")
  56. END test_exec.