Skip to content

Commit

Permalink
Updating examples to be a bit cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Shaw committed Feb 20, 2016
1 parent 4091650 commit 83091ca
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 23 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ The following projects work with similar concepts as xo:
* [sqlc](https://github.com/relops/sqlc)

# TODO #
* Column mapping option on custom queries
* Finish support for --{incl, excl}[ude] types
* Finish support for ignoring fields (ie, fields managed by database such as
'modified' timestamps)
Expand Down
10 changes: 8 additions & 2 deletions examples/mysql/booktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"database/sql"
"flag"
"fmt"
"log"
"time"
Expand All @@ -11,12 +12,17 @@ import (
"github.com/knq/xo/examples/mysql/models"
)

var flagVerbose = flag.Bool("v", false, "verbose")

func main() {
var err error

// set logging
models.XOLog = func(s string, p ...interface{}) {
fmt.Printf("> SQL: %s -- %v\n", s, p)
flag.Parse()
if *flagVerbose {
models.XOLog = func(s string, p ...interface{}) {
fmt.Printf("> SQL: %s -- %v\n", s, p)
}
}

// open database
Expand Down
10 changes: 6 additions & 4 deletions examples/mysql/gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ DBNAME=booktest

DB=mysql://$DBUSER:$DBPASS@$DBHOST/$DBNAME

EXTRA=$1

SRC=$(realpath $(cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd ))

XOBIN=$(which xo)
Expand Down Expand Up @@ -55,9 +57,9 @@ CREATE FUNCTION say_hello(s text) RETURNS text
RETURN CONCAT('hello ', s);
ENDSQL

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

cat << ENDSQL | $XOBIN $DB -v -N -M -B -T AuthorBookResult --query-type-comment='AuthorBookResult is the result of a search.' -o $SRC/models
cat << ENDSQL | $XOBIN $DB -N -M -B -T AuthorBookResult --query-type-comment='AuthorBookResult is the result of a search.' -o $SRC/models $EXTRA
SELECT
a.author_id AS author_id,
a.name AS author_name,
Expand All @@ -73,8 +75,8 @@ ENDSQL
pushd $SRC &> /dev/null

go build
./mysql
./mysql $EXTRA

popd &> /dev/null

mysql -u $DBUSER -p$DBPASS $DBNAME <<< 'select * from books;'
mysql -u $DBUSER -p$DBPASS $DBNAME -e 'select * from books;'
10 changes: 8 additions & 2 deletions examples/postgres/booktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"database/sql"
"flag"
"fmt"
"log"
"time"
Expand All @@ -11,12 +12,17 @@ import (
"github.com/knq/xo/examples/postgres/models"
)

var flagVerbose = flag.Bool("v", false, "verbose")

func main() {
var err error

// set logging
models.XOLog = func(s string, p ...interface{}) {
fmt.Printf("> SQL: %s -- %v\n", s, p)
flag.Parse()
if *flagVerbose {
models.XOLog = func(s string, p ...interface{}) {
fmt.Printf("> SQL: %s -- %v\n", s, p)
}
}

// open database
Expand Down
8 changes: 5 additions & 3 deletions examples/postgres/gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ DBNAME=booktest

DB=postgres://$DBUSER:$DBPASS@$DBHOST/$DBNAME

EXTRA=$1

SRC=$(realpath $(cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd ))

XOBIN=$(which xo)
Expand Down Expand Up @@ -60,9 +62,9 @@ $$ LANGUAGE plpgsql;
ENDSQL

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

cat << ENDSQL | $XOBIN $DB -v -N -M -B -T AuthorBookResult --query-type-comment='AuthorBookResult is the result of a search.' -o $SRC/models
cat << ENDSQL | $XOBIN $DB -N -M -B -T AuthorBookResult --query-type-comment='AuthorBookResult is the result of a search.' -o $SRC/models $EXTRA
SELECT
a.author_id::integer AS author_id,
a.name::text AS author_name,
Expand All @@ -78,7 +80,7 @@ ENDSQL
pushd $SRC &> /dev/null

go build
./postgres
./postgres $EXTRA

popd &> /dev/null

Expand Down
10 changes: 8 additions & 2 deletions examples/sqlite/booktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"database/sql"
"flag"
"fmt"
"log"
"time"
Expand All @@ -11,12 +12,17 @@ import (
"github.com/knq/xo/examples/sqlite/models"
)

var flagVerbose = flag.Bool("v", false, "verbose")

func main() {
var err error

// set logging
models.XOLog = func(s string, p ...interface{}) {
fmt.Printf("> SQL: %s -- %v\n", s, p)
flag.Parse()
if *flagVerbose {
models.XOLog = func(s string, p ...interface{}) {
fmt.Printf("> SQL: %s -- %v\n", s, p)
}
}

// open database
Expand Down
18 changes: 12 additions & 6 deletions examples/sqlite/gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

DBNAME=booktest.sqlite3

EXTRA=$1

SRC=$(realpath $(cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd ))

DB=file:$SRC/$DBNAME
Expand Down Expand Up @@ -44,10 +46,9 @@ CREATE INDEX books_title_idx ON books(title, year);
ENDSQL

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

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

cat << ENDSQL | $XOBIN $DB -v -N -M -B -T AuthorBookResult --query-type-comment='AuthorBookResult is the result of a search.' -o $SRC/models
cat << ENDSQL | $XOBIN $DB -N -M -B -T AuthorBookResult --query-type-comment='AuthorBookResult is the result of a search.' -o $SRC/models $EXTRA
SELECT
a.author_id,
a.name AS author_name,
Expand All @@ -57,14 +58,19 @@ SELECT
b.tags AS book_tags
FROM books b
JOIN authors a ON a.author_id = b.author_id
WHERE LIKE(b.tags, '%' || %%tag string%% || '%')
WHERE b.tags LIKE '%' || %%tag string%% || '%'
ENDSQL

pushd $SRC &> /dev/null

go build
./sqlite
./sqlite $EXTRA

popd &> /dev/null

sqlite3 $DB <<< 'select * from books;'
sqlite3 $DB << ENDSQL
.headers on
.mode column
.width 7 9 4 20 4 21 15
select * from books;
ENDSQL
2 changes: 1 addition & 1 deletion examples/sqlite/models/authorbookresult.xo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func AuthorBookResultsByTag(db XODB, tag string) ([]*AuthorBookResult, error) {
`b.tags AS book_tags ` +
`FROM books b ` +
`JOIN authors a ON a.author_id = b.author_id ` +
`WHERE LIKE(b.tags, '%' || ? || '%')`
`WHERE b.tags LIKE '%' || ? || '%'`

// run query
XOLog(sqlstr, tag)
Expand Down
2 changes: 0 additions & 2 deletions loaders/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package loaders
import (
"database/sql"
"errors"
"fmt"
"net/url"
"path"
"sort"
Expand Down Expand Up @@ -134,7 +133,6 @@ func SqLoadTableColumns(args *internal.ArgType, db *sql.DB, tableTpl *internal.T

// set col info
c.Field = snaker.SnakeToCamel(c.ColumnName)
fmt.Printf(">>> %s -- not null: %t // %t\n", sqC.ColumnName, sqC.NotNull, c.IsNullable)
c.Len, c.NilType, c.Type = SqParseType(args, c.DataType, c.IsNullable)

// set primary key
Expand Down

0 comments on commit 83091ca

Please sign in to comment.