sqlite3_load_extension.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. // +build !sqlite_omit_load_extension
  6. package sqlite3
  7. /*
  8. #ifndef USE_LIBSQLITE3
  9. #include <sqlite3-binding.h>
  10. #else
  11. #include <sqlite3.h>
  12. #endif
  13. #include <stdlib.h>
  14. */
  15. import "C"
  16. import (
  17. "errors"
  18. "unsafe"
  19. )
  20. func (c *SQLiteConn) loadExtensions(extensions []string) error {
  21. rv := C.sqlite3_enable_load_extension(c.db, 1)
  22. if rv != C.SQLITE_OK {
  23. return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
  24. }
  25. for _, extension := range extensions {
  26. cext := C.CString(extension)
  27. defer C.free(unsafe.Pointer(cext))
  28. rv = C.sqlite3_load_extension(c.db, cext, nil, nil)
  29. if rv != C.SQLITE_OK {
  30. C.sqlite3_enable_load_extension(c.db, 0)
  31. return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
  32. }
  33. }
  34. rv = C.sqlite3_enable_load_extension(c.db, 0)
  35. if rv != C.SQLITE_OK {
  36. return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
  37. }
  38. return nil
  39. }
  40. // LoadExtension load the sqlite3 extension.
  41. func (c *SQLiteConn) LoadExtension(lib string, entry string) error {
  42. rv := C.sqlite3_enable_load_extension(c.db, 1)
  43. if rv != C.SQLITE_OK {
  44. return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
  45. }
  46. clib := C.CString(lib)
  47. defer C.free(unsafe.Pointer(clib))
  48. centry := C.CString(entry)
  49. defer C.free(unsafe.Pointer(centry))
  50. rv = C.sqlite3_load_extension(c.db, clib, centry, nil)
  51. if rv != C.SQLITE_OK {
  52. return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
  53. }
  54. rv = C.sqlite3_enable_load_extension(c.db, 0)
  55. if rv != C.SQLITE_OK {
  56. return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
  57. }
  58. return nil
  59. }