types

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KindNull          byte = 0
	KindInt64         byte = 1
	KindUint64        byte = 2
	KindFloat32       byte = 3
	KindFloat64       byte = 4
	KindString        byte = 5
	KindBytes         byte = 6
	KindMysqlDuration byte = 9
	KindMysqlEnum     byte = 10
	KindMysqlTime     byte = 13
	KindInterface     byte = 14
)

Kind constants.

View Source
const (
	// UnspecifiedFsp is the unspecified fractional seconds part.
	UnspecifiedFsp = int8(-1)
	// DefaultFsp is the default digit of fractional seconds part.
	// MySQL use 0 as the default Fsp.
	DefaultFsp = int8(0)
)
View Source
const (
	TimeFormat = "2006-01-02 15:04:05"
	// TimeFSPFormat is time format with fractional seconds precision.
	TimeFSPFormat = "2006-01-02 15:04:05.000000"
)

Time format without fractional seconds precision.

View Source
const (
	// MinYear is the minimum for mysql year type.
	MinYear int16 = 1901
	// MaxYear is the maximum for mysql year type.
	MaxYear int16 = 2155
	// MaxDuration is the maximum for duration.
	MaxDuration int64 = 838*10000 + 59*100 + 59
	// MinTime is the minimum for mysql time type.
	MinTime = -gotime.Duration(838*3600+59*60+59) * gotime.Second
	// MaxTime is the maximum for mysql time type.
	MaxTime = gotime.Duration(838*3600+59*60+59) * gotime.Second
	// ZeroDatetimeStr is the string representation of a zero datetime.
	ZeroDatetimeStr = "0000-00-00 00:00:00"
	// ZeroDateStr is the string representation of a zero date.
	ZeroDateStr = "0000-00-00"

	// TimeMaxHour is the max hour for mysql time type.
	TimeMaxHour = 838
	// TimeMaxMinute is the max minute for mysql time type.
	TimeMaxMinute = 59
	// TimeMaxSecond is the max second for mysql time type.
	TimeMaxSecond = 59
	// TimeMaxValue is the maximum value for mysql time type.
	TimeMaxValue = TimeMaxHour*10000 + TimeMaxMinute*100 + TimeMaxSecond
	// TimeMaxValueSeconds is the maximum second value for mysql time type.
	TimeMaxValueSeconds = TimeMaxHour*3600 + TimeMaxMinute*60 + TimeMaxSecond
)
View Source
const MyDecimalStructSize = 40

MyDecimalStructSize is the struct size of MyDecimal.

Variables

View Source
var (
	// MonthNames lists names of months, which are used in builtin time function `monthname`.
	MonthNames = []string{
		"January", "February",
		"March", "April",
		"May", "June",
		"July", "August",
		"September", "October",
		"November", "December",
	}
)
View Source
var ZeroCoreTime = CoreTime(0)

ZeroCoreTime is the zero value for TimeInternal type.

View Source
var (

	// ZeroCoreTime is the zero value for Time type.
	ZeroTime = Time{}
)

Functions

func DecimalAdd

func DecimalAdd(from1, from2, to *MyDecimal) error

DecimalAdd adds two decimals, sets the result to 'to'. Note: DO NOT use `from1` or `from2` as `to` since the metadata of `to` may be changed during evaluating.

func DecimalBinSize

func DecimalBinSize(precision, frac int) int

DecimalBinSize returns the size of array to hold a binary representation of a decimal.

func DecimalDiv

func DecimalDiv(from1, from2, to *MyDecimal, fracIncr int) error

DecimalDiv does division of two decimals.

from1 - dividend from2 - divisor to - quotient fracIncr - increment of fraction

func DecimalMod

func DecimalMod(from1, from2, to *MyDecimal) error

DecimalMod does modulus of two decimals.

    from1   - dividend
    from2   - divisor
    to      - modulus

RETURN VALUE
  E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_DIV_ZERO;

NOTES
  see do_div_mod()

DESCRIPTION
  the modulus R in    R = M mod N

 is defined as

   0 <= |R| < |M|
   sign R == sign M
   R = M - k*N, where k is integer

 thus, there's no requirement for M or N to be integers

func DecimalMul

func DecimalMul(from1, from2, to *MyDecimal) error

DecimalMul multiplies two decimals.

    from1, from2 - factors
    to      - product

RETURN VALUE
  E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW;

NOTES
  in this implementation, with wordSize=4 we have digitsPerWord=9,
  and 63-digit number will take only 7 words (basically a 7-digit
  "base 999999999" number).  Thus there's no need in fast multiplication
  algorithms, 7-digit numbers can be multiplied with a naive O(n*n)
  method.

  XXX if this library is to be used with huge numbers of thousands of
  digits, fast multiplication must be implemented.

func DecimalPeak

func DecimalPeak(b []byte) (int, error)

DecimalPeak returns the length of the encoded decimal.

func DecimalSub

func DecimalSub(from1, from2, to *MyDecimal) error

DecimalSub subs one decimal from another, sets the result to 'to'.

func FormatIntWidthN

func FormatIntWidthN(num, n int) string

FormatIntWidthN uses to format int with width. Insufficient digits are filled by 0.

func GetMaxFloat

func GetMaxFloat(flen int, decimal int) float64

nolint GetMaxFloat gets the max float for given flen and decimal.

func Round

func Round(f float64, dec int) float64

Round rounds the argument f to dec decimal places. dec defaults to 0 if not specified. dec can be negative to cause dec digits left of the decimal point of the value f to become zero. nolint

func RoundFloat

func RoundFloat(f float64) float64

RoundFloat rounds float val to the nearest even integer value with float64 format, like MySQL Round function. RoundFloat uses default rounding mode, see https://dev.mysql.com/doc/refman/5.7/en/precision-math-rounding.html so rounding use "round to nearest even". e.g, 1.5 -> 2, -1.5 -> -2.

func Truncate

func Truncate(f float64, dec int) float64

Truncate truncates the argument f to dec decimal places. dec defaults to 0 if not specified. dec can be negative to cause dec digits left of the decimal point of the value f to become zero. nolint

Types

type CoreTime

type CoreTime uint64

CoreTime is the internal struct type for Time.

func FromDate

func FromDate(year int, month int, day int, hour int, minute int, second int, microsecond int) CoreTime

FromDate makes a internal time representation from the given date.

func (CoreTime) Day

func (t CoreTime) Day() int

Day returns the day value.

func (CoreTime) GoTime

func (t CoreTime) GoTime(loc *gotime.Location) (gotime.Time, error)

GoTime converts Time to GoTime.

func (CoreTime) Hour

func (t CoreTime) Hour() int

Hour returns the hour value.

func (CoreTime) IsLeapYear

func (t CoreTime) IsLeapYear() bool

IsLeapYear returns if it's leap year.

func (CoreTime) Microsecond

func (t CoreTime) Microsecond() int

Microsecond returns the microsecond value.

func (CoreTime) Minute

func (t CoreTime) Minute() int

Minute returns the minute value.

func (CoreTime) Month

func (t CoreTime) Month() int

Month returns the month value.

func (CoreTime) Second

func (t CoreTime) Second() int

Second returns the second value.

func (CoreTime) String

func (t CoreTime) String() string

String implements fmt.Stringer.

func (CoreTime) Week

func (t CoreTime) Week(mode int) int

Week returns week value.

func (CoreTime) Weekday

func (t CoreTime) Weekday() gotime.Weekday

Weekday returns weekday value.

func (CoreTime) Year

func (t CoreTime) Year() int

Year returns the year value.

func (CoreTime) YearDay

func (t CoreTime) YearDay() int

YearDay returns year and day.

func (CoreTime) YearWeek

func (t CoreTime) YearWeek(mode int) (int, int)

YearWeek returns year and week.

type Datum

type Datum struct {
	// contains filtered or unexported fields
}

Datum is a data box holds different kind of data. It has better performance and is easier to use than `interface{}`. nolint

func (*Datum) Collation

func (d *Datum) Collation() string

Collation gets the collation of the datum.

func (*Datum) Frac

func (d *Datum) Frac() int

Frac gets the frac of the datum.

func (*Datum) GetBytes

func (d *Datum) GetBytes() []byte

GetBytes gets bytes value.

func (*Datum) GetFloat32

func (d *Datum) GetFloat32() float32

GetFloat32 gets float32 value.

func (*Datum) GetFloat64

func (d *Datum) GetFloat64() float64

GetFloat64 gets float64 value.

func (*Datum) GetInt64

func (d *Datum) GetInt64() int64

GetInt64 gets int64 value.

func (*Datum) GetInterface

func (d *Datum) GetInterface() interface{}

GetInterface gets interface value.

func (*Datum) GetMysqlDuration

func (d *Datum) GetMysqlDuration() Duration

GetMysqlDuration gets Duration value

func (*Datum) GetMysqlEnum

func (d *Datum) GetMysqlEnum() Enum

GetMysqlEnum gets Enum value

func (*Datum) GetMysqlTime

func (d *Datum) GetMysqlTime() Time

GetMysqlTime gets types.Time value

func (*Datum) GetString

func (d *Datum) GetString() string

GetString gets string value.

func (*Datum) GetUint64

func (d *Datum) GetUint64() uint64

GetUint64 gets uint64 value.

func (*Datum) IsNull

func (d *Datum) IsNull() bool

IsNull checks if datum is null.

func (*Datum) Kind

func (d *Datum) Kind() byte

Kind gets the kind of the datum.

func (*Datum) Length

func (d *Datum) Length() int

Length gets the length of the datum.

func (*Datum) SetBytes

func (d *Datum) SetBytes(b []byte)

SetBytes sets bytes value to datum.

func (*Datum) SetCollation

func (d *Datum) SetCollation(collation string)

SetCollation sets the collation of the datum.

func (*Datum) SetFloat32

func (d *Datum) SetFloat32(f float32)

SetFloat32 sets float32 value.

func (*Datum) SetFloat64

func (d *Datum) SetFloat64(f float64)

SetFloat64 sets float64 value.

func (*Datum) SetFrac

func (d *Datum) SetFrac(frac int)

SetFrac sets the frac of the datum.

func (*Datum) SetInt64

func (d *Datum) SetInt64(i int64)

SetInt64 sets int64 value.

func (*Datum) SetInterface

func (d *Datum) SetInterface(x interface{})

SetInterface sets interface to datum.

func (*Datum) SetLength

func (d *Datum) SetLength(l int)

SetLength sets the length of the datum.

func (*Datum) SetMysqlDuration

func (d *Datum) SetMysqlDuration(b Duration)

SetMysqlDuration sets Duration value

func (*Datum) SetMysqlEnum

func (d *Datum) SetMysqlEnum(b Enum, collation string)

SetMysqlEnum sets Enum value

func (*Datum) SetMysqlTime

func (d *Datum) SetMysqlTime(b Time)

SetMysqlTime sets types.Time value

func (*Datum) SetNull

func (d *Datum) SetNull()

SetNull sets datum to nil.

func (*Datum) SetString

func (d *Datum) SetString(s string, collation string)

SetString sets string value.

func (*Datum) SetUint64

func (d *Datum) SetUint64(i uint64)

SetUint64 sets uint64 value.

type Duration

type Duration struct {
	gotime.Duration
	// Fsp is short for Fractional Seconds Precision.
	// See http://dev.mysql.com/doc/refman/5.7/en/fractional-seconds.html
	Fsp int8
}

Duration is the type for MySQL TIME type.

func (Duration) Hour

func (d Duration) Hour() int

Hour returns current hour. e.g, hour("11:11:11") -> 11

func (Duration) String

func (d Duration) String() string

String returns the time formatted using default TimeFormat and fsp.

type Enum

type Enum struct {
	Name string
}

Enum is for MySQL enum type.

func (Enum) String

func (e Enum) String() string

String implements fmt.Stringer interface.

type MyDecimal

type MyDecimal struct {
	// contains filtered or unexported fields
}

MyDecimal represents a decimal value.

func DecimalNeg

func DecimalNeg(from *MyDecimal) *MyDecimal

DecimalNeg reverses decimal's sign.

func NewDecFromFloatForTest

func NewDecFromFloatForTest(f float64) *MyDecimal

NewDecFromFloatForTest creates a MyDecimal from float, as it returns no error, it should only be used in test.

func NewDecFromInt

func NewDecFromInt(i int64) *MyDecimal

NewDecFromInt creates a MyDecimal from int.

func NewDecFromStringForTest

func NewDecFromStringForTest(s string) *MyDecimal

NewDecFromStringForTest creates a MyDecimal from string, as it returns no error, it should only be used in test.

func NewDecFromUint

func NewDecFromUint(i uint64) *MyDecimal

NewDecFromUint creates a MyDecimal from uint.

func NewMaxOrMinDec

func NewMaxOrMinDec(negative bool, prec, frac int) *MyDecimal

NewMaxOrMinDec returns the max or min value decimal for given precision and fraction.

func (*MyDecimal) Compare

func (d *MyDecimal) Compare(to *MyDecimal) int

Compare compares one decimal to another, returns -1/0/1.

func (*MyDecimal) FromBin

func (d *MyDecimal) FromBin(bin []byte, precision, frac int) (binSize int, err error)

FromBin Restores decimal from its binary fixed-length representation.

func (*MyDecimal) FromFloat64

func (d *MyDecimal) FromFloat64(f float64) error

FromFloat64 creates a decimal from float64 value.

func (*MyDecimal) FromInt

func (d *MyDecimal) FromInt(val int64) *MyDecimal

FromInt sets the decimal value from int64.

func (*MyDecimal) FromString

func (d *MyDecimal) FromString(str []byte) error

FromString parses decimal from string.

func (*MyDecimal) FromUint

func (d *MyDecimal) FromUint(val uint64) *MyDecimal

FromUint sets the decimal value from uint64.

func (*MyDecimal) GetDigitsFrac

func (d *MyDecimal) GetDigitsFrac() int8

GetDigitsFrac returns the digitsFrac.

func (*MyDecimal) GetDigitsInt

func (d *MyDecimal) GetDigitsInt() int8

GetDigitsInt returns the digitsInt.

func (*MyDecimal) IsNegative

func (d *MyDecimal) IsNegative() bool

IsNegative returns whether a decimal is negative.

func (*MyDecimal) IsZero

func (d *MyDecimal) IsZero() bool

IsZero checks whether it's a zero decimal.

func (*MyDecimal) PrecisionAndFrac

func (d *MyDecimal) PrecisionAndFrac() (precision, frac int)

PrecisionAndFrac returns the internal precision and frac number.

func (*MyDecimal) Round

func (d *MyDecimal) Round(to *MyDecimal, frac int, roundMode RoundMode) (err error)

Round rounds the decimal to "frac" digits.

   to			- result buffer. d == to is allowed
   frac			- to what position after fraction point to round. can be negative!
   roundMode		- round to nearest even or truncate
			ModeHalfEven rounds normally.
			Truncate just truncates the decimal.

NOTES

scale can be negative !
one TRUNCATED error (line XXX below) isn't treated very logical :(

RETURN VALUE

eDecOK/eDecTruncated

func (*MyDecimal) Shift

func (d *MyDecimal) Shift(shift int) error

Shift shifts decimal digits in given number (with rounding if it need), shift > 0 means shift to left shift, shift < 0 means right shift. In fact it is multiplying on 10^shift.

RETURN

eDecOK          OK
eDecOverflow    operation lead to overflow, number is untoched
eDecTruncated   number was rounded to fit into buffer

func (*MyDecimal) String

func (d *MyDecimal) String() string

String returns the decimal string representation rounded to resultFrac.

func (*MyDecimal) ToBin

func (d *MyDecimal) ToBin(precision, frac int) ([]byte, error)

ToBin converts decimal to its binary fixed-length representation two representations of the same length can be compared with memcmp with the correct -1/0/+1 result

  PARAMS
		precision/frac - if precision is 0, internal value of the decimal will be used,
		then the encoded value is not memory comparable.

  NOTE
    the buffer is assumed to be of the size DecimalBinSize(precision, frac)

  RETURN VALUE
  	bin     - binary value
    errCode - eDecOK/eDecTruncate/eDecOverflow

  DESCRIPTION
    for storage decimal numbers are converted to the "binary" format.

    This format has the following properties:
      1. length of the binary representation depends on the {precision, frac}
      as provided by the caller and NOT on the digitsInt/digitsFrac of the decimal to
      convert.
      2. binary representations of the same {precision, frac} can be compared
      with memcmp - with the same result as DecimalCompare() of the original
      decimals (not taking into account possible precision loss during
      conversion).

    This binary format is as follows:
      1. First the number is converted to have a requested precision and frac.
      2. Every full digitsPerWord digits of digitsInt part are stored in 4 bytes
         as is
      3. The first digitsInt % digitesPerWord digits are stored in the reduced
         number of bytes (enough bytes to store this number of digits -
         see dig2bytes)
      4. same for frac - full word are stored as is,
         the last frac % digitsPerWord digits - in the reduced number of bytes.
      5. If the number is negative - every byte is inversed.
      5. The very first bit of the resulting byte array is inverted (because
         memcmp compares unsigned bytes, see property 2 above)

    Example:

      1234567890.1234

    internally is represented as 3 words

      1 234567890 123400000

    (assuming we want a binary representation with precision=14, frac=4)
    in hex it's

      00-00-00-01  0D-FB-38-D2  07-5A-EF-40

    now, middle word is full - it stores 9 decimal digits. It goes
    into binary representation as is:

      ...........  0D-FB-38-D2 ............

    First word has only one decimal digit. We can store one digit in
    one byte, no need to waste four:

                01 0D-FB-38-D2 ............

    now, last word. It's 123400000. We can store 1234 in two bytes:

                01 0D-FB-38-D2 04-D2

    So, we've packed 12 bytes number in 7 bytes.
    And now we invert the highest bit to get the final result:

                81 0D FB 38 D2 04 D2

    And for -1234567890.1234 it would be

                7E F2 04 C7 2D FB 2D

func (*MyDecimal) ToFloat64

func (d *MyDecimal) ToFloat64() (float64, error)

ToFloat64 converts decimal to float64 value.

func (*MyDecimal) ToHashKey

func (d *MyDecimal) ToHashKey() ([]byte, error)

ToHashKey removes the leading and trailing zeros and generates a hash key. Two Decimals dec0 and dec1 with different fraction will generate the same hash keys if dec0.Compare(dec1) == 0.

func (*MyDecimal) ToInt

func (d *MyDecimal) ToInt() (int64, error)

ToInt returns int part of the decimal, returns the result and errcode.

func (*MyDecimal) ToString

func (d *MyDecimal) ToString() (str []byte)

ToString converts decimal to its printable string representation without rounding.

RETURN VALUE

    str       - result string
    errCode   - eDecOK/eDecTruncate/eDecOverflow

func (*MyDecimal) ToUint

func (d *MyDecimal) ToUint() (uint64, error)

ToUint returns int part of the decimal, returns the result and errcode.

func (*MyDecimal) WriteBin

func (d *MyDecimal) WriteBin(precision, frac int, buf []byte) ([]byte, error)

WriteBin encode and write the binary encoded to target buffer

type RoundMode

type RoundMode int32

RoundMode is the type for round mode.

const (
	DivFracIncr = 4

	// ModeHalfEven rounds normally.
	ModeHalfEven RoundMode = 5
	// Truncate just truncates the decimal.
	ModeTruncate RoundMode = 10
)

constant values.

type Set

type Set struct {
	Name string
}

Set is for MySQL Set type.

func (Set) String

func (e Set) String() string

String implements fmt.Stringer interface.

type Time

type Time struct {
	// contains filtered or unexported fields
}

Time is the struct for handling datetime, timestamp and date.

func (Time) Clock

func (t Time) Clock() (hour int, minute int, second int)

Clock returns the hour, minute, and second within the day specified by t.

func (Time) CoreTime

func (t Time) CoreTime() CoreTime

CoreTime returns core time.

func (Time) DateFormat

func (t Time) DateFormat(layout string) (string, error)

DateFormat returns a textual representation of the time value formatted according to layout. See http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format

func (*Time) FromPackedUint

func (t *Time) FromPackedUint(packed uint64) error

FromPackedUint decodes Time from a packed uint64 value.

func (Time) Fsp

func (t Time) Fsp() int8

Fsp returns fsp value.

func (Time) InvalidZero

func (t Time) InvalidZero() bool

InvalidZero returns a boolean indicating whether the month or day is zero.

func (Time) IsZero

func (t Time) IsZero() bool

IsZero returns a boolean indicating whether the time is equal to ZeroCoreTime.

func (*Time) SetCoreTime

func (t *Time) SetCoreTime(ct CoreTime)

SetCoreTime updates core time.

func (*Time) SetFsp

func (t *Time) SetFsp(fsp int8)

SetFsp updates the fsp in Time.

func (*Time) SetType

func (t *Time) SetType(tp uint8)

SetType updates the type in Time. Only DateTime/Date/Timestamp is valid.

func (Time) String

func (t Time) String() string

func (Time) ToPackedUint

func (t Time) ToPackedUint() (uint64, error)

ToPackedUint encodes Time to a packed uint64 value.

 1 bit  0
17 bits year*13+month   (year 0-9999, month 0-12)
 5 bits day             (0-31)
 5 bits hour            (0-23)
 6 bits minute          (0-59)
 6 bits second          (0-59)
24 bits microseconds    (0-999999)

Total: 64 bits = 8 bytes

0YYYYYYY.YYYYYYYY.YYdddddh.hhhhmmmm.mmssssss.ffffffff.ffffffff.ffffffff

func (Time) Type

func (t Time) Type() uint8

Type returns type value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL