From 3390592f47161beb0acfa052b9d57f704869feaf Mon Sep 17 00:00:00 2001
From: Carlos Nieto <jose.carlos@menteslibres.net>
Date: Wed, 16 Apr 2014 18:43:05 -0500
Subject: [PATCH] Adding example for QL.

---
 ql/_example/example.sql |  6 +++
 ql/_example/main.go     | 84 +++++++++++++++++++++++++++++++++++++++++
 ql/_example/test.sh     |  4 ++
 3 files changed, 94 insertions(+)
 create mode 100644 ql/_example/example.sql
 create mode 100644 ql/_example/main.go
 create mode 100755 ql/_example/test.sh

diff --git a/ql/_example/example.sql b/ql/_example/example.sql
new file mode 100644
index 00000000..5a04bfc7
--- /dev/null
+++ b/ql/_example/example.sql
@@ -0,0 +1,6 @@
+DROP TABLE IF EXISTS birthdays;
+
+CREATE TABLE birthdays (
+  name string,
+  born time
+);
diff --git a/ql/_example/main.go b/ql/_example/main.go
new file mode 100644
index 00000000..013517e8
--- /dev/null
+++ b/ql/_example/main.go
@@ -0,0 +1,84 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"time"
+	"upper.io/db"      // Imports the main db package.
+	_ "upper.io/db/ql" // Imports the ql adapter.
+)
+
+var settings = db.Settings{
+	Database: `example.db`, // Path to database file.
+}
+
+type Birthday struct {
+	// Maps the "Name" property to the "name" column of the "birthdays" table.
+	Name string `field:"name"`
+	// Maps the "Born" property to the "born" column of the "birthdays" table.
+	Born time.Time `field:"born"`
+}
+
+func main() {
+
+	// Attemping to open the "example.db" database file.
+	sess, err := db.Open("ql", settings)
+
+	if err != nil {
+		log.Fatalf("db.Open(): %q\n", err)
+	}
+
+	// Remember to close the database session.
+	defer sess.Close()
+
+	// Pointing to the "birthdays" table.
+	birthdayCollection, err := sess.Collection("birthdays")
+
+	if err != nil {
+		log.Fatalf("sess.Collection(): %q\n", err)
+	}
+
+	// Attempt to remove existing rows (if any).
+	err = birthdayCollection.Truncate()
+
+	if err != nil {
+		log.Fatalf("Truncate(): %q\n", err)
+	}
+
+	// Inserting some rows into the "birthdays" table.
+
+	birthdayCollection.Append(Birthday{
+		Name: "Hayao Miyazaki",
+		Born: time.Date(1941, time.January, 5, 0, 0, 0, 0, time.Local),
+	})
+
+	birthdayCollection.Append(Birthday{
+		Name: "Nobuo Uematsu",
+		Born: time.Date(1959, time.March, 21, 0, 0, 0, 0, time.Local),
+	})
+
+	birthdayCollection.Append(Birthday{
+		Name: "Hironobu Sakaguchi",
+		Born: time.Date(1962, time.November, 25, 0, 0, 0, 0, time.Local),
+	})
+
+	// Let's query for the results we've just inserted.
+	var res db.Result
+
+	res = birthdayCollection.Find()
+
+	var birthdays []Birthday
+
+	// Query all results and fill the birthdays variable with them.
+	err = res.All(&birthdays)
+
+	if err != nil {
+		log.Fatalf("res.All(): %q\n", err)
+	}
+
+	// Printing to stdout.
+	for _, birthday := range birthdays {
+		fmt.Printf("%s was born in %s.\n", birthday.Name, birthday.Born.Format("January 2, 2006"))
+	}
+
+}
diff --git a/ql/_example/test.sh b/ql/_example/test.sh
new file mode 100755
index 00000000..a6de8c67
--- /dev/null
+++ b/ql/_example/test.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+rm -f example.db
+cat example.sql | ql -db example.db
+go run main.go
-- 
GitLab