@@ -42,7 +42,12 @@ dsn_url
4242: (string) A data source name in URL form where the "protocol" element
4343identifies the database resource being accessed (e.g. "sqlite://",
4444"mysql://", "postgres://"). A data source name are rescribed
45- at <https://github.com/golang/go/wiki/SQLInterface>.
45+ at <https://go.dev/wiki/SQLInterface>. For the specificly supported
46+ datatabase connection strings see
47+ <https://pkg.go.dev/github.com/glebarez/go-sqlite>,
48+ <https://pkg.go.dev/github.com/go-sql-driver/mysql#readme-dsn-data-source-name>
49+ and <https://pkg.go.dev/github.com/lib/pq>
50+
4651
4752header_row
4853: (boolean) if true print a header row in the output, false for no
@@ -112,20 +117,36 @@ configuration file
112117: Force the line ending per row to carage return and
113118line feed if true, false use line feed
114119
115- # EXAMPLE
120+ -sql FILENAME
121+ : Read sql statement from a file instead of the command line.
122+
123+ # EXAMPLES
116124
117125Using the "dbcfg.json" configuration file, display ten rows
118126from table "mytable" in database indicated in "dbcfg.json".
119127
128+ ~~~sql
120129 {app_name} dbcfg.json 'SELECT * FROM mytable LIMIT 10'
130+ ~~~
121131
122132The CSV output is written standard out and can be redirected into
123133a file if desired.
124134
135+ ~~~shell
125136 {app_name} dbcfg.json 'SELECT * FROM mytable LIMIT 10' \
126137 >ten-rows.csv
138+ ~~~
139+
140+ Read SQL from a file and connect to Postgres without SSL you
141+ can pass the ` + "`" + `-sql` + "`" + ` and ` + "`" + `-dsn` + "`" + ` options.
142+
143+ ~~~shell
144+ {app_name} \
145+ -dsn "postgres://${USER}@/${DB_NAME}?sslmode=disable" \
146+ -sql query.sql \
147+ >my_data.csv
148+ ~~~
127149
128- {app_name} {version}
129150`
130151)
131152
@@ -148,6 +169,7 @@ func main() {
148169 writeHeaderRow , useCRLF := sqlCfg .WriteHeaderRow , sqlCfg .UseCRLF
149170 dsn , delimiter := "" , ""
150171 fName , stmt := "" , ""
172+ sqlFName := ""
151173
152174 // Handle options
153175 flag .BoolVar (& showHelp , "help" , showHelp , "display help" )
@@ -157,6 +179,7 @@ func main() {
157179 flag .BoolVar (& useCRLF , "use-crlf" , useCRLF , "delimited rows with a carriage return and line feed" )
158180 flag .StringVar (& dsn , "dsn" , dsn , "connect using the data source name provided in URL form" )
159181 flag .StringVar (& delimiter , "delimiter" , "" , "set the delimiter, defaults to comma" )
182+ flag .StringVar (& sqlFName , "sql" , "" , "read the SQL statement from a file, '-' will cause a read from standard input" )
160183 flag .Parse ()
161184 args := flag .Args ()
162185
@@ -184,8 +207,20 @@ func main() {
184207 case 2 :
185208 fName , stmt = args [0 ], args [1 ]
186209 default :
187- fmt .Fprintf (eout , "missing configuration and SQL query statement" )
188- os .Exit (1 )
210+ if sqlFName == "" {
211+ fmt .Fprintf (eout , "missing configuration and SQL query statement" )
212+ os .Exit (1 )
213+ }
214+ }
215+
216+ // Read SQL from file if specified.
217+ if sqlFName != "" {
218+ src , err := os .ReadFile (sqlFName )
219+ if err != nil {
220+ fmt .Fprintf (eout , "%s\n " , err )
221+ os .Exit (1 )
222+ }
223+ stmt = fmt .Sprintf ("%s" , src )
189224 }
190225
191226 // Load configuration if provided
0 commit comments