Upgrade to Pro — share decks privately, control downloads, hide ads and more …

dgw generating Go struct from potgresql table definition

dgw generating Go struct from potgresql table definition

36e72b299b441378e41b6c445296b959?s=128

Akira Chiku

July 03, 2017
Tweet

Transcript

  1.  EHX (FOFSBUJOH(PTUSVDU GSPN1PTUHSF42- HPMBOHUPLZP+VM  "LJSB$IJLV

  2. NF  /BNF"LJSB$IJLV 5XJUUFS!@BDIJLV (JU)VC!BDIJLV (PPHMF4FBSDI"LJSB$IJLV'JSF

  3. BHFOEB  Ø  #BDLHSPVOE Ø  8IBU Ø  8IZ Ø  )PX

  4.  CBDLHSPVOE

  5. CBDLHSPVOE QSPEVDU   Ø  QSPEVDU Ø  "QQCBTFEJOTUBOU BOESFMPBEBCMFWJSUVBMQMBTUJD 7*4"QSFQBJEDBSEGPSFWFSZPOF

    Ø  J04 "OESPJE Ø  IUUQTWBOEMFKQ
  6. CBDLHSPVOE QSPEVDUTJ[F   Ø  TJ[F BTPG+VM   Ø 

    PGFOEQPJOUT JOUFSOBM"1*  Ø  PGFYUFSOBMTFSWJDFT Ø  OPUJODMVEJOH4FOUSZ/FX3FMJD4UBUVT1BHFJP  Ø  PGUBCMFT 1PTUHSF42-  Ø  PGMJOFT (PDPEFXJUIPVUWFOEPSEJS  Ø  5PUBM  Ø  8JUIPVUUFTUDPEF 
  7.  XIBU

  8. EHX  Ø  IUUQTHJUIVCDPNBDIJLVEHX Ø  *UHFOFSBUFT(PTUSVDU BOETJNQMF5BCMF3PX%BUB(BUFXBZ GVODUJPOTNFUIPETGSPN1PTUHSF42-UBCMFEFOJUJPOT Ø  5BCMF3PX%BUB(BUFXBZ

    Ø  1SJNJUJWFGPSNPGNBQQJOHCFUXFFO(PBOE3%#.4 UBCMFBOESPX OPUMJLF"DUJWF3FDPSE%BUB.BQQFS  Ø  IUUQTNBSUJOGPXMFSDPNFBB$BUBMPHUBCMF%BUB(BUFXBZIUNM Ø  IUUQTNBSUJOGPXMFSDPNFBB$BUBMPHSPX%BUB(BUFXBZIUNM Ø  VTFS@BDDPVOUUBCMF6TFS"DDPVOUTUSVDU Ø  AUZQF6TFS"DDPVOUTUSVDUAXJUIA*%A A&NBJMA  A-BTU/BNFA BOEA'JSTU/BNFABUUSJCVUFT Ø  A6TFS"DDPVOU$SFBUF 2VFSZFS ANFUIPEUPJOTFSU SFDPSE Ø  A(FU6TFS"DDPVOU#Z1L QLJOU AGVODUJPOUPTFMFDU SFDPSECZQL
  9. &YBNQMF  CREATE TABLE user_account ( id bigserial primary key

    , email text not null unique , last_name text not null , first_name text not null );
  10.  // UserAccount represents public.user_account type UserAccount struct { ID

    int64 // id Email string // email LastName string // last_name FirstName string // first_name } // Create inserts the UserAccount to the database. func (r *UserAccount) Create(db Queryer) error { err := db.QueryRow( `INSERT INTO user_account (email, last_name, first_name) VALUES ($1, $2, $3) RETURNING id`, &r.Email, &r.LastName, &r.FirstName).Scan(&r.ID) if err != nil { return errors.Wrap(err, "failed to insert user_account") } return nil } // GetUserAccountByPk select the UserAccount from the database. func GetUserAccountByPk(db Queryer, pk0 int64) (*UserAccount, error) { var r UserAccount err := db.QueryRow( `SELECT id, email, last_name, first_name FROM user_account WHERE id = $1`, pk0).Scan(&r.ID, &r.Email, &r.LastName, &r.FirstName) if err != nil { return nil, errors.Wrap(err, "failed to select user_account") } return &r, nil }
  11. 4JNJMBSBQQSPBDI  Ø  YP Ø  IUUQTHJUIVCDPNLORYP Ø  4VQQPSUJOH1PTUHSF42-.Z42-0SBDMF.442-4FSWFS 42-JUF Ø 

    EHXJTIFBWJMZJOTQJSFECZYP Ø  TRMCPJMFS Ø  IUUQTHJUIVCDPNWBUUMFTRMCPJMFS Ø  4VQQPSUJOH1PTUHSF42-.Z42-.442-4FSWFS Ø  3BJMTTUZMF EBUBCBTFSTU03.
  12. 'FBUVSF    Ø  EHXDBOQSPQFSMZEFUFDUDPNQPTJUFQSJNBSZLFZT BOEBVUP HFOFSBUJOHDPMVNOT Ø  SFUVOJOHBVUPHFOFSBUFEAVVJEA

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