本文整理匯總了Golang中github.com/flike/kingshard/sqlparser.String函數的典型用法代碼示例。如果您正苦於以下問題:Golang String函數的具體用法?Golang String怎麽用?Golang String使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了String函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: handleAdmin
func (c *ClientConn) handleAdmin(admin *sqlparser.Admin) error {
var err error
var result *mysql.Resultset
region := sqlparser.String(admin.Region)
err = c.checkCmdOrder(region, admin.Columns)
if err != nil {
return err
}
switch strings.ToLower(region) {
case NodeRegion:
err = c.handleNodeCmd(admin.Rows)
case ServerRegion:
result, err = c.handleServerCmd(admin.Rows)
default:
return fmt.Errorf("admin %s not supported now", region)
}
if err != nil {
golog.Error("ClientConn", "handleAdmin", err.Error(),
c.connectionId, "sql", sqlparser.String(admin))
return err
}
if result != nil {
return c.writeResultset(c.status, result)
}
return c.writeOK(nil)
}
示例2: handleSetNames
func (c *ClientConn) handleSetNames(ch, ci sqlparser.ValExpr) error {
var cid mysql.CollationId
var ok bool
value := sqlparser.String(ch)
value = strings.Trim(value, "'`\"")
charset := strings.ToLower(value)
if charset == "null" {
return c.writeOK(nil)
}
if ci == nil {
cid, ok = mysql.CharsetIds[charset]
if !ok {
return fmt.Errorf("invalid charset %s", charset)
}
} else {
collate := sqlparser.String(ci)
collate = strings.Trim(value, "'`\"")
cid, ok = mysql.CollationNames[collate]
if !ok {
return fmt.Errorf("invalid charset %s", charset)
}
}
c.charset = charset
c.collation = cid
return c.writeOK(nil)
}
示例3: handleNodeCmd
func (c *ClientConn) handleNodeCmd(rows sqlparser.InsertRows) error {
var err error
var opt, nodeName, role, addr string
vals := rows.(sqlparser.Values)
if len(vals) == 0 {
return errors.ErrCmdUnsupport
}
tuple := vals[0].(sqlparser.ValTuple)
if len(tuple) != len(cmdNodeOrder) {
return errors.ErrCmdUnsupport
}
opt = sqlparser.String(tuple[0])
opt = strings.Trim(opt, "'")
nodeName = sqlparser.String(tuple[1])
nodeName = strings.Trim(nodeName, "'")
role = sqlparser.String(tuple[2])
role = strings.Trim(role, "'")
addr = sqlparser.String(tuple[3])
addr = strings.Trim(addr, "'")
switch strings.ToLower(opt) {
case ADMIN_OPT_ADD:
err = c.AddDatabase(
nodeName,
role,
addr,
)
case ADMIN_OPT_DEL:
err = c.DeleteDatabase(
nodeName,
role,
addr,
)
case ADMIN_OPT_UP:
err = c.UpDatabase(
nodeName,
role,
addr,
)
case ADMIN_OPT_DOWN:
err = c.DownDatabase(
nodeName,
role,
addr,
)
default:
err = errors.ErrCmdUnsupport
golog.Error("ClientConn", "handleNodeCmd", err.Error(),
c.connectionId, "opt", opt)
}
return err
}
示例4: 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
}
示例5: buildSelectPlan
func (r *Router) buildSelectPlan(statement sqlparser.Statement) (*Plan, error) {
plan := &Plan{}
var where *sqlparser.Where
var err error
var tableName string
stmt := statement.(*sqlparser.Select)
switch v := (stmt.From[0]).(type) {
case *sqlparser.AliasedTableExpr:
tableName = sqlparser.String(v.Expr)
case *sqlparser.JoinTableExpr:
if ate, ok := (v.LeftExpr).(*sqlparser.AliasedTableExpr); ok {
tableName = sqlparser.String(ate.Expr)
} else {
tableName = sqlparser.String(v)
}
default:
tableName = sqlparser.String(v)
}
plan.Rule = r.GetRule(tableName) //根據表名獲得分表規則
where = stmt.Where
plan.TableIndexs = makeList(0, len(plan.Rule.TableToNode))
if where != nil {
plan.Criteria = where.Expr //路由條件
err = plan.calRouteIndexs()
if err != nil {
golog.Error("Route", "BuildSelectPlan", err.Error(), 0)
return nil, err
}
} else {
//if shard select without where,send to all nodes and all tables
plan.RouteTableIndexs = plan.TableIndexs
plan.RouteNodeIndexs = makeList(0, len(plan.Rule.Nodes))
}
if plan.Rule.Type != DefaultRuleType && len(plan.RouteTableIndexs) == 0 {
golog.Error("Route", "BuildSelectPlan", errors.ErrNoCriteria.Error(), 0)
return nil, errors.ErrNoCriteria
}
//generate sql,如果routeTableindexs為空則表示不分表,不分表則發default node
err = r.generateSelectSql(plan, stmt)
if err != nil {
return nil, err
}
return plan, nil
}
示例6: buildInsertPlan
func (r *Router) buildInsertPlan(statement sqlparser.Statement) (*Plan, error) {
plan := &Plan{}
stmt := statement.(*sqlparser.Insert)
if _, ok := stmt.Rows.(sqlparser.SelectStatement); ok {
return nil, errors.ErrSelectInInsert
}
/*根據sql語句的表,獲得對應的分片規則*/
plan.Rule = r.GetRule(sqlparser.String(stmt.Table))
if stmt.OnDup != nil {
err := plan.Rule.checkUpdateExprs(sqlparser.UpdateExprs(stmt.OnDup))
if err != nil {
return nil, err
}
}
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", "BuildInsertPlan", err.Error(), 0)
return nil, err
}
err = r.generateInsertSql(plan, stmt)
if err != nil {
return nil, err
}
return plan, nil
}
示例7: 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
}
示例8: buildDeletePlan
func (r *Router) buildDeletePlan(db string, statement sqlparser.Statement) (*Plan, error) {
plan := &Plan{}
var where *sqlparser.Where
var err error
stmt := statement.(*sqlparser.Delete)
plan.Rule = r.GetRule(db, sqlparser.String(stmt.Table))
where = stmt.Where
if where != nil {
plan.Criteria = where.Expr //路由條件
err = plan.calRouteIndexs()
if err != nil {
golog.Error("Route", "BuildUpdatePlan", err.Error(), 0)
return nil, err
}
} else {
//if shard delete without where,send to all nodes and all tables
plan.RouteTableIndexs = plan.Rule.SubTableIndexs
plan.RouteNodeIndexs = makeList(0, len(plan.Rule.Nodes))
}
if plan.Rule.Type != DefaultRuleType && len(plan.RouteTableIndexs) == 0 {
golog.Error("Route", "BuildDeletePlan", errors.ErrNoCriteria.Error(), 0)
return nil, errors.ErrNoCriteria
}
//generate sql,如果routeTableindexs為空則表示不分表,不分表則發default node
err = r.generateDeleteSql(plan, stmt)
if err != nil {
return nil, err
}
return plan, nil
}
示例9: handleSetAutoCommit
func (c *ClientConn) handleSetAutoCommit(val sqlparser.ValExpr) error {
flag := sqlparser.String(val)
flag = strings.Trim(flag, "'`\"")
// autocommit允許為 0, 1, ON, OFF, "ON", "OFF", 不允許"0", "1"
if flag == `0` || flag == `1` {
_, ok := val.(sqlparser.NumVal)
if !ok {
return fmt.Errorf("set autocommit error")
}
}
switch strings.ToUpper(flag) {
case `1`, `ON`:
c.status |= mysql.SERVER_STATUS_AUTOCOMMIT
if c.status&mysql.SERVER_STATUS_IN_TRANS > 0 {
c.status &= ^mysql.SERVER_STATUS_IN_TRANS
}
for _, co := range c.txConns {
if e := co.SetAutoCommit(1); e != nil {
co.Close()
c.txConns = make(map[*backend.Node]*backend.BackendConn)
return fmt.Errorf("set autocommit error, %v", e)
}
co.Close()
}
c.txConns = make(map[*backend.Node]*backend.BackendConn)
case `0`, `OFF`:
c.status &= ^mysql.SERVER_STATUS_AUTOCOMMIT
default:
return fmt.Errorf("invalid autocommit flag %s", flag)
}
return c.writeOK(nil)
}
示例10: buildDeletePlan
func (r *Router) buildDeletePlan(statement sqlparser.Statement) (*Plan, error) {
plan := &Plan{}
var where *sqlparser.Where
stmt := statement.(*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.TableIndexs = makeList(0, len(plan.Rule.TableToNode))
err := plan.calRouteIndexs()
if err != nil {
golog.Error("Route", "BuildDeletePlan", err.Error(), 0)
return nil, err
}
if plan.Rule.Type != DefaultRuleType && len(plan.RouteTableIndexs) == 0 {
golog.Error("Route", "BuildDeletePlan", errors.ErrNoCriteria.Error(), 0)
return nil, errors.ErrNoCriteria
}
//generate sql,如果routeTableindexs為空則表示不分表,不分表則發default node
err = r.generateDeleteSql(plan, stmt)
if err != nil {
return nil, err
}
return plan, nil
}
示例11: 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
}
示例12: handleUseDB
func (c *ClientConn) handleUseDB(stmt *sqlparser.UseDB) error {
if len(stmt.DB) == 0 {
return fmt.Errorf("must have database, not %s", sqlparser.String(stmt))
}
c.db = string(stmt.DB)
return c.writeOK(nil)
}
示例13: handleServerCmd
func (c *ClientConn) handleServerCmd(rows sqlparser.InsertRows) (*mysql.Resultset, error) {
var err error
var result *mysql.Resultset
var opt, k, v string
vals := rows.(sqlparser.Values)
if len(vals) == 0 {
return nil, errors.ErrCmdUnsupport
}
tuple := vals[0].(sqlparser.ValTuple)
if len(tuple) != len(cmdServerOrder) {
return nil, errors.ErrCmdUnsupport
}
opt = sqlparser.String(tuple[0])
opt = strings.Trim(opt, "'")
k = sqlparser.String(tuple[1])
k = strings.Trim(k, "'")
v = sqlparser.String(tuple[2])
v = strings.Trim(v, "'")
switch strings.ToLower(opt) {
case ADMIN_OPT_SHOW:
result, err = c.handleAdminShow(k, v)
case ADMIN_OPT_CHANGE:
err = c.handleAdminChange(k, v)
case ADMIN_OPT_ADD:
err = c.handleAdminAdd(k, v)
case ADMIN_OPT_DEL:
err = c.handleAdminDelete(k, v)
case ADMIN_SAVE_CONFIG:
err = c.handleAdminSave(k, v)
default:
err = errors.ErrCmdUnsupport
golog.Error("ClientConn", "handleNodeCmd", err.Error(),
c.connectionId, "opt", opt)
}
if err != nil {
return nil, err
}
return result, nil
}
示例14: buildSelectPlan
func (r *Router) buildSelectPlan(statement sqlparser.Statement) (*Plan, error) {
plan := &Plan{}
var where *sqlparser.Where
var tableName string
stmt := statement.(*sqlparser.Select)
if ate, ok := (stmt.From[0]).(*sqlparser.AliasedTableExpr); ok {
tableName = sqlparser.String(ate.Expr)
} else {
tableName = sqlparser.String(stmt.From[0])
}
plan.Rule = r.GetRule(tableName) //根據表名獲得分表規則
where = stmt.Where
if where != nil {
plan.Criteria = where.Expr /*路由條件*/
} else {
plan.Rule = r.DefaultRule
}
plan.TableIndexs = makeList(0, len(plan.Rule.TableToNode))
err := plan.calRouteIndexs()
if err != nil {
golog.Error("Route", "BuildSelectPlan", err.Error(), 0)
return nil, err
}
if plan.Rule.Type != DefaultRuleType && len(plan.RouteTableIndexs) == 0 {
golog.Error("Route", "BuildSelectPlan", errors.ErrNoCriteria.Error(), 0)
return nil, errors.ErrNoCriteria
}
//generate sql,如果routeTableindexs為空則表示不分表,不分表則發default node
err = r.generateSelectSql(plan, stmt)
if err != nil {
return nil, err
}
return plan, nil
}
示例15: 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
}