Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
SQL Relational Algebra for Go.
branch: master

This branch is 7 commits behind chuckpreslar:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
managers
nodes
sql
test
visitors
.gitignore
HISTORY.md
Makefile
README.md
codex.go

README.md

codex

Codex is NOT an ORM, but a relation algebra inspired by Arel for generating SQL. Project is still early in development but stable.

Build Status

Installation

With Google's Go installed on your machine:

$ go get -u github.com/chuckpreslar/codex

Usage

To show a little sample of what using Codex looks like, lets assume you have a table in your database (we'll call it users) and you want to select all the records it contains. The SQL looks a little like this:

SELECT "users".* FROM "users"

Now using Codex:

import (
  /* Maybe some other imports. */
  "github.com/chuckpreslar/codex"
)


users := codex.Table("users")
sql, err := users.ToSql()

Now that wasn't too bad, was it?

Selections

Projections

// ...

users := codex.Table("users")
sql, err := users.Project("id", "email", "first_name", "last_name").ToSql()

Filtering

// ...

users := codex.Table("users")
sql, err := users.Where(users("id").Eq(1).Or(users("email").Eq("test@example.com"))).ToSql()

Joins

// ...

users := codex.Table("users")
orders := codex.Table("orders")
sql, err := users.InnerJoin(orders).On(orders("user_id").Eq(users("id"))).ToSql()

// SELECT "users".* FROM "users" INNER JOIN "orders" ON "orders"."user_id" = "users"."id"

Insertions

// ...

sql, err := users.Insert("Jon", "Doe", "jon@example.com").
    Into("first_name", "last_name", "email").ToSql()

// INSERT INTO "users" ("first_name", "last_name", "email") VALUES ('Jon', 'Doe', 'jon@example.com')

Modifications

// ...

sql, err := users.Set("first_name", "last_name", "email").
    To("Jon", "Doe", "jon@example.com").
    Where(users("id").Eq(1)).ToSql()

// UPDATE "users" SET "first_name" = 'Jon', "last_name" = 'Doe', "email" = 'jon@example.com'
// WHERE "users"."id" = 1

Deletions

// ...

sql, err := users.Delete(users("id").Eq(1)).ToSql()

// DELETE FROM "users" WHERE "users"."id" = 1

Creations

The codex package currently provides a few common SQL data and constraint types as constants to use with creating and altering tables.

Constraints

  • NOT_NULL
  • UNIQUE
  • PRIMARY_KEY
  • FOREIGN_KEY
  • CHECK
  • DEFAULT

Types

  • STRING
  • TEXT
  • BOOLEAN
  • INTEGER
  • FLOAT
  • DECIMAL
  • DATE
  • TIME
  • DATETIME
  • TIMESTAMP
// ...

users := codex.CreateTable("users").
  AddColumn("first_name", codex.STRING).
  AddColumn("last_name", codex.STRING).
  AddColumn("email", codex.STRING).
  AddColumn("id", codex.INTEGER).
  AddConstraint("first_name", codex.NOT_NULL).
  AddConstraint("id", codex.PRIMARY_KEY).
  AddConstraint("email", codex.UNIQUE, "users_uniq_email") // Optional last argument supplies index name.

sql, err := users.ToSql()

// CREATE TABLE "users" (
//  "first_name" varchar(255),
//  "last_name" varchar(255),
//  "email" varchar(255),
//  "id" integer
// );
// ALTER TABLE "users" ALTER "first_name" SET NOT NULL;
// ALTER TABLE "users" ADD PRIMARY KEY("id");
// ALTER TABLE "users" ADD CONSTRAINT "users_email_uniq" UNIQUE("email");

Note: The AddConstraint receiver method when passed a constraint of codex.FOREIGN_KEY, treats its options argument as a SQL REFERENCE followed by the optional name of the index. Though optional to codex, a FOREIGN KEY constraint should always be given a minimum of a reference.

Alterations

Alterations work the same as Creations, only the the initializer function AlterTable is used in place of CreateTable.

Documentation

View godoc or visit godoc.org.

$ godoc codex

License

The MIT License (MIT)

Copyright (c) 2013 Chuck Preslar

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.