Beispiel #1
0
func CheckFieldForIncrBy(field, fields uint64, count int64) {
	if gomodel.NumFields(field) == 0 || field&fields == 0 {
		panic(errors.Newf("unexpected field type %d", field))
	}

	if count != -1 && count != 1 {
		panic(errors.Newf("unexpected field incrby count %d, must be -1 or 1", count))
	}
}
Beispiel #2
0
// option can be string(redis addr), redis.Pool, RedisOption and *RedisOption
func NewRedis(option interface{}, codec encoding.Codec) *RedisStore {
	if option == nil {
		option = RedisOption{}
	}

	var pool redis.Pool
	switch o := option.(type) {
	case string:
		pool = (RedisOption{Addr: o}).newPool()
	case redis.Pool:
		pool = o
	case *redis.Pool:
		pool = *o
	case RedisOption:
		pool = o.newPool()
	case *RedisOption:
		pool = o.newPool()
	default:
		panic(errors.Newf("unexpected option type, must be string or [*]redis.Pool or [*]RedisOption, but got %T", o))
	}

	if codec == nil {
		codec = encoding.JSON
	}

	return &RedisStore{
		pool:  pool,
		codec: codec,
	}
}
Beispiel #3
0
func IfaceToInt64(v interface{}) (int64, error) {
	switch v := v.(type) {
	case int8:
		return int64(v), nil
	case int16:
		return int64(v), nil
	case int32:
		return int64(v), nil
	case int64:
		return int64(v), nil
	case int:
		return int64(v), nil
	case uint8:
		return int64(v), nil
	case uint16:
		return int64(v), nil
	case uint32:
		return int64(v), nil
	case uint64:
		return int64(v), nil
	case uint:
		return int64(v), nil
	}
	return 0, errors.Newf("%v(%T) is not an integer", v, v)
}
Beispiel #4
0
func (v *Values) String(name string) (string, error) {
	val, has := v.Vals[name]
	if !has {
		return "", errors.Newf("%s is not found", name)
	}
	return val, nil
}
Beispiel #5
0
func (s *Section) modelTable2(v Visitor, tab *sqlparser.TableName) (*Table, error) {
	tabname := string(tab.Name)
	model := v.Models[tabname]
	if model == nil {
		return nil, errors.Newf("model %s hasn't been registered", tabname)
	}

	return model, nil
}
Beispiel #6
0
func (v Visitor) modelTable(modelbuf *bytes2.Buffer, table **Table) error {
	model := modelbuf.String()

	*table = v.Models[model]
	if *table == nil {
		return errors.Newf("model %s isn't registered", model)
	}

	return nil
}
Beispiel #7
0
func (v Visitor) writeField(table *Table, withModel bool, sqlbuf, modelbuf, fieldbuf *bytes2.Buffer) error {
	field := fieldbuf.String()
	col := table.Fields.Get(field)
	if col == nil {
		return errors.Newf("field %s of model %s not found", field, modelbuf.String())
	}

	if withModel {
		sqlbuf.WriteString(table.Name)
		sqlbuf.WriteByte('.')
	}
	sqlbuf.WriteString(col.(string))

	return nil
}
Beispiel #8
0
func (s *Section) replace(v Visitor) error {
	for tabalias, cols := range s.Columns {
		tabnodes, has := s.Tables[tabalias]
		if !has {
			return errors.Newf("table alias %s not found in sql", tabalias)
		}

		tabnode := tabnodes[0]
		if tabnode.Table == nil {
			// don't replace columns of subquery section because of alias
			continue
		}

		model, err := s.modelTable2(v, tabnode.Table)
		if err != nil {
			return err
		}

		for field, cols2 := range cols {
			for _, col := range cols2 {
				col.Name = []byte(model.Fields.DefGet(field, "").(string))
			}
		}
	}

	for _, tabnodes := range s.Tables {
		for _, tabnode := range tabnodes {
			if tabnode.Table != nil {
				if string(tabnode.Table.Name) == "DUAL" {
					continue
				}

				model, err := s.modelTable2(v, tabnode.Table)
				if err != nil {
					return err
				}

				tabnode.Table.Name = []byte(model.Name)
			} else {
				if err := tabnode.Subquery.replace(v); err != nil {
					return err
				}
			}
		}
	}

	return nil
}
Beispiel #9
0
// The last two arguments must be "start" and "count" of limition with type "int"
func (db *DB) ArgsLimit(store Store, model Model, fields, whereFields uint64, args ...interface{}) error {
	argc := len(args)
	if argc < 2 {
		panic(errors.Newf("ArgsLimit need at least two parameters, but only got %d", argc))
	}
	start, err := conv.IfaceToInt64(args[argc-2])
	errors.Panicln(err)
	count, err := conv.IfaceToInt64(args[argc-1])
	errors.Panicln(err)

	arg1, arg2 := db.driver.ParamLimit(start, count)
	args[argc-2], args[argc-1] = arg1, arg2

	stmt, err := db.Table(model).StmtLimit(db, fields, whereFields)
	scanner := Query(stmt, err, args...)
	defer scanner.Close()

	return scanner.Limit(store, int(count))
}
Beispiel #10
0
func FuncForIncrByFieldCount(defaultRunner gomodel.Executor, model gomodel.Model, fields, whereFields uint64, noAffectsError error) IncrByFunc {
	if gomodel.NumFields(whereFields) == 0 || gomodel.NumFields(fields) == 0 {
		panic(errors.Newf("unexpected field count of fields %d and whereField %d", fields, whereFields))
	}

	return func(exec gomodel.Executor, field uint64, whereArgs ...interface{}) error {
		var count int64
		switch arg := whereArgs[0].(type) {
		case int:
			count = int64(arg)
		case int8:
			count = int64(arg)
		case int16:
			count = int64(arg)
		case int32:
			count = int64(arg)
		case int64:
			count = int64(arg)
		case uint8:
			count = int64(arg)
		case uint16:
			count = int64(arg)
		case uint32:
			count = int64(arg)
		case uint64:
			count = int64(arg)
		default:
			panic(fmt.Sprintf("count %v must be an integer", arg))
		}
		CheckFieldForIncrBy(field, fields, count)
		if exec == nil {
			exec = defaultRunner
		}
		c, err := exec.ArgsIncrBy(model, field, whereFields, whereArgs...)
		return dberrs.NoAffects(c, err, noAffectsError)
	}
}
Beispiel #11
0
// Hex2Uint convert a hexadecimal string to uint
// if string is invalid, return an error
func Hex2Uint(str string) (uint64, error) {
	if len(str) > 2 {
		if head := str[:2]; head == "0x" || head == "0X" {
			str = str[2:]
		}
	}

	var n uint64
	for _, c := range str {
		if c >= '0' && c <= '9' {
			c = c - '0'
		} else if c >= 'a' && c <= 'f' {
			c = c - 'a' + 10
		} else if c >= 'A' && c <= 'F' {
			c = c - 'A' + 10
		} else {
			return 0, errors.Newf("Invalid hexadecimal string %s", str)
		}
		n = n << 4
		n |= uint64(c)
	}

	return n, nil
}
Beispiel #12
0
// SetDefaultLocale setup default locale
func (tr *translation) setDefaultLocale(locale string) (err error) {
	if tr.defLocale = tr.locales[locale]; tr.defLocale == nil {
		err = errors.Newf("Default locale %s has not been loaded", locale)
	}
	return
}
Beispiel #13
0
func Parse(r io.Reader, w Writer) error {
	br := io2.BufReader(r)

	rawBuff := bytes2.MakeBuffer(0, 1024)
	evalBuff := bytes2.MakeBuffer(0, 64)
	blockBuff := bytes2.MakeBuffer(0, 248)
	topBuff := bytes2.MakeBuffer(0, 64)
	inclNameBuff := bytes2.MakeBuffer(0, 8)
	inclDataBuff := bytes2.MakeBuffer(0, 8)

	stat := STAT_RAW

	line := 1
	row := 1

	for {
		r, _, err := br.ReadRune()
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}

		switch stat {
		case STAT_RAW:
			if r == '{' {
				stat = STAT_PREFIX
				err = writeReset(w.WriteRaw, rawBuff)
				if err != nil {
					return err
				}
			} else {
				rawBuff.WriteRune(r)
			}
		case STAT_PREFIX:
			if r == '=' {
				stat = STAT_EVAL
			} else if r == '%' {
				stat = STAT_BLOCK
			} else if r == '^' {
				stat = STAT_TOP
			} else if r == '+' {
				stat = STAT_INCLUDE
			} else {
				return errors.Newf("unexpected character %c at %d:%d, expect '%' or '='", r, line, row)
			}
		case STAT_INCLUDE:
			if r == ' ' {
			} else if r == '"' {
				stat = STAT_INCLUDE_NAME
			} else {
				return errors.Newf(`unexpected character %c at %d:%d, expect '"'`, r, line, row)
			}
		case STAT_INCLUDE_NAME:
			if r == '"' {
				stat = STAT_INCLUDE_DATA
			} else {
				_, err = inclNameBuff.WriteRune(r)
				if err != nil {
					return err
				}
			}
		case STAT_INCLUDE_DATA:
			if r == '+' {
				matched, err := expect(br, '}')
				if err != nil {
					return err
				}
				if !matched {
					return errors.Newf("unexpected character at %d:%d, expect '}'", line, row)
				}
				err = w.WriteInclude(inclNameBuff.Bytes(), inclDataBuff.Bytes())
				if err != nil {
					return err
				}
				inclNameBuff.Reset()
				inclDataBuff.Reset()
				stat = STAT_RAW
			} else {
				_, err = inclDataBuff.WriteRune(r)
				if err != nil {
					return err
				}
			}
		default:
			p := w.WriteEval
			b := evalBuff
			er := '='
			if stat == STAT_BLOCK {
				p = w.WriteBlock
				b = blockBuff
				er = '%'
			} else if stat == STAT_TOP {
				p = w.WriteTop
				b = topBuff
				er = '^'
			}
			if r == er {
				matched, err := expect(br, '}')
				if err != nil {
					return err
				}
				if !matched {
					return errors.Newf("unexpected character at %d:%d, expect '}'", line, row)
				}
				err = writeReset(p, b)
				if err != nil {
					return err
				}
				stat = STAT_RAW
			} else {
				b.WriteRune(r)
			}
		}

		if r == '\n' {
			line += 1
			row = 1
		} else {
			row += 1
		}
	}
	if stat != STAT_RAW {
		return errors.Newf("unexpected eof")
	}
	return writeReset(w.WriteRaw, rawBuff)
}