本文整理汇总了Golang中github.com/corestoreio/csfw/storage/dbr.SelectBuilder.ToSql方法的典型用法代码示例。如果您正苦于以下问题:Golang SelectBuilder.ToSql方法的具体用法?Golang SelectBuilder.ToSql怎么用?Golang SelectBuilder.ToSql使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/corestoreio/csfw/storage/dbr.SelectBuilder
的用法示例。
在下文中一共展示了SelectBuilder.ToSql方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SQLQueryToColumns
// 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)
}
示例2: LoadStringEntities
// 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
}
示例3: LoadStringEntities
// 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
}