本文整理匯總了Golang中github.com/flike/kingshard/sqlparser.NewParserError函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewParserError函數的具體用法?Golang NewParserError怎麽用?Golang NewParserError使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewParserError函數的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetPlan
/*生成一個route plan*/
func (r *Router) GetPlan(statement sqlparser.Statement) (plan *Plan) {
plan = &Plan{}
var where *sqlparser.Where
//因為實現Statement接口的方法都是指針類型,所以type對應類型也是指針類型
switch stmt := statement.(type) {
case *sqlparser.Insert:
if _, ok := stmt.Rows.(sqlparser.SelectStatement); ok {
panic(sqlparser.NewParserError("select in insert not allowed"))
}
/*根據sql語句的表,獲得對應的分片規則*/
plan.rule = r.GetRule(sqlparser.String(stmt.Table))
if stmt.OnDup != nil {
plan.rule.checkUpdateExprs(sqlparser.UpdateExprs(stmt.OnDup))
}
plan.criteria = plan.routingAnalyzeValues(stmt.Rows.(sqlparser.Values))
plan.fullList = makeList(0, len(plan.rule.Nodes))
return plan
case *sqlparser.Replace:
if _, ok := stmt.Rows.(sqlparser.SelectStatement); ok {
panic(sqlparser.NewParserError("select in replace not allowed"))
}
plan.rule = r.GetRule(sqlparser.String(stmt.Table))
plan.criteria = plan.routingAnalyzeValues(stmt.Rows.(sqlparser.Values))
plan.fullList = makeList(0, len(plan.rule.Nodes))
return plan
case *sqlparser.Select:
plan.rule = r.GetRule(sqlparser.String(stmt.From[0])) //根據表名獲得分表規則
where = stmt.Where
case *sqlparser.Update:
plan.rule = r.GetRule(sqlparser.String(stmt.Table))
plan.rule.checkUpdateExprs(stmt.Exprs)
where = stmt.Where
case *sqlparser.Delete:
plan.rule = r.GetRule(sqlparser.String(stmt.Table))
where = stmt.Where
}
if where != nil {
plan.criteria = where.Expr /*路由條件*/
} else {
plan.rule = r.DefaultRule
}
plan.fullList = makeList(0, len(plan.rule.Nodes))
return plan
}
示例2: checkValuesType
func (plan *Plan) checkValuesType(vals sqlparser.Values) sqlparser.Values {
// Analyze first value of every item in the list
for i := 0; i < len(vals); i++ {
switch tuple := vals[i].(type) {
case sqlparser.ValTuple:
result := plan.getValueType(tuple[0])
if result != VALUE_NODE {
panic(sqlparser.NewParserError("insert is too complex"))
}
default:
panic(sqlparser.NewParserError("insert is too complex"))
}
}
return vals
}
示例3: buildReplacePlan
func (r *Router) buildReplacePlan(statement sqlparser.Statement) (*Plan, error) {
plan := &Plan{}
stmt := statement.(*sqlparser.Replace)
if _, ok := stmt.Rows.(sqlparser.SelectStatement); ok {
panic(sqlparser.NewParserError("select in replace not allowed"))
}
plan.Rule = r.GetRule(sqlparser.String(stmt.Table))
plan.Criteria = plan.checkValuesType(stmt.Rows.(sqlparser.Values))
plan.TableIndexs = makeList(0, len(plan.Rule.TableToNode))
err := plan.calRouteIndexs()
if err != nil {
golog.Error("Route", "BuildReplacePlan", err.Error(), 0)
return nil, err
}
err = r.generateReplaceSql(plan, stmt)
if err != nil {
return nil, err
}
return plan, nil
}
示例4: buildReplacePlan
func (r *Router) buildReplacePlan(db string, statement sqlparser.Statement) (*Plan, error) {
plan := &Plan{}
plan.Rows = make(map[int]sqlparser.Values)
stmt := statement.(*sqlparser.Replace)
if _, ok := stmt.Rows.(sqlparser.SelectStatement); ok {
panic(sqlparser.NewParserError("select in replace not allowed"))
}
if stmt.Columns == nil {
return nil, errors.ErrIRNoColumns
}
plan.Rule = r.GetRule(db, sqlparser.String(stmt.Table))
err := plan.GetIRKeyIndex(stmt.Columns)
if err != nil {
return nil, err
}
plan.Criteria = plan.checkValuesType(stmt.Rows.(sqlparser.Values))
err = plan.calRouteIndexs()
if err != nil {
golog.Error("Route", "BuildReplacePlan", err.Error(), 0)
return nil, err
}
err = r.generateReplaceSql(plan, stmt)
if err != nil {
return nil, err
}
return plan, nil
}
示例5: getBoundValue
/*獲得valExpr對應的值*/
func (plan *Plan) getBoundValue(valExpr sqlparser.ValExpr) interface{} {
switch node := valExpr.(type) {
case sqlparser.ValTuple: //ValTuple可以是一個slice
if len(node) != 1 {
panic(sqlparser.NewParserError("tuples not allowed as insert values"))
}
// TODO: Change parser to create single value tuples into non-tuples.
return plan.getBoundValue(node[0])
case sqlparser.StrVal:
return string(node)
case sqlparser.NumVal:
val, err := strconv.ParseInt(string(node), 10, 64)
if err != nil {
panic(sqlparser.NewParserError("%s", err.Error()))
}
return val
case sqlparser.ValArg:
panic("Unexpected token")
}
panic("Unexpected token")
}
示例6: getInsertTableIndex
func (plan *Plan) getInsertTableIndex(vals sqlparser.Values) int {
index := -1
for i := 0; i < len(vals); i++ {
first_value_expression := vals[i].(sqlparser.ValTuple)[0]
newIndex := plan.getTableIndexByValue(first_value_expression)
if index == -1 {
index = newIndex
} else if index != newIndex {
panic(sqlparser.NewParserError("insert or replace has multiple shard targets"))
}
}
return index
}
示例7: checkUpdateExprs
/*UpdateExprs對應set後麵的表達式*/
func (r *Rule) checkUpdateExprs(exprs sqlparser.UpdateExprs) {
if r.Type == DefaultRuleType {
return
} else if len(r.Nodes) == 1 {
return
}
for _, e := range exprs {
if string(e.Name.Name) == r.Key {
panic(sqlparser.NewParserError("routing key can not in update expression"))
}
}
}
示例8: adjustShardIndex
func (plan *Plan) adjustShardIndex(valExpr sqlparser.ValExpr, index int) int {
value := plan.getBoundValue(valExpr)
//生成一個範圍的接口,[100,120)
s, ok := plan.Rule.Shard.(RangeShard)
if !ok {
return index
}
//value是否和shard[index].Start相等
if s.EqualStart(value, index) {
index--
if index < 0 {
panic(sqlparser.NewParserError("invalid range sharding"))
}
}
return index
}
示例9: shardListFromPlan
/*從plan中得到shard list*/
func (plan *Plan) shardListFromPlan() (shardList []int) {
if plan.criteria == nil { //如果沒有分表條件,則是全表掃描
return plan.fullList
}
//default rule will route all sql to one node
//if rule has one node, we also can route directly
if plan.rule.Type == DefaultRuleType || len(plan.rule.Nodes) == 1 {
if len(plan.fullList) != 1 {
panic(sqlparser.NewParserError("invalid rule nodes num %d, must 1", plan.fullList))
}
return plan.fullList
}
switch criteria := plan.criteria.(type) {
case sqlparser.Values: //代表insert中values
index := plan.findInsertShard(criteria)
return []int{index}
case sqlparser.BoolExpr:
return plan.routingAnalyzeBoolean(criteria)
default:
return plan.fullList
}
}