本文整理匯總了Golang中github.com/flike/kingshard/sqlparser.Statement類的典型用法代碼示例。如果您正苦於以下問題:Golang Statement類的具體用法?Golang Statement怎麽用?Golang Statement使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Statement類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: generateSelectSql
func (r *Router) generateSelectSql(plan *Plan, stmt sqlparser.Statement) error {
sqls := make(map[string][]string)
node, ok := stmt.(*sqlparser.Select)
if ok == false {
return errors.ErrStmtConvert
}
if len(plan.RouteNodeIndexs) == 0 {
return errors.ErrNoRouteNode
}
if len(plan.RouteTableIndexs) == 0 {
buf := sqlparser.NewTrackedBuffer(nil)
stmt.Format(buf)
nodeName := r.Nodes[0]
sqls[nodeName] = []string{buf.String()}
} else {
tableCount := len(plan.RouteTableIndexs)
for i := 0; i < tableCount; i++ {
tableIndex := plan.RouteTableIndexs[i]
nodeIndex := plan.Rule.TableToNode[tableIndex]
nodeName := r.Nodes[nodeIndex]
selectSql := r.rewriteSelectSql(plan, node, tableIndex)
if _, ok := sqls[nodeName]; ok == false {
sqls[nodeName] = make([]string, 0, tableCount)
}
sqls[nodeName] = append(sqls[nodeName], selectSql)
}
}
plan.RewrittenSqls = sqls
return nil
}
示例2: generateUpdateSql
func (r *Router) generateUpdateSql(plan *Plan, stmt sqlparser.Statement) error {
sqls := make(map[string][]string)
node, ok := stmt.(*sqlparser.Update)
if ok == false {
return errors.ErrStmtConvert
}
if len(plan.RouteNodeIndexs) == 0 {
return errors.ErrNoRouteNode
}
if len(plan.RouteTableIndexs) == 0 {
buf := sqlparser.NewTrackedBuffer(nil)
stmt.Format(buf)
nodeName := r.Nodes[0]
sqls[nodeName] = []string{buf.String()}
} else {
nodeCount := len(plan.RouteNodeIndexs)
if 1 < nodeCount {
golog.Error("Router", "generateUpdateSql", errors.ErrUpdateInMulti.Error(), 0,
"RouteNodeIndexs", plan.RouteNodeIndexs)
return errors.ErrUpdateInMulti
}
tableCount := len(plan.RouteTableIndexs)
for i := 0; i < tableCount; i++ {
buf := sqlparser.NewTrackedBuffer(nil)
buf.Fprintf("update %v%v",
node.Comments,
node.Table,
)
fmt.Fprintf(buf, "_%04d", plan.RouteTableIndexs[i])
buf.Fprintf(" set %v%v%v%v",
node.Exprs,
node.Where,
node.OrderBy,
node.Limit,
)
tableIndex := plan.RouteTableIndexs[i]
nodeIndex := plan.Rule.TableToNode[tableIndex]
nodeName := r.Nodes[nodeIndex]
if _, ok := sqls[nodeName]; ok == false {
sqls[nodeName] = make([]string, 0, tableCount)
}
sqls[nodeName] = append(sqls[nodeName], buf.String())
}
}
plan.RewrittenSqls = sqls
return nil
}
示例3: generateSelectSql
func (r *Router) generateSelectSql(plan *Plan, stmt sqlparser.Statement) error {
sqls := make(map[string][]string)
node, ok := stmt.(*sqlparser.Select)
if ok == false {
return ErrStmtConvert
}
if len(plan.RouteNodeIndexs) == 0 {
return ErrNoRouteNode
}
if len(plan.RouteTableIndexs) == 0 {
buf := sqlparser.NewTrackedBuffer(nil)
stmt.Format(buf)
nodeName := r.Nodes[0]
sqls[nodeName] = []string{buf.String()}
} else {
tableCount := len(plan.RouteTableIndexs)
for i := 0; i < tableCount; i++ {
buf := sqlparser.NewTrackedBuffer(nil)
buf.Fprintf("select %v%s%v from %v",
node.Comments,
node.Distinct,
node.SelectExprs,
node.From,
)
fmt.Fprintf(buf, "_%04d", plan.RouteTableIndexs[i])
buf.Fprintf("%v%v%v%v%v%s",
node.Where,
node.GroupBy,
node.Having,
node.OrderBy,
node.Limit,
node.Lock,
)
tableIndex := plan.RouteTableIndexs[i]
nodeIndex := plan.Rule.TableToNode[tableIndex]
nodeName := r.Nodes[nodeIndex]
if _, ok := sqls[nodeName]; ok == false {
sqls[nodeName] = make([]string, 0, tableCount)
}
sqls[nodeName] = append(sqls[nodeName], buf.String())
}
}
plan.RewrittenSqls = sqls
return nil
}
示例4: generateInsertSql
func (r *Router) generateInsertSql(plan *Plan, stmt sqlparser.Statement) error {
sqls := make(map[string][]string)
node, ok := stmt.(*sqlparser.Insert)
if ok == false {
return errors.ErrStmtConvert
}
if len(plan.RouteNodeIndexs) == 0 {
return errors.ErrNoRouteNode
}
if len(plan.RouteTableIndexs) == 0 {
buf := sqlparser.NewTrackedBuffer(nil)
stmt.Format(buf)
nodeName := r.Nodes[0]
sqls[nodeName] = []string{buf.String()}
} else {
tableCount := len(plan.RouteTableIndexs)
for i := 0; i < tableCount; i++ {
buf := sqlparser.NewTrackedBuffer(nil)
tableIndex := plan.RouteTableIndexs[i]
nodeIndex := plan.Rule.TableToNode[tableIndex]
nodeName := r.Nodes[nodeIndex]
buf.Fprintf("insert %vinto ", node.Comments)
table := sqlparser.String(node.Table)
if table[len(table)-1] == '`' {
fmt.Fprintf(buf, "%s_%04d`", table[:len(table)-1], plan.RouteTableIndexs[i])
} else {
fmt.Fprintf(buf, "%s_%04d", table, plan.RouteTableIndexs[i])
}
buf.Fprintf("%v %v%v",
node.Columns,
plan.Rows[tableIndex],
node.OnDup)
if _, ok := sqls[nodeName]; ok == false {
sqls[nodeName] = make([]string, 0, tableCount)
}
sqls[nodeName] = append(sqls[nodeName], buf.String())
}
}
plan.RewrittenSqls = sqls
return nil
}
示例5: generateInsertSql
func (r *Router) generateInsertSql(plan *Plan, stmt sqlparser.Statement) error {
sqls := make(map[string][]string)
node, ok := stmt.(*sqlparser.Insert)
if ok == false {
return ErrStmtConvert
}
if len(plan.RouteNodeIndexs) == 0 {
return ErrNoRouteNode
}
if len(plan.RouteTableIndexs) == 0 {
buf := sqlparser.NewTrackedBuffer(nil)
stmt.Format(buf)
nodeName := r.Nodes[0]
sqls[nodeName] = []string{buf.String()}
} else {
nodeCount := len(plan.RouteNodeIndexs)
if 1 < nodeCount {
golog.Error("Router", "generateInsertSql", ErrInsertInMulti.Error(), 0)
return ErrInsertInMulti
}
tableCount := len(plan.RouteTableIndexs)
for i := 0; i < tableCount; i++ {
buf := sqlparser.NewTrackedBuffer(nil)
buf.Fprintf("insert %vinto %v", node.Comments, node.Table)
fmt.Fprintf(buf, "_%04d", plan.RouteTableIndexs[i])
buf.Fprintf("%v %v%v",
node.Columns,
node.Rows,
node.OnDup)
tableIndex := plan.RouteTableIndexs[i]
nodeIndex := plan.Rule.TableToNode[tableIndex]
nodeName := r.Nodes[nodeIndex]
if _, ok := sqls[nodeName]; ok == false {
sqls[nodeName] = make([]string, 0, tableCount)
}
sqls[nodeName] = append(sqls[nodeName], buf.String())
}
}
plan.RewrittenSqls = sqls
return nil
}
示例6: generateSelectSql
func (r *Router) generateSelectSql(plan *Plan, stmt sqlparser.Statement) error {
sqls := make(map[string][]string)
node, ok := stmt.(*sqlparser.Select)
if ok == false {
return errors.ErrStmtConvert
}
if len(plan.RouteNodeIndexs) == 0 {
return errors.ErrNoRouteNode
}
if len(plan.RouteTableIndexs) == 0 {
buf := sqlparser.NewTrackedBuffer(nil)
stmt.Format(buf)
nodeName := r.Nodes[0]
sqls[nodeName] = []string{buf.String()}
} else {
tableCount := len(plan.RouteTableIndexs)
for i := 0; i < tableCount; i++ {
buf := sqlparser.NewTrackedBuffer(nil)
buf.Fprintf("select %v%s%v from ",
node.Comments,
node.Distinct,
node.SelectExprs,
)
switch v := (node.From[0]).(type) {
case *sqlparser.AliasedTableExpr:
if len(v.As) != 0 {
fmt.Fprintf(buf, "%s_%04d AS %s",
sqlparser.String(v.Expr),
plan.RouteTableIndexs[i],
string(v.As),
)
} else {
fmt.Fprintf(buf, "%s_%04d",
sqlparser.String(v.Expr),
plan.RouteTableIndexs[i],
)
}
case *sqlparser.JoinTableExpr:
if ate, ok := (v.LeftExpr).(*sqlparser.AliasedTableExpr); ok {
if len(ate.As) != 0 {
fmt.Fprintf(buf, "%s_%04d AS %s",
sqlparser.String(ate.Expr),
plan.RouteTableIndexs[i],
string(ate.As),
)
} else {
fmt.Fprintf(buf, "%s_%04d",
sqlparser.String(ate.Expr),
plan.RouteTableIndexs[i],
)
}
} else {
fmt.Fprintf(buf, "%s_%04d",
sqlparser.String(v.LeftExpr),
plan.RouteTableIndexs[i],
)
}
buf.Fprintf(" %s %v", v.Join, v.RightExpr)
if v.On != nil {
buf.Fprintf(" on %v", v.On)
}
default:
fmt.Fprintf(buf, "%s_%04d",
sqlparser.String(node.From[0]),
plan.RouteTableIndexs[i],
)
}
buf.Fprintf("%v%v%v%v%s",
node.Where,
node.GroupBy,
node.Having,
node.OrderBy,
node.Lock,
)
tableIndex := plan.RouteTableIndexs[i]
nodeIndex := plan.Rule.TableToNode[tableIndex]
nodeName := r.Nodes[nodeIndex]
if _, ok := sqls[nodeName]; ok == false {
sqls[nodeName] = make([]string, 0, tableCount)
}
sqls[nodeName] = append(sqls[nodeName], buf.String())
}
}
plan.RewrittenSqls = sqls
return nil
}