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