Fork github.com/mattn/go-sqlite3 with adjustment for go1.16.2
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.

82 lines
1.9 KiB

  1. // +build !libsqlite3 sqlite_serialize
  2. package sqlite3
  3. /*
  4. #ifndef USE_LIBSQLITE3
  5. #include <sqlite3-binding.h>
  6. #else
  7. #include <sqlite3.h>
  8. #endif
  9. #include <stdlib.h>
  10. #include <stdint.h>
  11. */
  12. import "C"
  13. import (
  14. "fmt"
  15. "math"
  16. "reflect"
  17. "unsafe"
  18. )
  19. // Serialize returns a byte slice that is a serialization of the database.
  20. //
  21. // See https://www.sqlite.org/c3ref/serialize.html
  22. func (c *SQLiteConn) Serialize(schema string) ([]byte, error) {
  23. if schema == "" {
  24. schema = "main"
  25. }
  26. var zSchema *C.char
  27. zSchema = C.CString(schema)
  28. defer C.free(unsafe.Pointer(zSchema))
  29. var sz C.sqlite3_int64
  30. ptr := C.sqlite3_serialize(c.db, zSchema, &sz, 0)
  31. if ptr == nil {
  32. return nil, fmt.Errorf("serialize failed")
  33. }
  34. defer C.sqlite3_free(unsafe.Pointer(ptr))
  35. if sz > C.sqlite3_int64(math.MaxUint32) {
  36. return nil, fmt.Errorf("serialized database is too large (%d bytes)", sz)
  37. }
  38. cBuf := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
  39. Data: uintptr(unsafe.Pointer(ptr)),
  40. Len: int(sz),
  41. Cap: int(sz),
  42. }))
  43. res := make([]byte, int(sz))
  44. copy(res, cBuf)
  45. return res, nil
  46. }
  47. // Deserialize causes the connection to disconnect from the current database and
  48. // then re-open as an in-memory database based on the contents of the byte slice.
  49. //
  50. // See https://www.sqlite.org/c3ref/deserialize.html
  51. func (c *SQLiteConn) Deserialize(b []byte, schema string) error {
  52. if schema == "" {
  53. schema = "main"
  54. }
  55. var zSchema *C.char
  56. zSchema = C.CString(schema)
  57. defer C.free(unsafe.Pointer(zSchema))
  58. tmpBuf := (*C.uchar)(C.sqlite3_malloc64(C.sqlite3_uint64(len(b))))
  59. cBuf := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
  60. Data: uintptr(unsafe.Pointer(tmpBuf)),
  61. Len: len(b),
  62. Cap: len(b),
  63. }))
  64. copy(cBuf, b)
  65. rc := C.sqlite3_deserialize(c.db, zSchema, tmpBuf, C.sqlite3_int64(len(b)),
  66. C.sqlite3_int64(len(b)), C.SQLITE_DESERIALIZE_FREEONCLOSE)
  67. if rc != C.SQLITE_OK {
  68. return fmt.Errorf("deserialize failed with return %v", rc)
  69. }
  70. return nil
  71. }