diff --git a/README.md b/README.md index 05bda4c0..3d488b61 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/examples/mysql/booktest.go b/examples/mysql/booktest.go index e676a109..176f1109 100644 --- a/examples/mysql/booktest.go +++ b/examples/mysql/booktest.go @@ -2,6 +2,7 @@ package main import ( "database/sql" + "flag" "fmt" "log" "time" @@ -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 diff --git a/examples/mysql/gen.sh b/examples/mysql/gen.sh index e89e6825..3ea58d08 100755 --- a/examples/mysql/gen.sh +++ b/examples/mysql/gen.sh @@ -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) @@ -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, @@ -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;' diff --git a/examples/postgres/booktest.go b/examples/postgres/booktest.go index 7a61ec9a..311a7fa9 100644 --- a/examples/postgres/booktest.go +++ b/examples/postgres/booktest.go @@ -2,6 +2,7 @@ package main import ( "database/sql" + "flag" "fmt" "log" "time" @@ -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 diff --git a/examples/postgres/gen.sh b/examples/postgres/gen.sh index a99bf5e4..08a15442 100755 --- a/examples/postgres/gen.sh +++ b/examples/postgres/gen.sh @@ -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) @@ -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, @@ -78,7 +80,7 @@ ENDSQL pushd $SRC &> /dev/null go build -./postgres +./postgres $EXTRA popd &> /dev/null diff --git a/examples/sqlite/booktest.go b/examples/sqlite/booktest.go index 332e286c..a23f0ae2 100644 --- a/examples/sqlite/booktest.go +++ b/examples/sqlite/booktest.go @@ -2,6 +2,7 @@ package main import ( "database/sql" + "flag" "fmt" "log" "time" @@ -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 diff --git a/examples/sqlite/gen.sh b/examples/sqlite/gen.sh index ab38b156..1c498ebe 100755 --- a/examples/sqlite/gen.sh +++ b/examples/sqlite/gen.sh @@ -2,6 +2,8 @@ DBNAME=booktest.sqlite3 +EXTRA=$1 + SRC=$(realpath $(cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )) DB=file:$SRC/$DBNAME @@ -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, @@ -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 diff --git a/examples/sqlite/models/authorbookresult.xo.go b/examples/sqlite/models/authorbookresult.xo.go index da67be05..620e123b 100644 --- a/examples/sqlite/models/authorbookresult.xo.go +++ b/examples/sqlite/models/authorbookresult.xo.go @@ -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) diff --git a/loaders/sqlite.go b/loaders/sqlite.go index 98eb3f40..5b19cfd8 100644 --- a/loaders/sqlite.go +++ b/loaders/sqlite.go @@ -3,7 +3,6 @@ package loaders import ( "database/sql" "errors" - "fmt" "net/url" "path" "sort" @@ -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