Exemple #1
0
func main() {
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()

	err = sess.Tx(func(tx sqlbuilder.Tx) error {
		// Use tx like you would normally use sess:
		total, err := tx.Collection("books").Find().Count()
		if err != nil {
			return err
		}
		log.Printf("total within tx: %d", total)

		// This won't work on our testing sandbox, you'll have to try it out on a local env.
		// if err = tx.Collection("books").Find().Delete(); err != nil {
		//   return err
		// }
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}

	total, err := sess.Collection("books").Find().Count()
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("total outside tx: %d", total)
}
Exemple #2
0
func main() {
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()

	req := sess.Select(
		"b.id AS book_id",
		db.Raw("b.*"),
		db.Raw("a.*"),
	).From("books b").
		Join("authors a").On("b.author_id = a.id").
		OrderBy("b.title")

	var books []BookAuthor
	if err := req.All(&books); err != nil {
		log.Fatal(err)
	}

	for _, book := range books {
		log.Printf(
			"Book: %q. ID: %d, Author: %s\n",
			book.Book.Title,
			book.BookID,
			book.Author.LastName,
		)
	}
}
Exemple #3
0
func main() {
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()

	// Set this to true to enable the query logger which will print all SQL
	// statements to stdout.
	db.Conf.SetLogging(false)

	// Define a result set without passing a condition to Find(), this means we
	// want to match all the elements on the books table.
	res := sess.Collection("books").Find()

	// We can use this res object later in different queries, here we'll use it
	// to fetch all the books on our catalog in descending order.
	var books []Book
	if err := res.OrderBy("title DESC").All(&books); err != nil {
		log.Fatal(err)
	}

	// The books slice has been populated!
	log.Println("Books:")
	for _, book := range books {
		log.Printf("%q (ID: %d)\n", book.Title, book.ID)
	}
}
Exemple #4
0
func main() {
	// Establish a connection.
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close() // Close the connection at the end of the program.

	log.Println("Connection established!")
}
Exemple #5
0
func main() {
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()

	howManyBooks, err := sess.Collection("books").Find().Count()
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("We have %d books in our database.\n", howManyBooks)
}
Exemple #6
0
func main() {
	settings, err := postgresql.ParseURL(connectDSN)
	if err != nil {
		log.Fatal(err)
	}

	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()

	log.Println("Connection established!")
}
Exemple #7
0
func main() {
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()

	req := sess.Collection("stock").Find()

	log.Println("Items in stock:")
	var item Stock
	for req.Next(&item) {
		log.Printf("%#v", item)
	}
}
Exemple #8
0
func main() {
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()

	req := sess.Collection("employees").Find().OrderBy("last_name")

	log.Println("A list of employees:")

	var employee Employee
	for req.Next(&employee) {
		log.Printf("#%d: %s\n", employee.ID, employee.LastName)
	}
}
Exemple #9
0
func main() {
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}

	defer sess.Close()

	var book Book
	if err := sess.Collection("books").Find("title LIKE ?", "Perl%").One(&book); err != nil {
		log.Fatal(err)
	}

	log.Println("We have one Perl related book:")
	log.Printf("%q (ID: %d)\n", book.Title, book.ID)
}
Exemple #10
0
func main() {
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()

	res := sess.Collection("customers").Find().OrderBy("last_name")
	defer res.Close()

	log.Println("Our customers:")

	var customer Customer
	for res.Next(&customer) {
		log.Printf("%d: %s, %s\n", customer.ID, customer.LastName, customer.FirstName)
	}
}
Exemple #11
0
Fichier : main.go Projet : upper/db
func main() {
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()

	var books []Book
	err = sess.Collection("books").Find().All(&books)
	if err != nil {
		log.Fatal(err)
	}

	for _, book := range books {
		log.Printf("%q (ID: %d)\n", book.Title, book.ID)
	}
}
Exemple #12
0
func main() {
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()

	type joinResult struct {
		// Note that the ID field is ambiguous, that's why we need this field.
		ID      int `db:"book_id"`
		Book    `db:",inline"`
		Author  `db:",inline"`
		Subject `db:",inline"`
	}

	req := sess.Select(
		"b.id AS book_id", // See ID field on joinResult type.
		db.Raw("b.*"),
		db.Raw("a.*"),
		db.Raw("s.*"),
	).From("books b").
		Join("authors a").On("b.author_id = a.id").
		Join("subjects s").On("b.subject_id = s.id").
		OrderBy("b.title")

	iter := req.Iterator()

	var books []joinResult

	if err := iter.All(&books); err != nil {
		log.Fatal(err)
	}

	for _, book := range books {
		log.Printf(
			"Book: %q. ID: %d, Author: %s, %s. Subject: %s. Location: %s\n",
			book.Book.Title,
			book.ID,
			book.Author.LastName,
			book.Author.FirstName,
			book.Subject.Subject,
			book.Subject.Location,
		)
	}
}
Exemple #13
0
func main() {
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()

	// Check if we have any employee with a NULL name.
	req := sess.Collection("employees").Find().Where("first_name IS NULL")

	var employee Employee
	if err := req.One(&employee); err != nil {
		log.Printf("All employees have a first name!")
		return
	}

	log.Printf("The employee #%d %q has no name (%#v).", employee.ID, employee.LastName, employee)
}
Exemple #14
0
func main() {
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()

	req := sess.SelectFrom("books").OrderBy("id")

	var books []Book
	iter := req.Iterator()
	if err := iter.All(&books); err != nil {
		log.Fatal(err)
	}

	log.Printf("A list of books:")
	for _, book := range books {
		log.Printf("%#v\n", book)
	}
}
Exemple #15
0
func main() {
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()

	var res db.Result

	res = sess.Collection("authors").Find().OrderBy("last_name").Limit(5)

	var authors []Author
	if err := res.All(&authors); err != nil {
		log.Fatal(err)
	}

	log.Println("Authors (5):")
	for _, author := range authors {
		log.Printf(
			"Last Name: %s\tID: %d\n",
			author.LastName,
			author.ID,
		)
	}

	res = sess.Collection("employees").Find().OrderBy("last_name").Limit(5)

	var employees []Author
	if err := res.All(&employees); err != nil {
		log.Fatal(err)
	}

	log.Println("Employees (5):")
	for _, employee := range employees {
		log.Printf(
			"Last Name: %s\tID: %d\n",
			employee.LastName,
			employee.ID,
		)
	}
}
Exemple #16
0
func main() {
	sess, err := postgresql.Open(settings)
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()

	since := time.Date(2001, time.September, 1, 0, 0, 0, 0, time.Local)
	until := time.Date(2001, time.October, 1, 0, 0, 0, 0, time.Local).Add(time.Second * -1)

	req := sess.Collection("shipments").Find().
		Where("ship_date > ? AND ship_date < ?", since, until).
		OrderBy("ship_date")

	log.Printf("Shipments between %v and %v:\n", since, until)

	var shipment Shipment
	for req.Next(&shipment) {
		log.Printf("When: %v, ISBN: %s\n", shipment.ShipDate, shipment.ISBN)
	}
}