Documentation ¶
Index ¶
- Constants
- Variables
- func CloseDB() error
- func InitDB(connectionString string) error
- type PagingInfo
- type Tweet
- type TweetsQuery
- type TwitterAccount
- func (account *TwitterAccount) Delete() error
- func (account *TwitterAccount) GetTweetFromID(id string) (Tweet, error)
- func (account *TwitterAccount) GetTweets(query TweetsQuery) ([]Tweet, int, error)
- func (account *TwitterAccount) IsTransient() bool
- func (account *TwitterAccount) MetaData() sqlboiler.EntityMetaData
- func (account *TwitterAccount) Save() error
- type TwitterAccountList
- type TwitterAccountQuery
- type User
Constants ¶
const ( // TwitterAccountsOrderByUsername is for ordering TwitterAccounts by Username TwitterAccountsOrderByUsername = "username" // TwitterAccountsOrderByDateCreated is for ordering TwitterAccounts by DateCreated TwitterAccountsOrderByDateCreated = "date_created" )
const ( // UsersOrderByDateCreated is for ordering users by DateCreated UsersOrderByDateCreated = "date_created" // UsersOrderByEmail is for ordering users by Email address UsersOrderByEmail = "email" )
const (
// TweetsOrderByDateCreated is for ordering Tweets by DateCreated
TweetsOrderByDateCreated = "date_created"
)
Variables ¶
var ErrEntityNotFound = errors.New("db: Entity not found")
ErrEntityNotFound is returned when a database Entity is not found, returned from functions that return a single Entity (e.g. EntityFromID)
var TweetDelete = func(tweet *Tweet) error { return sqlboiler.EntityDelete(tweet, dbx) }
TweetDelete deletes the Tweet from the database
var TweetSave = func(tweet *Tweet) error { return sqlboiler.EntitySave(tweet, dbx) }
TweetSave saves the Tweet struct to the database.
var TweetsSortableColumns = []string{ TweetsOrderByDateCreated, }
TweetsSortableColumns is a list of allowed sortable columns
var TwitterAccountDelete = func(account *TwitterAccount) error { return sqlboiler.EntityDelete(account, dbx) }
TwitterAccountDelete deletes the TwitterAccount from the database
var TwitterAccountFromID = func(id string) (TwitterAccountList, error) { var account TwitterAccountList if !isUUID.MatchString(id) { return account, ErrEntityNotFound } cmd := `SELECT ` + sqlboiler.GetFullColumnListString(&TwitterAccount{}, "ta") + `, COUNT(t.id) AS num_tweets FROM twitter_accounts ta LEFT OUTER JOIN tweets t ON ta.id = t.twitter_account_id WHERE ta.id = $1 GROUP BY ta.id` err := dbx.QueryRowx(cmd, id).StructScan(&account) if err == sql.ErrNoRows { return account, ErrEntityNotFound } return account, err }
TwitterAccountFromID returns a TwitterAccount record with given ID
var TwitterAccountGetTweetFromID = func(account *TwitterAccount, tweetID string) (Tweet, error) { var tweet Tweet cmd := `SELECT id, ` + sqlboiler.GetColumnListString(&tweet, "") + ` FROM tweets WHERE twitter_account_id = $1 AND id = $2` err := dbx.Get(&tweet, cmd, account.ID, tweetID) if err == sql.ErrNoRows { return tweet, ErrEntityNotFound } return tweet, err }
TwitterAccountGetTweetFromID gets a TwitterAccount's Tweet by its ID
var TwitterAccountGetTweets = func(account *TwitterAccount, query TweetsQuery) ([]Tweet, int, error) { var tweets []Tweet totalRecords := 0 if account.IsTransient() { return tweets, totalRecords, nil } var queryParams []interface{} queryParams = append(queryParams, account.ID) queryParams = append(queryParams, query.BuildOrderBy()) queryParams = append(queryParams, query.Limit()) queryParams = append(queryParams, query.Offset()) cmd := `SELECT id, ` + sqlboiler.GetColumnListString(&Tweet{}, "") + ` FROM tweets WHERE twitter_account_id = $1 ` if !query.ToBePostedSince.IsZero() { cmd += `AND is_posted = false AND post_on > $5 ` queryParams = append(queryParams, query.ToBePostedSince) } cmd += `ORDER BY $2 LIMIT $3 OFFSET $4` rows, err := dbx.Queryx(cmd, queryParams...) if err != nil { return tweets, totalRecords, err } for rows.Next() { tweet := Tweet{} err = rows.StructScan(&tweet) if err != nil { return tweets, totalRecords, err } tweets = append(tweets, tweet) } rows.Close() // count tweets var countParams []interface{} countCmd := `SELECT COUNT(*) FROM tweets WHERE twitter_account_id = $1` countParams = append(countParams, account.ID) if !query.ToBePostedSince.IsZero() { countCmd += ` AND is_posted = false AND post_on > $2` countParams = append(countParams, query.ToBePostedSince) } err = dbx.Get(&totalRecords, countCmd, countParams...) return tweets, totalRecords, err }
TwitterAccountGetTweets loads Tweets child entites for TwitterAccount
var TwitterAccountSave = func(account *TwitterAccount) error { return sqlboiler.EntitySave(account, dbx) }
TwitterAccountSave saves the TwitterAccount struct to the database.
var TwitterAccountsAll = func(query TwitterAccountQuery) ([]TwitterAccountList, int, error) { var accounts []TwitterAccountList recordCount := 0 cmd := sq. Select(sqlboiler.GetFullColumnList(&TwitterAccount{}, "ta")...).Column("COUNT(t.id) AS num_tweets"). From("twitter_accounts ta"). LeftJoin("tweets t ON ta.id = t.twitter_account_id") if query.ContainsUsername != "" { cmd = cmd.Where("ta.username LIKE ?", query.ContainsUsername) } if !query.HasTweetsToBePostedSince.IsZero() { cmd = cmd.Where("t.is_posted = ? AND t.post_on > ?", false, query.HasTweetsToBePostedSince) } if query.UserID != "" { cmd = cmd.Where("ta.user_id = ?", query.UserID) } cmd = cmd. GroupBy("ta.id"). OrderBy(query.BuildOrderBy()). Limit(uint64(query.Limit())). Offset(uint64(query.Offset())) sqlString, args, err := cmd.PlaceholderFormat(sq.Dollar).ToSql() if err != nil { return nil, recordCount, err } rows, err := dbx.Queryx(sqlString, args...) if err != nil { return nil, recordCount, err } defer rows.Close() for rows.Next() { account := TwitterAccountList{} err = rows.StructScan(&account) if err != nil { return nil, recordCount, err } accounts = append(accounts, account) } countCmd := sq. Select("COUNT(DISTINCT ta.id)"). From("twitter_accounts ta") if query.ContainsUsername != "" || !query.HasTweetsToBePostedSince.IsZero() { countCmd = countCmd. LeftJoin("tweets t ON ta.id = t.twitter_account_id") if query.ContainsUsername != "" { countCmd = countCmd.Where("ta.username LIKE ?", query.ContainsUsername) } if !query.HasTweetsToBePostedSince.IsZero() { countCmd = countCmd.Where("t.is_posted = ? AND t.post_on > ?", false, query.HasTweetsToBePostedSince) } if query.UserID != "" { countCmd = countCmd.Where("ta.user_id = ?", query.UserID) } } countSQL, countArgs, err := countCmd.PlaceholderFormat(sq.Dollar).ToSql() if err != nil { return nil, recordCount, err } err = dbx.Get(&recordCount, countSQL, countArgs...) return accounts, recordCount, err }
TwitterAccountsAll returns all TwitterAccount records from the database
var TwitterAccountsSortableColumns = []string{ TwitterAccountsOrderByUsername, TwitterAccountsOrderByDateCreated, }
TwitterAccountsSortableColumns is a list of allowed sortable columns
var UserDelete = func(user *User) error { return sqlboiler.EntityDelete(user, dbx) }
UserDelete deletes the User from the database
var UserFromEmail = func(email string) (User, error) { var user User cmd := `SELECT ` + sqlboiler.GetFullColumnListString(&user, "") + ` FROM users WHERE email = $1` err := dbx.QueryRowx(cmd, email).StructScan(&user) if err == sql.ErrNoRows { return user, ErrEntityNotFound } else if err != nil { return user, err } return user, nil }
UserFromEmail returns the User record matching an email address
var UserFromID = func(id string) (User, error) { var user User if !isUUID.MatchString(id) { return user, ErrEntityNotFound } err := sqlboiler.EntityGetByID(&user, id, dbx) if err == sqlboiler.ErrEntityNotFound { return user, ErrEntityNotFound } return user, err }
UserFromID returns a User record with given ID
var UserSave = func(user *User) error { return sqlboiler.EntitySave(user, dbx) }
UserSave saves the User struct to the database.
var UsersAll = func(query PagingInfo) ([]User, int, error) { var users []User recordCount := 0 cmd, _, err := sq. Select(sqlboiler.GetFullColumnList(&User{}, "")...). From("users"). OrderBy(query.BuildOrderBy()). Limit(uint64(query.Limit())).Offset(uint64(query.Offset())). ToSql() if err != nil { return nil, recordCount, err } rows, err := dbx.Queryx(cmd) if err != nil { return nil, recordCount, err } defer rows.Close() for rows.Next() { user := User{} err = rows.StructScan(&user) if err != nil { return nil, recordCount, err } users = append(users, user) } err = dbx.Get(&recordCount, "SELECT COUNT(*) FROM users") return users, recordCount, err }
UsersAll returns all User records from the database
var UsersSortableColumns = []string{ UsersOrderByDateCreated, UsersOrderByEmail, }
UsersSortableColumns is a list of allowed sortable columns
Functions ¶
Types ¶
type PagingInfo ¶
PagingInfo contains information about paging when calling queries that return multiple records
func (PagingInfo) BuildOrderBy ¶
func (paging PagingInfo) BuildOrderBy() string
BuildOrderBy builds an ORDER BY sql string based on the OrderBy and Asc properties of the PagingInfo struct
func (PagingInfo) Limit ¶
func (paging PagingInfo) Limit() int
Limit converts 'Page' and 'RecordsPerPage' into a limit value that can be used in database queries
func (PagingInfo) Offset ¶
func (paging PagingInfo) Offset() int
Offset converts 'Page' and 'RecordsPerPage' into an offset value that can be used in database queries
type Tweet ¶
type Tweet struct { ID string `db:"id"` AccountID string `db:"twitter_account_id"` Tweet string `db:"tweet"` PostOn time.Time `db:"post_on"` IsPosted bool `db:"is_posted"` DateCreated time.Time `db:"date_created"` }
Tweet maps to tweets table
func (*Tweet) IsTransient ¶
IsTransient determines if Tweet record has been saved to the database, true means Tweet struct has NOT been saved, false means it has.
func (*Tweet) MetaData ¶
func (tweet *Tweet) MetaData() sqlboiler.EntityMetaData
MetaData returns meta data information about the Tweet entity
type TweetsQuery ¶
type TweetsQuery struct { PagingInfo ToBePostedSince time.Time }
TweetsQuery is a search query for searching Tweets, used by db.TwitterAccountGetTweets
type TwitterAccount ¶
type TwitterAccount struct { ID string `db:"id"` UserID string `db:"user_id"` Username string `db:"username"` DateCreated time.Time `db:"date_created"` ConsumerKey string `db:"consumer_key"` ConsumerSecret string `db:"consumer_secret"` AccessToken string `db:"access_token"` AccessTokenSecret string `db:"access_token_secret"` }
TwitterAccount maps to twitter_accounts table
func (*TwitterAccount) Delete ¶
func (account *TwitterAccount) Delete() error
Delete deletes the TwitterAccount from the database
func (*TwitterAccount) GetTweetFromID ¶
func (account *TwitterAccount) GetTweetFromID(id string) (Tweet, error)
GetTweetFromID gets this TwitterAccount's Tweet by ID
func (*TwitterAccount) GetTweets ¶
func (account *TwitterAccount) GetTweets(query TweetsQuery) ([]Tweet, int, error)
GetTweets loads Tweets child entites for TwitterAccount
func (*TwitterAccount) IsTransient ¶
func (account *TwitterAccount) IsTransient() bool
IsTransient determines if TwitterAccount record has been saved to the database, true means TwitterAccount struct has NOT been saved, false means it has.
func (*TwitterAccount) MetaData ¶
func (account *TwitterAccount) MetaData() sqlboiler.EntityMetaData
MetaData returns meta data information about the TwitterAccount entity
func (*TwitterAccount) Save ¶
func (account *TwitterAccount) Save() error
Save saves the TwitterAccount struct to the database.
type TwitterAccountList ¶
type TwitterAccountList struct { TwitterAccount NumTweets int `db:"num_tweets"` }
TwitterAccountList is a TwitterAccount struct that includes a NumTweets field
type TwitterAccountQuery ¶
type TwitterAccountQuery struct { PagingInfo ContainsUsername string UserID string HasTweetsToBePostedSince time.Time }
TwitterAccountQuery is a search query for searching TwitterAccounts, used by db.TwitterAccountsAll
type User ¶
type User struct { ID string `db:"id"` Name string `db:"name"` Email string `db:"email"` HashedPassword string `db:"hashed_password"` AuthToken sql.NullString `db:"auth_token"` IsAdmin bool `db:"is_admin"` IsService bool `db:"is_service"` DateCreated time.Time `db:"date_created"` }
User maps to users table
func (*User) IsTransient ¶
IsTransient determines if User record has been saved to the database, true means User struct has NOT been saved, false means it has.
func (*User) MetaData ¶
func (user *User) MetaData() sqlboiler.EntityMetaData
MetaData returns meta data information about the User entity