github.com/samonzeweb/godb (go): Package godb is query builder and struct mapper.  godb does not manage relationships like Active Record or Entity Framework, it's not a full-featured ORM. Its goal is to be more productive than manually doing mapping between Go structs and databases tables.  godb needs adapters to use databases, some are packaged with godb for :  Start with an adapter, and the Open method which returns a godb.DB pointer :  There are three ways to executes SQL with godb :  Using raw queries you can execute any SQL queries and get the results into a slice of structs (or single struct) using the automatic mapping.  Structs tools looks more 'orm-ish' as they're take instances of objects or slices to run select, insert, update and delete.  Statements tools stand between raw queries and structs tools. It's easier to use than raw queries, but are limited to simpler cases.  The statements tools are based on types :  Example :  The SelectStatement type could also build a query using columns from a structs. It facilitates the build of queries returning values from multiple table (or views). See struct mapping explanations, in particular the `rel` part.  Example :  The structs tools are based on types :  Examples :  Raw queries are executed using the RawSQL type.  The query could be a simple hand-written string, or something complex builded using SQLBuffer and Conditions.  Example :  Stucts contents are mapped to databases columns with tags, like in previous example with the Book struct. The tag is 'db' and its content is :  For autoincrement identifier simple use both 'key' and 'auto'.  Example :  More than one field could have the 'key' keyword, but with most databases drivers none of them could have the 'auto' keyword, because executing an insert query only returns one value : the last inserted id : https://golang.org/pkg/database/sql/driver/#RowsAffected.LastInsertId .  With PostgreSQL you cas have multiple fields with 'key' and 'auto' options.  Structs could be nested. A nested struct is mapped only if has the 'db' tag. The tag value is a columns prefix applied to all fields columns of the struct. The prefix is not mandatory, a blank string is allowed (no prefix).  A nested struct could also have an optionnal `rel` attribute of the form `rel=relationname`. It's useful to build a select query using multiples relations (table, view, ...). See the example using the BooksWithInventories type.  Example  Databases columns are :  The mapping is managed by the 'dbreflect' subpackage. Normally its direct use is not necessary, except in one case : some structs are scannable and have to be considered like fields, and mapped to databases columns. Common case are time.Time, or sql.NullString, ... You can register a custom struct with the `RegisterScannableStruct` and a struct instance, for example the time.Time is registered like this :  The structs statements use the struct name as table name. But you can override this simply by simplementing a TableName method :  Statements and structs tools manage 'where' and 'group by' sql clauses. These conditional clauses are build either with raw sql code, or build with the Condition struct like this :  WhereQ methods take a Condition instance build by godb.Q . Where mathods take raw SQL, but is just a syntactic sugar. These calls are equivalents :  Multiple calls to Where or WhereQ are allowed, these calls are equivalents :  Slices are managed in a particular way : a single placeholder is replaced with multiple ones. This allows code like :  The SQLBuffer exists to ease the build of complex raw queries. It's also used internaly by godb. Its use and purpose are simple : concatenate sql parts (accompagned by their arguments) in an efficient way.  Example :  For all databases, structs updates and deletes manage optimistic locking when a dedicated integer row is present. Simply tags it with `oplock` :  When an update or delete operation fails, Do() returns the `ErrOpLock` error.  With PostgreSQL and SQL Server, godb manages optimistic locking with automatic fields. Just add a dedicated field in the struct and tag it with `auto,oplock`.  With PostgreSQL you can use the `xmin` system column like this :  For more informations about `xmin` see https://www.postgresql.org/docs/10/static/ddl-system-columns.html  With SQL Server you can use a `rowversion` field with the `mssql.Rowversion` type like this :  For more informations about the `rowversion` data type see https://docs.microsoft.com/en-us/sql/t-sql/data-types/rowversion-transact-sql  godb keep track of time consumed while executing queries. You can reset it and get the time consumed since Open or the previous reset :  You can log all executed queried and details of condumed time. Simply add a logger :  godb takes advantage of PostgreSQL RETURNING clause, and SQL Server OUTPUT clause.  With statements tools you have to add a RETURNING clause with the Suffix method and call DoWithReturning method instead of Do(). It's optionnal.  With StructInsert it's transparent, the RETURNING or OUTPUT clause is added for all 'auto' columns and it's managed for you. One of the big advantage is with BulkInsert : for others databases the rows are inserted but the new keys are unkonwns. With PostgreSQL and SQL Server the slice is updated for all inserted rows.  It also enables optimistic locking with *automatic* columns.  godb has two prepared statements caches, one to use during transactions, and one to use outside of a transaction. Both use a LRU algorithm.  The transaction cache is enabled by default, but not the other. A transaction (sql.Tx) isn't shared between goroutines, using prepared statement with it has a predictable behavious. But without transaction a prepared statement could have to be reprepared on a different connection if needed, leading to unpredictable performances in high concurrency scenario.  Enabling the non transaction cache could improve performances with single goroutine batch. With multiple goroutines accessing the same database : it depends ! A benchmark would be wise.  Using statements tools and structs tools you can execute select queries and get an iterator instead of filling a slice of struct instances. This could be useful if the request's result is big and you don't want to allocate too much memory. On the other side you will write almost as much code as with the `sql` package, but with an automatic struct mapping, and a request builder.  Iterators are also available with raw queries. In this cas you cas executes any kind of sql code, not just select queries.  To get an interator simply use the `DoWithIterator` method instead of `Do`. The iterator usage is similar to the standard `sql.Rows` type. Don't forget to check that there are no errors with the `Err` method, and don't forget to call `Close` when the iterator is no longer useful, especially if you don't scan all the resultset.  To avoid performance cost godb.DB does not implement synchronization. So a given instance of godb.DB should not be used by multiple goroutines. But a godb.DB instance can be created and used as a blueprint and cloned for each goroutine. See Clone and Clear methods.  A typical use case is a web server. When the application starts a godb.DB is created, and cloned in each http handler with Clone, and ressources are to be freed calling Clear (use defer statement).
Keywords: database|go|golang|orm|sql