Skip to content

Commit

Permalink
Large rewrite to fix issues with index names
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Shaw committed Feb 26, 2016
1 parent 83091ca commit e6d56b3
Show file tree
Hide file tree
Showing 73 changed files with 3,310 additions and 3,089 deletions.
2 changes: 1 addition & 1 deletion examples/mysql/booktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func main() {
*/

// retrieve first book
books0, err := models.BooksByTitle(db, "my book title", 2016)
books0, err := models.BooksByTitleYear(db, "my book title", 2016)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/mysql/gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ENDSQL

$XOBIN $DB -o $SRC/models $EXTRA

cat << ENDSQL | $XOBIN $DB -N -M -B -T AuthorBookResult --query-type-comment='AuthorBookResult is the result of a search.' -o $SRC/models $EXTRA
$XOBIN $DB -N -M -B -T AuthorBookResult --query-type-comment='AuthorBookResult is the result of a search.' -o $SRC/models $EXTRA << ENDSQL
SELECT
a.author_id AS author_id,
a.name AS author_name,
Expand Down
54 changes: 27 additions & 27 deletions examples/mysql/models/author.xo.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,33 @@ func (a *Author) Delete(db XODB) error {
return nil
}

// AuthorsByName retrieves rows from booktest.authors, each as a Author.
// AuthorByAuthorID retrieves a row from booktest.authors as a Author.
//
// Looks up using index authors_author_id_pkey.
func AuthorByAuthorID(db XODB, authorID int) (*Author, error) {
var err error

// sql query
const sqlstr = `SELECT ` +
`author_id, name ` +
`FROM booktest.authors ` +
`WHERE author_id = ?`

// run query
XOLog(sqlstr, authorID)
a := Author{
_exists: true,
}

err = db.QueryRow(sqlstr, authorID).Scan(&a.AuthorID, &a.Name)
if err != nil {
return nil, err
}

return &a, nil
}

// AuthorsByName retrieves a row from booktest.authors as a Author.
//
// Looks up using index authors_name_idx.
func AuthorsByName(db XODB, name string) ([]*Author, error) {
Expand Down Expand Up @@ -162,29 +188,3 @@ func AuthorsByName(db XODB, name string) ([]*Author, error) {

return res, nil
}

// AuthorByAuthorID retrieves a row from booktest.authors as a Author.
//
// Looks up using index author_id.
func AuthorByAuthorID(db XODB, authorID int) (*Author, error) {
var err error

// sql query
const sqlstr = `SELECT ` +
`author_id, name ` +
`FROM booktest.authors ` +
`WHERE author_id = ?`

a := Author{
_exists: true,
}

// run query
XOLog(sqlstr, authorID)
err = db.QueryRow(sqlstr, authorID).Scan(&a.AuthorID, &a.Name)
if err != nil {
return nil, err
}

return &a, nil
}
85 changes: 62 additions & 23 deletions examples/mysql/models/book.xo.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,36 +133,82 @@ func (b *Book) Delete(db XODB) error {
return nil
}

// BookByIsbn retrieves a row from booktest.books as a Book.
// Author returns the Author associated with the Book's AuthorID (author_id).
//
// Looks up using index isbn.
func BookByIsbn(db XODB, isbn string) (*Book, error) {
// Generated from books_ibfk_1.
func (b *Book) Author(db XODB) (*Author, error) {
return AuthorByAuthorID(db, b.AuthorID)
}

// BooksByAuthorID retrieves a row from booktest.books as a Book.
//
// Looks up using index author_id.
func BooksByAuthorID(db XODB, authorID int) ([]*Book, error) {
var err error

// sql query
const sqlstr = `SELECT ` +
`book_id, author_id, isbn, book_type, title, year, available, tags ` +
`FROM booktest.books ` +
`WHERE isbn = ?`
`WHERE author_id = ?`

// run query
XOLog(sqlstr, authorID)
q, err := db.Query(sqlstr, authorID)
if err != nil {
return nil, err
}
defer q.Close()

// load results
res := []*Book{}
for q.Next() {
b := Book{
_exists: true,
}

// scan
err = q.Scan(&b.BookID, &b.AuthorID, &b.Isbn, &b.BookType, &b.Title, &b.Year, &b.Available, &b.Tags)
if err != nil {
return nil, err
}

res = append(res, &b)
}

return res, nil
}

// BookByBookID retrieves a row from booktest.books as a Book.
//
// Looks up using index books_book_id_pkey.
func BookByBookID(db XODB, bookID int) (*Book, error) {
var err error

// sql query
const sqlstr = `SELECT ` +
`book_id, author_id, isbn, book_type, title, year, available, tags ` +
`FROM booktest.books ` +
`WHERE book_id = ?`

// run query
XOLog(sqlstr, bookID)
b := Book{
_exists: true,
}

// run query
XOLog(sqlstr, isbn)
err = db.QueryRow(sqlstr, isbn).Scan(&b.BookID, &b.AuthorID, &b.Isbn, &b.BookType, &b.Title, &b.Year, &b.Available, &b.Tags)
err = db.QueryRow(sqlstr, bookID).Scan(&b.BookID, &b.AuthorID, &b.Isbn, &b.BookType, &b.Title, &b.Year, &b.Available, &b.Tags)
if err != nil {
return nil, err
}

return &b, nil
}

// BooksByTitle retrieves rows from booktest.books, each as a Book.
// BooksByTitleYear retrieves a row from booktest.books as a Book.
//
// Looks up using index books_title_idx.
func BooksByTitle(db XODB, title string, year int) ([]*Book, error) {
func BooksByTitleYear(db XODB, title string, year int) ([]*Book, error) {
var err error

// sql query
Expand Down Expand Up @@ -198,35 +244,28 @@ func BooksByTitle(db XODB, title string, year int) ([]*Book, error) {
return res, nil
}

// BookByBookID retrieves a row from booktest.books as a Book.
// BookByIsbn retrieves a row from booktest.books as a Book.
//
// Looks up using index book_id.
func BookByBookID(db XODB, bookID int) (*Book, error) {
// Looks up using index isbn.
func BookByIsbn(db XODB, isbn string) (*Book, error) {
var err error

// sql query
const sqlstr = `SELECT ` +
`book_id, author_id, isbn, book_type, title, year, available, tags ` +
`FROM booktest.books ` +
`WHERE book_id = ?`
`WHERE isbn = ?`

// run query
XOLog(sqlstr, isbn)
b := Book{
_exists: true,
}

// run query
XOLog(sqlstr, bookID)
err = db.QueryRow(sqlstr, bookID).Scan(&b.BookID, &b.AuthorID, &b.Isbn, &b.BookType, &b.Title, &b.Year, &b.Available, &b.Tags)
err = db.QueryRow(sqlstr, isbn).Scan(&b.BookID, &b.AuthorID, &b.Isbn, &b.BookType, &b.Title, &b.Year, &b.Available, &b.Tags)
if err != nil {
return nil, err
}

return &b, nil
}

// Book returns the Author associated with the Book's AuthorID (author_id).
//
// Generated from books_ibfk_1.
func (b *Book) Author(db XODB) (*Author, error) {
return AuthorByAuthorID(db, b.AuthorID)
}
6 changes: 3 additions & 3 deletions examples/mysql/models/booktype.xo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"errors"
)

// BookType is the 'book_type' enum type.
// BookType is the 'book_type' enum type from booktest.
type BookType uint16

const (
// FictionBookType is the book_type for 'FICTION'.
// FictionBookType is the 'FICTION' BookType.
FictionBookType = BookType(1)

// NonfictionBookType is the book_type for 'NONFICTION'.
// NonfictionBookType is the 'NONFICTION' BookType.
NonfictionBookType = BookType(2)
)

Expand Down
Loading

0 comments on commit e6d56b3

Please sign in to comment.