本文整理匯總了Golang中database/sql.NullString.Valid方法的典型用法代碼示例。如果您正苦於以下問題:Golang NullString.Valid方法的具體用法?Golang NullString.Valid怎麽用?Golang NullString.Valid使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類database/sql.NullString
的用法示例。
在下文中一共展示了NullString.Valid方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: checkStringForNull
func checkStringForNull(eventStr string, event *sql.NullString) {
if len(eventStr) == 0 {
event.Valid = false
} else {
event.String = eventStr
event.Valid = true
}
}
示例2: main
func main() {
db, err := sql.Open("postgres", "user=postgres password=pass dbname=mecab sslmode=disable")
checkErr(err)
//データの検索
//rows, err := db.Query("SELECT * FROM words LIMIT 3")
rows, err := db.Query("SELECT * FROM words WHERE first_char IS NULL")
checkErr(err)
for rows.Next() {
var surface string
var original string
var reading string
var first_c sql.NullString
var last_c sql.NullString
err = rows.Scan(&surface, &original, &reading, &first_c, &last_c)
checkErr(err)
/*
fmt.Println(surface)
fmt.Println(original)
fmt.Println(reading)
fmt.Println(reading[0:3])
fmt.Println(reading[len(reading)-3 : len(reading)])
fmt.Println(first_c)
fmt.Println(last_c)
*/
first_c.String = reading[0:3]
last_c.String = reading[len(reading)-3 : len(reading)]
//データの更新
stmt, err := db.Prepare("update words set first_char=$1, last_char=$2 where original=$3")
checkErr(err)
first_c.Valid = true
last_c.Valid = true
_, err = stmt.Exec(first_c.String, last_c.String, original)
checkErr(err)
/*
affect, err := res.RowsAffected()
checkErr(err)
fmt.Println(affect)
*/
}
db.Close()
fmt.Println("finish")
}
示例3: SaveThing
func (w *DatabaseWorld) SaveThing(thing *Thing) (ok bool) {
tabletext, err := json.Marshal(thing.Table)
if err != nil {
log.Println("Error serializing table data for thing", thing.Id, ":", err.Error())
return false
}
var parent sql.NullInt64
if thing.Parent != 0 {
parent.Int64 = int64(thing.Parent)
parent.Valid = true
}
var owner sql.NullInt64
if thing.Owner != 0 && thing.Type.HasOwner() {
owner.Int64 = int64(thing.Owner)
owner.Valid = true
}
var program sql.NullString
if thing.Program != nil {
program.String = thing.Program.Text
program.Valid = true
}
// TODO: save the allow list
_, err = w.db.Exec("UPDATE thing SET name = $1, parent = $2, owner = $3, adminlist = $4, denylist = $5, tabledata = $6, program = $7 WHERE id = $8",
thing.Name, parent, owner, thing.AdminList, thing.DenyList,
types.JsonText(tabletext), program, thing.Id)
if err != nil {
log.Println("Error saving a thing", thing.Id, ":", err.Error())
return false
}
return true
}
示例4: FRES
func (b *BaseConditionQuery) FRES(value interface{}) interface{} {
if (*b.SqlClause).IsAllowEmptyStringQuery() {
return value
}
switch value.(type) {
case sql.NullString:
var nstr sql.NullString = value.(sql.NullString)
if nstr.Valid && nstr.String == "" {
nstr.Valid = false
}
return nstr
case *sql.NullString:
var nstr *sql.NullString = value.(*sql.NullString)
if nstr.Valid && nstr.String == "" {
nstr.Valid = false
}
return nstr
case string:
var str string = value.(string)
if str == "" {
var null sql.NullString
null.Valid = false
return null
}
case *string:
var strx string = *value.(*string)
if strx == "" {
var null sql.NullString
null.Valid = false
return null
}
default:
panic("This type not supported :" + GetType(value))
}
return value
}
示例5: CheckAndComvertEmptyToNull
func (b *BasePmb) CheckAndComvertEmptyToNull(value interface{}) interface{} {
if DBCurrent_I.EmptyStringParameterAllowed {
return value
}
switch value.(type) {
case sql.NullString:
var nstr sql.NullString = value.(sql.NullString)
if nstr.Valid && nstr.String == "" {
nstr.Valid = false
}
return nstr
case *sql.NullString:
var nstr *sql.NullString = value.(*sql.NullString)
if nstr.Valid && nstr.String == "" {
nstr.Valid = false
}
return nstr
case string:
var str string = value.(string)
if str == "" {
var null sql.NullString
null.Valid = false
return null
}
case *string:
var strx string = *value.(*string)
if strx == "" {
var null sql.NullString
null.Valid = false
return null
}
default:
panic("This type not supported :" + GetType(value))
}
return value
}
示例6: Value
func (h Hstore) Value() (driver.Value, error) {
hstore := hstore.Hstore{Map: map[string]sql.NullString{}}
if len(h) == 0 {
return nil, nil
}
for key, value := range h {
var s sql.NullString
if value != nil {
s.String = *value
s.Valid = true
}
hstore.Map[key] = s
}
return hstore.Value()
}
示例7: CreateContainer
func (db *SQLDB) CreateContainer(container Container, ttl time.Duration) (SavedContainer, error) {
if !(isValidCheckID(container.ContainerIdentifier) || isValidStepID(container.ContainerIdentifier)) {
return SavedContainer{}, ErrInvalidIdentifier
}
tx, err := db.conn.Begin()
if err != nil {
return SavedContainer{}, err
}
defer tx.Rollback()
checkSource, err := json.Marshal(container.CheckSource)
if err != nil {
return SavedContainer{}, err
}
envVariables, err := json.Marshal(container.EnvironmentVariables)
if err != nil {
return SavedContainer{}, err
}
user := container.User
interval := fmt.Sprintf("%d second", int(ttl.Seconds()))
if container.PipelineName != "" && container.PipelineID == 0 {
// containers that belong to some pipeline must be identified by pipeline ID not name
return SavedContainer{}, errors.New("container metadata must include pipeline ID")
}
var pipelineID sql.NullInt64
if container.PipelineID != 0 {
pipelineID.Int64 = int64(container.PipelineID)
pipelineID.Valid = true
}
var resourceID sql.NullInt64
if container.ResourceID != 0 {
resourceID.Int64 = int64(container.ResourceID)
resourceID.Valid = true
}
var resourceTypeVersion string
if container.ResourceTypeVersion != nil {
resourceTypeVersionBytes, err := json.Marshal(container.ResourceTypeVersion)
if err != nil {
return SavedContainer{}, err
}
resourceTypeVersion = string(resourceTypeVersionBytes)
}
var buildID sql.NullInt64
if container.BuildID != 0 {
buildID.Int64 = int64(container.BuildID)
buildID.Valid = true
}
workerName := container.WorkerName
if workerName == "" {
workerName = container.WorkerName
}
var attempts sql.NullString
if len(container.Attempts) > 0 {
attemptsBlob, err := json.Marshal(container.Attempts)
if err != nil {
return SavedContainer{}, err
}
attempts.Valid = true
attempts.String = string(attemptsBlob)
}
var imageResourceSource sql.NullString
if container.ImageResourceSource != nil {
marshaled, err := json.Marshal(container.ImageResourceSource)
if err != nil {
return SavedContainer{}, err
}
imageResourceSource.String = string(marshaled)
imageResourceSource.Valid = true
}
var imageResourceType sql.NullString
if container.ImageResourceType != "" {
imageResourceType.String = container.ImageResourceType
imageResourceType.Valid = true
}
_, err = tx.Exec(`
INSERT INTO containers (handle, resource_id, step_name, pipeline_id, build_id, type, worker_name, expires_at, ttl, check_type, check_source, plan_id, working_directory, env_variables, attempts, stage, image_resource_type, image_resource_source, process_user, resource_type_version)
VALUES ($1, $2, $3, $4, $5, $6, $7, NOW() + $8::INTERVAL, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20)`,
container.Handle,
resourceID,
container.StepName,
pipelineID,
buildID,
container.Type.String(),
workerName,
interval,
//.........這裏部分代碼省略.........
示例8: CreateContainer
func (db *SQLDB) CreateContainer(container Container, ttl time.Duration) (Container, error) {
if !isValidID(container.ContainerIdentifier) {
return Container{}, ErrInvalidIdentifier
}
tx, err := db.conn.Begin()
if err != nil {
return Container{}, err
}
checkSource, err := json.Marshal(container.CheckSource)
if err != nil {
return Container{}, err
}
envVariables, err := json.Marshal(container.EnvironmentVariables)
if err != nil {
return Container{}, err
}
user := container.User
interval := fmt.Sprintf("%d second", int(ttl.Seconds()))
var pipelineID sql.NullInt64
if container.PipelineName != "" {
pipeline, err := db.GetPipelineByTeamNameAndName(atc.DefaultTeamName, container.PipelineName)
if err != nil {
return Container{}, fmt.Errorf("failed to find pipeline: %s", err.Error())
}
pipelineID.Int64 = int64(pipeline.ID)
pipelineID.Valid = true
}
var resourceID sql.NullInt64
if container.ResourceID != 0 {
resourceID.Int64 = int64(container.ResourceID)
resourceID.Valid = true
}
var buildID sql.NullInt64
if container.BuildID != 0 {
buildID.Int64 = int64(container.BuildID)
buildID.Valid = true
}
workerName := container.WorkerName
if workerName == "" {
workerName = container.WorkerName
}
var attempts sql.NullString
if len(container.Attempts) > 0 {
attemptsBlob, err := json.Marshal(container.Attempts)
if err != nil {
return Container{}, err
}
attempts.Valid = true
attempts.String = string(attemptsBlob)
}
var imageResourceSource sql.NullString
if container.ImageResourceSource != nil {
marshaled, err := json.Marshal(container.ImageResourceSource)
if err != nil {
return Container{}, err
}
imageResourceSource.String = string(marshaled)
imageResourceSource.Valid = true
}
var imageResourceType sql.NullString
if container.ImageResourceType != "" {
imageResourceType.String = container.ImageResourceType
imageResourceType.Valid = true
}
defer tx.Rollback()
_, err = tx.Exec(`
INSERT INTO containers (handle, resource_id, step_name, pipeline_id, build_id, type, worker_name, expires_at, check_type, check_source, plan_id, working_directory, env_variables, attempts, stage, image_resource_type, image_resource_source, process_user)
VALUES ($1, $2, $3, $4, $5, $6, $7, NOW() + $8::INTERVAL, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)`,
container.Handle,
resourceID,
container.StepName,
pipelineID,
buildID,
container.Type.String(),
workerName,
interval,
container.CheckType,
checkSource,
string(container.PlanID),
container.WorkingDirectory,
envVariables,
attempts,
string(container.Stage),
imageResourceType,
imageResourceSource,
//.........這裏部分代碼省略.........
示例9: FindContainerByIdentifier
func (db *SQLDB) FindContainerByIdentifier(id ContainerIdentifier) (Container, bool, error) {
err := deleteExpired(db)
if err != nil {
return Container{}, false, err
}
var imageResourceSource sql.NullString
if id.ImageResourceSource != nil {
marshaled, err := json.Marshal(id.ImageResourceSource)
if err != nil {
return Container{}, false, err
}
imageResourceSource.String = string(marshaled)
imageResourceSource.Valid = true
}
var imageResourceType sql.NullString
if id.ImageResourceType != "" {
imageResourceType.String = id.ImageResourceType
imageResourceType.Valid = true
}
var containers []Container
selectQuery := `
SELECT ` + containerColumns + `
FROM containers c ` + containerJoins + `
`
conditions := []string{}
params := []interface{}{}
if isValidCheckID(id) {
checkSourceBlob, err := json.Marshal(id.CheckSource)
if err != nil {
return Container{}, false, err
}
conditions = append(conditions, "resource_id = $1")
params = append(params, id.ResourceID)
conditions = append(conditions, "check_type = $2")
params = append(params, id.CheckType)
conditions = append(conditions, "check_source = $3")
params = append(params, checkSourceBlob)
conditions = append(conditions, "stage = $4")
params = append(params, string(id.Stage))
} else if isValidStepID(id) {
conditions = append(conditions, "build_id = $1")
params = append(params, id.BuildID)
conditions = append(conditions, "plan_id = $2")
params = append(params, string(id.PlanID))
conditions = append(conditions, "stage = $3")
params = append(params, string(id.Stage))
} else {
return Container{}, false, ErrInvalidIdentifier
}
if imageResourceSource.Valid && imageResourceType.Valid {
conditions = append(conditions, fmt.Sprintf("image_resource_source = $%d", len(params)+1))
params = append(params, imageResourceSource.String)
conditions = append(conditions, fmt.Sprintf("image_resource_type = $%d", len(params)+1))
params = append(params, imageResourceType.String)
} else {
conditions = append(conditions, "image_resource_source IS NULL")
conditions = append(conditions, "image_resource_type IS NULL")
}
selectQuery += "WHERE " + strings.Join(conditions, " AND ")
rows, err := db.conn.Query(selectQuery, params...)
if err != nil {
return Container{}, false, err
}
for rows.Next() {
container, err := scanContainer(rows)
if err != nil {
return Container{}, false, nil
}
containers = append(containers, container)
}
switch len(containers) {
case 0:
return Container{}, false, nil
case 1:
return containers[0], true, nil
default:
return Container{}, false, ErrMultipleContainersFound
}
}
示例10: CreateNullString
func CreateNullString(s string) sql.NullString {
var ns sql.NullString
ns.Valid = true
ns.String = s
return ns
}
示例11: setString
func setString(s *sql.NullString, v string) {
if v != "" {
s.Valid = true
s.String = v
}
}