当前位置: 首页>>代码示例>>Golang>>正文


Golang SelectBuilder.ToSql方法代码示例

本文整理汇总了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)
}
开发者ID:joao-parana,项目名称:csfw,代码行数:30,代码来源:database.go

示例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
}
开发者ID:joao-parana,项目名称:csfw,代码行数:34,代码来源:database.go

示例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
}
开发者ID:hafeez3000,项目名称:csfw,代码行数:35,代码来源:database.go


注:本文中的github.com/corestoreio/csfw/storage/dbr.SelectBuilder.ToSql方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。