<commit_before>package account

import (
	"database/sql"

	"github.com/remeh/smartwitter/log"
	"github.com/remeh/smartwitter/storage"
)

type userDAO struct {
	DB *sql.DB
}

// ----------------------

var dao *userDAO

func UserDAO() *userDAO {
	if dao != nil {
		return dao
	}

	dao = &userDAO{
		DB: storage.DB(),
	}

	if err := dao.InitStmt(); err != nil {
		log.Error("Can't prepare UserDAO")
		panic(err)
	}

	return dao
}

func (d *userDAO) InitStmt() error {
	var err error
	return err
}

func (d *userDAO) UpsertOnLogin(u *User) error {
	if _, err := d.DB.Exec(`
		INSERT INTO "user" ("uid", "creation_time", "last_login", "twitter_token", "twitter_secret", "twitter_id", "twitter_name", "twitter_username", "session_token")
		VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
		ON CONFLICT ("uid") DO UPDATE SET
			"creation_time" = $2,
			"last_login" = $3,
			"twitter_token" = $4,
			"twitter_secret" = $5,
			"twitter_id" = $6,
			"twitter_name" = $7,
			"twitter_username" = $8,
			"session_token" = $9
	`, u.Uid, u.CreationTime, u.LastLogin, u.TwitterToken, u.TwitterSecret, u.TwitterId, u.TwitterName, u.TwitterUsername, u.SessionToken); err != nil {
		return err
	}
	return nil
}

func (d *userDAO) FindBySession(sessionToken string) (*User, error) {
	rv := &User{}

	if err := d.DB.QueryRow(`
		SELECT "uid", "creation_time", "last_login", "twitter_token", "twitter_secret", "twitter_id", "twitter_name", "twitter_username", "twitter_token" FROM "user"
		WHERE
			session_token = $1
		LIMIT 1
	`, sessionToken).Scan(
		&rv.Uid,
		&rv.CreationTime,
		&rv.LastLogin,
		&rv.TwitterToken,
		&rv.TwitterSecret,
		&rv.TwitterId,
		&rv.TwitterName,
		&rv.TwitterUsername); err != nil {
		return nil, err
	}

	rv.SessionToken = sessionToken

	return rv, nil
}

func (d *userDAO) Exists(sessionToken string) (bool, error) {
	var s int
	if err := d.DB.QueryRow(`
		SELECT length("session_token") FROM "user"
		WHERE
			"session_token" = $1
			AND<commit_msg>account: userdao: redondant query scanned parameter.<commit_after>package account

import (
	"database/sql"

	"github.com/remeh/smartwitter/log"
	"github.com/remeh/smartwitter/storage"
)

type userDAO struct {
	DB *sql.DB
}

// ----------------------

var dao *userDAO

func UserDAO() *userDAO {
	if dao != nil {
		return dao
	}

	dao = &userDAO{
		DB: storage.DB(),
	}

	if err := dao.InitStmt(); err != nil {
		log.Error("Can't prepare UserDAO")
		panic(err)
	}

	return dao
}

func (d *userDAO) InitStmt() error {
	var err error
	return err
}

func (d *userDAO) UpsertOnLogin(u *User) error {
	if _, err := d.DB.Exec(`
		INSERT INTO "user" ("uid", "creation_time", "last_login", "twitter_token", "twitter_secret", "twitter_id", "twitter_name", "twitter_username", "session_token")
		VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
		ON CONFLICT ("uid") DO UPDATE SET
			"creation_time" = $2,
			"last_login" = $3,
			"twitter_token" = $4,
			"twitter_secret" = $5,
			"twitter_id" = $6,
			"twitter_name" = $7,
			"twitter_username" = $8,
			"session_token" = $9
	`, u.Uid, u.CreationTime, u.LastLogin, u.TwitterToken, u.TwitterSecret, u.TwitterId, u.TwitterName, u.TwitterUsername, u.SessionToken); err != nil {
		return err
	}
	return nil
}

func (d *userDAO) FindBySession(sessionToken string) (*User, error) {
	rv := &User{}

	if err := d.DB.QueryRow(`
		SELECT "uid", "creation_time", "last_login", "twitter_token", "twitter_secret", "twitter_id", "twitter_name", "twitter_username" FROM "user"
		WHERE
			session_token = $1
		LIMIT 1
	`, sessionToken).Scan(
		&rv.Uid,
		&rv.CreationTime,
		&rv.LastLogin,
		&rv.TwitterToken,
		&rv.TwitterSecret,
		&rv.TwitterId,
		&rv.TwitterName,
		&rv.TwitterUsername); err != nil {
		return nil, err
	}

	rv.SessionToken = sessionToken

	return rv, nil
}

func (d *userDAO) Exists(sessionToken string) (bool, error) {
	var s int
	if err := d.DB.QueryRow(`
		SELECT length("session_token") FROM "user"
		WHERE
			"session_token" = $1
			AND
{'fraction_non_alphanumeric': 0.1719939117199391, 'fraction_numerical': 0.010147133434804667, 'mean_word_length': 3.7620772946859904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6298864', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1536, 'n_words': 371}