blib/lib/SQL/Shell/Manual.pm

filter: Statementstmt 100% 5 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 100% 6 / 6 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
CRAP 1.0
« lib/SQL/Shell.pm ↑ Summary
1 package SQL::Shell::Manual;
2  
3 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 $VERSION = sprintf"%d.%03d", q$Revision: 1.6 $ =~ /: (\d+)\.(\d+)/;
5  
6 1 1;
7  
8 =head1 NAME
9  
10 SQL::Shell::Manual - user guide for sql shell
11  
12 =head1 SYNOPSIS
13  
14 sqlsh -d DBI:Oracle:IFLDEV -u scott -p tiger
15  
16 =head1 DESCRIPTION
17  
18 This is a guide to using sqlsh. sqlsh is an interactive shell run from the command-line for workling with databases.
19 It can also be run in "batch mode" taking a list of commands from stdin (using the -i switch) or you can pass a single command to it on the command-line.
20  
21 =head2 Connecting
22  
23 Either set a DSN in the environment as DBI_DSN, supply with the -d option or use the connect command:
24  
25 unixbox% sqlsh
26 unixbox% sqlsh -d DBI:Oracle:IFLDEV -u scott -p tiger
27  
28 You can also connect from inside sqlsh:
29  
30 unixbox% sqlsh
31 > connect DBI:Oracle:IFLDEV scott tiger
32 DBI:Oracle:IFLDEV> show $dbh Name
33 +--------+
34 | Name |
35 +--------+
36 | IFLDEV |
37 +--------+
38  
39 and disconnect:
40  
41 DBI:Oracle:IFLDEV> disconnect
42 > show $dbh Name
43 Not connected to database.
44  
45 If you don't supply a password, sqlsh will prompt you:
46  
47 unixbox% sqlsh -d DBI:Oracle:IFLDEV -u scott
48 Enter password for scott:
49  
50 You can specify a blank password by passing -p:
51  
52 unixbox% sqlsh -d DBI:Oracle:IFLDEV -u guest -p
53  
54 From within sqlsh you can get a list of DBI drivers:
55  
56 unixbox% sqlsh
57 > show drivers
58
59 CSV
60 DBM
61 ExampleP
62 Excel
63 File
64 Multiplex
65 Oracle
66 Proxy
67 SQLite
68 Sponge
69 mysql
70
71 and a list of possible data sources for a driver:
72  
73 unixbox% sqlsh
74 > show datasources Oracle
75
76 dbi:Oracle:GISCPS
77 dbi:Oracle:IFL1
78 dbi:Oracle:IFLDEV
79 dbi:Oracle:IFLTEST
80  
81 Common DBI DSNs include:
82  
83 DBI:Oracle:<SID>
84 DBI:mysql:<DB>
85 DBI:ADO:<DSN>
86 DBI:Excel:file=<xls>
87 DBI:CSV:f_dir=<dir>
88 DBI:SQLite:dbname=<filename>
89  
90 =head2 Exploring the schema
91  
92 =head3 show tables
93  
94 This lists the tables in your database along with other attributes that may
95 be provided by your platform and driver:
96  
97 DBI:SQLite:dbname=test.db> show tables
98 +-----------+-------------+--------------------+--------------+---------+
99 | TABLE_CAT | TABLE_SCHEM | TABLE_NAME | TABLE_TYPE | REMARKS |
100 +-----------+-------------+--------------------+--------------+---------+
101 | NULL | main | sqlite_master | SYSTEM TABLE | NULL |
102 | NULL | temp | sqlite_temp_master | SYSTEM TABLE | NULL |
103 | NULL | main | commands | TABLE | NULL |
104 +-----------+-------------+--------------------+--------------+---------+
105  
106 For some database drivers this may include some system tables.
107  
108 =head3 show tablecounts
109  
110 This lists the tables with a rowcount for each:
111  
112 DBI:SQLite:dbname=test.db> show tablecounts
113 +-----------------------------+------+
114 | table | rows |
115 +-----------------------------+------+
116 | "main"."sqlite_master" | 2 |
117 | "temp"."sqlite_temp_master" | 0 |
118 | "main"."commands" | 12 |
119 +-----------------------------+------+
120  
121 For some database drivers this may include some system tables. This
122 command does a C<SELECT COUNT(*) FROM> on every table in your database. You
123 may not want to do this on databases with large numbers of tables, and/or
124 tables with large numbers of rows.
125  
126 =head3 show catalogs
127  
128 If your platform supports it, this shows a listing of available database
129 catalogs.
130  
131 DBI:ODBC:localdb> show catalogs
132 +----------------+
133 | TABLE_CAT |
134 +----------------+
135 | AdventureWorks |
136 | master |
137 | msdb |
138 | tempdb |
139 +----------------+
140  
141 =head3 show schemas
142  
143 This command will list the schemas available in your database. Note that
144 this is different from C<show schema> (singular), which shows table
145 descriptions for every table in your schema (see below).
146  
147 DBI:SQLite:dbname=test.db> show schemas
148 +-------------+
149 | TABLE_SCHEM |
150 +-------------+
151 | main |
152 | temp |
153 +-------------+
154  
155 =head3 show tabletypes
156  
157 List the available table-types in your database.
158  
159 DBI:SQLite:dbname=test.db> show tabletypes
160 +-----------------+
161 | TABLE_TYPE |
162 +-----------------+
163 | LOCAL TEMPORARY |
164 | SYSTEM TABLE |
165 | TABLE |
166 | VIEW |
167 +-----------------+
168
169 =head3 desc
170  
171 Lists the columns in a table:
172
173 DBI:Oracle:IFLDEV> desc commands
174 +-------------+----------------+------+
175 | Field | Type | Null |
176 +-------------+----------------+------+
177 | COMMAND | VARCHAR2(200) | YES |
178 | DESCRIPTION | VARCHAR2(1020) | YES |
179 +-------------+----------------+------+
180
181 =head3 show schema
182  
183 Lists the columns in a table, for each table in the schema:
184  
185 DBI:Oracle:IFLDEV> show schema
186
187 schema dump
188 COMMANDS:
189 +-------------+----------------+------+
190 | Field | Type | Null |
191 +-------------+----------------+------+
192 | COMMAND | VARCHAR2(200) | YES |
193 | DESCRIPTION | VARCHAR2(1020) | YES |
194 +-------------+----------------+------+
195  
196 =head2 Current shell settings (show settings)
197  
198 To list some C<sqlsh> internal settings:
199  
200 DBI:SQLite:dbname=test.db> show settings
201 +------------------+-------+
202 | PARAMETER | VALUE |
203 +------------------+-------+
204 | auto-commit | on |
205 | delimiter | \t |
206 | enter-whitespace | |
207 | escape | off |
208 | longreadlen | 512 |
209 | longtruncok | on |
210 | multiline | off |
211 | verbose | on |
212 | width | 80 |
213 +------------------+-------+
214  
215 Note that not all settings are yet included in this output.
216  
217 =head2 Querying the database
218  
219 DBI:SQLite:dbname=test.db> select * from commands
220 +------------------+--------------------------------------------------------------+
221 | command | desc |
222 +------------------+--------------------------------------------------------------+
223 | show drivers | Displays a list of DBI drivers |
224 | show datasources | Displays a list of available data sources for a driver |
225 | connect | Connects to a data source |
226 | disconnect | Disconnects from a data source |
227 | show tables | List the tables in the schema |
228 | show tablecounts | List the tables in the schema with a rowcount for each table |
229 | show schema | Lists the columns in each table in the schema |
230 | desc | List the columns in a table |
231 | set | Set a parameter |
232 | help | Displays sqlsh help in your $PAGER |
233 | reload | Reloads sqlsh |
234 | exit | Quits sqlsh |
235 +------------------+--------------------------------------------------------------+
236  
237 =head3 BLOB values
238  
239 You can control the amount of BLOB data fetched by setting the C<longreadlen> parameter.
240  
241
242 DBI:Oracle:IFLDEV> set longreadlen 4096
243 LongReadLen set to '4096'
244
245 DBI:Oracle:IFLDEV> show $dbh LongReadLen
246 +-------------+
247 | LongReadLen |
248 +-------------+
249 | 4096 |
250 +-------------+
251  
252 Note that the C<longtruncok> parameter should also be set (it is by default):
253
254 DBI:Oracle:IFLDEV> show $dbh LongTruncOk
255 +-------------+
256 | LongTruncOk |
257 +-------------+
258 | 1 |
259 +-------------+
260  
261  
262 =head3 Values containing non-word characters
263  
264 Suppose we have values in our database which contain whitespace characters (e.g. tabs):
265  
266 DBI:Oracle:IFLDEV> set enter-whitespace on
267 Whitespace may be entered as \n, \r and \t
268  
269 DBI:Oracle:IFLDEV> insert into commands(command,description) values('test', 'one\ttwo')
270 INSERT commands: 1 rows affected
271  
272 When we query the table we see these as literal values:
273  
274 DBI:Oracle:IFLDEV> select * from commands
275 +---------+-------------+
276 | COMMAND | DESCRIPTION |
277 +---------+-------------+
278 | test | one two |
279 +---------+-------------+
280  
281 We can instead chose to display them escaped:
282  
283 DBI:Oracle:IFLDEV> set escape show-whitespace
284 DBI:Oracle:IFLDEV> select * from commands
285 +---------+-------------+
286 | COMMAND | DESCRIPTION |
287 +---------+-------------+
288 | test | one\ttwo |
289 +---------+-------------+
290  
291 Alternatively we can use uri-escaping:
292  
293 DBI:Oracle:IFLDEV> set escape uri-escape on
294 DBI:Oracle:IFLDEV> select * from commands
295 +---------+-------------+
296 | COMMAND | DESCRIPTION |
297 +---------+-------------+
298 | test | one%09two |
299 +---------+-------------+
300  
301 =head3 Entering multi-line statements
302  
303 To enable multiline mode:
304  
305 DBI:Oracle:IFLDEV> set multiline on
306  
307 You can then build up statements over multiple lines, ending with a semicolon, e.g.:
308  
309 DBI:Oracle:IFLDEV> select
310 DBI:Oracle:IFLDEV> count(*)
311 DBI:Oracle:IFLDEV> from
312 DBI:Oracle:IFLDEV> commands
313 DBI:Oracle:IFLDEV> ;
314 +----------+
315 | COUNT(*) |
316 +----------+
317 | 12 |
318 +----------+
319  
320 To disable multiline mode, remember you need to end the statement in a semicolon:
321  
322 DBI:Oracle:IFLDEV> set multiline off;
323  
324 =head3 Altering the display mode
325  
326 The default (C<box>) display mode is similar to that used by the mysql client - it works well for tables of fairly short values.
327 The C<record> display mode is good for viewing single records:
328  
329 DBI:SQLite:dbname=test.db> set display-mode record
330 DBI:SQLite:dbname=test.db> select * from commands where command='desc'
331 --------------------------------------------------------------------------------
332 command | desc
333 desc | List the columns in a table
334 --------------------------------------------------------------------------------
335  
336 The C<spaced> display mode (despite sounding like a description of sqlsh's author) provides a minimum clutter view of the data.
337 The C<tabbed> display mode generally looks horrendous but is useful for a quick cut+paste of delimited values.
338 The C<sql> display mode generates insert statements using a $table placeholder for where the data is to be inserted.
339 The C<xml> display mode generates element-only XML which can be parsed into a list of hashes with XML::Simple.
340  
341 =head2 Altering the database
342  
343 By default transactions are not automatically committed so you must explicitly commit them:
344  
345 DBI:Oracle:IFLDEV> insert into commands(command, description) values ('dump','Writes a table or query results to a delimited file')
346 INSERT commands: 1 rows affected
347
348 DBI:Oracle:IFLDEV> commit
349  
350 and you can roll back mistakes:
351
352 DBI:Oracle:IFLDEV> delete from commands
353 DELETE commands: 11 rows affected
354
355 DBI:Oracle:IFLDEV> rollback
356 DBI:Oracle:IFLDEV> select count(*) from commands
357 +----------+
358 | COUNT(*) |
359 +----------+
360 | 12 |
361 +----------+
362  
363 If you prefer, you can switch autocommit on:
364
365 set autocommit on
366 insert ...
367 update ...
368  
369 This is the preferred mode of operation when connecting to some database
370 platforms like SQL Server. Depending on the platform, not all commands work
371 within a "transaction", and some platforms prefer that they be run with
372 autocommit on. Your mileage may vary.
373  
374 =head3 Clearing the database
375  
376 The C<wipe tables> command can be used to remove all the data each of the tables in the database:
377  
378 DBI:Oracle:IFLDEV> wipe tables
379 Wipe all data from:
380
381 COMMANDS
382
383 Are you sure you want to do this? (type 'yes' if you are) yes
384
385 Wiped all data in database
386  
387 It prompts you to confirm before anihilating your database.
388  
389 =head2 The send and recv commands
390  
391 These commands were added in v1.16. Their purpose is to give the user more
392 control over how commands are interpreted by the shell before they are sent
393 to the DB without the need to make the shell identify the commands to determine
394 whether to expect output or not. They are intended to deal with
395 platform-specific variations of SQL syntax that aren't covered by the generic
396 command-matching process in C<sqlsh>.
397  
398 C<send> is used when you don't expect output from the command, and C<recv> is
399 used for cases where the command provides output, and you'd like it
400 rendered and displayed, as if it had come from a C<select>.
401  
402 =head3 Example 1
403  
404 On IBM Netezza, you can query previously deleted records by giving the
405 database the following command:
406  
407 set show_deleted_records=TRUE
408  
409 If you try to do that from C<sqlsh>, it will tell you that the command is
410 not recognized, because C<sqlsh> has a built-in C<set> command, and it is
411 trying to match it to what you have typed at the prompt.
412  
413 The solution is to use the C<send> command to submit the C<set> expression to
414 the DB without having it intercepted by C<sqlsh>:
415  
416 send set show_deleted_records=TRUE
417  
418 =head3 Example 2
419  
420 On SQL Server there are a number of procedure calls that provide output. For
421 instance:
422  
423 exec xp_cmdshell 'dir *.exe'
424  
425 or simply
426  
427 xp_cmdshell 'dir *.exe'
428  
429 While C<sqlsh> supports the C<execute> command, this is intended to run
430 commands from a local SQL file. In this case, C<sqlsh> would just reply
431 that the command is not recognized. The solution is to use the C<recv>
432 command:
433  
434 recv exec xp_cmdshell 'dir *.exe'
435  
436 or
437 recv xp_cmdshell 'dir *.exe'
438  
439 This will make C<sqlsh> submit the exec expression to the DB as if it were
440 a C<select> command, so that any output is rendered and displayed.
441  
442 =head3 Example 3
443  
444 Several database platforms allow giving the C<use> command to query a
445 different database. You are in effect switching databases without
446 disconnecting. The command looks as follows:
447  
448 use MY_DB_NAME
449  
450 If I give this command while in C<sqlsh>, it will not be recognized,
451 however, I could pass it on to the DB by using C<send>:
452  
453 send use MY_DB_NAME
454  
455 In this case we type C<send> instead or C<recv> because we don't expect any
456 output from the C<use> command.
457  
458 Note that if your DB platform supports the C<use> command, you may also need
459 a command to tell you which database you're currently using. This is
460 platform-dependent, but I will provide an example from SQL Server:
461  
462 select dbname() as current_database
463
464 =head2 Dumping delimited data
465  
466 C<dump> can either be used to dump an entire table:
467  
468 dump mytable into export.txt
469
470 or the rowset resulting from a query:
471
472 dump select type, count(*) from mytable group by type into histogram.txt delimited by :
473  
474 An example:
475  
476 DBI:SQLite:dbname=test.db> dump commands into commands.csv delimited by ,
477 Dumping commands into commands.csv
478 Dumped 12 rows into commands.csv
479
480 DBI:SQLite:dbname=test.db> more commands.csv
481 command,desc
482 show drivers,Displays a list of DBI drivers
483 show datasources,Displays a list of available data sources for a driver
484 connect,Connects to a data source
485 disconnect,Disconnects from a data source
486 show tables,List the tables in the schema with a rowcount for each table
487 show schema,Lists the columns in each table in the schema
488 desc,List the columns in a table
489 set,Set a parameter
490 help,Displays sqlsh help in your $PAGER
491 reload,Reloads sqlsh
492 exit,Quits sqlsh
493  
494 You can also dump all the tables in a database into a directory:
495  
496 dump all tables into dumpdir/
497  
498 =head2 Logging
499  
500 You can chose to log commands:
501  
502 log commands logfile.txt
503  
504 or query results:
505
506 log queries dumpfile.txt
507  
508 or both:
509  
510 log all history.log
511  
512 =head2 Exporting data as XML
513  
514 DBI:Oracle:IFLDEV> set log-mode xml
515
516 DBI:Oracle:IFLDEV> log queries export.xml
517 Logging queries to export.xml
518
519 DBI:Oracle:IFLDEV>> select * from commands where command like 'show%'
520 +------------------+--------------------------------------------------------------+
521 | COMMAND | DESCRIPTION |
522 +------------------+--------------------------------------------------------------+
523 | show drivers | Displays a list of DBI drivers |
524 | show datasources | Displays a list of available data sources for a driver |
525 | show tables | List the tables in the schema |
526 | show tablecounts | List the tables in the schema with a rowcount for each table |
527 | show schema | Lists the columns in each table in the schema |
528 +------------------+--------------------------------------------------------------+
529  
530 DBI:Oracle:IFLDEV>> more export.xml
531 <rowset>
532 <record>
533 <COMMAND>show drivers</COMMAND>
534 <DESCRIPTION>Displays a list of DBI drivers</DESCRIPTION>
535 </record>
536 <record>
537 <COMMAND>show datasources</COMMAND>
538 <DESCRIPTION>Displays a list of available data sources for a driver</DESCRIPTION>
539 </record>
540 <record>
541 <COMMAND>show tables</COMMAND>
542 <DESCRIPTION>List the tables in the schema</DESCRIPTION>
543 </record>
544 <record>
545 <COMMAND>show tablecounts</COMMAND>
546 <DESCRIPTION>List the tables in the schema with a rowcount for each table</DESCRIPTION>
547 </record>
548 <record>
549 <COMMAND>show schema</COMMAND>
550 <DESCRIPTION>Lists the columns in each table in the schema</DESCRIPTION>
551 </record>
552 </rowset>
553
554 DBI:Oracle:IFLDEV>> no log
555 Stopped logging queries
556  
557 =head2 Exporting data as SQL
558  
559 DBI:Oracle:IFLDEV> set log-mode sql
560  
561 DBI:Oracle:IFLDEV> log queries export.sql
562 Logging queries to export.sql
563  
564 DBI:Oracle:IFLDEV>> select * from commands where command like 'show%'
565 +------------------+--------------------------------------------------------------+
566 | COMMAND | DESCRIPTION |
567 +------------------+--------------------------------------------------------------+
568 | show drivers | Displays a list of DBI drivers |
569 | show datasources | Displays a list of available data sources for a driver |
570 | show tables | List the tables in the schema |
571 | show tablecounts | List the tables in the schema with a rowcount for each table |
572 | show schema | Lists the columns in each table in the schema |
573 +------------------+--------------------------------------------------------------+
574  
575 DBI:Oracle:IFLDEV>> more export.sql
576 INSERT into $table (COMMAND,DESCRIPTION) VALUES ('show drivers','Displays a list of DBI drivers');
577 INSERT into $table (COMMAND,DESCRIPTION) VALUES ('show datasources','Displays a list of available data sources for a driver');
578 INSERT into $table (COMMAND,DESCRIPTION) VALUES ('show tables','List the tables in the schema');
579 INSERT into $table (COMMAND,DESCRIPTION) VALUES ('show tablecounts','List the tables in the schema with a rowcount for each table');
580 INSERT into $table (COMMAND,DESCRIPTION) VALUES ('show schema','Lists the columns in each table in the schema');
581
582 DBI:Oracle:IFLDEV>> no log
583 Stopped logging queries
584  
585 You can then replace $table with the table name you want the INSERT stataments to be issued against:
586
587 unixbox% perl -p -i -e 's/\$table/show_commands/' export.sql
588  
589 =head2 Loading data
590  
591 Loading a tab-delimited text file is simple:
592  
593 load export.txt into mytable
594  
595 Here's an example:
596  
597 DBI:SQLite:dbname=test.db> create table commands(command varchar(50), desc varchar(255))
598 CREATE table commands: 0 rows affected
599  
600 DBI:SQLite:dbname=test.db> load commands.tsv into commands
601 Loaded 12 rows into commands from commands.tsv
602  
603 As with C<dump> you can change the delimiter character:
604  
605 load export.csv into mytable delimited by ,
606
607 You can also specify character set translations:
608  
609 load export.txt into mytable from CP1252 to UTF-8
610  
611 if your database engine cannot do the character set conversions itself.
612 See L<Locale::Recode> for a list of character set names.
613  
614 =head2 Manipulating the command history
615  
616 You can dump out the history to a file:
617  
618 save history to history.txt
619
620 You can also load in a set of commands into the history:
621
622 load history from handy_queries.sql
623  
624 This can be useful in conjunction with C<log commands>.
625 You can clear the history at any time with:
626  
627 clear history
628  
629 and display it with:
630
631 show history
632
633 =head2 Running batches of commands
634  
635 You can execute a sequence of sqlsh commands from a file:
636  
637 > execute commands.sqlsh
638  
639 that might have been generated by C<save history> or C<log commands>.
640 You can also pipe commands into sqlsh on STDIN if you call it with the C<-i> switch:
641  
642 unixbox% sqlsh -d DBI:Oracle:IFLDEV -u scott -p tiger -i < commands.sqlsh
643  
644 =head1 VERSION
645  
646 v1.17
647  
648 =head1 AUTHOR
649  
650 John Alden
651 Miguel Gualdron
652  
653 =cut
654
« lib/SQL/Shell.pm ↑ Summary