sqlite3_opt_serialize.go 1.9 KB

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