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

dgw generating Go struct from potgresql table d...

dgw generating Go struct from potgresql table definition

Akira Chiku

July 03, 2017
Tweet

More Decks by Akira Chiku

Other Decks in Programming

Transcript

  1. CBDLHSPVOE QSPEVDUTJ[F   Ø  TJ[F BTPG+VM   Ø 

    PGFOEQPJOUT JOUFSOBM"1*  Ø  PGFYUFSOBMTFSWJDFT Ø  OPUJODMVEJOH4FOUSZ/FX3FMJD4UBUVT1BHFJP  Ø  PGUBCMFT 1PTUHSF42-  Ø  PGMJOFT (PDPEFXJUIPVUWFOEPSEJS  Ø  5PUBM  Ø  8JUIPVUUFTUDPEF 
  2. 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
  3. &YBNQMF  CREATE TABLE user_account ( id bigserial primary key

    , email text not null unique , last_name text not null , first_name text not null );
  4.  // 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 }
  5. 4JNJMBSBQQSPBDI  Ø  YP Ø  IUUQTHJUIVCDPNLORYP Ø  4VQQPSUJOH1PTUHSF42-.Z42-0SBDMF.442-4FSWFS 42-JUF Ø 

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

    ACJHTFSJBMA FUD Ø  EHXIBTDPOHVSBCMFUZQFNBQQJOHCFUXFFO1PTUSHF42-BOE (P Ø  EHXQSPWJEFTDVTUPNUFNQMBUFGFBUVSF Ø  EFWFMPQFSTDBOVTFUIFJSPXOUFNQMBUFGPSTUSVDU NFUIPE  BOEGVODUJPO
  7. /PU'FBUVSF    Ø  EHXEPFTOUDBSFBCPVUSFMBUJPOT Ø  POMZTUSVDUXJUIA$SFBUFANFUIPE BOEGVODUJPOXIJDI TFMFDUTSPXCZ1,BSFHFOFSBUFE

    Ø  *UTUBZTCFJOHNJOJNBMUBCMFSPXEBUBHBUFXBZ Ø  EHXPOMZTVQQPSUT1P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