Search code examples
sqlgogoland

Syntax highlight SQL queries when using non-standard SQL packages in Goland IDE


When using Goland it is very useful to get SQL syntax highlight and autocompletion based on my connected datasources. Unfortunately this only seems to work when I'm using the standard Go sql package and does not work when I use custom packages that effectively wrap the sql package calls. I was wondering if it's possible to tell Goland that specific functions / parameters are actually SQL queries / SQL statements.

Here's an example of Goland allowing SQL completion for methods on sql.DB struct vs not allowing completion on custom query.ReadOnlyDB struct:

Works for sql.DB does not work for custom query.ReadOnlyDB


Solution

  • Currently, you can specify //language=SQL comment before the statement:

    package main
    
    import "fmt"
    
    func main() {
        //language=SQL
        str := "SELECT USERNAME FROM EXAMPLE"
        fmt.Println(str)
    }
    

    There are a few tickets to improve SQL highlighting in GoLand and you can follow them:

    • GO-10398. SQL highlighting for custom packages/proxies.
    • GO-10011. Inject SQL automatically to strings that start with SQL queries (SELECT, CREATE, DELETE, and so on).