Example #1
0
func (ti *TableInfo) fetchColumns(conn *mysql.MySqlConn) error {
	columns, err := conn.Execute(fmt.Sprintf("show full columns from `%s`", ti.Name))
	if err != nil {
		return errors.Trace(err)
	}

	for _, row := range columns.Values {
		v, err := sqltypes.BuildValue(row[5])
		if err != nil {
			return errors.Trace(err)
		}

		var collation string
		if row[2] != nil {
			collation = string(row[2].([]byte))
		}
		extra := string(row[6].([]byte))
		columnType := string(row[1].([]byte))
		columnName := string(row[0].([]byte))
		ti.AddColumn(columnName, columnType, collation,
			v, extra)
	}

	log.Debugf("%s %+v", ti.Name, ti.Columns)

	return nil
}
Example #2
0
func EncodeValue(buf *bytes.Buffer, value interface{}) error {
	switch bindVal := value.(type) {
	case nil:
		buf.WriteString("null")
	case []sqltypes.Value:
		for i := 0; i < len(bindVal); i++ {
			if i != 0 {
				buf.WriteString(", ")
			}
			if err := EncodeValue(buf, bindVal[i]); err != nil {
				return err
			}
		}
	case [][]sqltypes.Value:
		for i := 0; i < len(bindVal); i++ {
			if i != 0 {
				buf.WriteString(", ")
			}
			buf.WriteByte('(')
			if err := EncodeValue(buf, bindVal[i]); err != nil {
				return err
			}
			buf.WriteByte(')')
		}
	case []interface{}:
		buf.WriteByte('(')
		for i, v := range bindVal {
			if i != 0 {
				buf.WriteString(", ")
			}
			if err := EncodeValue(buf, v); err != nil {
				return err
			}
		}
		buf.WriteByte(')')
	case TupleEqualityList:
		if err := bindVal.Encode(buf); err != nil {
			return err
		}
	default:
		v, err := sqltypes.BuildValue(bindVal)
		if err != nil {
			return err
		}
		v.EncodeSql(buf)
	}
	return nil
}
Example #3
0
func (si *SchemaInfo) CreateOrUpdateTable(tableName string) {
	conn, err := si.connPool.PopConn()
	if err != nil {
		log.Fatal(err)
	}

	defer func() {
		si.connPool.PushConn(conn, err)
	}()

	tables, err := conn.Execute(fmt.Sprintf("%s and table_name = '%s'", base_show_tables, tableName))
	if err != nil {
		log.Fatalf("Error fetching table %s: %v", tableName, err)
	}
	/*
		if len(tables.Rows) != 1 {
			// This can happen if DDLs race with each other.
			return
		}
	*/

	if len(tables.Values) == 0 { //table not exist
		log.Warningf("table %s not exist", tableName)
		return
	}

	create_time, err := sqltypes.BuildValue(tables.Values[0][2]) // create_time
	if err != nil {
		log.Error(err)
		return
	}

	tableInfo, err := NewTableInfo(
		conn,
		tableName,
		string(tables.Values[0][1].([]byte)), // table_type
		create_time,
		string(tables.Values[0][3].([]byte)), // table_comment
		si.cachePool,
	)
	if err != nil {
		// This can happen if DDLs race with each other.
		log.Error(err)
		return
	}

	if _, ok := si.tables[tableName]; ok {
		// If the table already exists, we overwrite it with the latest info.
		// This also means that the query cache needs to be cleared.
		// Otherwise, the query plans may not be in sync with the schema.
		si.queries.Clear()
		log.Infof("Updating table %s", tableName)
	}
	si.tables[tableName] = tableInfo

	if tableInfo.CacheType == schema.CACHE_NONE {
		log.Infof("Initialized table: %s", tableName)
	} else {
		log.Infof("Initialized cached table: %s, prefix: %s", tableName, tableInfo.Cache.prefix)
	}
}