當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。