Skip to content

Commit

Permalink
Add IsUnique
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur White committed Jul 6, 2017
1 parent 0ae5f56 commit b2ed520
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions funcs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package check

import (
"database/sql"
"fmt"
"regexp"
"strconv"
Expand Down Expand Up @@ -134,3 +135,16 @@ func IsPhone(v string) []string {
}
return []string{ErrNotPhone}
}

// IsUnique checks if v is unique in database.
// If check pass, nil is returned.
func IsUnique(db *sql.DB, table, column, placeholder, v string) []string {
var n int
if err := db.QueryRow("SELECT COUNT() FROM "+table+" WHERE "+column+" = "+placeholder, v).Scan(&n); err != nil {
panic(err)
}
if n > 0 {
return []string{ErrNotUnique}
}
return nil
}

0 comments on commit b2ed520

Please sign in to comment.