Skip to content

Commit

Permalink
Improve the implementation of select into
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Aug 17, 2024
1 parent 23f322c commit 77e5751
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
31 changes: 18 additions & 13 deletions crates/gitql-engine/src/engine_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,21 +573,26 @@ fn execute_into_statement(
) -> Result<(), String> {
let mut buffer = String::new();

let line_terminated_by = &statement.lines_terminated;
let field_termianted_by = &statement.fields_terminated;
let enclosing = &statement.enclosed;

// Headers
let header = gitql_object.titles.join(&statement.fields_terminated);
let header = gitql_object.titles.join(field_termianted_by);
buffer.push_str(&header);
buffer.push_str(&statement.lines_terminated);

// Rows
let rows = &gitql_object.groups[0].rows;
for row in rows {
let row_values: Vec<String> = row
.values
.iter()
.map(|r| value_to_string_with_optional_enclosing(r, &statement.enclosed))
.collect();
buffer.push_str(&row_values.join(&statement.fields_terminated));
buffer.push_str(&statement.lines_terminated);
buffer.push_str(line_terminated_by);

// Rows of the main group
if let Some(main_group) = gitql_object.groups.first() {
for row in &main_group.rows {
let row_values: Vec<String> = row
.values
.iter()
.map(|r| value_to_string_with_optional_enclosing(r, enclosing))
.collect();
buffer.push_str(&row_values.join(field_termianted_by));
buffer.push_str(line_terminated_by);
}
}

let file_result = std::fs::File::create(statement.file_path.clone());
Expand Down
2 changes: 1 addition & 1 deletion docs/statement/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ SELECT name FROM branches INTO OUTFILE "branches.txt"
You can format the output result with options for example

```sql
SELECT * FROM branches INTO OUTFILE "branches" FIELDS TERMINATED BY "," LINES TERMINATED BY "\n" ENCLOSED "|"
SELECT * FROM branches INTO OUTFILE "branches.txt" FIELDS TERMINATED BY "," LINES TERMINATED BY "\n" ENCLOSED "|"
```

If you want to just dump the data without any format you can use `INTO DUMPFILE`
Expand Down

0 comments on commit 77e5751

Please sign in to comment.