Skip to content

Commit 89bb0fa

Browse files
committed
Add initial methods for storing and retrieving templates
1 parent 0cdf312 commit 89bb0fa

5 files changed

Lines changed: 86 additions & 1 deletion

.sqlx/query-74ddd4d18b2b3529839b609905af032ed741abbd5f26fb9dd0c3079a313ef958.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-f3b05af5405d08d1577c12fbc483d19c75504c917b65e59ae7f396508251079b.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE templates;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Add new table for additional templates
2+
CREATE TABLE templates (
3+
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
4+
name TEXT NOT NULL CHECK (length(name) > 0),
5+
template TEXT NOT NULL CHECK (length(name) > 0),
6+
beamline INTEGER NOT NULL REFERENCES beamline(id) ON DELETE CASCADE ON UPDATE CASCADE
7+
)

src/db_service.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::path::Path;
1919
pub use error::ConfigurationError;
2020
use error::NewConfigurationError;
2121
use sqlx::sqlite::{SqliteConnectOptions, SqliteRow};
22-
use sqlx::{query_as, FromRow, QueryBuilder, Row, Sqlite, SqlitePool};
22+
use sqlx::{query, query_as, FromRow, QueryBuilder, Row, Sqlite, SqlitePool};
2323
use tracing::{info, instrument, trace};
2424

2525
use crate::paths::{
@@ -35,6 +35,12 @@ pub struct SqliteScanPathService {
3535
pool: SqlitePool,
3636
}
3737

38+
#[derive(Debug)]
39+
pub struct NamedTemplate {
40+
pub name: String,
41+
pub template: String,
42+
}
43+
3844
#[derive(Debug, PartialEq, Eq)]
3945
struct RawPathTemplate<F>(String, PhantomData<F>);
4046

@@ -341,6 +347,39 @@ impl SqliteScanPathService {
341347
.ok_or(ConfigurationError::MissingInstrument(instrument.into()))
342348
}
343349

350+
pub async fn additional_templates(
351+
&self,
352+
beamline: &str,
353+
) -> Result<Vec<NamedTemplate>, ConfigurationError> {
354+
Ok(query_as!(
355+
NamedTemplate,
356+
"SELECT templates.name, templates.template
357+
FROM beamline JOIN templates ON beamline.id = templates.beamline
358+
WHERE beamline.name = ?",
359+
beamline
360+
)
361+
.fetch_all(&self.pool)
362+
.await?)
363+
}
364+
365+
pub async fn register_template(
366+
&self,
367+
beamline: &str,
368+
name: String,
369+
template: String,
370+
) -> Result<(), ConfigurationError> {
371+
query!(
372+
"INSERT INTO templates (name, template, beamline)
373+
VALUES (?, ?, (SELECT id FROM beamline WHERE name = ?));",
374+
name,
375+
template,
376+
beamline
377+
)
378+
.execute(&self.pool)
379+
.await?;
380+
Ok(())
381+
}
382+
344383
/// Create a db service from a new empty/schema-less DB
345384
#[cfg(test)]
346385
pub(crate) async fn uninitialised() -> Self {

0 commit comments

Comments
 (0)