Codex is NOT an ORM, but a relation algebra inspired by Arel for generating SQL. Project is still early in development but stable.
With Google's Go installed on your machine:
$ go get -u github.com/chuckpreslar/codex
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?
// ...
users := codex.Table("users")
sql, err := users.Project("id", "email", "first_name", "last_name").ToSql()
// ...
users := codex.Table("users")
sql, err := users.Where(users("id").Eq(1).Or(users("email").Eq("[email protected]"))).ToSql()
// ...
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"
// ...
sql, err := users.Insert("Jon", "Doe", "[email protected]").
Into("first_name", "last_name", "email").ToSql()
// INSERT INTO "users" ("first_name", "last_name", "email") VALUES ('Jon', 'Doe', '[email protected]')
// ...
sql, err := users.Set("first_name", "last_name", "email").
To("Jon", "Doe", "[email protected]").
Where(users("id").Eq(1)).ToSql()
// UPDATE "users" SET "first_name" = 'Jon', "last_name" = 'Doe', "email" = '[email protected]'
// WHERE "users"."id" = 1
// ...
sql, err := users.Delete(users("id").Eq(1)).ToSql()
// DELETE FROM "users" WHERE "users"."id" = 1
The codex package currently provides a few common SQL data and constraint types as constants to use with creating and altering tables. These constants can be found within codex's sql
package.
// ...
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.NotNull).
AddConstraint("id", codex.PrimaryKey).
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.ForeignKey
, treats its options argument as a SQL REFERENCE
followed by the optional name of the index.
Alterations work the same as Creations, only the the initializer function AlterTable
is used in place of CreateTable
.
View godoc or visit godoc.org.
$ godoc codex
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.