Created
October 25, 2024 16:36
-
-
Save nenodias/01161556e4498d67242e9620c8317e8f to your computer and use it in GitHub Desktop.
Example Mysql Table with *string id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"time" | |
"github.com/vingarcia/ksql" | |
"github.com/vingarcia/ksql/adapters/kmysql" | |
) | |
var SociosTable = ksql.NewTable("socios", "cnpj_basico") | |
type Socio struct { | |
CnpjBasico *string `ksql:"cnpj_basico"` | |
IdSocio *string `ksql:"id_socio"` | |
Nome *string `ksql:"nome"` | |
DocumentoSocio *string `ksql:"documento_socio"` | |
Qualificacao *int64 `ksql:"qualificacao"` | |
DataEntrada *time.Time `ksql:"data_entrada"` | |
IdPais *int64 `ksql:"id_pais"` | |
CpfRepresentanteLegal *string `ksql:"cpf_representante_legal"` | |
NomeRepresentanteLegal *string `ksql:"nome_representante_legal"` | |
QualificacaoRepresentanteLegal *int64 `ksql:"qualificacao_representante_legal"` | |
FaixaEtariaSocio *string `ksql:"faixa_etaria_socio"` | |
} | |
func main() { | |
ctx := context.Background() | |
config := map[string]string{ | |
"database": "database", | |
"username": "root", | |
"password": "root", | |
"host": "localhost", | |
"port": "3306", | |
} | |
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", config["username"], config["password"], config["host"], config["port"], config["database"]) | |
db, err := kmysql.New(ctx, dsn, ksql.Config{}) | |
if err != nil { | |
log.Fatalf("unable connect to database: %s", err) | |
} | |
defer db.Close() | |
/* | |
_, err = db.Exec(ctx, | |
`CREATE TABLE socios ( | |
cnpj_basico text DEFAULT NULL, | |
id_socio text DEFAULT NULL, | |
nome text DEFAULT NULL, | |
documento_socio text DEFAULT NULL, | |
qualificacao int DEFAULT NULL, | |
data_entrada date DEFAULT NULL, | |
id_pais int DEFAULT NULL, | |
cpf_representante_legal text DEFAULT NULL, | |
nome_representante_legal text DEFAULT NULL, | |
qualificacao_representante_legal int DEFAULT NULL, | |
faixa_etaria_socio text DEFAULT NULL | |
)`, | |
) | |
if err != nil { | |
log.Fatalf("error creating table: %s", err) | |
} */ | |
err = db.Insert(ctx, SociosTable, &Socio{ | |
CnpjBasico: ptr("fakeCNPJ"), | |
Nome: ptr("fakeName"), | |
DocumentoSocio: ptr("fakeDocumentoSocio"), | |
Qualificacao: ptr[int64](42), | |
DataEntrada: ptr(time.Now()), | |
IdPais: ptr[int64](43), | |
CpfRepresentanteLegal: ptr("fakeCpfReprLegal"), | |
NomeRepresentanteLegal: ptr("fakeNomeReprLegal"), | |
QualificacaoRepresentanteLegal: ptr[int64](44), | |
FaixaEtariaSocio: ptr("fakeFaixaEtariaSocio"), | |
}) | |
if err != nil { | |
panic(err.Error()) | |
} | |
fmt.Println("SUCESSO") | |
} | |
func ptr[T any](obj T) *T { | |
return &obj | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment