doc.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Package sqlite3 provides interface to SQLite3 databases.
  3. This works as a driver for database/sql.
  4. Installation
  5. go get github.com/mattn/go-sqlite3
  6. Supported Types
  7. Currently, go-sqlite3 supports the following data types.
  8. +------------------------------+
  9. |go | sqlite3 |
  10. |----------|-------------------|
  11. |nil | null |
  12. |int | integer |
  13. |int64 | integer |
  14. |float64 | float |
  15. |bool | integer |
  16. |[]byte | blob |
  17. |string | text |
  18. |time.Time | timestamp/datetime|
  19. +------------------------------+
  20. SQLite3 Extension
  21. You can write your own extension module for sqlite3. For example, below is an
  22. extension for a Regexp matcher operation.
  23. #include <pcre.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <sqlite3ext.h>
  27. SQLITE_EXTENSION_INIT1
  28. static void regexp_func(sqlite3_context *context, int argc, sqlite3_value **argv) {
  29. if (argc >= 2) {
  30. const char *target = (const char *)sqlite3_value_text(argv[1]);
  31. const char *pattern = (const char *)sqlite3_value_text(argv[0]);
  32. const char* errstr = NULL;
  33. int erroff = 0;
  34. int vec[500];
  35. int n, rc;
  36. pcre* re = pcre_compile(pattern, 0, &errstr, &erroff, NULL);
  37. rc = pcre_exec(re, NULL, target, strlen(target), 0, 0, vec, 500);
  38. if (rc <= 0) {
  39. sqlite3_result_error(context, errstr, 0);
  40. return;
  41. }
  42. sqlite3_result_int(context, 1);
  43. }
  44. }
  45. #ifdef _WIN32
  46. __declspec(dllexport)
  47. #endif
  48. int sqlite3_extension_init(sqlite3 *db, char **errmsg,
  49. const sqlite3_api_routines *api) {
  50. SQLITE_EXTENSION_INIT2(api);
  51. return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8,
  52. (void*)db, regexp_func, NULL, NULL);
  53. }
  54. It needs to be built as a so/dll shared library. And you need to register
  55. the extension module like below.
  56. sql.Register("sqlite3_with_extensions",
  57. &sqlite3.SQLiteDriver{
  58. Extensions: []string{
  59. "sqlite3_mod_regexp",
  60. },
  61. })
  62. Then, you can use this extension.
  63. rows, err := db.Query("select text from mytable where name regexp '^golang'")
  64. Connection Hook
  65. You can hook and inject your code when the connection is established by setting
  66. ConnectHook to get the SQLiteConn.
  67. sql.Register("sqlite3_with_hook_example",
  68. &sqlite3.SQLiteDriver{
  69. ConnectHook: func(conn *sqlite3.SQLiteConn) error {
  70. sqlite3conn = append(sqlite3conn, conn)
  71. return nil
  72. },
  73. })
  74. You can also use database/sql.Conn.Raw (Go >= 1.13):
  75. conn, err := db.Conn(context.Background())
  76. // if err != nil { ... }
  77. defer conn.Close()
  78. err = conn.Raw(func (driverConn interface{}) error {
  79. sqliteConn := driverConn.(*sqlite3.SQLiteConn)
  80. // ... use sqliteConn
  81. })
  82. // if err != nil { ... }
  83. Go SQlite3 Extensions
  84. If you want to register Go functions as SQLite extension functions
  85. you can make a custom driver by calling RegisterFunction from
  86. ConnectHook.
  87. regex = func(re, s string) (bool, error) {
  88. return regexp.MatchString(re, s)
  89. }
  90. sql.Register("sqlite3_extended",
  91. &sqlite3.SQLiteDriver{
  92. ConnectHook: func(conn *sqlite3.SQLiteConn) error {
  93. return conn.RegisterFunc("regexp", regex, true)
  94. },
  95. })
  96. You can then use the custom driver by passing its name to sql.Open.
  97. var i int
  98. conn, err := sql.Open("sqlite3_extended", "./foo.db")
  99. if err != nil {
  100. panic(err)
  101. }
  102. err = db.QueryRow(`SELECT regexp("foo.*", "seafood")`).Scan(&i)
  103. if err != nil {
  104. panic(err)
  105. }
  106. See the documentation of RegisterFunc for more details.
  107. */
  108. package sqlite3