// MsQueryColumns parses the query and generates a type for it.
547349
func MsQueryColumns(args *internal.ArgType, inspect []string) ([]*models.Column, error) {
	var err error

	// process inspect -- cannot have 'order by' in a CREATE VIEW
	ins := []string{}
	for _, l := range inspect {
		if !strings.HasPrefix(strings.ToUpper(l), "ORDER BY ") {
			ins = append(ins, l)
		}
	}

	// create temporary view xoid
	xoid := "_xo_" + internal.GenRandomID()
	viewq := `CREATE VIEW ` + xoid + ` AS ` + strings.Join(ins, "\n")
	models.XOLog(viewq)
	_, err = args.DB.Exec(viewq)
	if err != nil {
		return nil, err
	}

	// load columns
	cols, err := models.MsTableColumns(args.DB, args.Schema, xoid)

	// drop inspect view
	dropq := `DROP VIEW ` + xoid
	models.XOLog(dropq)
	_, _ = args.DB.Exec(dropq)

	// load column information
	return cols, err
}
1000096
func (rc *SQLiteRows) Columns() []string {
	rc.s.mu.Lock()
	defer rc.s.mu.Unlock()
	if rc.s.s != nil && rc.nc != len(rc.cols) {
		rc.cols = make([]string, rc.nc)
		for i := 0; i < rc.nc; i++ {
			rc.cols[i] = C.GoString(C.sqlite3_column_name(rc.s.s, C.int(i)))
		}
	}
	return rc.cols
}
1134350
func (m *SqlSource) ColumnPositions() map[string]int {
	if len(m.colIndex) > 0 {
		return m.colIndex
	}
	if m.Source == nil {
		return nil
	}
	cols := make(map[string]int)
	for idx, col := range m.Source.Columns {
		left, right, ok := col.LeftRight()
		//u.Debugf("aliasing: l:%v r:%v ok?%v", left, right, ok)
		if ok {
			cols[right] = idx
		} else {
			cols[left] = idx
		}
	}
	m.colIndex = cols
	return m.colIndex
}
352435
func (p *Parser) parseColumnIndexColumns(ctx *parseCtx, container interface {
	AddColumns(...model.IndexColumn)
}) error {

	var cols []model.IndexColumn

	ctx.skipWhiteSpaces()
	if t := ctx.next(); t.Type != LPAREN {
		return newParseError(ctx, t, "expected LPAREN while parsing index column: %s", t.Type)
	}

OUTER:
	for {
		ctx.skipWhiteSpaces()
		t := ctx.next()
		if !(t.Type == IDENT || t.Type == BACKTICK_IDENT) {
			return newParseError(ctx, t, "should IDENT or BACKTICK_IDENT")
		}

		col := model.NewIndexColumn(t.Value)
		cols = append(cols, col)

		ctx.skipWhiteSpaces()
		switch t = ctx.next(); t.Type {
		case LPAREN:
			t := ctx.next()
			if t.Type != NUMBER {
				return newParseError(ctx, t, "expected NUMBER")
			}
			tlen := t.Value
			ctx.skipWhiteSpaces()
			if t = ctx.next(); t.Type != RPAREN {
				return newParseError(ctx, t, "expected RPAREN")
			}
			col.SetLength(tlen)
		default:
			ctx.rewind()
		}

		// optional sort direction
		switch t = ctx.peek(); t.Type {
		case ASC:
			ctx.advance()
			col.SetSortDirection(model.SortDirectionAscending)
		case DESC:
			ctx.advance()
			col.SetSortDirection(model.SortDirectionDescending)
		}

		ctx.skipWhiteSpaces()
		switch t = ctx.next(); t.Type {
		case COMMA:
			// search next
			continue
		case RPAREN:
			break OUTER
		default:
			return newParseError(ctx, t, "expected COMMA or RPAREN")
		}
	}

	container.AddColumns(cols...)
	return nil
}
122799
func (r *Row) ColumnTypes() ([]*sql.ColumnType, error) {
	if r.err != nil {
		return []*sql.ColumnType{}, r.err
	}
	return r.rows.ColumnTypes()
}
1369684
func MyTableColumns(db XODB, schema string, table string) ([]*Column, error) {
	var err error

	// sql query
	const sqlstr = `SELECT ` +
		`ordinal_position AS field_ordinal, ` +
		`column_name, ` +
		`IF(data_type = 'enum', column_name, column_type) AS data_type, ` +
		`IF(is_nullable = 'YES', false, true) AS not_null, ` +
		`column_default AS default_value, ` +
		`IF(column_key = 'PRI', true, false) AS is_primary_key ` +
		`FROM information_schema.columns ` +
		`WHERE table_schema = ? AND table_name = ? ` +
		`ORDER BY ordinal_position`

	// run query
	XOLog(sqlstr, schema, table)
	q, err := db.Query(sqlstr, schema, table)
	if err != nil {
		return nil, err
	}
	defer q.Close()

	// load results
	res := []*Column{}
	for q.Next() {
		c := Column{}

		// scan
		err = q.Scan(&c.FieldOrdinal, &c.ColumnName, &c.DataType, &c.NotNull, &c.DefaultValue, &c.IsPrimaryKey)
		if err != nil {
			return nil, err
		}

		res = append(res, &c)
	}

	return res, nil
}
845348
func (t *queryTable) translateRowsToColumns() error {
	t.cols = make([]array.Interface, len(t.Cols()))
	for i := range t.row.Columns {
		col := t.Cols()[i]
		switch col.Type {
		case flux.TFloat:
			b := arrow.NewFloatBuilder(&memory.Allocator{})
			b.Reserve(t.Len())
			for _, row := range t.row.Values {
				val, ok := row[i].(float64)
				if !ok {
					return fmt.Errorf("unsupported type %T found in column %s of type %s", val, col.Label, col.Type)
				}
				b.Append(val)
			}
			t.cols[i] = b.NewArray()
			b.Release()
		case flux.TInt:
			b := arrow.NewIntBuilder(&memory.Allocator{})
			b.Reserve(t.Len())
			for _, row := range t.row.Values {
				val, ok := row[i].(int64)
				if !ok {
					return fmt.Errorf("unsupported type %T found in column %s of type %s", val, col.Label, col.Type)
				}
				b.Append(val)
			}
			t.cols[i] = b.NewArray()
			b.Release()
		case flux.TUInt:
			b := arrow.NewUintBuilder(&memory.Allocator{})
			b.Reserve(t.Len())
			for _, row := range t.row.Values {
				val, ok := row[i].(uint64)
				if !ok {
					return fmt.Errorf("unsupported type %T found in column %s of type %s", val, col.Label, col.Type)
				}
				b.Append(val)
			}
			t.cols[i] = b.NewArray()
			b.Release()
		case flux.TString:
			b := arrow.NewStringBuilder(&memory.Allocator{})
			b.Reserve(t.Len())
			for _, row := range t.row.Values {
				val, ok := row[i].(string)
				if !ok {
					return fmt.Errorf("unsupported type %T found in column %s of type %s", val, col.Label, col.Type)
				}
				b.AppendString(val)
			}
			t.cols[i] = b.NewArray()
			b.Release()
		case flux.TBool:
			b := arrow.NewBoolBuilder(&memory.Allocator{})
			b.Reserve(t.Len())
			for _, row := range t.row.Values {
				val, ok := row[i].(bool)
				if !ok {
					return fmt.Errorf("unsupported type %T found in column %s of type %s", val, col.Label, col.Type)
				}
				b.Append(val)
			}
			t.cols[i] = b.NewArray()
			b.Release()
		case flux.TTime:
			b := arrow.NewIntBuilder(&memory.Allocator{})
			b.Reserve(t.Len())
			for _, row := range t.row.Values {
				switch val := row[i].(type) {
				case int64:
					b.Append(val)
				case float64:
					b.Append(int64(val))
				case string:
					tm, err := time.Parse(time.RFC3339, val)
					if err != nil {
						return fmt.Errorf("could not parse string %q as time: %v", val, err)
					}
					b.Append(tm.UnixNano())
				default:
					return fmt.Errorf("unsupported type %T found in column %s", val, col.Label)
				}
			}
			t.cols[i] = b.NewArray()
			b.Release()
		default:
			return fmt.Errorf("invalid type %T found in column %s", col.Type, col.Label)
		}
	}

	for j := len(t.row.Columns); j < len(t.Cols()); j++ {
		b := arrow.NewStringBuilder(&memory.Allocator{})
		b.Reserve(t.Len())

		var value string
		if key := t.Cols()[j].Label; key == "_measurement" {
			value = t.row.Name
		} else {
			value = t.row.Tags[key]
		}

		for i := 0; i < t.Len(); i++ {
			b.AppendString(value)
		}
		t.cols[j] = b.NewArray()
		b.Release()
	}
	return nil
}
759229
func (mysqld *Mysqld) GetPrimaryKeyColumns(dbName, table string) ([]string, error) {
	conn, err := getPoolReconnect(context.TODO(), mysqld.dbaPool)
	if err != nil {
		return nil, err
	}
	defer conn.Recycle()
	qr, err := conn.ExecuteFetch(fmt.Sprintf("SHOW INDEX FROM %s.%s", sqlescape.EscapeID(dbName), sqlescape.EscapeID(table)), 100, true)
	if err != nil {
		return nil, err
	}
	keyNameIndex := -1
	seqInIndexIndex := -1
	columnNameIndex := -1
	for i, field := range qr.Fields {
		switch field.Name {
		case "Key_name":
			keyNameIndex = i
		case "Seq_in_index":
			seqInIndexIndex = i
		case "Column_name":
			columnNameIndex = i
		}
	}
	if keyNameIndex == -1 || seqInIndexIndex == -1 || columnNameIndex == -1 {
		return nil, fmt.Errorf("unknown columns in 'show index' result: %v", qr.Fields)
	}

	columns := make([]string, 0, 5)
	var expectedIndex int64 = 1
	for _, row := range qr.Rows {
		// skip non-primary keys
		if row[keyNameIndex].ToString() != "PRIMARY" {
			continue
		}

		// check the Seq_in_index is always increasing
		seqInIndex, err := sqltypes.ToInt64(row[seqInIndexIndex])
		if err != nil {
			return nil, err
		}
		if seqInIndex != expectedIndex {
			return nil, fmt.Errorf("unexpected index: %v != %v", seqInIndex, expectedIndex)
		}
		expectedIndex++

		columns = append(columns, row[columnNameIndex].ToString())
	}
	return columns, err
}
468473
func SqTableColumns(db XODB, table string) ([]*SqColumn, error) {
	var err error

	// sql query
	var sqlstr = `PRAGMA table_info(` + table + `)`

	// run query
	XOLog(sqlstr)
	q, err := db.Query(sqlstr, table)
	if err != nil {
		return nil, err
	}
	defer q.Close()

	// load results
	res := []*SqColumn{}
	for q.Next() {
		sc := SqColumn{}

		// scan
		err = q.Scan(&sc.FieldOrdinal, &sc.ColumnName, &sc.DataType, &sc.NotNull, &sc.DefaultValue, &sc.PkColIndex)
		if err != nil {
			return nil, err
		}

		res = append(res, &sc)
	}

	return res, nil
}
945278
func (st *symtab) SetResultColumns(rcs []*resultColumn) {
	for _, rc := range rcs {
		rc.column.st = st
	}
	st.ResultColumns = rcs
}
1269161
func (b *MssqlBuilder) RenameColumn(table, oldName, newName string) *Query {
	sql := fmt.Sprintf("sp_name '%v.%v', '%v', 'COLUMN'", table, oldName, newName)
	return b.NewQuery(sql)
}
1306960
func (b *SqliteBuilder) RenameColumn(table, oldName, newName string) *Query {
	q := b.NewQuery("")
	q.LastError = errors.New("SQLite does not support renaming columns")
	return q
}
687437
func (b *BaseBuilder) AddColumn(table, col, typ string) *Query {
	sql := fmt.Sprintf("ALTER TABLE %v ADD %v %v", b.db.QuoteTableName(table), b.db.QuoteColumnName(col), typ)
	return b.NewQuery(sql)
}
1017690
func (adapter *SQLiteAdapter) AlterColumnQuery(tableName, columnName string, sqlColumnType types.SQLColumnType, length, order int) (string, string) {
	sqlType, _ := adapter.TypeMapping(sqlColumnType)
	if length > 0 {
		sqlType = Cleanf("%s(%d)", sqlType, length)
	}

	query := Cleanf("ALTER TABLE %s ADD COLUMN %s %s;",
		adapter.SecureName(tableName),
		adapter.SecureName(columnName),
		sqlType)

	dictionaryQuery := Cleanf(`
		INSERT INTO %s (%s,%s,%s,%s,%s,%s)
		VALUES ('%s','%s',%d,%d,%d,%d);`,

		types.SQLDictionaryTableName,

		types.SQLColumnLabelTableName, types.SQLColumnLabelColumnName,
		types.SQLColumnLabelColumnType, types.SQLColumnLabelColumnLength,
		types.SQLColumnLabelPrimaryKey, types.SQLColumnLabelColumnOrder,

		tableName, columnName, sqlColumnType, length, 0, order)

	return query, dictionaryQuery
}
797602
func FindCols(cols []*Column, names []string) ([]*Column, error) {
	var rcols []*Column
	for _, name := range names {
		col := FindCol(cols, name)
		if col != nil {
			rcols = append(rcols, col)
		} else {
			return nil, errUnknownColumn.Gen("unknown column %s", name)
		}
	}

	return rcols, nil
}
697751
func (s *DB) UpdateColumns(values interface{}) *DB {
	return s.NewScope(s.Value).
		Set("gorm:update_column", true).
		Set("gorm:save_associations", false).
		InstanceSet("gorm:update_interface", values).
		callCallbacks(s.parent.callbacks.updates).db
}
1034970
func (rows *OCI8Rows) Columns() []string {
	names := make([]string, len(rows.defines))
	for i, define := range rows.defines {
		names[i] = define.name
	}
	return names
}