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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, SystemTime};

use flow_component::{BoxFuture, Component, ComponentError, RuntimeCallback};
use futures::StreamExt;
use once_cell::sync::Lazy;
use regex::{Captures, Regex};
use tracing::Span;
use url::Url;
use wick_config::config::components::{ComponentConfig, OperationConfig, SqlComponentConfig, SqlOperationDefinition};
use wick_config::config::{ErrorBehavior, Metadata};
use wick_config::Resolver;
use wick_interface_types::{ComponentSignature, Field, OperationSignatures, Type};
use wick_packet::{Invocation, Observer, Packet, PacketSender, PacketStream, RuntimeConfig};

use crate::common::{Connection, DatabaseProvider};
use crate::{common, Error};

#[derive(Debug, Clone, Copy, PartialEq)]
enum DbKind {
  Mssql,
  Postgres,
  Sqlite,
}
#[derive(Clone)]
struct Client {
  inner: Arc<dyn DatabaseProvider + Send + Sync>,
}

impl Client {
  async fn new(
    url: &Url,
    config: &mut SqlComponentConfig,
    _metadata: Option<Metadata>,
    _root_config: Option<RuntimeConfig>, // TODO use this
    resolver: &Resolver,
  ) -> Result<Self, Error> {
    let client: Arc<dyn DatabaseProvider + Send + Sync> = match url.scheme() {
      "mssql" => {
        normalize_operations(config.operations_mut(), DbKind::Mssql);
        Arc::new(crate::mssql_tiberius::AzureSqlComponent::new(config.clone(), resolver).await?)
      }
      "postgres" => {
        normalize_operations(config.operations_mut(), DbKind::Postgres);
        Arc::new(crate::sqlx::SqlXComponent::new(config.clone(), resolver).await?)
      }
      "file" | "sqlite" => {
        normalize_operations(config.operations_mut(), DbKind::Sqlite);
        Arc::new(crate::sqlx::SqlXComponent::new(config.clone(), resolver).await?)
      }
      _ => return Err(Error::InvalidScheme(url.scheme().to_owned())),
    };

    Ok(Self { inner: client })
  }

  fn inner(&self) -> &Arc<dyn DatabaseProvider + Sync + Send> {
    &self.inner
  }
}

#[async_trait::async_trait]
impl DatabaseProvider for Client {
  fn get_statement<'a>(&'a self, id: &'a str) -> Option<&'a str> {
    self.inner().get_statement(id)
  }

  async fn get_connection<'a, 'b>(&'a self) -> Result<Connection<'b>, Error>
  where
    'a: 'b,
  {
    self.inner().get_connection().await
  }
}

/// The Azure SQL Wick component.
#[derive(Clone)]
#[must_use]
pub struct SqlComponent {
  provider: Client,
  signature: Arc<ComponentSignature>,
  url: Url,
  config: SqlComponentConfig,
  root_config: Option<RuntimeConfig>,
}

impl std::fmt::Debug for SqlComponent {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("SqlComponent")
      .field("signature", &self.signature)
      .field("url", &self.url)
      .field("config", &self.config)
      .field("root_config", &self.root_config)
      .finish()
  }
}

impl SqlComponent {
  /// Instantiate a new Azure SQL component.
  pub async fn new(
    mut config: SqlComponentConfig,
    root_config: Option<RuntimeConfig>,
    metadata: Option<Metadata>,
    resolver: &Resolver,
  ) -> Result<Self, ComponentError> {
    validate(&config, resolver)?;
    let sig = common::gen_signature(
      "wick/component/sql",
      config.operation_signatures(),
      config.config(),
      &metadata,
    )?;

    let url = common::convert_url_resource(resolver, config.resource())?;

    validate(&config, resolver)?;
    let provider = Client::new(&url, &mut config, metadata, root_config.clone(), resolver).await?;

    Ok(Self {
      provider,
      signature: Arc::new(sig),
      url,
      root_config,
      config,
    })
  }
}

impl Component for SqlComponent {
  fn handle(
    &self,
    invocation: Invocation,
    _data: Option<RuntimeConfig>, // TODO: this needs to be used
    _callback: Arc<RuntimeCallback>,
  ) -> BoxFuture<Result<PacketStream, ComponentError>> {
    let client = self.provider.clone();
    let opdef = self
      .config
      .get_operation(invocation.target().operation_id())
      .ok_or_else(|| Error::MissingOperation(invocation.target().operation_id().to_owned()))
      .cloned();

    Box::pin(async move {
      let opdef = opdef?;
      let stmt = client.get_statement(opdef.name()).unwrap().to_owned();

      let input_names: Vec<_> = opdef.inputs().iter().map(|i| i.name.clone()).collect();
      let (invocation, stream) = invocation.split();
      let input_streams = wick_packet::split_stream(stream, input_names);
      let (tx, rx) = invocation.make_response();
      tokio::spawn(async move {
        let start = SystemTime::now();
        let span = invocation.span.clone();
        let Ok(mut connection) = client.get_connection().await else {
          invocation.trace(|| {
            error!("could not get connection to database");
          });
          return;
        };
        if let Err(e) = handle_call(&mut connection, opdef, input_streams, tx.clone(), &stmt, span).await {
          invocation.trace(|| {
            error!(error = %e, "error handling sql operation");
          });
          let _ = tx.error(wick_packet::Error::component_error(e.to_string()));
        }
        let _ = tx.send(Packet::done("output"));
        let duration = SystemTime::now().duration_since(start).unwrap();
        invocation.trace(|| {
          debug!(?duration, target=%invocation.target,"mssql operation complete");
        });
      });

      Ok(rx)
    })
  }

  fn signature(&self) -> &ComponentSignature {
    &self.signature
  }
}

fn validate(config: &SqlComponentConfig, _resolver: &Resolver) -> Result<(), Error> {
  let bad_ops: Vec<_> = config
    .operations()
    .iter()
    .filter(|op| {
      let outputs = op.outputs();
      outputs.len() > 1 || outputs.len() == 1 && outputs[0] != Field::new("output", Type::Object)
    })
    .map(|op| op.name().to_owned())
    .collect();

  if !bad_ops.is_empty() {
    return Err(Error::InvalidOutput(bad_ops));
  }

  Ok(())
}

async fn handle_call<'a, 'b, 'c>(
  connection: &'a mut Connection<'c>,
  opdef: SqlOperationDefinition,
  input_streams: Vec<PacketStream>,
  tx: PacketSender,
  stmt: &'b str,
  span: Span,
) -> Result<(), Error>
where
  'b: 'a,
{
  let error_behavior = opdef.on_error();

  connection.start(error_behavior).await?;

  let result = handle_stream(connection, opdef, input_streams, tx, stmt, span.clone()).await;
  if let Err(e) = result {
    span.in_scope(|| error!(error = %e, "error in sql operation"));
    let err = Error::OperationFailed(e.to_string());
    connection.handle_error(e, error_behavior).await?;
    return Err(err);
  }
  connection.finish().await?;

  Ok(())
}

async fn handle_stream<'a, 'b, 'c>(
  connection: &'a mut Connection<'c>,
  opdef: SqlOperationDefinition,
  mut input_streams: Vec<PacketStream>,
  tx: PacketSender,
  stmt: &'b str,
  span: Span,
) -> Result<(), Error>
where
  'b: 'a,
{
  span.in_scope(|| debug!(stmt = %stmt, "preparing query for stream"));
  'outer: loop {
    let mut incoming_packets = Vec::new();

    for input in &mut input_streams {
      let packet = input.next().await;

      incoming_packets.push(packet);
    }

    let num_done = incoming_packets.iter().filter(|r| r.is_none()).count();
    if num_done > 0 {
      if num_done != opdef.inputs().len() {
        return Err(Error::MissingInput);
      }
      break 'outer;
    }
    let incoming_packets = incoming_packets.into_iter().map(|r| r.unwrap()).collect::<Vec<_>>();

    if let Some(Err(e)) = incoming_packets.iter().find(|r| r.is_err()) {
      return Err(Error::ComponentError(e.clone()));
    }
    let fields = opdef.inputs();
    let mut type_wrappers = Vec::new();

    for packet in incoming_packets {
      let packet = packet.unwrap();
      if packet.is_done() {
        break 'outer;
      }
      if packet.is_open_bracket() || packet.is_close_bracket() {
        let _ = tx.send(packet.set_port("output"));
        continue 'outer;
      }
      let ty = fields.iter().find(|f| f.name() == packet.port()).unwrap().ty().clone();
      type_wrappers.push((ty, packet));
    }

    let start = SystemTime::now();
    let result = match &opdef {
      SqlOperationDefinition::Query(_) => {
        query(connection, tx.clone(), opdef.clone(), type_wrappers, stmt, span.clone()).await
      }
      SqlOperationDefinition::Exec(_) => {
        exec(connection, tx.clone(), opdef.clone(), type_wrappers, stmt, span.clone()).await
      }
    };
    let duration = SystemTime::now().duration_since(start).unwrap();

    span.in_scope(|| debug!(μs = duration.as_micros(), "executed query"));

    if let Err(e) = result {
      if opdef.on_error() == ErrorBehavior::Ignore {
        let _ = tx.send(Packet::err("output", e.to_string()));
      } else {
        return Err(Error::ErrorInStream(e.to_string()));
      }
    };

    if opdef.inputs().len() == 0 {
      break 'outer;
    }
  }
  Ok(())
}

async fn query<'a, 'b, 'c>(
  client: &'a mut Connection<'c>,
  tx: PacketSender,
  def: SqlOperationDefinition,
  args: Vec<(Type, Packet)>,
  stmt: &'b str,
  _span: Span,
) -> Result<Duration, Error>
where
  'b: 'a,
{
  let start = SystemTime::now();

  let bound_args = common::bind_args(def.arguments(), &args)?;

  let mut rows = client.query(stmt, bound_args).await?;

  while let Some(row) = rows.next().await {
    let _ = match row {
      Ok(row) => tx.send(Packet::encode("output", row)),
      Err(e) => tx.send(Packet::err("output", e.to_string())),
    };
  }

  let duration = SystemTime::now().duration_since(start).unwrap();

  Ok(duration)
}

async fn exec<'a, 'b, 'c>(
  connection: &'a mut Connection<'c>,
  tx: PacketSender,
  def: SqlOperationDefinition,
  args: Vec<(Type, Packet)>,
  stmt: &'b str,
  _span: Span,
) -> Result<Duration, Error>
where
  'b: 'a,
{
  let start = SystemTime::now();

  let bound_args = common::bind_args(def.arguments(), &args)?;
  let packet = match connection.exec(stmt.to_owned(), bound_args).await {
    Ok(num) => Packet::encode("output", num),
    Err(err) => Packet::err("output", err.to_string()),
  };

  let _ = tx.send(packet);

  let duration = SystemTime::now().duration_since(start).unwrap();

  Ok(duration)
}

static POSITIONAL_ARGS: Lazy<Regex> = Lazy::new(|| Regex::new(r"\$(?<id>\d+)\b").unwrap());
static WICK_ID_ARGS: Lazy<Regex> = Lazy::new(|| Regex::new(r"\$\{(?<id>\w+)\}").unwrap());

fn normalize_operations(ops: &mut Vec<SqlOperationDefinition>, db: DbKind) {
  for operations in ops {
    match operations {
      wick_config::config::components::SqlOperationDefinition::Query(ref mut op) => {
        let (mut query, args) = normalize_inline_ids(op.query(), op.arguments().to_vec());
        if db == DbKind::Mssql {
          query = normalize_mssql_query(query);
        }
        let query = query.to_string();
        op.set_query(query);
        op.set_arguments(args);
      }
      wick_config::config::components::SqlOperationDefinition::Exec(ref mut op) => {
        let (mut query, args) = normalize_inline_ids(op.exec(), op.arguments().to_vec());
        if db == DbKind::Mssql {
          query = normalize_mssql_query(query);
        }
        let query = query.to_string();
        op.set_exec(query);
        op.set_arguments(args);
      }
    };
  }
}

// This translates `${id}` to positional `$1` arguments.
fn normalize_inline_ids(orig_query: &str, mut orig_args: Vec<String>) -> (Cow<str>, Vec<String>) {
  if orig_query.contains('$') {
    // replace all instances of ${id} with @p1, @p2, etc and append the id to the args

    let mut id_map: HashMap<String, usize> = orig_args
      .iter()
      .enumerate()
      .map(|(i, id)| (id.clone(), i + 1))
      .collect();

    let captures = WICK_ID_ARGS.captures_iter(orig_query);
    for id in captures {
      let id = id.name("id").unwrap().as_str().to_owned();
      if !id_map.contains_key(&id) {
        id_map.insert(id.clone(), id_map.len() + 1);
        orig_args.push(id.clone());
      }
    }

    let normalized = WICK_ID_ARGS.replace_all(orig_query, |cap: &Captures| {
      let id = cap.name("id").unwrap().as_str();
      let id = id_map[id];
      format!("${}", id)
    });
    debug!(%orig_query,%normalized, "sql:inline-replacement");
    (normalized, orig_args)
  } else {
    (Cow::Borrowed(orig_query), orig_args)
  }
}

// This translates `$1..$n` to `@p1..@pn` to be compatible with Tiberius.
fn normalize_mssql_query(original: Cow<str>) -> Cow<str> {
  if original.contains('$') {
    let normalized = POSITIONAL_ARGS.replace_all(&original, "@p${id}");
    debug!(%original,%normalized, "sql:mssql:normalized query");
    Cow::Owned(normalized.to_string())
  } else {
    original
  }
}

#[cfg(test)]
mod test {
  use anyhow::Result;

  use super::*;

  #[test]
  fn test_mssql_query_normalization() -> Result<()> {
    let query = "select id,name from users where id=$1;";
    let expected = "select id,name from users where id=@p1;";
    let actual = normalize_mssql_query(Cow::Borrowed(query));
    assert_eq!(actual, expected);

    Ok(())
  }

  #[rstest::rstest]
  #[case("select id,name from users where id=${id};",[],"select id,name from users where id=$1;",["id"])]
  #[case("select id,name from users where email=$1, id=${id};",["email"],"select id,name from users where email=$1, id=$2;",["email","id"])]
  #[case("select id,name from users where email=$1, id=${id}, email=${email};",["email"],"select id,name from users where email=$1, id=$2, email=$1;",["email","id"])]
  #[case("select id,name from users where id=${id}, id2=${id}, id3=${id};",[],"select id,name from users where id=$1, id2=$1, id3=$1;",["id"])]
  fn test_inline_id_normalization<const K: usize, const U: usize>(
    #[case] orig_query: &str,
    #[case] orig_args: [&str; K],
    #[case] expected_query: &str,
    #[case] expected_args: [&str; U],
  ) -> Result<()> {
    let (actual, actual_args) =
      normalize_inline_ids(orig_query, orig_args.iter().copied().map(|s| s.to_owned()).collect());
    let expected_args = expected_args.iter().map(|s| s.to_owned()).collect::<Vec<_>>();
    assert_eq!(actual, expected_query);
    assert_eq!(actual_args, expected_args);

    Ok(())
  }
}

#[cfg(test)]
mod integration_test {
  use anyhow::Result;
  use flow_component::{panic_callback, Component};
  use futures::StreamExt;
  use serde_json::json;
  use wick_config::config::components::{
    ComponentConfig,
    SqlComponentConfigBuilder,
    SqlOperationDefinition,
    SqlQueryOperationDefinitionBuilder,
  };
  use wick_config::config::ResourceDefinition;
  use wick_interface_types::{Field, Type};
  use wick_packet::{packet_stream, Invocation, Packet};

  use super::SqlComponent;

  async fn init_mssql_component() -> Result<SqlComponent> {
    let docker_host = std::env::var("TEST_HOST").unwrap();
    let password = std::env::var("TEST_PASSWORD").unwrap();
    let db_host = docker_host.split(':').next().unwrap();
    let port = std::env::var("MSSQL_PORT").unwrap();
    let user = "SA";
    let db_name = "wick_test";

    let mut config = SqlComponentConfigBuilder::default()
      .resource("db")
      .tls(false)
      .build()
      .unwrap();
    let op = SqlQueryOperationDefinitionBuilder::default()
      .name("test")
      .query("select id,name from users where id=$1;")
      .inputs([Field::new("input", Type::I32)])
      .outputs([Field::new("output", Type::Object)])
      .arguments(["input".to_owned()])
      .build()
      .unwrap();

    config.operations_mut().push(SqlOperationDefinition::Query(op));
    let mut app_config = wick_config::config::AppConfiguration::default();
    app_config.add_resource(
      "db",
      ResourceDefinition::Url(
        format!("mssql://{}:{}@{}:{}/{}", user, password, db_host, port, db_name)
          .try_into()
          .unwrap(),
      ),
    );

    let component = SqlComponent::new(config, None, None, &app_config.resolver()).await?;

    Ok(component)
  }

  #[test_logger::test(tokio::test)]
  async fn test_mssql_basic() -> Result<()> {
    let db = init_mssql_component().await?;
    let input = packet_stream!(("input", 1_i32));
    let inv = Invocation::test("mssql", "wick://__local__/test", input, None)?;
    let response = db.handle(inv, Default::default(), panic_callback()).await.unwrap();
    let packets: Vec<_> = response.collect().await;

    assert_eq!(
      packets,
      vec![
        Ok(Packet::encode("output", json!({"id":1_i32, "name":"Test User"}))),
        Ok(Packet::done("output"))
      ]
    );
    Ok(())
  }

  async fn init_pg_component() -> Result<SqlComponent> {
    let docker_host = std::env::var("TEST_HOST").unwrap();
    let db_host = docker_host.split(':').next().unwrap();
    let password = std::env::var("TEST_PASSWORD").unwrap();
    let port = std::env::var("POSTGRES_PORT").unwrap();
    let user = "postgres";
    let db_name = "wick_test";

    let mut config = SqlComponentConfigBuilder::default()
      .resource("db")
      .tls(false)
      .build()
      .unwrap();
    let op = SqlQueryOperationDefinitionBuilder::default()
      .name("test")
      .query("select id,name from users where id=$1;")
      .inputs([Field::new("input", Type::I32)])
      .outputs([Field::new("output", Type::Object)])
      .arguments(["input".to_owned()])
      .build()
      .unwrap();

    config.operations_mut().push(SqlOperationDefinition::Query(op));
    let mut app_config = wick_config::config::AppConfiguration::default();
    app_config.add_resource(
      "db",
      ResourceDefinition::Url(
        format!("postgres://{}:{}@{}:{}/{}", user, password, db_host, port, db_name)
          .try_into()
          .unwrap(),
      ),
    );

    let component = SqlComponent::new(config, None, None, &app_config.resolver()).await?;

    Ok(component)
  }

  #[test_logger::test(tokio::test)]
  async fn test_pg_basic() -> Result<()> {
    let pg = init_pg_component().await?;
    let input = packet_stream!(("input", 1_u32));
    let inv = Invocation::test("postgres", "wick://__local__/test", input, None)?;
    let response = pg.handle(inv, Default::default(), panic_callback()).await.unwrap();
    let packets: Vec<_> = response.collect().await;

    assert_eq!(
      packets,
      vec![
        Ok(Packet::encode("output", json!({"id":1_i32, "name":"Test User"}))),
        Ok(Packet::done("output"))
      ]
    );
    Ok(())
  }

  async fn init_sqlite_component() -> Result<SqlComponent> {
    let db = std::env::var("SQLITE_DB").unwrap();

    let mut config = SqlComponentConfigBuilder::default()
      .resource("db")
      .tls(false)
      .build()
      .unwrap();
    let op = SqlQueryOperationDefinitionBuilder::default()
      .name("test")
      .query("select id,name from users where id=$1;")
      .inputs([Field::new("input", Type::I32)])
      .outputs([Field::new("output", Type::Object)])
      .arguments(["input".to_owned()])
      .build()
      .unwrap();

    config.operations_mut().push(SqlOperationDefinition::Query(op));
    let mut app_config = wick_config::config::AppConfiguration::default();
    app_config.add_resource(
      "db",
      ResourceDefinition::Url(format!("file://{}", db).try_into().unwrap()),
    );

    let component = SqlComponent::new(config, None, None, &app_config.resolver()).await?;

    Ok(component)
  }

  #[test_logger::test(tokio::test)]
  async fn test_sqlite_basic() -> Result<()> {
    let pg = init_sqlite_component().await?;
    let input = packet_stream!(("input", 1_i32));
    let inv = Invocation::test("sqlite", "wick://__local__/test", input, None)?;
    let response = pg.handle(inv, Default::default(), panic_callback()).await.unwrap();
    let packets: Vec<_> = response.collect().await;

    assert_eq!(
      packets,
      vec![
        Ok(Packet::encode("output", json!({"id":1_i32, "name":"Test User"}))),
        Ok(Packet::done("output"))
      ]
    );
    Ok(())
  }
}