├── movies.db ├── Gopkg.toml ├── Gopkg.lock ├── LICENSE ├── README.md └── main.go /movies.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slackhq/sqlite-go-connect/HEAD/movies.db -------------------------------------------------------------------------------- /Gopkg.toml: -------------------------------------------------------------------------------- 1 | # Gopkg.toml example 2 | # 3 | # Refer to https://golang.github.io/dep/docs/Gopkg.toml.html 4 | # for detailed Gopkg.toml documentation. 5 | # 6 | # required = ["github.com/user/thing/cmd/thing"] 7 | # ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] 8 | # 9 | # [[constraint]] 10 | # name = "github.com/user/project" 11 | # version = "1.0.0" 12 | # 13 | # [[constraint]] 14 | # name = "github.com/user/project2" 15 | # branch = "dev" 16 | # source = "github.com/myfork/project2" 17 | # 18 | # [[override]] 19 | # name = "github.com/x/y" 20 | # version = "2.4.0" 21 | # 22 | # [prune] 23 | # non-go = false 24 | # go-tests = true 25 | # unused-packages = true 26 | 27 | 28 | [prune] 29 | go-tests = true 30 | unused-packages = true 31 | -------------------------------------------------------------------------------- /Gopkg.lock: -------------------------------------------------------------------------------- 1 | # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. 2 | 3 | 4 | [[projects]] 5 | branch = "master" 6 | digest = "1:7654989089e5bd5b6734ec3be8b695e87d3f1f8d95620b343fd7d3995a5b60d7" 7 | name = "github.com/jmoiron/sqlx" 8 | packages = [ 9 | ".", 10 | "reflectx", 11 | ] 12 | pruneopts = "UT" 13 | revision = "0dae4fefe7c0e190f7b5a78dac28a1c82cc8d849" 14 | 15 | [[projects]] 16 | digest = "1:3cafc6a5a1b8269605d9df4c6956d43d8011fc57f266ca6b9d04da6c09dee548" 17 | name = "github.com/mattn/go-sqlite3" 18 | packages = ["."] 19 | pruneopts = "UT" 20 | revision = "25ecb14adfc7543176f7d85291ec7dba82c6f7e4" 21 | version = "v1.9.0" 22 | 23 | [solve-meta] 24 | analyzer-name = "dep" 25 | analyzer-version = 1 26 | input-imports = [ 27 | "github.com/jmoiron/sqlx", 28 | "github.com/mattn/go-sqlite3", 29 | ] 30 | solver-name = "gps-cdcl" 31 | solver-version = 1 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Slack 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sqlite-go-connect 2 | 3 | Simple go app to load the SQLite file (`movies.db`) included in this repo. 4 | 5 | ## Installation 6 | 7 | 1. Install go (tested with v1.11) and set $GOPATH 8 | 2. Install dep (tested with v0.5.0) 9 | 3. Run `dep ensure` to install dependencies 10 | 11 | ## Running 12 | 13 | 1. Run `go run main.go` to run the app 14 | 2. Make sure you see output like this 15 | ``` 16 | ------------------------------------ 17 | Title: Memento 18 | Director: Christopher Nolan 19 | Year: 2000 20 | Runtime: 113 21 | ------------------------------------ 22 | Title: Raiders of the Lost Ark 23 | Director: Steven Spielberg 24 | Year: 1981 25 | Runtime: 115 26 | ------------------------------------ 27 | Title: Gran Torino 28 | Director: Clint Eastwood 29 | Year: 2008 30 | Runtime: 116 31 | ------------------------------------ 32 | ... 33 | ``` 34 | 35 | ## Preparing for the onsite 36 | 37 | We want you to feel prepared for the on-site. Before you arrive, be able to answer the following questions using the dataset we've provided: 38 | 39 | 1. What is the longest-running movie? 40 | 2. What movie has the most actors? 41 | 3. What is the breakdown of top movies by rating? 42 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | 7 | "github.com/jmoiron/sqlx" 8 | _ "github.com/mattn/go-sqlite3" 9 | ) 10 | 11 | func main() { 12 | 13 | // load movies db file 14 | db, err := sql.Open("sqlite3", "./movies.db") 15 | if err != nil { 16 | panic(err) 17 | } 18 | 19 | // query to select top 10 shortest runtime movies by given directors and years 20 | sql := "SELECT title, director, year, runtime FROM movies WHERE director IN (?) AND year IN (?) ORDER BY runtime limit ?" 21 | directors := []string{"Clint Eastwood", "Christopher Nolan", "Steven Spielberg"} 22 | years := []int{1981, 1989, 1993, 2000, 2008} 23 | count := 10 24 | 25 | // expanding '?' placeholders for array args. Not needed for queries without array args 26 | query, args, err := sqlx.In(sql, directors, years, count) 27 | if err != nil { 28 | panic(err) 29 | } 30 | 31 | rows, err := db.Query(query, args...) 32 | if err != nil { 33 | panic(err) 34 | } 35 | 36 | var title string 37 | var director string 38 | var runtime int 39 | var year int 40 | 41 | fmt.Println("------------------------------------") 42 | for rows.Next() { 43 | err = rows.Scan(&title, &director, &year, &runtime) 44 | if err != nil { 45 | panic(err) 46 | } 47 | 48 | // print each movie 49 | fmt.Printf("Title: %v\n", title) 50 | fmt.Printf("Director: %v\n", director) 51 | fmt.Printf("Year: %v\n", year) 52 | fmt.Printf("Runtime: %v\n", runtime) 53 | fmt.Println("------------------------------------") 54 | } 55 | 56 | rows.Close() 57 | db.Close() 58 | } 59 | --------------------------------------------------------------------------------