This is a protobuf-plugin to generate SQL-Select mappers in go to insert, update and query protbuf messages directly from tables or views.
Install with:
go install github.com/roderm/protoc-gen-go-sqlmap
protoc --go-sqlmap_out=. [your-protobuf-definition].proto
- add dependency
buf.build/roderm/protoc-gen-go-sqlmap
- add
go-sqlmap
andgo
to yourbuf.gen.yaml
To make use of this plugin, add following extensions to you .proto-file:
import "github.com/roderm/protoc-gen-go-sqlmap/proto/sqlgen/sqlgen.proto" // Import of the extensions
// or with buf
import "proto/sqlgen/sqlgen.proto"
message Some {
option (sqlgen.table) = {name:"tbl_some", crud:[R]}; // table name and generate read-function
string Id = 1 [(sqlgen.col) = {pk: AUTO, name = "some_id" }]; // It's an autogenerated key in the field "some_id"
repeated Attribute Attributes = 2; [(sqlgeb.col) = {name "some_id",fk = "tbl_some_attributes.some_id"}]; // Make a one-to-many link to load
}
message Attribute {
option (sqlgen.table) = {name:"tbl_some_attributes", crud:[C,R,D]}; // genereate insert,select and delete function
string Id = 1 [(sqlgen.col) = {pk= AUTO, name = "attributes_id" }];
string Name = 2 [(sqlgeb.col).name = "attributes_name"];
bytes Value = 3 [(sqlgeb.col).name = "attributes_value"];
Some Parent = 4 [(sqlgeb.col) = {name: "some_id", fk = "tbl_some.some_id"}]; // Make a one-to-one link to load
}
After generation of the .sqlmap.go-file you should be able to load the data as following:
store = package.NewStore(db_conn)
rows, err = store.Some(context.TODO(), package.SomeWithAttributes())
Additional there can be given extra parameters for filtering (Currently only forks for Postgres/Cockroach):
store.Some(context.TODO(), SomeFilter(pg.EQ("some_id", "some_id"))
or for the sub-query:
store.Some(context.TODO(), package.SomeWithAttributes(AttributeFilter(pg.EQ("name", "FirstArgument")))
- [-] Queries
- loading of one-to-many
- loading of one-to-one
- add parameter to not resolve references (problem with json-encoding)
- [-] Insert
- Insert single message to table
- resolve foreign-key (and create)
- Delete
- delete a data-row
- Implement update
- Should have use replace and conflict handling parameter
- Decouple gogo
- Write generator test
- Support multiple PKs
- Add multiple FKs for single Message
- Support
oneOf
type in proto3 - Improve filter to no use column-names
- Load external messages to resolve foreign keys
- Implement more Database-Engines & syntax's
- CockroachDB
- PostgreSQL
- MySQL / MariaDB
- MSSQL
- SQLite