Commit 3e18d6a7 authored by Robert Czechowski's avatar Robert Czechowski
Browse files

Add script to generate db connection sources from base source

parent e8c96464
Pipeline #389 passed with stage
in 6 minutes and 59 seconds
This diff is collapsed.
#![cfg(feature = "postgres")]
extern crate postgres;
use postgres::Connection;
use time;
use time::Duration;
use db_conn::{MedalConnection, MedalObject};
use db_objects::*;
use helpers;
trait Queryable {
fn query_map_one<T, F>(&self, sql: &str, params: &[&dyn postgres::types::ToSql], f: F)
-> postgres::Result<Option<T>>
where F: FnOnce(postgres::rows::Row<'_>) -> T;
fn query_map_many<T, F>(&self, sql: &str, params: &[&dyn postgres::types::ToSql], f: F) -> postgres::Result<Vec<T>>
where F: FnMut(postgres::rows::Row<'_>) -> T;
fn exists(&self, sql: &str, params: &[&dyn postgres::types::ToSql]) -> bool;
fn get_last_id(&self) -> Option<i32>;
}
impl Queryable for Connection {
fn query_map_one<T, F>(&self, sql: &str, params: &[&dyn postgres::types::ToSql], f: F)
-> postgres::Result<Option<T>>
where F: FnOnce(postgres::rows::Row<'_>) -> T {
let rows = self.query(sql, params)?;
Ok(rows.iter().next().map(f))
}
fn query_map_many<T, F>(&self, sql: &str, params: &[&dyn postgres::types::ToSql], f: F) -> postgres::Result<Vec<T>>
where F: FnMut(postgres::rows::Row<'_>) -> T {
Ok(self.query(sql, params)?.iter().map(f).collect())
}
fn exists(&self, sql: &str, params: &[&dyn postgres::types::ToSql]) -> bool {
let stmt = self.prepare(sql).unwrap();
!stmt.query(params).unwrap().is_empty()
}
fn get_last_id(&self) -> Option<i32> {
self.query("SELECT lastval()", &[]).unwrap().iter().next().map(|row| {
let r: i64 = row.get(0);
r as i32
})
}
// Empty line intended
}
impl MedalObject<Connection> for Submission {
fn save(&mut self, conn: &Connection) {
match self.get_id() {
Some(_id) => unimplemented!(),
None => {
let query = "INSERT INTO submission (task, session, grade, validated, nonvalidated_grade,
subtask_identifier, value, date, needs_validation)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)";
conn.execute(query,
&[&self.task,
&self.session_user,
&self.grade,
&self.validated,
&self.nonvalidated_grade,
&self.subtask_identifier,
&self.value,
&self.date,
&self.needs_validation])
.unwrap();
self.set_id(conn.get_last_id().unwrap());
}
}
}
}
impl MedalObject<Connection> for Grade {
fn save(&mut self, conn: &Connection) {
let query = "INSERT INTO grade (taskgroup, session, grade, validated)
VALUES ($1, $2, $3, $4)
ON CONFLICT ON CONSTRAINT grade_pkey DO UPDATE SET grade = excluded.grade, validated = excluded.validated";
conn.execute(query, &[&self.taskgroup, &self.user, &self.grade, &self.validated]).unwrap();
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* WARNING *
* *
* This file is auto generated by ./generate_connectors.sh *
* *
* Do not edit this file directly. Instead edit one of the corresponding *
* .header.rs oder .base.rs files. *
* *
* *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#![cfg(feature = "postgres")]
extern crate postgres;
......@@ -81,7 +94,6 @@ impl MedalObject<Connection> for Grade {
conn.execute(query, &[&self.taskgroup, &self.user, &self.grade, &self.validated]).unwrap();
}
}
impl MedalObject<Connection> for Participation {
fn save(&mut self, conn: &Connection) {
let query = "INSERT INTO participation (contest, session, start_date)
......@@ -861,7 +873,7 @@ impl MedalConnection for Connection {
name: row.get(10),
positionalnumber: None,
tasks: Vec::new() },
Task { id: Some(row.get(11)), taskgroup: row.get(9), location: row.get(11), stars: row.get(13) })
Task { id: Some(row.get(11)), taskgroup: row.get(9), location: row.get(12), stars: row.get(13) })
})
.unwrap();
let mut taskgroupcontest_iter = taskgroupcontest.into_iter();
......
#![cfg(feature = "rusqlite_new")]
extern crate rusqlite;
use rusqlite::Connection;
use time;
use time::Duration;
use db_conn::{MedalConnection, MedalObject};
use db_objects::*;
use helpers;
trait Queryable {
fn query_map_one<T, F>(&self, sql: &str, params: &[&dyn rusqlite::types::ToSql], f: F)
-> rusqlite::Result<Option<T>>
where F: FnOnce(&rusqlite::Row) -> T;
fn query_map_many<T, F>(&self, sql: &str, params: &[&dyn rusqlite::types::ToSql], f: F) -> rusqlite::Result<Vec<T>>
where F: FnMut(&rusqlite::Row) -> T;
fn exists(&self, sql: &str, params: &[&dyn rusqlite::types::ToSql]) -> bool;
fn get_last_id(&self) -> Option<i32>;
}
impl Queryable for Connection {
fn query_map_one<T, F>(&self, sql: &str, params: &[&dyn rusqlite::types::ToSql], f: F)
-> rusqlite::Result<Option<T>>
where F: FnOnce(&rusqlite::Row) -> T {
let mut stmt = self.prepare(sql)?;
let mut rows = stmt.query(params)?;
match rows.next() {
None => Ok(None),
Some(Err(e)) => Err(e),
Some(Ok(row)) => Ok(Some(f(&row))),
}
}
fn query_map_many<T, F>(&self, sql: &str, params: &[&dyn rusqlite::types::ToSql], f: F) -> rusqlite::Result<Vec<T>>
where F: FnMut(&rusqlite::Row) -> T {
let mut stmt = self.prepare(sql)?;
let rows = stmt.query_map(params, f)?;
Ok(rows.map(|x| x.unwrap()).collect())
}
fn exists(&self, sql: &str, params: &[&dyn rusqlite::types::ToSql]) -> bool {
let mut stmt = self.prepare(sql).unwrap();
stmt.exists(params).unwrap()
}
fn get_last_id(&self) -> Option<i32> { self.query_row("SELECT last_insert_rowid()", &[], |row| row.get(0)).ok() }
}
impl MedalObject<Connection> for Submission {
fn save(&mut self, conn: &Connection) {
match self.get_id() {
Some(_id) => unimplemented!(),
None => {
let query = "INSERT INTO submission (task, session, grade, validated, nonvalidated_grade,
subtask_identifier, value, date, needs_validation)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)";
conn.execute(query,
&[&self.task,
&self.session_user,
&self.grade,
&self.validated,
&self.nonvalidated_grade,
&self.subtask_identifier,
&self.value,
&self.date,
&self.needs_validation])
.unwrap();
self.set_id(conn.get_last_id().unwrap());
}
}
}
}
impl MedalObject<Connection> for Grade {
fn save(&mut self, conn: &Connection) {
let query = "INSERT OR REPLACE INTO grade (taskgroup, session, grade, validated)
VALUES (?1, ?2, ?3, ?4)";
conn.execute(query, &[&self.taskgroup, &self.user, &self.grade, &self.validated]).unwrap();
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* WARNING *
* *
* This file is auto generated by ./generate_connectors.sh *
* *
* Do not edit this file directly. Instead edit one of the corresponding *
* .header.rs oder .base.rs files. *
* *
* *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#![cfg(feature = "rusqlite_new")]
extern crate rusqlite;
......@@ -77,10 +90,10 @@ impl MedalObject<Connection> for Grade {
fn save(&mut self, conn: &Connection) {
let query = "INSERT OR REPLACE INTO grade (taskgroup, session, grade, validated)
VALUES (?1, ?2, ?3, ?4)";
conn.execute(query, &[&self.taskgroup, &self.user, &self.grade, &self.validated]).unwrap();
}
}
impl MedalObject<Connection> for Participation {
fn save(&mut self, conn: &Connection) {
let query = "INSERT INTO participation (contest, session, start_date)
......
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* WARNING *
* *
* This file is auto generated by ./generate_connectors.sh *
* *
* Do not edit this file directly. Instead edit one of the corresponding *
* .header.rs oder .base.rs files. *
* *
* *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#!/bin/sh
cat db_conn_warning_header.txt db_conn_sqlite_new.header.rs db_conn.base.rs | sed 's/\$/\?/g' | sed 's/{ "postgres" }/{ "sqlite_v2" }/' | sed 's/batch_execute/execute_batch/' > db_conn_sqlite_new.rs
cat db_conn_warning_header.txt db_conn_postgres.header.rs db_conn.base.rs > db_conn_postgres.rs
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment