-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
106 lines (82 loc) · 2.04 KB
/
main.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
/*
#cgo LDFLAGS: -shared
#include "postgres.h"
#include "fmgr.h"
#include "utils/elog.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/nabstime.h"
#include "utils/timestamp.h"
#include "datatype/timestamp.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(Ulid);
PG_FUNCTION_INFO_V1(UlidToTime);
PG_FUNCTION_INFO_V1(UlidToLocalTime);
Datum void_datum(){
PG_RETURN_VOID();
}
void elog_notice(char* string) {
elog(NOTICE, string, "");
}
void elog_error(char* string) {
elog(ERROR, string, "");
}
Datum get_arg(PG_FUNCTION_ARGS, uint i) {
return PG_GETARG_DATUM(i);
}
Datum cstring_to_datum(char *val) {
return CStringGetDatum(cstring_to_text(val));
}
char* datum_to_cstring(Datum val) {
return DatumGetCString(text_to_cstring((struct varlena *)val));
}
Datum timestamp_to_datum(Timestamp val) {
return TimestampGetDatum(val);
}
Datum timestamptz_to_datum(TimestampTz val) {
return TimestampTzGetDatum(val);
}
*/
import "C"
import (
"log"
"time"
"unsafe"
)
func main() {}
type Datum C.Datum
func toDatum(val string) Datum {
return (Datum)(C.cstring_to_datum(C.CString(val)))
}
func tsToDatum(val time.Time) Datum {
return (Datum)(C.timestamp_to_datum(C.Timestamp((val.Unix() - 946684800) * int64(C.USECS_PER_SEC))))
}
func tstzToDatum(val time.Time) Datum {
return (Datum)(C.timestamptz_to_datum(C.TimestampTz((val.Unix() - 946684800) * int64(C.USECS_PER_SEC))))
}
func getArgText(fcinfo *C.FunctionCallInfoData, n int) string {
return C.GoString(C.datum_to_cstring(C.get_arg((*C.struct_FunctionCallInfoData)(unsafe.Pointer(fcinfo)), C.uint(n))))
}
type elogLevel int
const (
noticeLevel elogLevel = iota
errorLevel
)
type elog struct {
Level elogLevel
}
func (e *elog) Write(p []byte) (n int, err error) {
switch e.Level {
case noticeLevel:
C.elog_notice(C.CString(string(p)))
case errorLevel:
C.elog_error(C.CString(string(p)))
}
return len(p), nil
}
func NewErrorLogger(prefix string, flag int) *log.Logger {
return log.New(&elog{Level: errorLevel}, prefix, flag)
}