Exemple #1
0
func (m *MysqlProxy) DeleteTable(table *schema.MysqlTable) {
	curTables := []*schema.MysqlTable{}
	curTableIds := []string{}

	for _, one := range m.Tables {
		if one.Name != table.Name {
			curTables = append(curTables, one)
		}
	}

	for _, one := range m.TableIds {
		if one != table.Id {
			curTableIds = append(curTableIds, one)
		}
	}

	// delete the relations.
	m.TableIds = curTableIds
	m.Tables = curTables

	err := redis.UpdateDB("main", redis.EncodeData(m), "MysqlProxy")

	if err != nil {
		fmt.Printf("Delete table error when write redis: %s\n", err)
		return
	}

	schema.Tables = curTables

	// delete selfs.
	table.Destroy()
}
Exemple #2
0
func analyzeSelectExprs(exprs sqlparser.SelectExprs, table *schema.MysqlTable) (selects []int, err error) {
	selects = make([]int, 0, len(exprs))
	for _, expr := range exprs {
		switch expr := expr.(type) {
		case *sqlparser.StarExpr:
			// Append all columns.
			for colIndex := range table.Columns {
				selects = append(selects, colIndex)
			}
		case *sqlparser.NonStarExpr:
			name := sqlparser.GetColName(expr.Expr)
			if name == "" {
				// Not a simple column name.
				return nil, nil
			}
			colIndex := table.FindColumn(name)
			if colIndex == -1 {
				return nil, fmt.Errorf("column %s not found in table %s", name, table.Name)
			}
			selects = append(selects, colIndex)
		default:
			log.Println("unreachable")
		}
	}
	return selects, nil
}
Exemple #3
0
// to come true ExecCore interface
func (d *InsertDML) Do() (interface{}, error) {
	d.Mut.Lock()
	defer d.Mut.Unlock()

	// get the relation true table.
	var err error
	var shardTables []*schema.MysqlShardTable
	var curTable *schema.MysqlTable
	for i := 0; i < len(schema.Tables); i++ {
		if schema.Tables[i].Name == d.TableName {
			curTable = schema.Tables[i]
			shardTables = curTable.Shards
			break
		}
	}
	if shardTables == nil {
		return nil, errors.New("no found any one this table " + d.TableName)
	}

	// get the last true table.
	tableLen := len(shardTables)
	var table *schema.MysqlShardTable

	for i := 0; i < tableLen; i++ {
		if shardTables[i].RowTotal < uint64(5000000) {
			table = shardTables[i]
			break
		}
	}

	if table == nil {
		// to build new shard table
		table, err = curTable.BuildNewShardTable()
		if err != nil {
			return nil, err
		}
	}

	// parse the sql, to find the auto increment key,
	// then change its' value to table global id.
	sql, params, err := curTable.ParseMergeInsertGlobalId(d.Sql, table)
	if err != nil {
		return nil, err
	}

	master := host.GetBetterHost(table.ShardDB.HostGroup.Master, "master")
	conn, err := master.ConnToDB(table.ShardDB.Name)
	if err != nil {
		conn.Close()
		return nil, err
	}

	stmt, err := conn.Prepare(sql)
	if err != nil {
		conn.Close()
		return nil, err
	}

	res, err := stmt.Exec(params...)
	if err != nil {
		conn.Close()
		return nil, err
	}

	stmt.Close()
	conn.Close()

	curTable.RowTotal++
	curTable.UpdateToRedisDB()

	return res.LastInsertId()
}