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


Golang NullString.Value方法代码示例

本文整理汇总了Golang中database/sql.NullString.Value方法的典型用法代码示例。如果您正苦于以下问题:Golang NullString.Value方法的具体用法?Golang NullString.Value怎么用?Golang NullString.Value使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在database/sql.NullString的用法示例。


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

示例1: listAllConnections

func (database Database) listAllConnections() (res DbUsersWithConnections) {
	res = make(DbUsersWithConnections)

	rows, err := database.db.Query(`
		-- Left join because we want users without connections as well
		SELECT u1.id, u1.username, u2.id, u2.username FROM 
			user AS u1 LEFT JOIN connection ON u1.id = connection.fromUser
			LEFT JOIN user AS u2 ON u2.id = connection.toUser
			ORDER BY u1.id
			`)
	checkErr(err)
	defer rows.Close()
	for rows.Next() {
		var fromUser User
		var toUsername sql.NullString
		var toId sql.NullInt64
		err := rows.Scan(&fromUser.Id, &fromUser.Username, &toId, &toUsername)
		checkErr(err)

		if toId.Valid {
			// this user has at least one connection, unpack the nullable values
			toIdValue, _ := toId.Value()
			toUsernameValue, _ := toUsername.Value()
			res[fromUser] = append(res[fromUser], User{toIdValue.(int64), toUsernameValue.(string)})
		} else {
			// this user doesn't have any connections
			res[fromUser] = []User{}
		}
	}
	return res
}
开发者ID:rutchkiwi,项目名称:go-rest-api,代码行数:31,代码来源:db.go

示例2: TestStringOrNull

func TestStringOrNull(t *testing.T) {
	var (
		nullString sql.NullString
		value      driver.Value
		err        error
	)

	// When the string is empty
	nullString = StringOrNull("")

	// nullString.Valid should be false
	assert.False(t, nullString.Valid)

	// nullString.Value() should return nil
	value, err = nullString.Value()
	assert.Nil(t, err)
	assert.Nil(t, value)

	// When the string is not empty
	nullString = StringOrNull("foo")

	// nullString.Valid should be true
	assert.True(t, nullString.Valid)

	// nullString.Value() should return the string
	value, err = nullString.Value()
	assert.Nil(t, err)
	assert.Equal(t, "foo", value)
}
开发者ID:chenhougen,项目名称:go-oauth2-server,代码行数:29,代码来源:util_test.go

示例3: getBook

func getBook(db *sql.DB, id string) (Book, error) {

	var (
		BookIDValue    driver.Value
		BookTitleValue driver.Value
		InfoValue      driver.Value
		BookInfoValue  driver.Value
		AuthorValue    driver.Value
		AuthorBioValue driver.Value
		CategoryValue  driver.Value
		DiedValue      driver.Value

		book Book
	)

	rows, err := db.Query("SELECT BkId, Bk, Betaka, Inf, Auth, AuthInf, cat, AD FROM main")
	if err != nil {
		log.Println(err)
		return book, err
	}

	defer rows.Close()

	for rows.Next() {

		var (
			bookid    sql.NullString
			booktitle sql.NullString
			info      sql.NullString
			bookinfo  sql.NullString
			author    sql.NullString
			authorbio sql.NullString
			category  sql.NullString
			died      sql.NullString
		)

		if err := rows.Scan(&bookid, &booktitle, &info, &bookinfo, &author, &authorbio, &category, &died); err != nil {
			log.Println(err)
			return book, err
		}

		if bookid.Valid {
			BookIDValue, err = bookid.Value()
			if err != nil {
				log.Println(err)
				return book, err
			}

		} else {
			BookIDValue = "null"
		}

		if booktitle.Valid {
			BookTitleValue, err = booktitle.Value()
			if err != nil {
				log.Println(err)
				return book, err
			}
		} else {
			BookTitleValue = "null"
		}

		if info.Valid {
			InfoValue, err = info.Value()
			if err != nil {
				log.Println(err)
				return book, err
			}
		} else {
			InfoValue = "null"
		}

		if bookinfo.Valid {
			BookInfoValue, err = bookinfo.Value()
			if err != nil {
				log.Println(err)
				return book, err
			}
		} else {
			BookInfoValue = "null"
		}

		if author.Valid {
			AuthorValue, err = author.Value()
			if err != nil {
				log.Println(err)
				return book, err
			}
		} else {
			AuthorValue = "null"
		}

		if authorbio.Valid {
			AuthorBioValue, err = authorbio.Value()
			if err != nil {
				log.Println(err)
				return book, err
			}
		} else {
			AuthorBioValue = "null"
//.........这里部分代码省略.........
开发者ID:shaybix,项目名称:ahsan,代码行数:101,代码来源:export.go

示例4: getPages

func getPages(db *sql.DB, id string) ([]Page, error) {

	var (
		PageIDValue     driver.Value
		PageBodyValue   driver.Value
		VolumeValue     driver.Value
		PageNumberValue driver.Value

		book    Book
		pages   []Page
		chapter Chapter

		page Page
		f    *os.File
		es   *elastic.Client
	)

	var trim string

	if strings.HasPrefix(id, "00") {
		trim = "00"
	} else if strings.HasPrefix(id, "0") {
		trim = "0"
	}

	newid := strings.TrimPrefix(id, trim)

	rows, err := db.Query("SELECT id, nass, part, page FROM b" + newid)
	if err != nil {
		log.Println(err)
		return pages, err
	}

	defer rows.Close()

	book, err = getBook(db, id)
	if err != nil {

		log.Println(err)
		return pages, err
	}

	if *saveJSON == true {
		f, err = os.Create("json/" + newid + ".json")
		if err != nil {
			return pages, err
		}

		defer f.Close()
	}

	if *indexDB == true {
		es, err = elastic.NewClient(
			elastic.SetSniff(false),
			elastic.SetURL("http://localhost:32769"),
		)
		if err != nil {
			log.Println(err)
			return pages, err
		}
	}

	count := 0

	for rows.Next() {

		var (
			pageid     sql.NullString
			pagebody   sql.NullString
			volume     sql.NullString
			pagenumber sql.NullString
		)
		if err := rows.Scan(&pageid, &pagebody, &volume, &pagenumber); err != nil {
			log.Println(err)
			return pages, err
		}

		if pageid.Valid {
			PageIDValue, err = pageid.Value()
			if err != nil {
				log.Println(err)
				return pages, err
			}
		} else {
			PageIDValue = "null"
		}

		if pagebody.Valid {
			PageBodyValue, err = pagebody.Value()
			if err != nil {
				log.Println(err)
				return pages, err
			}
		} else {
			PageBodyValue = "null"
		}

		if volume.Valid {
			VolumeValue, err = volume.Value()
			if err != nil {
//.........这里部分代码省略.........
开发者ID:shaybix,项目名称:ahsan,代码行数:101,代码来源:export.go

示例5: getChapter

func getChapter(db *sql.DB, id, pageid string) (Chapter, error) {

	var (
		HeadingValue      driver.Value
		HeadingLevelValue driver.Value
		SubLevelValue     driver.Value
		PageIDValue       driver.Value

		chapter Chapter
	)

	var trim string

	if strings.HasPrefix(id, "00") {
		trim = "00"
	} else if strings.HasPrefix(id, "0") {
		trim = "0"
	}

	id = strings.TrimPrefix(id, trim)

	rows, err := db.Query("SELECT tit, lvl, sub, id FROM t" + id + " WHERE id <= '" + pageid + "' ORDER BY id DESC LIMIT 1")
	if err != nil {
		log.Println(err)
		return chapter, err
	}

	defer rows.Close()

	for rows.Next() {

		var (
			heading      sql.NullString
			headinglevel sql.NullString
			sublevel     sql.NullString
			pageid       sql.NullString
		)

		if err := rows.Scan(&heading, &headinglevel, &sublevel, &pageid); err != nil {
			log.Println(err)
			return chapter, err
		}

		if heading.Valid {
			HeadingValue, err = heading.Value()
			if err != nil {
				log.Println(err)
				return chapter, err
			}
		} else {
			HeadingValue = "null"
		}

		if headinglevel.Valid {
			HeadingLevelValue, err = headinglevel.Value()
			if err != nil {
				log.Println(err)
				return chapter, err
			}
		} else {
			HeadingLevelValue = "null"
		}

		if sublevel.Valid {
			SubLevelValue, err = sublevel.Value()
			if err != nil {
				log.Println(err)
				return chapter, err
			}
		} else {
			SubLevelValue = "null"
		}

		if pageid.Valid {
			PageIDValue, err = pageid.Value()
			if err != nil {
				log.Println(err)
				return chapter, err
			}
		} else {
			PageIDValue = "null"
		}

		chapter = Chapter{

			Heading:      HeadingValue.(string),
			HeadingLevel: HeadingLevelValue.(string),
			SubLevel:     SubLevelValue.(string),
			PageID:       PageIDValue.(string),
		}

	}

	return chapter, nil

}
开发者ID:shaybix,项目名称:ahsan,代码行数:96,代码来源:export.go

示例6: Value

func (ns MyNullString) Value() (driver.Value, error) {
	n := sql.NullString{String: ns.String}
	return n.Value()
}
开发者ID:SiroDiaz,项目名称:csuf,代码行数:4,代码来源:3.go


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