// SQLQueryToColumns generates from a SQL query an array containing all the column properties. // dbSelect argument can be nil but then you must provide query strings which will be joined to the final query. func SQLQueryToColumns(db *sql.DB, dbSelect *dbr.SelectBuilder, query ...string) (Columns, error) { tableName := "tmp_" + randSeq(20) dropTable := func() { _, err := db.Exec("DROP TABLE IF EXISTS `" + tableName + "`") if err != nil { panic(err) } } dropTable() defer dropTable() qry := strings.Join(query, " ") var args []interface{} if qry == "" && dbSelect != nil { var err error qry, args, err = dbSelect.ToSql() if err != nil { return nil, errgo.Mask(err) } } _, err := db.Exec("CREATE TABLE `"+tableName+"` AS "+qry, args...) if err != nil { return nil, errgo.Mask(err) } return GetColumns(db, tableName) }
// LoadStringEntities executes a SELECT query and returns a slice containing columns names and its string values func LoadStringEntities(db *sql.DB, dbSelect *dbr.SelectBuilder) ([]StringEntities, error) { qry, args, err := dbSelect.ToSql() if err != nil { return nil, errgo.Mask(err) } rows, err := db.Query(qry, args...) if err != nil { return nil, errgo.Mask(err) } defer rows.Close() columnNames, err := rows.Columns() if err != nil { return nil, errgo.Mask(err) } ret := make([]StringEntities, 0, 2000) rss := newRowTransformer(columnNames) for rows.Next() { if err := rows.Scan(rss.cp...); err != nil { return nil, errgo.Mask(err) } err := rss.toString() if err != nil { return nil, errgo.Mask(err) } rss.append(&ret) } return ret, nil }
// LoadStringEntities executes a SELECT query and returns a slice containing columns names and its string values func LoadStringEntities(db *sql.DB, dbSelect *dbr.SelectBuilder, query ...string) ([]StringEntities, error) { qry := strings.Join(query, " ") var args []interface{} if qry == "" && dbSelect != nil { qry, args = dbSelect.ToSql() } rows, err := db.Query(qry, args...) if err != nil { return nil, log.Error("codegen.LoadStringEntities.Query420", "err", err) } defer rows.Close() columnNames, err := rows.Columns() if err != nil { return nil, log.Error("codegen.LoadStringEntities.Columns", "err", err) } ret := make([]StringEntities, 0, 2000) rss := newRowTransformer(columnNames) for rows.Next() { if err := rows.Scan(rss.cp...); err != nil { return nil, log.Error("codegen.LoadStringEntities.Scan", "err", err) } err := rss.toString() if err != nil { return nil, log.Error("codegen.LoadStringEntities.ToString", "err", err) } rss.append(&ret) } return ret, nil }