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

Akira Chiku

July 03, 2017
Tweet

More Decks by Akira Chiku

Other Decks in Programming

Transcript


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

    View full-size slide

  2. NF

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

    View full-size slide

  3. BHFOEB

    Ø  #BDLHSPVOE
    Ø  8IBU
    Ø  8IZ
    Ø  )PX

    View full-size slide

  4. CBDLHSPVOE QSPEVDU


    Ø  QSPEVDU
    Ø  "QQCBTFEJOTUBOU BOESFMPBEBCMFWJSUVBMQMBTUJD
    7*4"QSFQBJEDBSEGPSFWFSZPOF
    Ø  J04 "OESPJE
    Ø  IUUQTWBOEMFKQ

    View full-size slide

  5. CBDLHSPVOE QSPEVDUTJ[F


    Ø  TJ[F BTPG+VM

    Ø  PGFOEQPJOUT JOUFSOBM"1*

    Ø  PGFYUFSOBMTFSWJDFT
    Ø  OPUJODMVEJOH4FOUSZ/FX3FMJD4UBUVT1BHFJP

    Ø  PGUBCMFT 1PTUHSF42-

    Ø  PGMJOFT (PDPEFXJUIPVUWFOEPSEJS

    Ø  5PUBM
    Ø  8JUIPVUUFTUDPEF

    View full-size slide

  6. EHX

    Ø  IUUQTHJUIVCDPNBDIJLVEHX
    Ø  *UHFOFSBUFT(PTUSVDU BOETJNQMF5BCMF3PX%BUB(BUFXBZ
    GVODUJPOTNFUIPETGSPN1PTUHSF42-UBCMFEFOJUJPOT
    Ø  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

    View full-size slide

  7. &YBNQMF

    CREATE TABLE user_account (
    id bigserial primary key
    , email text not null unique
    , last_name text not null
    , first_name text not null
    );

    View full-size slide


  8. // 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
    }

    View full-size slide

  9. 4JNJMBSBQQSPBDI

    Ø  YP
    Ø  IUUQTHJUIVCDPNLORYP
    Ø  4VQQPSUJOH1PTUHSF42-.Z42-0SBDMF.442-4FSWFS
    42-JUF
    Ø  EHXJTIFBWJMZJOTQJSFECZYP
    Ø  TRMCPJMFS
    Ø  IUUQTHJUIVCDPNWBUUMFTRMCPJMFS
    Ø  4VQQPSUJOH1PTUHSF42-.Z42-.442-4FSWFS
    Ø  3BJMTTUZMF EBUBCBTFSTU03.

    View full-size slide

  10. 'FBUVSF


    Ø  EHXDBOQSPQFSMZEFUFDUDPNQPTJUFQSJNBSZLFZT BOEBVUP
    HFOFSBUJOHDPMVNOT
    Ø  SFUVOJOHBVUPHFOFSBUFEAVVJEA ACJHTFSJBMA FUD
    Ø  EHXIBTDPOHVSBCMFUZQFNBQQJOHCFUXFFO1PTUSHF42-BOE
    (P
    Ø  EHXQSPWJEFTDVTUPNUFNQMBUFGFBUVSF
    Ø  EFWFMPQFSTDBOVTFUIFJSPXOUFNQMBUFGPSTUSVDU NFUIPE
    BOEGVODUJPO

    View full-size slide

  11. /PU'FBUVSF


    Ø  EHXEPFTOUDBSFBCPVUSFMBUJPOT
    Ø  POMZTUSVDUXJUIA$SFBUFANFUIPE BOEGVODUJPOXIJDI
    TFMFDUTSPXCZ1,BSFHFOFSBUFE
    Ø  *UTUBZTCFJOHNJOJNBMUBCMFSPXEBUBHBUFXBZ
    Ø  EHXPOMZTVQQPSUT1PTUHSF42-
    Ø  XFBSFDVSSFOUMZOPUVTJOHPUIFS3%#.4PUIFSUIBO
    1PTUHSF42-

    View full-size slide

  12. "CJUPGIJTUPSZ

    Ø  8FSTUTUBSUFECVJMEJOHUIFQSPEVDUXJUIIBOEDSBGUFESBX42-
    Ø  CBDLJO (PCBTFE DPEFSTU03.MJCSBSJFTBSFCJUTIBLZ
    Ø  SFFDUJPOTFWFSZXIFSF HFOFSBUFE42-OPUBMXBZFDJFOU
    Ø  TJODF*DBNFGSPN%KBOHP03. 42-"MDIFNZXPSME *LOPX
    03.JTHSFBUJONBOZXBZT
    Ø  CVU*UIPVHIUNBZCFDPEFSTU03.JTOPUGPS(PBUUIBU
    NPNFOU
    Ø  TUBSUFEXSJUJOHSBX42-GPSPVSQSPEVDU
    Ø  NPRBEB BOE*GFMUQSFUUZDPOEFOUXSJUJOHDPNQMFY42-
    Ø  IPXFWFS JUXBTFYUSFNFMZSFQFUJUJWF CPSJOH BOEFSSPSQSPOFUP
    DSFBUFTJNQMF42-
    Ø  QSPCMFNXBTOPUDPNQMFY42- CVUSBUIFSTJNQMF BOETNBMM42-
    XJUIMPUTPGDPMVNOT
    Ø  BUUIFTBNFUJNF XFBQQSFDJBUFEIPX03.IBWFCFFOTBWJOHPVS
    UJNF

    View full-size slide

  13. %FOFQSPCMFN

    Ø  %FOFQSPCMFNXIJDIXFTIPVMETPMWF
    Ø  NBOVBMMZXSJUJOHJOTFSUSPXTFMFDUCZQL42-BSFFWJM
    Ø  DPMT BSHT BOEQMBDFIPMEFSTIBWFUPCFFYBDUMZJOPSEFS
    Ø  UIFNPTUGSFRVFOUQBUUFSO
    JOPVSQSPEVDU JODMVEJOH
    UFTUEBUBQSFQBSBUJPO

    Ø  TJODFPVSQSPEVDUEPFTOUZFUSFBMMZIBWFBOBMZUJDTGFBUVSF
    XJUIDPNQMFYEBUBNBOJQVMBUJPO
    Ø  %FOFQSPCMFNTXIJDIXFMFBWFVOTPMWFE
    Ø  XSJUJOH42-XJUINVMUJQMFKPJOT DBTFFYQSFTTJPO BOEDPNQMFY
    QSFEJDBUFT
    Ø  PGBMM42-JOPVSQSPEVDU
    Ø  UIJTJTXIFSFSBX42-SPDLT
    Ø  EZOBNJD42-DPOTUSVDUJPOEFQFOEJOHPOVTFSJOQVU
    Ø  NBZCFXFTIPVMEVTFRVFSZCVJMEFS CVUOPUZFUJNQMFNFOUFE
    XIJDINFBOTOPUUIBUOFDFTTBSZBUUIFNPNFOU

    View full-size slide

  14. IPX

    Ø  IUUQTHJUIVCDPNBDIJLVEHX
    Ø  6TFGVMJGZPVVTFXJUIHPHFOFSBUFDPNNBOE

    View full-size slide

  15. 2"

    8FBSFIJSJOH

    View full-size slide