本文整理匯總了Golang中github.com/corestoreio/csfw/storage/dbr.SessionRunner.SelectBySql方法的典型用法代碼示例。如果您正苦於以下問題:Golang SessionRunner.SelectBySql方法的具體用法?Golang SessionRunner.SelectBySql怎麽用?Golang SessionRunner.SelectBySql使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/corestoreio/csfw/storage/dbr.SessionRunner
的用法示例。
在下文中一共展示了SessionRunner.SelectBySql方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetColumns
// GetColumns returns all columns from a table. It discards the column entity_type_id from some
// entity tables.
func GetColumns(dbrSess dbr.SessionRunner, table string) (Columns, error) {
var cols = make(Columns, 0, 100)
sel := dbrSess.SelectBySql("SHOW COLUMNS FROM " + dbr.Quoter.Table(table))
selSql, selArg := sel.ToSql()
rows, err := sel.Query(selSql, selArg...)
if err != nil {
return nil, log.Error("csdb.GetColumns.Query", "err", err, "query", selSql, "args", selArg)
}
defer rows.Close()
col := Column{}
for rows.Next() {
err := rows.Scan(&col.Field, &col.Type, &col.Null, &col.Key, &col.Default, &col.Extra)
if err != nil {
return nil, log.Error("csdb.GetColumns.Rows.Scan", "err", err, "query", selSql, "args", selArg)
}
cols = append(cols, col)
}
err = rows.Err()
if err != nil {
return nil, log.Error("csdb.GetColumns.Rows.Err", "err", err, "query", selSql, "args", selArg)
}
return cols, nil
}
示例2: GetTables
// GetTables returns all tables from a database. AndWhere can be optionally applied.
// Only first index (0) will be added.
func GetTables(dbrSess dbr.SessionRunner, sql ...string) ([]string, error) {
qry := "SHOW TABLES"
if len(sql) > 0 && sql[0] != "" {
if false == dbr.Stmt.IsSelect(sql[0]) {
qry = qry + " LIKE '" + sql[0] + "'"
} else {
qry = sql[0]
}
}
if PkgLog.IsDebug() { // this if reduces 9 allocs ...
defer log.WhenDone(PkgLog).Debug("Stats", "Package", "codegen", "Step", "GetTables", "query", qry)
}
sb := dbrSess.SelectBySql(qry)
query, args, err := sb.ToSql()
if err != nil {
return nil, errgo.Mask(err)
}
rows, err := sb.Query(query, args...)
if err != nil {
return nil, errgo.Mask(err)
}
defer rows.Close()
var tableName string
var tableNames = make([]string, 0, 200)
for rows.Next() {
if err := rows.Scan(&tableName); err != nil {
return nil, errgo.Mask(err)
}
tableNames = append(tableNames, tableName)
}
if err = rows.Err(); err != nil {
return nil, errgo.Mask(err)
}
return tableNames, nil
}