Mercurial > sqlpython
annotate docs/source/capabilities.rst @ 532:506156a9915e tip
add history file support
author | zhangmingyuan240@gmail.com |
---|---|
date | Mon, 18 Jun 2012 16:06:42 +0800 |
parents | 539c11ad1b7e |
children |
rev | line source |
---|---|
281 | 1 ============================== |
247 | 2 SQLPython's extra capabilities |
3 ============================== | |
4 | |
444 | 5 SQLPython's primary function is to allow entry of SQL queries, much like |
6 SQL*Plus, psql, and the mysql command-line client. It aims to reproduce | |
7 as many SQL*Plus capabilities as possible. In addition, it offers several | |
8 extra features inspired by other command-line clients. | |
247 | 9 |
279 | 10 Neatened output |
11 =============== | |
12 | |
13 When printing query results, sqlpython economizes on screen space by allocating | |
14 only the width each column actually needs. | |
15 | |
16 Smart prompt | |
17 ============ | |
18 | |
19 sqlpython automatically uses `username`@`instance`> as its prompt, helping | |
20 avoid wrong-instance and wrong-user errors. | |
21 | |
22 Tab completion | |
23 ============== | |
24 | |
25 When typing SQL commands, hitting `<TAB>` after entering part of an object | |
26 or column name brings up a list of appropriate possibilities or, if there | |
27 is only one possibility, fills in the rest of the name. This feature is | |
28 not yet very reliable, but can save typing. | |
29 | |
30 Scripting | |
31 ========= | |
32 | |
33 Like SQL\*Plus, sqlpython can run scripts (text files with series of SQL and | |
34 sqlpython commands) with `@/path/to/script.sql` or (for online scripts) | |
35 `@http://scripthost/scriptlibrary/script.sql`. | |
36 | |
37 History | |
38 ======= | |
39 | |
456 | 40 If used on a \*nix machine with ``readline`` installed, then ``bash``-like access |
445
b34ea206c7d1
debug bind var dependence on arg.parsed.expanded
catherine@Drou
parents:
444
diff
changeset
|
41 to the command history is available. |
b34ea206c7d1
debug bind var dependence on arg.parsed.expanded
catherine@Drou
parents:
444
diff
changeset
|
42 |
279 | 43 The up- and down-arrow keys allow you to scroll through the lines entered so far |
44 in your sqlpython session. | |
45 | |
46 Commands are also entered into a command history. | |
47 | |
286 | 48 history *or* hi |
49 List entire command history | |
279 | 50 |
286 | 51 list *or* li |
52 List only last command | |
279 | 53 |
286 | 54 hi `<N>` |
55 List command number <N> from history. | |
279 | 56 |
286 | 57 hi `<N>-`, hi `-<N>` |
58 List commands from <N> onward, or up to <N> | |
279 | 59 |
286 | 60 hi `<str>` |
61 Lists commands that include the string <str> | |
279 | 62 |
286 | 63 hi `/<regex>/` |
64 Lists commands that match the regular expression <regex> | |
279 | 65 |
286 | 66 run, r, *or* `\\g` |
67 Run the most recent command again | |
279 | 68 |
286 | 69 run `<N>` |
70 Run command <N> | |
279 | 71 |
286 | 72 run `<str>`, run `/<regex>/` |
73 Run command matching <str> or <regex> (as for `history`) - | |
74 if multiple items would match, run most recent | |
279 | 75 |
281 | 76 Special I/O destinations |
77 ======================== | |
78 | |
79 Much as in a UNIX shell, you can follow a command with a special output destination. | |
80 | |
81 `> {filename}` sends the output to a file. This is more convenient than SQL\*Plus's | |
82 SPOOL {filename}... SPOOL OFF (though you can use those as well). | |
83 | |
84 `>` alone (no filename) sends the output to the paste buffer. | |
85 | |
86 `|` pipes the output to an operating-system command. | |
87 | |
88 When `< {filename}` is included in your command, it is replaced with the contents of | |
89 {filename} before the command is run. | |
90 | |
91 Examples:: | |
92 | |
283 | 93 Need examples!!!! |
94 | |
281 | 95 Special output formats |
96 ====================== | |
97 | |
98 By replacing the `;` that terminates a SELECT statement with a backslash-character | |
99 sequence, you can get output in a number of useful formats. The `terminators` | |
283 | 100 command lists them, for your convenience. |
281 | 101 |
283 | 102 ========== ======================== ================================ |
103 terminator format Useful for | |
104 ========== ======================== ================================ | |
105 ; standard Oracle format | |
106 \\c CSV (with headings) sending to spreadsheets | |
107 \\C CSV (no headings) | |
108 \\g list wide output with linewraps | |
109 \\G aligned list | |
110 \\h HTML table web reports | |
111 \\i INSERT statements copying to other instances | |
112 \\j JSON | |
329
3efffbf7481f
fixed bug in assigning 0, null to bind vars
Catherine Devlin <catherine.devlin@gmail.com>
parents:
294
diff
changeset
|
113 \\r ReStructured Text inclusion in documentation |
283 | 114 \\s CSV (with headings) |
115 \\S CSV (no headings) | |
329
3efffbf7481f
fixed bug in assigning 0, null to bind vars
Catherine Devlin <catherine.devlin@gmail.com>
parents:
294
diff
changeset
|
116 \\t transposed "wide" tables like v$database |
283 | 117 \\x XML |
118 \\l line plot, with markers | |
119 \\L scatter plot (no lines) | |
120 \\b bar graph | |
121 \\p pie chart | |
122 ========== ======================== ================================ | |
281 | 123 |
124 Most of these output formats are even more useful when combined with special output | |
329
3efffbf7481f
fixed bug in assigning 0, null to bind vars
Catherine Devlin <catherine.devlin@gmail.com>
parents:
294
diff
changeset
|
125 destinations. For example, `SELECT * FROM party\\h > /var/www/party_report.html` |
281 | 126 could create an HTML report in the webserver's documents directory, ready to serve. |
127 | |
247 | 128 UNIX-like commands |
129 ================== | |
130 | |
279 | 131 Many sqlpython commands allow you to act as though the database objects |
132 were files in a UNIX filesystem. Many of the commands also accept flags | |
133 to modify their behavior. | |
134 | |
286 | 135 ls `{object type/object name, with wildcards}` |
281 | 136 Lists objects from the data dictionaries, as though they were in a |
137 *object_type*/*object_name* directory structure. Thus, `ls view/\*` | |
138 lists all the user's views. Calling with no argument is equivalent | |
139 to `ls *`. | |
140 | |
141 Options:: | |
142 | |
143 -l, --long long descriptions | |
144 -a, --all all schemas' objects (otherwise, you only get your own) | |
145 -t, --timesort Sort by last_ddl_time | |
146 -r, --reverse Reverse order while sorting | |
247 | 147 |
281 | 148 `ls -lt *;10` lists the ten items with the most recent last_ddl_time; |
149 this can be a good way to answer the question, "What was I working on?" | |
150 | |
286 | 151 cat `{remainder of query}` |
152 Shorthand for "SELECT * FROM". Can be combined with anything else | |
153 that fits into a SELECT statement (WHERE, ORDER BY, etc.) | |
281 | 154 |
286 | 155 grep `{target}` `{table}` `[{table2,...}]` |
156 Equivalent to SELECT * FROM {table} WHERE *any column* LIKE '%{target}%'. | |
157 Useful when you don't know, don't remember, or don't care which column | |
158 a value may be found in. | |
281 | 159 |
160 Options:: | |
161 | |
162 -i, --ignore-case Case-insensitive search | |
247 | 163 |
281 | 164 find -c {target}, find -t {column} |
165 Lists all tables or columns whose names contain {target}. More convenient than | |
166 querying user_tab_columns/all_tab_columns or user_tables/all_tables. | |
167 Options:: | |
283 | 168 |
281 | 169 -a Find all objects (not just my own) |
170 | |
286 | 171 Data dictionary exploration |
172 =========================== | |
173 | |
174 refs `{table_name}` | |
175 Lists all foreign key constraints on the table or referring to the table. | |
176 | |
177 deps `{object_name}` | |
178 Lists all objects dependent upon the named object. | |
179 | |
180 comments `{table_name}` | |
181 Prints comments on a table and its columns. | |
182 | |
183 PL/SQL source code | |
184 ================== | |
185 | |
186 pull {object_name} | |
187 Displays the PL/SQL source code for {object_name}. | |
188 | |
189 Options: | |
190 -d, --dump dump results to files (object_type/object_name.sql) | |
191 -f, --full get dependent objects as well | |
192 -a, --all all schemas' objects | |
193 | |
194 bzr, git, hg `{object_name}` | |
195 Dump source code to files, as `pull -f`, but also creates or commits to a | |
196 repository of the appropriate distributed version control system | |
197 (Bazaar, Git, or Mercurial, respectively). | |
198 | |
199 find `{target}` | |
200 Lists all PL/SQL objects whose source code contains the {target} string. | |
201 Always case-insensitive. | |
202 Options:: | |
203 | |
204 -a Search all PL/SQL objects (not just my own) | |
205 | |
247 | 206 PostgreSQL-like shortcuts |
207 ========================= | |
208 | |
283 | 209 psql, the command-line client for the open-source database `PostgreSQL <http://www.postgresql.org/>`_ uses a number |
210 of backslash-character sequences as convenient shortcuts. sqlpython steals many of | |
211 them. | |
212 | |
213 ===== =================== | |
247 | 214 \\c connect |
215 \\d desc | |
216 \\e edit | |
217 \\g run | |
218 \\h help | |
219 \\i load | |
220 \\o spool | |
221 \\p list | |
222 \\q quit | |
223 \\w save | |
224 \\db _dir_tablespaces | |
225 \\dd comments | |
226 \\dn _dir_schemas | |
227 \\dt _dir_tables | |
228 \\dv _dir_views | |
229 \\di _dir_indexes | |
230 \\? help psql | |
283 | 231 ===== =================== |
281 | 232 |
233 Bind variables | |
234 ============== | |
235 | |
236 Bind variables work in sqlpython as they do in SQL\*Plus, but they are set dynamically; there | |
237 is no need to declare them before use. The syntax for setting them is more permissive than | |
238 in SQL\*Plus; all these are recognized:: | |
239 | |
240 exec :mybind := 'value' | |
241 exec :mybind = 'value' | |
242 :mybind := 'value' | |
243 :mybind = 'value' | |
244 | |
245 The current values of all bind variables can be viewed with the `print` command. | |
246 | |
247 The `bind` command creates and populates bind variables for the final row of the most recent | |
248 SELECT statement executed; each column name is used as a bind variable, which is filled with | |
249 the value. `bind -r {rownumber}` does the same, but fills from row {rownumber} instead of | |
250 from the final row (row numbers begin at 0 for this command). | |
251 | |
252 When the `autobind` sqlpython parameter is True, a `bind` statement is issued automatically | |
253 after every query that returns exactly one row. | |
254 | |
445
b34ea206c7d1
debug bind var dependence on arg.parsed.expanded
catherine@Drou
parents:
444
diff
changeset
|
255 Once bind variables are defined, they can be used in SQL statements. The syntax |
b34ea206c7d1
debug bind var dependence on arg.parsed.expanded
catherine@Drou
parents:
444
diff
changeset
|
256 is dependnent on which RDBMS is being queried. |
b34ea206c7d1
debug bind var dependence on arg.parsed.expanded
catherine@Drou
parents:
444
diff
changeset
|
257 |
b34ea206c7d1
debug bind var dependence on arg.parsed.expanded
catherine@Drou
parents:
444
diff
changeset
|
258 ---------- ------------------------------------------ |
b34ea206c7d1
debug bind var dependence on arg.parsed.expanded
catherine@Drou
parents:
444
diff
changeset
|
259 RDBMS bind variable example |
b34ea206c7d1
debug bind var dependence on arg.parsed.expanded
catherine@Drou
parents:
444
diff
changeset
|
260 ---------- ------------------------------------------ |
b34ea206c7d1
debug bind var dependence on arg.parsed.expanded
catherine@Drou
parents:
444
diff
changeset
|
261 Oracle SELECT * FROM party WHERE name = :name; |
b34ea206c7d1
debug bind var dependence on arg.parsed.expanded
catherine@Drou
parents:
444
diff
changeset
|
262 postgreSQL SELECT * FROM party WHERE name = %(name)s; |
b34ea206c7d1
debug bind var dependence on arg.parsed.expanded
catherine@Drou
parents:
444
diff
changeset
|
263 MySQL SELECT * FROM party WHERE name = ; |
b34ea206c7d1
debug bind var dependence on arg.parsed.expanded
catherine@Drou
parents:
444
diff
changeset
|
264 ---------- ------------------------------------------ |
b34ea206c7d1
debug bind var dependence on arg.parsed.expanded
catherine@Drou
parents:
444
diff
changeset
|
265 |
281 | 266 Bind variables are available from within Python as a dictionary named `binds` (see Python). |
267 | |
268 Substitution variables | |
269 ====================== | |
270 | |
271 Substitution variables ("&" variables) work much as they do in SQL\*Plus. As in SQL\*Plus, | |
272 the `scan` parameter determines whether queries are scanned to replace substitution | |
273 variables. Unlike SQL\*Plus, sqlpython knows how annoying it is to hit a substitution | |
274 variable you didn't expect, so entering "SET SCAN OFF" when prompted for a substitution | |
275 variable actually aborts the substitution process. | |
276 | |
247 | 277 Wild SQL |
278 ======== | |
279 | |
280 Wild SQL is a nonstandard SQL feature that must be enabled with `set wildsql on`. When it is | |
282 | 281 enabled, column names in a SELECT statement do not need to be explicitly typed; they can be |
282 specified with special Wild SQL symbols: wildcards (`*`, `%`, `_`); column numbers (`#{N}`); | |
283 and NOT-style exclusion (`!`). The symbols can even be combined. | |
247 | 284 |
282 | 285 :: |
247 | 286 |
282 | 287 jrrt@orcl> cat party |
288 | |
289 NAME STR INT WIS DEX CON CHA | |
290 ------- --- --- --- --- --- --- | |
291 Frodo 8 14 16 15 14 16 | |
292 Gimli 17 12 10 11 17 11 | |
293 Legolas 13 15 14 18 15 17 | |
294 Sam 11 9 14 11 16 13 | |
295 | |
296 4 rows selected. | |
297 | |
298 jrrt@orcl> set wild on | |
299 wildsql - was: False | |
300 now: True | |
301 jrrt@orcl> select *i* from party; | |
302 | |
303 INT WIS | |
304 --- --- | |
305 14 16 | |
306 12 10 | |
307 15 14 | |
308 9 14 | |
309 | |
310 4 rows selected. | |
311 | |
312 jrrt@orcl> select #1, #5 from party; | |
313 | |
314 NAME DEX | |
315 ------- --- | |
316 Frodo 15 | |
317 Gimli 11 | |
318 Legolas 18 | |
319 Sam 11 | |
320 | |
321 4 rows selected. | |
322 | |
323 jrrt@orcl> select !str from party; | |
324 | |
325 NAME INT WIS DEX CON CHA | |
326 ------- --- --- --- --- --- | |
327 Frodo 14 16 15 14 16 | |
328 Gimli 12 10 11 17 11 | |
329 Legolas 15 14 18 15 17 | |
330 Sam 9 14 11 16 13 | |
331 | |
332 4 rows selected. | |
333 | |
334 jrrt@orcl> select n*, !#3, !c* from party; | |
335 | |
336 NAME STR WIS DEX | |
337 ------- --- --- --- | |
338 Frodo 8 16 15 | |
339 Gimli 17 10 11 | |
340 Legolas 13 14 18 | |
341 Sam 11 14 11 | |
342 | |
343 4 rows selected. | |
247 | 344 |
282 | 345 Wild SQL symbols only work in the first SELECT statement in a query; they do not work in |
346 subqueries, subsequent UNIONed queries, etc. | |
347 | |
348 Python | |
349 ====== | |
350 | |
351 The `py` command allows the user to execute Python commands, either one-at-a-time (with | |
352 `py {command}`) or in an interactive environment (beginning with a bare `py` statement, | |
294 | 353 and continuing until Ctrl-D, `quit()`, or `exit()` is entered). |
282 | 354 |
355 A history of result sets from each query is exposed to the python session as the list `r`; | |
294 | 356 the most recent result set is `r[-1]`. Each row can be references as a tuple, or as an |
357 object with an attribute for each column. | |
358 | |
359 Bind variables are exposed as the dictionary `binds`. Each row from each result set has | |
360 a .bind() method that fills a bind varible for each column with that row's value. | |
282 | 361 |
362 Resultsets in `r` are read-only, but `binds` can be written as well as read, and will | |
363 be working bind variables in the SQL environment. | |
364 | |
294 | 365 SQL and sqlpython commands can be issued from the Python environment with `sql("{your SQL}")`. |
366 | |
367 All variables are retained each time the python environment is entered (whether interactively, | |
368 or with one-line `py` statements). | |
282 | 369 :: |
247 | 370 |
294 | 371 0:testschema@orcl> select title, author from play; |
282 | 372 |
373 TITLE AUTHOR | |
374 --------------- ----------- | |
375 Timon of Athens Shakespeare | |
376 Twelfth Night Shakespeare | |
377 The Tempest Shakespeare | |
378 Agamemnon Aeschylus | |
379 | |
380 4 rows selected. | |
381 | |
294 | 382 0:testschema@orcl> py import urllib |
383 0:testschema@orcl> py current_season = urllib.urlopen('http://cincyshakes.com/').read() | |
384 0:testschema@orcl> py | |
385 Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) | |
386 [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 | |
387 Type "help", "copyright", "credits" or "license" for more information. | |
388 (mysqlpy) | |
389 | |
390 py <command>: Executes a Python command. | |
391 py: Enters interactive Python mode; end with `Ctrl-D`, `quit()`, or 'exit`. | |
392 Past SELECT results are exposed as list `r`; | |
393 most recent resultset is `r[-1]`. | |
394 SQL bind, substitution variables are exposed as `binds`, `substs`. | |
395 SQL and sqlpython commands can be issued with sql("your non-python command here"). | |
396 | |
282 | 397 >>> r[-1] |
398 [('Timon of Athens', 'Shakespeare'), ('Twelfth Night', 'Shakespeare'), ('The Tempest', 'Shakespeare'), ('Agamemnon', 'Aeschylus')] | |
294 | 399 >>> r[-1][0][0] |
400 'Timon of Athens' | |
282 | 401 >>> for row in r[-1]: |
294 | 402 ... print "%s, by %s" % (row.title, row.author) |
403 ... | |
404 Timon of Athens, by Shakespeare | |
405 Twelfth Night, by Shakespeare | |
406 The Tempest, by Shakespeare | |
407 Agamemnon, by Aeschylus | |
282 | 408 >>> [row.title for row in r[-1] if row.title in current_season] |
409 ['Timon of Athens', 'Twelfth Night'] | |
294 | 410 >>> binds['author'] = 'Shakespeare' |
411 >>> query = "SELECT title FROM play WHERE author = :author" | |
412 >>> sql(query) | |
413 | |
414 TITLE | |
415 --------------- | |
416 Timon of Athens | |
417 Twelfth Night | |
418 The Tempest | |
419 | |
420 3 rows selected. | |
421 | |
422 >>> r[-1] | |
423 [('Timon of Athens',), ('Twelfth Night',), ('The Tempest',)] | |
424 >>> r[-1][0] | |
425 ('Timon of Athens',) | |
426 >>> r[-1][0].bind() | |
427 >>> binds['title'] | |
428 'Timon of Athens' | |
429 >>> quit() | |
430 0:testschema@orcl> select title, author from play where title = :title; | |
282 | 431 |
432 TITLE AUTHOR | |
433 --------------- ----------- | |
434 Timon of Athens Shakespeare | |
435 | |
436 1 row selected. | |
294 | 437 |
281 | 438 Parameters |
439 ========== | |
440 | |
441 Several parameters control the behavior of sqlpython itself. | |
442 | |
283 | 443 ===================== =================================================== =============== |
444 parameter effect default | |
445 ===================== =================================================== =============== | |
446 autobind When True, single-row queries automatically `bind` False | |
447 commit_on_exit Automatically commits work at end of session True | |
448 continuation_prompt Prompt for second line and onward of long statement > | |
449 default_file_name The file opened by `edit`, if not specified afiedt.buf | |
450 echo Echo command entered before executing False | |
451 editor Text editor invoked by `edit`. varies | |
294 | 452 heading Print column names along with results True |
283 | 453 maxfetch Maximum number of rows to return from any query 1000 |
454 maxtselctrows Maximum # of rows from a tselect or \\n query 10 | |
455 prompt Probably unwise to change user@instance> | |
456 scan Interpret & as indicating substitution variables True | |
457 serveroutput Print DBMS_OUTPUT.PUT_LINE results True | |
458 sql_echo Print text of "behind-the-scenes" queries False | |
459 timeout In seconds 30 | |
460 timing Print time for each command to execute False | |
456 | 461 wildsql Accept \*, %, #, and ! in column names False |
283 | 462 ===================== =================================================== =============== |
281 | 463 |
464 The user can change these with the `set {paramname} {new-value}` statement. | |
283 | 465 The True/False parameters accept new values permissively, recognizing "True", "False", |
466 "T", "F", "yes", "no", "on", "off", etc. | |
281 | 467 |
283 | 468 `set` and `show` both list the current values of the sqlpython parameters. They |
469 also recognize any abbreviated parameter name, so long as it is long enough to be | |
470 unique. That is, `show maxf` is recognized as `show maxfetch`, but `show max` is | |
471 too short to distinguish between `maxfetch` and `maxtselctrows`. | |
472 | |
281 | 473 `show parameter {param}` shows current Oracle parameters (from v$parameter), as it does |
474 in SQL\*Plus. | |
475 | |
476 Tuning | |
477 ====== | |
478 | |
479 In sqlpython, `explain {SQL ID}` shows the execution plan for the SQL statement with the | |
480 given ID. If SQL ID is omitted, it defaults to the most recent SQL executed. | |
481 (This is not necessarily the last statement `EXPLAIN PLAN` was issued against.) | |
482 | |
483 Other specialized sqlpython tuning commands include: | |
484 | |
485 load | |
486 Displays OS load on cluster nodes (10gRAC) | |
487 | |
488 longops | |
489 Displays long-running operations | |
490 | |
491 sessinfo | |
492 Reports session info for the given sid, extended to RAC with gv$ | |
493 | |
494 top, top9i | |
495 Displays active sessions | |
496 | |
373 | 497 BLOB display |
498 ============ | |
499 | |
500 (Oracle only, for now) | |
501 | |
502 When a SELECT query returns BLOB columns, most SQL tools simply cannot | |
503 display the results. Sqlpython, however, will create | |
504 a local file for each BLOB returned (up to the parameter `bloblimit`), | |
505 and return the filepaths of the new files in the query results. In a | |
506 tool like the GNOME terminal, these filepaths work as right-clickable | |
507 links that can open the files. | |
508 | |
509 When the \\h terminator is used to generate HTML table output, if the | |
510 BLOBs are images, they will be embedded as images in the generated | |
511 table. | |
281 | 512 |
513 | |
514 | |
515 |