Comments (3)
You can compare Stmt.ColumnType
against sqlite.TypeNull
if you need to distinguish.
from go-sqlite.
Yep, that's what I ended up doing, so far these are the functions I've used so far to reduce boilerpalte in SQLite project...
func GetBytesStmt(stmt *sqlite.Stmt, param string) []byte {
expected := stmt.GetLen(param)
buf := make([]byte, expected)
if actual := stmt.GetBytes(param, buf); actual != expected {
panic(fmt.Errorf("expected %d bytes, got %d", expected, actual))
}
return buf
}
func NullableBytesStmt(stmt *sqlite.Stmt, param string) []byte {
if IsNull(stmt, param) {
return nil
}
return GetBytesStmt(stmt, param)
}
func IsNull(stmt *sqlite.Stmt, param string) bool {
col := stmt.ColumnIndex(param)
if col < 0 {
panic(fmt.Errorf("column %q not found", param))
}
return stmt.ColumnType(col) == sqlite.TypeNull
}
func GetNullableInt64(stmt *sqlite.Stmt, param string) *int64 {
if IsNull(stmt, param) {
return nil
}
x := stmt.GetInt64(param)
return &x
}
func GetNullableFloat64(stmt *sqlite.Stmt, param string) *float64 {
if IsNull(stmt, param) {
return nil
}
x := stmt.GetFloat(param)
return &x
}
func GetNullableText(stmt *sqlite.Stmt, param string) *string {
if IsNull(stmt, param) {
return nil
}
x := stmt.GetText(param)
return &x
}
func GetNullableBytes(stmt *sqlite.Stmt, param string) *[]byte {
if IsNull(stmt, param) {
return nil
}
b := GetBytesStmt(stmt, param)
return &b
}
const (
secondsInADay = 86400
UnixEpochJulianDay = 2440587.5
)
// TimeToJulianDay converts a time.Time into a Julian day.
func TimeToJulianDay(t time.Time) float64 {
return float64(t.UTC().Unix())/secondsInADay + UnixEpochJulianDay
}
// JulianDayToTime converts a Julian day into a time.Time.
func JulianDayToTime(d float64) time.Time {
return time.Unix(int64((d-UnixEpochJulianDay)*secondsInADay), 0).UTC()
}
func JulianNow() float64 {
return TimeToJulianDay(time.Now())
}
func TimestampJulian(ts *timestamppb.Timestamp) float64 {
return TimeToJulianDay(ts.AsTime())
}
func JulianDayToTimestamp(f float64) *timestamppb.Timestamp {
t := JulianDayToTime(f)
return timestamppb.New(t)
}
func JulianDayToTimestampStmt(stmt *sqlite.Stmt, param string) *timestamppb.Timestamp {
julianDays := stmt.GetFloat(param)
return JulianDayToTimestamp(julianDays)
}
func JulianDayToTimeStmt(stmt *sqlite.Stmt, param string) time.Time {
julianDays := stmt.GetFloat(param)
return JulianDayToTime(julianDays)
}
func JulianDayToTimeNullableStmt(stmt *sqlite.Stmt, param string) *time.Time {
if IsNull(stmt, param) {
return nil
}
t := JulianDayToTimeStmt(stmt, param)
return &t
}
Not sure if you'd be interested in a PR to include them.
from go-sqlite.
I'd accept a PR that adds ColumnIsNull
and IsNull
methods (I've written that enough times too), but I don't want to add the pointer versions: it would be a lot of methods.
from go-sqlite.
Related Issues (20)
- Allow schemas to disable foreign keys for a migration
- Support custom collating sequences
- Question about performance HOT 7
- sqlitemigration: Use table instead of pragma user_version HOT 4
- Helper functions for time.Time HOT 4
- Mutliple databases open slow down
- Panic in migration tool on anacrolix/torrent HOT 2
- Add ability to check that all `sqlitex.Exec*` parameters have been set HOT 1
- Force `OpenNoMutex` on all calls
- Transaction helpers don't work when manual BEGIN/END do HOT 5
- GetBool ? HOT 1
- Expose valid int64 types HOT 2
- User-defined aggregate window functions
- Log the generated SQL statement HOT 2
- Build error in Windows with "set GOARCH=386" HOT 2
- Online Backup API? HOT 1
- Support serialize/deserialize
- Foreign keys in pool? HOT 8
- Missing Columns information HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-sqlite.