Skip to content

Commit cbad25f

Browse files
committed
added docs for displaying query results
1 parent 6facfc0 commit cbad25f

File tree

2 files changed

+125
-0
lines changed

2 files changed

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

docs/source/statements/statements.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,6 @@ There is a special functionality to enable [USE keyspace](usekeyspace.md).
107107
schema-agreement
108108
lwt
109109
timeouts
110+
displaying-results
110111
timestamp-generators
111112
```

0 commit comments

Comments
 (0)