forked from mattn/go-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite3_opt_dbpage.go
More file actions
78 lines (61 loc) · 1.63 KB
/
sqlite3_opt_dbpage.go
File metadata and controls
78 lines (61 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
//go:build sqlite_dbpage
// +build sqlite_dbpage
package sqlite3
/*
#cgo CFLAGS: -DSQLITE_ENABLE_DBPAGE_VTAB=1
#cgo LDFLAGS: -lm
#define SQLITE_RSYNC_NO_MAIN
#define SQLITE_RSYNC_USE_H
#include "sqlite_rsync.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
*/
import "C"
import (
"os"
"errors"
"unsafe"
)
func RsyncOrigin(dbPath string, isUri bool, pIn, pOut *os.File) error {
cOriginPath := C.CString(dbPath)
defer C.free(unsafe.Pointer(cOriginPath))
ctx := C.SQLiteRsync{
zOrigin: cOriginPath,
iProtocol: C.PROTOCOL_VERSION,
isRemote: 1,
pIn: C.fdopen((C.int)(pIn.Fd()), C.CString("rb")),
pOut: C.fdopen((C.int)(pOut.Fd()), C.CString("wb")),
}
flags := (C.int)(0)
if isUri {
flags = C.SQLITE_OPEN_URI
}
// start replicating
C.originSide(&ctx, flags)
if ctx.nErr > 0 {
return errors.New("RSyncOrigin faild")
}
return nil
}
func RsyncReplica(replicaPath string, pIn, pOut *os.File) error {
cReplicaPath := C.CString(replicaPath)
defer C.free(unsafe.Pointer(cReplicaPath))
ctx := C.SQLiteRsync{
zReplica: cReplicaPath,
iProtocol: C.PROTOCOL_VERSION,
isRemote: 1,
pIn: C.fdopen((C.int)(pIn.Fd()), C.CString("rb")),
pOut: C.fdopen((C.int)(pOut.Fd()), C.CString("wb")),
}
// do the replica
C.replicaSide(&ctx)
if ctx.nErr > 0 {
return errors.New("RSyncReplica faild")
}
return nil
}