Coder Social home page Coder Social logo

IsNull? about go-sqlite HOT 3 CLOSED

zombiezen avatar zombiezen commented on May 31, 2024
IsNull?

from go-sqlite.

Comments (3)

zombiezen avatar zombiezen commented on May 31, 2024

You can compare Stmt.ColumnType against sqlite.TypeNull if you need to distinguish.

from go-sqlite.

delaneyj avatar delaneyj commented on May 31, 2024

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.

zombiezen avatar zombiezen commented on May 31, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.