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


Golang sqlutil.FetchRows函数代码示例

本文整理汇总了Golang中upper/io/db/util/sqlutil.FetchRows函数的典型用法代码示例。如果您正苦于以下问题:Golang FetchRows函数的具体用法?Golang FetchRows怎么用?Golang FetchRows使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了FetchRows函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: tableColumns

func (s *source) tableColumns(tableName string) ([]string, error) {

	// Making sure this table is allocated.
	tableSchema := s.schema.Table(tableName)

	if len(tableSchema.Columns) > 0 {
		return tableSchema.Columns, nil
	}

	q := fmt.Sprintf(`PRAGMA TABLE_INFO('%s')`, tableName)

	rows, err := s.doRawQuery(q)

	if s.columns == nil {
		s.columns = make(map[string][]columnSchemaT)
	}

	columns := []columnSchemaT{}

	if err = sqlutil.FetchRows(rows, &columns); err != nil {
		return nil, err
	}

	s.columns[tableName] = columns

	s.schema.TableInfo[tableName].Columns = make([]string, 0, len(s.columns))

	for i := range s.columns[tableName] {
		s.schema.TableInfo[tableName].Columns = append(s.schema.TableInfo[tableName].Columns, s.columns[tableName][i].Name)
	}

	return s.schema.TableInfo[tableName].Columns, nil
}
开发者ID:huuzkee-foundation,项目名称:dbcli,代码行数:33,代码来源:database.go

示例2: Count

func (self *Dh) Count(query string) int64 {
	var errs api.Err
	drv := self.SqlDriver()
	rows, err := drv.Query(query)
	if err != nil {
		errs.Push(api.Msg{
			Field: "Database select All  error: ",
			Error: err.Error(),
		})
		return 0
	}

	type Total struct {
		Count int64 `db:"count"`
	}

	total := []Total{}
	// Mapping to an array.
	if err = sqlutil.FetchRows(rows, &total); err != nil {
		errs.Push(api.Msg{
			Field: "Database select All  error: ",
			Error: err.Error(),
		})
	}

	defer rows.Close()
	return total[0].Count
}
开发者ID:worg,项目名称:go,代码行数:28,代码来源:dh.go

示例3: Collection

// Returns a collection instance by name.
func (self *source) Collection(names ...string) (db.Collection, error) {
	var rows *sql.Rows
	var err error

	if len(names) == 0 {
		return nil, db.ErrMissingCollectionName
	}

	col := &table{
		source: self,
		names:  names,
	}

	columns_t := []columnSchema_t{}

	for _, name := range names {
		chunks := strings.SplitN(name, " ", 2)

		if len(chunks) > 0 {

			name = chunks[0]

			if err := self.tableExists(name); err != nil {
				return nil, err
			}

			// Getting columns
			rows, err = self.doQuery(sqlgen.Statement{
				Type:  sqlgen.SqlSelect,
				Table: sqlgen.Table{`information_schema.columns`},
				Columns: sqlgen.Columns{
					{`column_name`},
					{`data_type`},
				},
				Where: sqlgen.Where{
					sqlgen.ColumnValue{sqlgen.Column{`table_catalog`}, `=`, sqlPlaceholder},
					sqlgen.ColumnValue{sqlgen.Column{`table_name`}, `=`, sqlPlaceholder},
				},
			}, self.config.Database, name)

			if err != nil {
				return nil, err
			}

			if err = sqlutil.FetchRows(rows, &columns_t); err != nil {
				return nil, err
			}

			col.Columns = make([]string, 0, len(columns_t))

			for _, column := range columns_t {
				col.Columns = append(col.Columns, column.Name)
			}
		}
	}

	return col, nil
}
开发者ID:icattlecoder,项目名称:db,代码行数:59,代码来源:database.go

示例4: TestRawQuery

func TestRawQuery(t *testing.T) {
	var sess db.Database
	var rows *sql.Rows
	var err error
	var drv *sql.DB

	type publicationType struct {
		ID       int64  `db:"id,omitempty"`
		Title    string `db:"title"`
		AuthorID int64  `db:"author_id"`
	}

	if sess, err = db.Open(Adapter, settings); err != nil {
		t.Fatal(err)
	}

	defer sess.Close()

	drv = sess.Driver().(*sql.DB)

	rows, err = drv.Query(`
		SELECT
			p.id,
			p.title AS publication_title,
			a.name AS artist_name
		FROM
			artist AS a,
			publication AS p
		WHERE
			a.id = p.author_id
	`)

	if err != nil {
		t.Fatal(err)
	}

	var all []publicationType

	if err = sqlutil.FetchRows(rows, &all); err != nil {
		t.Fatal(err)
	}

	if len(all) != 9 {
		t.Fatalf("Expecting some rows.")
	}
}
开发者ID:huuzkee-foundation,项目名称:dbcli,代码行数:46,代码来源:database_test.go

示例5: All

// Dumps all results into a pointer to an slice of structs or maps.
func (r *result) All(dst interface{}) error {
	var err error

	if r.cursor != nil {
		return db.ErrQueryIsPending
	}

	// Current cursor.
	err = r.setCursor()

	if err != nil {
		return err
	}

	defer r.Close()

	// Fetching all results within the cursor.
	err = sqlutil.FetchRows(r.cursor, dst)

	return err
}
开发者ID:SmarkSeven,项目名称:db,代码行数:22,代码来源:result.go


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