Skip to main content

chdb_rust/
arg.rs

1//! Query argument definitions for chDB.
2//!
3//! This module provides types for specifying query arguments such as output format,
4//! log level, and custom command-line arguments.
5
6use std::borrow::Cow;
7use std::ffi::CString;
8
9use crate::error::Error;
10use crate::format::OutputFormat;
11use crate::log_level::LogLevel;
12
13/// Query arguments that can be passed when executing queries.
14///
15/// `Arg` represents various command-line arguments that can be used to configure
16/// query execution. Most commonly, you'll use `OutputFormat` to specify the
17/// desired output format.
18///
19/// # Examples
20///
21/// ```no_run
22/// use chdb_rust::arg::Arg;
23/// use chdb_rust::format::OutputFormat;
24/// use chdb_rust::log_level::LogLevel;
25///
26/// // Specify output format
27/// let args = &[Arg::OutputFormat(OutputFormat::JSONEachRow)];
28///
29/// // Specify log level
30/// let args = &[Arg::LogLevel(LogLevel::Debug)];
31///
32/// // Use custom arguments
33/// let args = &[Arg::Custom("path".into(), Some("/tmp/db".into()))];
34/// ```
35#[derive(Debug)]
36pub enum Arg<'a> {
37    /// `--config-file=<value>`
38    ConfigFilePath(Cow<'a, str>),
39    /// `--log-level=<value>`
40    LogLevel(LogLevel),
41    /// `--output-format=<value>`
42    OutputFormat(OutputFormat),
43    /// --multiquery
44    MultiQuery,
45    /// Custom argument.
46    ///
47    /// "--path=/tmp/chdb" translates into one of the following:
48    /// 1. Arg::Custom("path".to_string().into(), Some("/tmp/chdb".to_string().into())).
49    /// 2. Arg::Custom("path".into(), Some("/tmp/chdb".into())).
50    ///
51    /// "--multiline" translates into one of the following:
52    /// 1. Arg::Custom("multiline".to_string().into(), None).
53    /// 2. Arg::Custom("multiline".into(), None).
54    ///
55    /// We should tell user where to look for officially supported arguments.
56    /// Here is some hint for now: <https://github.com/fixcik/chdb-rs/blob/master/OPTIONS.md>.
57    Custom(Cow<'a, str>, Option<Cow<'a, str>>),
58}
59
60impl<'a> Arg<'a> {
61    #[allow(dead_code)]
62    pub(crate) fn to_cstring(&self) -> Result<CString, Error> {
63        Ok(match self {
64            Self::ConfigFilePath(v) => CString::new(format!("--config-file={v}")),
65            Self::LogLevel(v) => CString::new(format!("--log-level={}", v.as_str())),
66            Self::OutputFormat(v) => CString::new(format!("--output-format={}", v.as_str())),
67            Self::MultiQuery => CString::new("-n"),
68            Self::Custom(k, v) => match v {
69                None => CString::new(k.as_ref()),
70                Some(v) => CString::new(format!("--{k}={v}")),
71            },
72        }?)
73    }
74
75    /// Extract `OutputFormat` from an `Arg` if it is an `OutputFormat` variant.
76    ///
77    /// This is a helper method used internally to extract output format information
78    /// from query arguments.
79    pub(crate) fn as_output_format(&self) -> Option<OutputFormat> {
80        match self {
81            Self::OutputFormat(f) => Some(*f),
82            _ => None,
83        }
84    }
85}
86
87/// Extract `OutputFormat` from a slice of `Arg`s.
88///
89/// This function searches through the provided arguments and returns the first
90/// `OutputFormat` found, or the default `TabSeparated` format if none is found.
91///
92/// # Arguments
93///
94/// * `args` - Optional slice of query arguments
95///
96/// # Returns
97///
98/// Returns the first `OutputFormat` found, or `OutputFormat::TabSeparated` as default.
99pub(crate) fn extract_output_format(args: Option<&[Arg]>) -> OutputFormat {
100    args.and_then(|args| args.iter().find_map(|a| a.as_output_format()))
101        .unwrap_or(OutputFormat::TabSeparated)
102}