Why is there a pyramid image on a SQL article ? Haha, good question ! This article is about how to organize a SQL table to represent a tree as a pyramid shape. This technique was explained to me briefly by very skilled SQL programmer. And I must say that I did not understand the interest at first. Only by putting the explanations on paper (and by doing some drawings) I understood all the interest of this clever method.

Continue reading

SQL scan in Go without headache If this is first time you do sql with Go you probably should read this tutorial first, and comme back here to improve scan. TLDR: Use sqlx for doing SQL. sqlx sit on top of standard sql library adding very convenient function especialy to scan result to a struct. SQL scan without sqlx Sql scan is a little bit tedious in Golang : the scan order must match your SQL query order.

Continue reading

or the unexpected behaviour of nullable type from sql to JSON Problem description When working with a database you will have to deal with nullable type, you have sql.null type in standard library. But if you use JSON, you will find that they don’t encode/decode in JSON has we intend : they expose their internal; example : a SqlnullInt (valid, and value 42) encoded in JSON will look like : { "number": { "Valid": true, "Int64": 42 } } but you wanted : { "number": 42 } Simple solution to encode to JSON Behaviour of the Go standart library is easy to overload : simply redefine the MarshalJSON for each type you want Here is an example to export a SQL NullInt64 as expected in JSON :

Continue reading

Author's picture

Tristan Giovangrandi

Happy Web developper @Content-Square and happy father

Full Stack & Embedded

France - Paris