forked from iamacarpet/go-sqlite3-win64
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.go
executable file
·83 lines (73 loc) · 1.68 KB
/
functions.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
// +build windows
// Copyright (C) 2016 Samuel Melrose <[email protected]>.
//
// Based on work by Yasuhiro Matsumoto <[email protected]>
// https://github.com/mattn/go-sqlite3
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package sqlite3
import (
"database/sql/driver"
"os"
"path/filepath"
"regexp"
"strings"
"unsafe"
)
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func basePath() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return ""
}
return dir + string(os.PathSeparator)
}
func BytePtrToString(p *byte) string {
var (
sizeTest byte
finalStr = make([]byte, 0)
)
for {
if *p == byte(0) {
break
}
finalStr = append(finalStr, *p)
p = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + unsafe.Sizeof(sizeTest)))
}
return string(finalStr[0:])
}
var tableNameRegexp = regexp.MustCompile(`(?is)^[\s]*INSERT[\s]+INTO[\s]+([^(\s]+)`)
func LastInsertID(c *SQLiteConn, table string, isTableName bool) (int64, error) {
if len(table) == 0 {
return 0, nil
}
if !isTableName {
matches := tableNameRegexp.FindStringSubmatch(table)
if matches == nil {
return 0, nil
}
table = strings.Trim(matches[1], "`")
}
rows, err := c.Query("SELECT last_insert_rowid() FROM `"+table+"`", nil)
if err != nil {
return 0, err
}
defer rows.Close()
v := make([]driver.Value, 1)
err = rows.Next(v)
if err != nil {
return 0, err
}
rowid, _ := v[0].(int64)
return rowid, nil
}