Skip to content

pgrunde/sol

 
 

Repository files navigation

Sol GoDoc Build Status

A relational database toolkit for Go:

  • Build complete database schemas
  • Create reusable and cross-dialect SQL statements
  • Allow struct instances and slices to be directly populated by the database

Quickstart

package main

import (
	"log"

	"github.com/aodin/sol"
	_ "github.com/aodin/sol/sqlite3"
	"github.com/aodin/sol/types"
)

// Create a database schema using sol's Table function
var Users = sol.Table("users",
	sol.Column("id", types.Integer().NotNull()),
	sol.Column("name", types.Varchar().Limit(32).NotNull()),
	sol.Column("password", types.Varchar().Limit(128).NotNull()),
	sol.PrimaryKey("id"),
)

// Structs are used to send and receive values to the database
type User struct {
	ID       int64
	Name     string
	Password string
}

func main() {
	// Connect to an in-memory sqlite3 instance
	conn, err := sol.Open("sqlite3", ":memory:")
	if err != nil {
		log.Panic(err)
	}
	defer conn.Close()

	// Create the users table
	conn.Query(Users.Create())

	// Insert a user - they can be inserted by value or reference
	admin := User{ID: 1, Name: "admin", Password: "secret"}
	conn.Query(Users.Insert().Values(admin))

	// Select a user - query methods must be given a pointer
	var user User
	conn.Query(Users.Select(), &user)
	log.Println(user)
}

Happy Hacking!

aodin, 2015

Limitless undying love which shines around me like a million suns

It calls me on and on, across the universe

John Lennon

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%