1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#![allow(missing_docs)]
use std::borrow::Cow;

// delete when we move away from the `property` crate.
use wick_interface_types::{Field, OperationSignatures};

use super::{ComponentConfig, OperationConfig};
use crate::config::{self, ErrorBehavior};
use crate::utils::impl_from_for;

#[derive(
  Debug,
  Clone,
  derive_builder::Builder,
  PartialEq,
  derive_asset_container::AssetManager,
  property::Property,
  serde::Serialize,
)]
#[property(get(public), set(public), mut(public, suffix = "_mut"))]
#[asset(asset(config::AssetReference))]
#[builder(setter(into))]
/// A component made out of other components
pub struct SqlComponentConfig {
  /// The TcpPort reference to listen on for connections.
  #[asset(skip)]
  pub(crate) resource: String,

  /// Whether or not to use TLS.
  #[asset(skip)]
  #[builder(default)]
  pub(crate) tls: bool,

  /// The configuration for the component.
  #[asset(skip)]
  #[builder(default)]
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub(crate) config: Vec<Field>,

  /// A list of operations to expose on this component.
  #[asset(skip)]
  #[builder(default)]
  #[property(skip)]
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub(crate) operations: Vec<SqlOperationDefinition>,
}

impl SqlComponentConfig {}

impl OperationSignatures for SqlComponentConfig {
  fn operation_signatures(&self) -> Vec<wick_interface_types::OperationSignature> {
    self.operations.clone().into_iter().map(Into::into).collect()
  }
}

impl_from_for!(SqlOperationDefinition, Query, SqlQueryOperationDefinition);
impl_from_for!(SqlOperationDefinition, Exec, SqlExecOperationDefinition);

impl From<SqlOperationDefinition> for wick_interface_types::OperationSignature {
  fn from(value: SqlOperationDefinition) -> Self {
    match value {
      SqlOperationDefinition::Query(v) => v.into(),
      SqlOperationDefinition::Exec(v) => v.into(),
    }
  }
}

impl ComponentConfig for SqlComponentConfig {
  type Operation = SqlOperationDefinition;

  fn operations(&self) -> &[Self::Operation] {
    &self.operations
  }

  fn operations_mut(&mut self) -> &mut Vec<Self::Operation> {
    &mut self.operations
  }
}

#[derive(Debug, Clone, PartialEq, serde::Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum SqlOperationDefinition {
  Query(SqlQueryOperationDefinition),
  Exec(SqlExecOperationDefinition),
}

#[derive(Debug, Clone, Copy)]
pub enum SqlOperationKind {
  Query,
  Exec,
}

impl std::fmt::Display for SqlOperationKind {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      SqlOperationKind::Query => write!(f, "sql query operation"),
      SqlOperationKind::Exec => write!(f, "sql exec operation"),
    }
  }
}

impl SqlOperationDefinition {
  #[must_use]
  pub const fn on_error(&self) -> ErrorBehavior {
    match self {
      SqlOperationDefinition::Query(v) => v.on_error,
      SqlOperationDefinition::Exec(v) => v.on_error,
    }
  }

  #[must_use]
  pub fn arguments(&self) -> &[String] {
    match self {
      SqlOperationDefinition::Query(v) => &v.arguments,
      SqlOperationDefinition::Exec(v) => &v.arguments,
    }
  }

  #[must_use]
  pub fn query(&self) -> &str {
    match self {
      SqlOperationDefinition::Query(v) => &v.query,
      SqlOperationDefinition::Exec(v) => &v.exec,
    }
  }

  #[must_use]
  pub const fn kind(&self) -> SqlOperationKind {
    match self {
      SqlOperationDefinition::Query(_) => SqlOperationKind::Query,
      SqlOperationDefinition::Exec(_) => SqlOperationKind::Exec,
    }
  }
}

impl OperationConfig for SqlOperationDefinition {
  fn name(&self) -> &str {
    match self {
      SqlOperationDefinition::Query(v) => &v.name,
      SqlOperationDefinition::Exec(v) => &v.name,
    }
  }

  fn inputs(&self) -> Cow<Vec<Field>> {
    match self {
      SqlOperationDefinition::Query(v) => v.inputs(),
      SqlOperationDefinition::Exec(v) => v.inputs(),
    }
  }

  fn outputs(&self) -> Cow<Vec<Field>> {
    match self {
      SqlOperationDefinition::Query(v) => v.outputs(),
      SqlOperationDefinition::Exec(v) => v.outputs(),
    }
  }
}

impl OperationConfig for SqlQueryOperationDefinition {
  fn name(&self) -> &str {
    &self.name
  }

  fn inputs(&self) -> Cow<Vec<Field>> {
    Cow::Borrowed(&self.inputs)
  }

  fn outputs(&self) -> Cow<Vec<Field>> {
    Cow::Borrowed(&self.outputs)
  }
}

impl OperationConfig for SqlExecOperationDefinition {
  fn name(&self) -> &str {
    &self.name
  }

  fn inputs(&self) -> Cow<Vec<Field>> {
    Cow::Borrowed(&self.inputs)
  }

  fn outputs(&self) -> Cow<Vec<Field>> {
    Cow::Borrowed(&self.outputs)
  }
}

impl From<SqlQueryOperationDefinition> for wick_interface_types::OperationSignature {
  fn from(operation: SqlQueryOperationDefinition) -> Self {
    // TODO: Properly use configured outputs here.
    // Forcing SQL components to have a single object output called "output" is a temporary
    // limitation
    let outputs = vec![Field::new("output", wick_interface_types::Type::Object)];

    Self::new(operation.name, operation.inputs, outputs, operation.config)
  }
}

impl From<SqlExecOperationDefinition> for wick_interface_types::OperationSignature {
  fn from(operation: SqlExecOperationDefinition) -> Self {
    let outputs = vec![Field::new("output", wick_interface_types::Type::U32)];

    Self::new(operation.name, operation.inputs, outputs, operation.config)
  }
}
#[derive(
  Debug,
  Clone,
  derive_builder::Builder,
  PartialEq,
  derive_asset_container::AssetManager,
  property::Property,
  serde::Serialize,
)]
#[property(get(public), set(public), mut(public, suffix = "_mut"))]
#[asset(asset(config::AssetReference))]
#[builder(setter(into))]
/// An operation whose implementation is a SQL query to execute on a database.
pub struct SqlQueryOperationDefinition {
  /// The name of the operation.
  #[asset(skip)]
  #[property(skip)]
  pub(crate) name: String,

  /// Types of the inputs to the operation.
  #[asset(skip)]
  #[property(skip)]
  #[builder(default)]
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub(crate) inputs: Vec<Field>,

  /// Types of the outputs to the operation.
  #[asset(skip)]
  #[property(skip)]
  #[builder(default)]
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub(crate) outputs: Vec<Field>,

  /// The configuration the operation needs.
  #[asset(skip)]
  #[builder(default)]
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub(crate) config: Vec<Field>,

  /// The query to execute.
  #[asset(skip)]
  pub(crate) query: String,

  /// The arguments to the query, defined as a list of input names.
  #[asset(skip)]
  #[builder(default)]
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub(crate) arguments: Vec<String>,

  /// The query to execute.
  #[asset(skip)]
  #[builder(default)]
  pub(crate) on_error: ErrorBehavior,
}

#[derive(
  Debug,
  Clone,
  derive_builder::Builder,
  PartialEq,
  derive_asset_container::AssetManager,
  property::Property,
  serde::Serialize,
)]
#[property(get(public), set(public), mut(public, suffix = "_mut"))]
#[asset(asset(config::AssetReference))]
#[builder(setter(into))]
/// An operation whose implementation is a SQL query to execute on a database and returns the number of rows affected or failure.
pub struct SqlExecOperationDefinition {
  /// The name of the operation.
  #[asset(skip)]
  #[property(skip)]
  pub(crate) name: String,

  /// Types of the inputs to the operation.
  #[asset(skip)]
  #[property(skip)]
  #[builder(default)]
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub(crate) inputs: Vec<Field>,

  /// Types of the outputs to the operation.
  #[asset(skip)]
  #[property(skip)]
  #[builder(default)]
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub(crate) outputs: Vec<Field>,

  /// The configuration the operation needs.
  #[asset(skip)]
  #[builder(default)]
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub(crate) config: Vec<Field>,

  /// The query to execute.
  #[asset(skip)]
  pub(crate) exec: String,

  /// The arguments to the query, defined as a list of input names.
  #[asset(skip)]
  #[builder(default)]
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub(crate) arguments: Vec<String>,

  /// The query to execute.
  #[asset(skip)]
  #[builder(default)]
  pub(crate) on_error: ErrorBehavior,
}