本文整理匯總了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
}