|
| 1 | +# Displaying Query Results |
| 2 | + |
| 3 | +This guide explains how to display query results from the database using the `RowsDisplayer` utility. The `RowsDisplayer` provides a flexible way to format and visualize query results as tables with various customization options, it tries to copy the behavior of the `cqlsh` utility. |
| 4 | + |
| 5 | +## Basic Usage |
| 6 | + |
| 7 | +To display query results, create a `RowsDisplayer` instance and configure its display settings: |
| 8 | + |
| 9 | +```rust |
| 10 | +let result: QueryRowsResult = session |
| 11 | + .query_unpaged("SELECT * FROM examples_ks.basic1", &[]) |
| 12 | + .await? |
| 13 | + .into_rows_result()?; |
| 14 | + |
| 15 | + let displayer = result.rows_displayer(); |
| 16 | + println!("{}", displayer); |
| 17 | +``` |
| 18 | + |
| 19 | +## Display Settings |
| 20 | + |
| 21 | +### Terminal Width |
| 22 | + |
| 23 | +Control the width of the output table: |
| 24 | + |
| 25 | +```rust |
| 26 | +displayer.set_terminal_width(80); |
| 27 | +``` |
| 28 | + |
| 29 | +- Setting width to 0 (default) disables wrapping |
| 30 | +- Table will attempt to wrap at specified width while maintaining readability |
| 31 | +- Columns are adjusted proportionally when wrapping |
| 32 | + |
| 33 | +### Color Output |
| 34 | + |
| 35 | +Enable or disable colored output: |
| 36 | + |
| 37 | +```rust |
| 38 | +displayer.use_color(true); // Enable colors (default) |
| 39 | +displayer.use_color(false); // Disable colors |
| 40 | +``` |
| 41 | + |
| 42 | +When enabled, different data types are displayed in distinct colors: |
| 43 | +- Numbers (integers, decimals, floats): Green |
| 44 | +- Text and strings: Yellow |
| 45 | +- Collections (lists, sets, maps): Blue |
| 46 | +- Errors: Red |
| 47 | +- Binary data: Magenta |
| 48 | + |
| 49 | +### Binary Data Display |
| 50 | + |
| 51 | +Configure how BLOB data is displayed using `ByteDisplaying`: |
| 52 | + |
| 53 | +```rust |
| 54 | +displayer.set_blob_displaying(ByteDisplaying::Hex); // Default |
| 55 | +displayer.set_blob_displaying(ByteDisplaying::Ascii); |
| 56 | +displayer.set_blob_displaying(ByteDisplaying::Dec); |
| 57 | +``` |
| 58 | + |
| 59 | +Options: |
| 60 | +- `Hex`: Display as hexadecimal values (e.g., "0A FF 42") |
| 61 | +- `Ascii`: Display as ASCII characters where possible |
| 62 | +- `Dec`: Display as decimal values (e.g., "213 7 66") |
| 63 | + |
| 64 | +### Number Formatting |
| 65 | + |
| 66 | +#### Integer Display |
| 67 | + |
| 68 | +Control scientific notation for integers: |
| 69 | + |
| 70 | +```rust |
| 71 | +displayer.set_exponent_displaying_integers(true); // Enable scientific notation |
| 72 | +displayer.set_exponent_displaying_integers(false); // Disable (default) |
| 73 | +``` |
| 74 | + |
| 75 | +#### Floating Point Precision |
| 76 | + |
| 77 | +Set the number of decimal places for floating point numbers: |
| 78 | + |
| 79 | +```rust |
| 80 | +displayer.set_floating_point_precision(6); // Show 6 decimal places (default) |
| 81 | +``` |
| 82 | + |
| 83 | +## Example Output |
| 84 | + |
| 85 | +Here's an example of how the output might look with default settings: |
| 86 | + |
| 87 | +``` |
| 88 | ++----------+-------------+----------------+-------------+ |
| 89 | +| id | name | values | created_at | |
| 90 | ++----------+-------------+----------------+-------------+ |
| 91 | +| 1 | Example | [1, 2, 3] | 2024-01-06 | |
| 92 | +| 2 | Test Data | [4, 5, 6] | 2024-01-06 | |
| 93 | ++----------+-------------+----------------+-------------+ |
| 94 | +``` |
| 95 | + |
| 96 | +## Best Practices |
| 97 | + |
| 98 | +1. **Terminal Width** |
| 99 | + - Set appropriate terminal width for better readability |
| 100 | + - Consider using terminal width detection if available |
| 101 | + - Use 0 width for untruncated output |
| 102 | + |
| 103 | +2. **Color Usage** |
| 104 | + - Enable colors for better type distinction |
| 105 | + - Disable colors when outputting to files or non-terminal destinations |
| 106 | + - Consider user accessibility settings |
| 107 | + |
| 108 | +3. **Binary Data** |
| 109 | + - Choose appropriate blob display format based on data content |
| 110 | + - Use Hex for general binary data |
| 111 | + - Use ASCII when data is known to be text |
| 112 | + - Use Dec for byte-oriented analysis |
| 113 | + |
| 114 | +4. **Number Formatting** |
| 115 | + - Adjust floating point precision based on data requirements |
| 116 | + - Enable scientific notation for very large/small numbers |
| 117 | + - Consider locale-specific formatting needs |
| 118 | + |
| 119 | +## Implementation Details |
| 120 | + |
| 121 | +The displayer uses the following traits internally: |
| 122 | +- `Display` for converting values to strings |
| 123 | +- Custom formatting traits for specific types |
| 124 | + |
| 125 | +Output is generated using Rust's formatting system (`fmt::Display`), ensuring efficient memory usage and proper error handling. |
0 commit comments