SHARE
TWEET

Untitled

a guest Oct 3rd, 2017 397 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package libbank
  2.  
  3. import (
  4.         "database/sql"
  5.         "fmt"
  6.         _ "github.com/lib/pq"
  7.         "golang.org/x/crypto/ssh/terminal"
  8.         "strings"
  9.         "syscall"
  10. )
  11.  
  12. const (
  13.         host   = "localhost"
  14.         port   = 5432
  15.         user   = "bankmngr"
  16.         dbname = "bankmngr"
  17. )
  18.  
  19. type Name struct {
  20.         Firstname string
  21.         Lastname  string
  22. }
  23.  
  24. /*Plan:
  25. We need a few functions:
  26.         one:
  27.                 CreateUser(emailaddress, firstname, lastname, dob, hash(password))
  28.                 insert into useraccounts values ('bigdaddytony@hotmail.com', 'Tony', 'FrostedFlakes(tm)', '2001-09-28', 'iambetter');
  29.         two:
  30.                 CreateBin(userid, Comment, balance) binid ommitted
  31.         three:
  32.                 AddSecurityQuestionToUser(userid, questionid, answer)
  33. */
  34.  
  35. func Connect() *sql.DB {
  36.         fmt.Print("Enter Password: ")
  37.         bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
  38.         password := string(bytePassword)
  39.         password = strings.TrimSpace(password)
  40.  
  41.         psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password='%s' dbname=%s sslmode=disable", host, port, user, password, dbname)
  42.         db, err := sql.Open("postgres", psqlInfo)
  43.         if err != nil {
  44.                 panic(err)
  45.         }
  46.  
  47.         err = db.Ping()
  48.  
  49.         fmt.Println("\nSuccessfully connected!")
  50.         return db
  51.         }
  52.  
  53. func GetGenericColumn(db *sql.DB, columnName string, tableName string) ([]string, error) {
  54.         rows, err := db.Query("select " + columnName + " from " + tableName)
  55.         if err != nil {
  56.                 return nil, err
  57.         }
  58.         var final []string
  59.         var temp string
  60.         for rows.Next() {
  61.                 err = rows.Scan(&temp)
  62.                 final = append(final, temp)
  63.         }
  64.         return final, nil
  65. }
  66.  
  67. func AddSecurityQuestionToUser(db *sql.DB, userid int, questionid int, answer int) error {
  68.         stmt, err := db.Prepare("insert into securityquestionstousers(questionid, answer, userid) values($1,$2,$3)")
  69.         if err != nil {
  70.                 return err
  71.         }
  72.         res, err := stmt.Exec(questionid, answer, userid)
  73.         if err != nil {
  74.                 return err
  75.         }
  76.         affect, err := res.RowsAffected()
  77.         if err != nil {
  78.                 return err
  79.         }
  80.         fmt.Printf("Rows changed \n%d\n", affect)
  81.         return nil
  82. }
  83.  
  84. func CreateBin(db *sql.DB, userid int, comment string, balance int) error {
  85.         stmt, err := db.Prepare("insert into bins(userid,balance,comment) values($1,$2,$3)")
  86.         if err != nil {
  87.                 return err
  88.         }
  89.  
  90.         defer stmt.Close()
  91.         res, err := stmt.Exec(userid, balance, comment)
  92.         if err != nil {
  93.                 return err
  94.         }
  95.         affect, err := res.RowsAffected()
  96.         if err != nil {
  97.                 return err
  98.         }
  99.         fmt.Printf("Rows changed \n%d\n", affect)
  100.         return nil
  101.         }
  102.  
  103. func CreateUser(db *sql.DB, emailaddress string, firstname string, lastname string, dob string, password string) error {
  104.         stmt, err := db.Prepare("insert into useraccounts(emailaddress,firstname,lastname,dob,password) values($1,$2,$3,$4,$5)")
  105.         if err != nil {
  106.                 return err
  107.         }
  108.         defer stmt.Close()
  109.         res, err := stmt.Exec(emailaddress, firstname, lastname, dob, password)
  110.         if err != nil {
  111.                 return err
  112.         }
  113.         affect, err := res.RowsAffected()
  114.         if err != nil {
  115.                 return err
  116.         }
  117.         fmt.Printf("Rows changed \n%d\n", affect)
  118.         return nil
  119. }
  120.  
  121. func checkerr(err error) {
  122.         if err != nil {
  123.                 panic(err)
  124.         }
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top