You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.6 KiB

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