sqlite3_load_extension.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
  31. }
  32. }
  33. rv = C.sqlite3_enable_load_extension(c.db, 0)
  34. if rv != C.SQLITE_OK {
  35. return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
  36. }
  37. return nil
  38. }
  39. // LoadExtension load the sqlite3 extension.
  40. func (c *SQLiteConn) LoadExtension(lib string, entry string) error {
  41. rv := C.sqlite3_enable_load_extension(c.db, 1)
  42. if rv != C.SQLITE_OK {
  43. return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
  44. }
  45. clib := C.CString(lib)
  46. defer C.free(unsafe.Pointer(clib))
  47. centry := C.CString(entry)
  48. defer C.free(unsafe.Pointer(centry))
  49. rv = C.sqlite3_load_extension(c.db, clib, centry, nil)
  50. if rv != C.SQLITE_OK {
  51. return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
  52. }
  53. rv = C.sqlite3_enable_load_extension(c.db, 0)
  54. if rv != C.SQLITE_OK {
  55. return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
  56. }
  57. return nil
  58. }