Exemple #1
0
func (r Record) Insert() error {
	conn, err := connection.Get()
	if err != nil {
		return err
	}
	args, err := json.Marshal(r.Args)
	if err != nil {
		return err
	}
	_, err = conn.Exec(
		fmt.Sprintf(`INSERT INTO record__%d_%d
				(uuid, domain, name, args, record_type, parser_date, parser) VALUES
				($1, $2, $3, $4, $5, $6, $7)`,
			r.Type.ID,
			r.Domain.TLD.ID,
		),
		r.UUID.String(),
		r.Domain.UUID.String(),
		r.Name,
		string(args),
		r.Type.ID,
		r.Date,
		r.Parser.ID,
	)
	return err
}
Exemple #2
0
func (n *Notification) Save() error {
	conn, err := connection.Get()
	if err != nil {
		return err
	}
	messages, err := json.Marshal(n.Messages)
	if err != nil {
		return err
	}
	archived, err := json.Marshal(n.Archived)
	if err != nil {
		return err
	}
	_, err = conn.Exec(
		`UPDATE notification SET 
			messages = $1,
			alerts = $2,
			archived = $3
		 WHERE user_id = $4`,
		messages,
		n.Alerts,
		archived,
		n.User.ID,
	)
	return err
}
Exemple #3
0
func NewInsert(name, table string, args ...string) (Insert, error) {
	conn, err := connection.Get()
	if err != nil {
		return Insert{}, err
	}
	bi := Insert{}
	serial := uniuri.NewLen(5)
	bi.name = fmt.Sprintf(name, serial)
	bi.table = fmt.Sprintf(table, bi.name)
	tx, err := conn.Begin()
	if err != nil {
		return bi, err
	}
	// close up our defer tx.Rollback()
	bi.tx = tx
	_, err = bi.tx.Exec(bi.table)
	if err != nil {
		return bi, err
	}
	// prepare args
	for idx, i := range args {
		args[idx] = fmt.Sprintf(`"%s"`, i)
	}
	bi.args = args
	stmt := fmt.Sprintf(
		`COPY %s (%s) FROM STDIN`,
		bi.name,
		strings.Join(bi.args, ", "),
	)
	bi.stmt, err = bi.tx.Prepare(stmt)
	if err != nil {
		return bi, err
	}
	return bi, nil
}
Exemple #4
0
func New(email, password string) (User, error) {
	if u, err := GetByEmail(email).One(); err == nil {
		if CheckPassword(email, password) {
			return u, nil
		}
		return User{}, errors.New("Email does not exists, or password does not match.")
	}
	u := User{}
	pass, err := PasswordCrypt([]byte(password))
	if err != nil {
		return u, err
	}
	u.Email = email
	u.Password = pass
	conn, err := connection.Get()
	if err != nil {
		return u, err
	}
	var id int32
	err = conn.QueryRow("SELECT insert_user($1, $2)", email, pass).Scan(&id)
	if err == nil {
		u.ID = id
	}
	return u, err
}
Exemple #5
0
func Get(query string, args ...interface{}) (Record, error) {
	conn, err := connection.Get()
	if err != nil {
		return Record{}, err
	}
	row := conn.QueryRow(query, args...)
	return parseRow(row)
}
Exemple #6
0
func Get(query string, args ...interface{}) (Watcher, error) {
	var result Watcher
	conn, err := connection.Get()
	if err != nil {
		return Watcher{}, err
	}
	row := conn.QueryRow(query, args...)
	result, err = parseRow(row)
	return result, err
}
Exemple #7
0
func (p *Parser) Finish() error {
	conn, err := connection.Get()
	if err != nil {
		return err
	}
	t := time.Now()
	p.Finished.Time = t
	_, err = conn.Exec("UPDATE parser SET finished_at = $1 WHERE id = $2", p.Finished, p.ID)
	return err
}
Exemple #8
0
func Get(query string, args ...interface{}) (Interval, error) {
	var result Interval
	conn, err := connection.Get()
	if err != nil {
		return Interval{}, err
	}
	row := conn.QueryRow(query, args...)
	result, err = parseRow(row)
	return result, err
}
Exemple #9
0
func (r Record) Insert() error {
	conn, err := connection.Get()
	if err != nil {
		return err
	}
	emails, err := json.Marshal(r.Emails)
	if err != nil {
		return err
	}
	data, err := r.Data.MarshalJSON()
	if err != nil {
		return err
	}
	raw, err := r.Raw.MarshalJSON()
	if err != nil {
		return err
	}
	contacts, err := r.Contacts.MarshalJSON()
	if err != nil {
		return err
	}
	organizations, err := json.Marshal(r.Organizations)
	if err != nil {
		return err
	}
	phones, err := json.Marshal(r.Phones)
	if err != nil {
		return err
	}
	postcodes, err := json.Marshal(r.Postcodes)
	if err != nil {
		return err
	}
	names, err := json.Marshal(r.Names)
	if err != nil {
		return err
	}

	_, err = conn.Exec(`INSERT INTO whois 
					(domain, data, raw_whois, contacts, emails, uuid, organizations, phones, postcodes, names) 
				VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`,
		r.Domain.UUID.String(),
		string(data),
		string(raw),
		string(contacts),
		string(emails),
		r.UUID.String(),
		string(organizations),
		string(phones),
		string(postcodes),
		string(names),
	)
	return err
}
Exemple #10
0
func (d Domain) Insert() error {
	conn, err := connection.Get()
	if err != nil {
		return err
	}
	_, err = conn.Exec("INSERT INTO domain (uuid, name, tld) VALUES ($1, $2, $3)",
		d.UUID.String(),
		d.Name,
		d.TLD.ID,
	)
	return err
}
Exemple #11
0
func New(value string) (Interval, error) {
	i := Interval{}
	i.Value = value
	conn, err := connection.Get()
	if err != nil {
		return i, err
	}
	var id int32
	err = conn.QueryRow("SELECT insert_interval($1)", value).Scan(&id)
	i.ID = id
	return i, err
}
Exemple #12
0
func (p Parser) Save() error {
	conn, err := connection.Get()
	if err != nil {
		return err
	}
	b, err := json.Marshal(p.Logs)
	if err != nil {
		return err
	}
	_, err = conn.Exec("UPDATE parser SET logs = $1 WHERE id = $2", string(b), p.ID)
	return err
}
Exemple #13
0
func SetupUserNotification(user users.User) (Notification, error) {
	note := Notification{
		User: user,
	}

	conn, err := connection.Get()
	if err != nil {
		return note, err
	}
	_, err = conn.Exec("INSERT INTO notification (user_id) VALUES ($1)", user.ID)
	if err != nil {
		return note, err
	}
	return note, nil
}
Exemple #14
0
func (p *Parser) Insert() error {
	conn, err := connection.Get()
	if err != nil {
		return err
	}
	var id int32
	err = conn.QueryRow("INSERT INTO parser (filename, parser_date, tld) VALUES ($1, $2, $3) RETURNING id",
		p.Filename,
		p.Date,
		p.TLD.ID,
	).Scan(&id)
	p.ID = id
	return err

}
Exemple #15
0
func New(name string) (TLD, error) {
	if tld, ok := byName[name]; ok {
		return tld, nil
	}
	conn, err := connection.Get()
	if err != nil {
		return TLD{}, err
	}
	var id int32
	err = conn.QueryRow("SELECT insert_tld($1)", name).Scan(&id)
	tld := TLD{
		ID:   id,
		Name: name,
	}
	return tld, err
}
Exemple #16
0
func CheckPassword(email, password string) bool {
	conn, err := connection.Get()
	if err != nil {
		return false
	}
	var pass []byte
	err = conn.QueryRow("SELECT password FROM users WHERE email = $1", email).Scan(&pass)
	if err != nil {
		return false
	}
	err = bcrypt.CompareHashAndPassword(pass, []byte(password))
	if err != nil {
		return false
	}
	return true
}
Exemple #17
0
func newResult(query string, args ...interface{}) Result {
	return Result{
		func() (int, error) {
			var count int
			conn, err := connection.Get()
			if err != nil {
				return count, err
			}
			query = strings.Replace(query, "SELECT *", "SELECT COUNT(*)", 1)
			err = conn.QueryRow(query, args...).Scan(&count)
			return count, err
		},
		func() (Record, error) {
			return Get(query, args...)
		},
		func() ([]Record, error) {
			return GetList(query, args...)
		},
	}
}
Exemple #18
0
func GetList(query string, args ...interface{}) ([]Record, error) {
	conn, err := connection.Get()
	if err != nil {
		return []Record{}, err
	}
	rows, err := conn.Query(query, args...)
	defer rows.Close()
	if err != nil {
		return []Record{}, err
	}
	var list []Record
	for rows.Next() {
		rt, err := parseRow(rows)
		if err != nil {
			return list, err
		}
		list = append(list, rt)
	}
	return list, rows.Err()
}
Exemple #19
0
func (w *Watcher) Save() error {
	conn, err := connection.Get()
	if err != nil {
		return err
	}
	w.Logs = append(w.Logs, Log{w.Updated})
	w.Updated = time.Now()
	b, err := json.Marshal(w.Logs)
	if err != nil {
		return err
	}
	var usersArr []int32
	for _, u := range w.Users {
		usersArr = append(usersArr, u.ID)
	}
	b2, err := json.Marshal(usersArr)
	if err != nil {
		return err
	}
	_, err = conn.Exec("UPDATE watcher SET updated = $1, logs = $2, users = $3 WHERE id = $4", w.Updated, b, b2, w.ID)
	return err
}
Exemple #20
0
func New(name string, tld tlds.TLD) (Type, error) {
	if _, ok := byNameAndTLD[name]; ok {
		if t, ok := byNameAndTLD[name][tld.ID]; ok {
			return t, nil
		}
	}
	conn, err := connection.Get()
	if err != nil {
		return Type{}, err
	}
	var id int32
	err = conn.QueryRow("SELECT ensure_record_table($1, $2)", name, tld.ID).Scan(&id)
	rt := Type{
		ID:   id,
		Name: name,
	}
	if err == nil {
		if _, ok := byNameAndTLD[name]; !ok {
			byNameAndTLD[name] = make(map[int32]Type)
		}
		byNameAndTLD[name][tld.ID] = rt
	}
	return rt, err
}
Exemple #21
0
func New(d domains.Domain, interval string) (Watcher, error) {
	w := Watcher{}
	w.Domain = d
	i, err := intervals.New(interval)
	if err != nil {
		// TODO: better wrapper for our db errors so can see in log which package
		return w, err
	}
	w.Interval = i
	w.Updated = time.Now()
	w.Added = time.Now()
	conn, err := connection.Get()
	if err != nil {
		return w, err
	}
	err = conn.QueryRow("INSERT INTO watcher (domain, interval) VALUES ($1, $2) RETURNING id",
		d.UUID.String(),
		i.ID,
	).Scan(&w.ID)
	if err != nil {
		return w, err
	}
	return GetByDomain(d).One()
}