mirror of
https://github.com/scottlamb/moonfire-nvr.git
synced 2025-04-29 06:37:51 -04:00
style: use rusqlite's {named_,}params! everywhere
This commit is contained in:
parent
9d6dec2565
commit
066c086050
24
db/auth.rs
24
db/auth.rs
@ -38,7 +38,7 @@ use lazy_static::lazy_static;
|
|||||||
use libpasta;
|
use libpasta;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use protobuf::Message;
|
use protobuf::Message;
|
||||||
use rusqlite::{Connection, Transaction, types::ToSql};
|
use rusqlite::{Connection, Transaction, params};
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
@ -355,7 +355,7 @@ impl State {
|
|||||||
from
|
from
|
||||||
user
|
user
|
||||||
"#)?;
|
"#)?;
|
||||||
let mut rows = stmt.query(&[] as &[&dyn ToSql])?;
|
let mut rows = stmt.query(params![])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let id = row.get(0)?;
|
let id = row.get(0)?;
|
||||||
let name: String = row.get(1)?;
|
let name: String = row.get(1)?;
|
||||||
@ -476,10 +476,10 @@ impl State {
|
|||||||
|
|
||||||
pub fn delete_user(&mut self, conn: &mut Connection, id: i32) -> Result<(), Error> {
|
pub fn delete_user(&mut self, conn: &mut Connection, id: i32) -> Result<(), Error> {
|
||||||
let tx = conn.transaction()?;
|
let tx = conn.transaction()?;
|
||||||
tx.execute("delete from user_session where user_id = ?", &[&id])?;
|
tx.execute("delete from user_session where user_id = ?", params![id])?;
|
||||||
{
|
{
|
||||||
let mut user_stmt = tx.prepare_cached("delete from user where id = ?")?;
|
let mut user_stmt = tx.prepare_cached("delete from user where id = ?")?;
|
||||||
if user_stmt.execute(&[&id])? != 1 {
|
if user_stmt.execute(params![id])? != 1 {
|
||||||
bail!("user {} not found", id);
|
bail!("user {} not found", id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -636,13 +636,13 @@ impl State {
|
|||||||
"#)?;
|
"#)?;
|
||||||
let addr = req.addr_buf();
|
let addr = req.addr_buf();
|
||||||
let addr: Option<&[u8]> = addr.as_ref().map(|a| a.as_ref());
|
let addr: Option<&[u8]> = addr.as_ref().map(|a| a.as_ref());
|
||||||
stmt.execute(&[
|
stmt.execute(params![
|
||||||
&req.when_sec as &dyn ToSql,
|
req.when_sec,
|
||||||
&req.user_agent,
|
req.user_agent,
|
||||||
&addr,
|
addr,
|
||||||
&(reason as i32),
|
reason as i32,
|
||||||
&detail,
|
detail,
|
||||||
&&hash.0[..],
|
&hash.0[..],
|
||||||
])?;
|
])?;
|
||||||
s.revocation = req;
|
s.revocation = req;
|
||||||
s.revocation_reason = Some(reason as i32);
|
s.revocation_reason = Some(reason as i32);
|
||||||
@ -740,7 +740,7 @@ fn lookup_session(conn: &Connection, hash: &SessionHash) -> Result<Session, Erro
|
|||||||
where
|
where
|
||||||
session_id_hash = ?
|
session_id_hash = ?
|
||||||
"#)?;
|
"#)?;
|
||||||
let mut rows = stmt.query(&[&&hash.0[..]])?;
|
let mut rows = stmt.query(params![&hash.0[..]])?;
|
||||||
let row = rows.next()?.ok_or_else(|| format_err!("no such session"))?;
|
let row = rows.next()?.ok_or_else(|| format_err!("no such session"))?;
|
||||||
let creation_addr: FromSqlIpAddr = row.get(8)?;
|
let creation_addr: FromSqlIpAddr = row.get(8)?;
|
||||||
let revocation_addr: FromSqlIpAddr = row.get(11)?;
|
let revocation_addr: FromSqlIpAddr = row.get(11)?;
|
||||||
|
14
db/check.rs
14
db/check.rs
@ -40,7 +40,7 @@ use fnv::FnvHashMap;
|
|||||||
use log::error;
|
use log::error;
|
||||||
use nix::fcntl::AtFlags;
|
use nix::fcntl::AtFlags;
|
||||||
use protobuf::prelude::MessageField;
|
use protobuf::prelude::MessageField;
|
||||||
use rusqlite::types::ToSql;
|
use rusqlite::params;
|
||||||
use crate::schema;
|
use crate::schema;
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ pub fn run(conn: &rusqlite::Connection, opts: &Options) -> Result<(), Error> {
|
|||||||
"#)?;
|
"#)?;
|
||||||
let mut garbage_stmt = conn.prepare_cached(
|
let mut garbage_stmt = conn.prepare_cached(
|
||||||
"select composite_id from garbage where sample_file_dir_id = ?")?;
|
"select composite_id from garbage where sample_file_dir_id = ?")?;
|
||||||
let mut rows = dir_stmt.query(&[] as &[&dyn ToSql])?;
|
let mut rows = dir_stmt.query(params![])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let mut meta = schema::DirMeta::default();
|
let mut meta = schema::DirMeta::default();
|
||||||
let dir_id: i32 = row.get(0)?;
|
let dir_id: i32 = row.get(0)?;
|
||||||
@ -88,7 +88,7 @@ pub fn run(conn: &rusqlite::Connection, opts: &Options) -> Result<(), Error> {
|
|||||||
// Open the directory (checking its metadata) and hold it open (for the lock).
|
// Open the directory (checking its metadata) and hold it open (for the lock).
|
||||||
let dir = dir::SampleFileDir::open(&dir_path, &meta)?;
|
let dir = dir::SampleFileDir::open(&dir_path, &meta)?;
|
||||||
let mut streams = read_dir(&dir, opts)?;
|
let mut streams = read_dir(&dir, opts)?;
|
||||||
let mut rows = garbage_stmt.query(&[&dir_id])?;
|
let mut rows = garbage_stmt.query(params![dir_id])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let id = CompositeId(row.get(0)?);
|
let id = CompositeId(row.get(0)?);
|
||||||
let s = streams.entry(id.stream()).or_insert_with(Stream::default);
|
let s = streams.entry(id.stream()).or_insert_with(Stream::default);
|
||||||
@ -103,7 +103,7 @@ pub fn run(conn: &rusqlite::Connection, opts: &Options) -> Result<(), Error> {
|
|||||||
let mut stmt = conn.prepare(r#"
|
let mut stmt = conn.prepare(r#"
|
||||||
select id, sample_file_dir_id from stream where sample_file_dir_id is not null
|
select id, sample_file_dir_id from stream where sample_file_dir_id is not null
|
||||||
"#)?;
|
"#)?;
|
||||||
let mut rows = stmt.query(&[] as &[&dyn ToSql])?;
|
let mut rows = stmt.query(params![])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let stream_id = row.get(0)?;
|
let stream_id = row.get(0)?;
|
||||||
let dir_id = row.get(1)?;
|
let dir_id = row.get(1)?;
|
||||||
@ -234,7 +234,7 @@ fn compare_stream(conn: &rusqlite::Connection, stream_id: i32, opts: &Options,
|
|||||||
where
|
where
|
||||||
composite_id between ? and ?
|
composite_id between ? and ?
|
||||||
"#)?;
|
"#)?;
|
||||||
let mut rows = stmt.query(&[&start.0, &end.0])?;
|
let mut rows = stmt.query(params![start.0, end.0])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let id = CompositeId(row.get(0)?);
|
let id = CompositeId(row.get(0)?);
|
||||||
let s = RecordingSummary {
|
let s = RecordingSummary {
|
||||||
@ -261,7 +261,7 @@ fn compare_stream(conn: &rusqlite::Connection, stream_id: i32, opts: &Options,
|
|||||||
where
|
where
|
||||||
composite_id between ? and ?
|
composite_id between ? and ?
|
||||||
"#)?;
|
"#)?;
|
||||||
let mut rows = stmt.query(&[&start.0, &end.0])?;
|
let mut rows = stmt.query(params![start.0, end.0])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let id = CompositeId(row.get(0)?);
|
let id = CompositeId(row.get(0)?);
|
||||||
let video_index: Vec<u8> = row.get(1)?;
|
let video_index: Vec<u8> = row.get(1)?;
|
||||||
@ -288,7 +288,7 @@ fn compare_stream(conn: &rusqlite::Connection, stream_id: i32, opts: &Options,
|
|||||||
where
|
where
|
||||||
composite_id between ? and ?
|
composite_id between ? and ?
|
||||||
"#)?;
|
"#)?;
|
||||||
let mut rows = stmt.query(&[&start.0, &end.0])?;
|
let mut rows = stmt.query(params![start.0, end.0])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let id = CompositeId(row.get(0)?);
|
let id = CompositeId(row.get(0)?);
|
||||||
stream.entry(id.recording())
|
stream.entry(id.recording())
|
||||||
|
148
db/db.rs
148
db/db.rs
@ -68,7 +68,7 @@ use lru_cache::LruCache;
|
|||||||
use openssl::hash;
|
use openssl::hash;
|
||||||
use parking_lot::{Mutex,MutexGuard};
|
use parking_lot::{Mutex,MutexGuard};
|
||||||
use protobuf::prelude::MessageField;
|
use protobuf::prelude::MessageField;
|
||||||
use rusqlite::types::ToSql;
|
use rusqlite::{named_params, params};
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use std::collections::{BTreeMap, VecDeque};
|
use std::collections::{BTreeMap, VecDeque};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
@ -584,7 +584,7 @@ fn init_recordings(conn: &mut rusqlite::Connection, stream_id: i32, camera: &Cam
|
|||||||
where
|
where
|
||||||
stream_id = :stream_id
|
stream_id = :stream_id
|
||||||
"#)?;
|
"#)?;
|
||||||
let mut rows = stmt.query_named(&[(":stream_id", &stream_id)])?;
|
let mut rows = stmt.query_named(named_params!{":stream_id": stream_id})?;
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let start = recording::Time(row.get(0)?);
|
let start = recording::Time(row.get(0)?);
|
||||||
@ -681,7 +681,7 @@ impl StreamStateChanger {
|
|||||||
let mut stmt = tx.prepare_cached(r#"
|
let mut stmt = tx.prepare_cached(r#"
|
||||||
delete from stream where id = ?
|
delete from stream where id = ?
|
||||||
"#)?;
|
"#)?;
|
||||||
if stmt.execute(&[&sid])? != 1 {
|
if stmt.execute(params![sid])? != 1 {
|
||||||
bail!("missing stream {}", sid);
|
bail!("missing stream {}", sid);
|
||||||
}
|
}
|
||||||
streams.push((sid, None));
|
streams.push((sid, None));
|
||||||
@ -696,13 +696,13 @@ impl StreamStateChanger {
|
|||||||
where
|
where
|
||||||
id = :id
|
id = :id
|
||||||
"#)?;
|
"#)?;
|
||||||
let rows = stmt.execute_named(&[
|
let rows = stmt.execute_named(named_params!{
|
||||||
(":rtsp_url", &sc.rtsp_url),
|
":rtsp_url": &sc.rtsp_url,
|
||||||
(":record", &sc.record),
|
":record": sc.record,
|
||||||
(":flush_if_sec", &sc.flush_if_sec),
|
":flush_if_sec": sc.flush_if_sec,
|
||||||
(":sample_file_dir_id", &sc.sample_file_dir_id),
|
":sample_file_dir_id": sc.sample_file_dir_id,
|
||||||
(":id", &sid),
|
":id": sid,
|
||||||
])?;
|
})?;
|
||||||
if rows != 1 {
|
if rows != 1 {
|
||||||
bail!("missing stream {}", sid);
|
bail!("missing stream {}", sid);
|
||||||
}
|
}
|
||||||
@ -722,14 +722,14 @@ impl StreamStateChanger {
|
|||||||
values (:camera_id, :sample_file_dir_id, :type, :rtsp_url, :record,
|
values (:camera_id, :sample_file_dir_id, :type, :rtsp_url, :record,
|
||||||
0, :flush_if_sec, 1)
|
0, :flush_if_sec, 1)
|
||||||
"#)?;
|
"#)?;
|
||||||
stmt.execute_named(&[
|
stmt.execute_named(named_params!{
|
||||||
(":camera_id", &camera_id),
|
":camera_id": camera_id,
|
||||||
(":sample_file_dir_id", &sc.sample_file_dir_id),
|
":sample_file_dir_id": sc.sample_file_dir_id,
|
||||||
(":type", &type_.as_str()),
|
":type": type_.as_str(),
|
||||||
(":rtsp_url", &sc.rtsp_url),
|
":rtsp_url": &sc.rtsp_url,
|
||||||
(":record", &sc.record),
|
":record": sc.record,
|
||||||
(":flush_if_sec", &sc.flush_if_sec),
|
":flush_if_sec": sc.flush_if_sec,
|
||||||
])?;
|
})?;
|
||||||
let id = tx.last_insert_rowid() as i32;
|
let id = tx.last_insert_rowid() as i32;
|
||||||
sids[i] = Some(id);
|
sids[i] = Some(id);
|
||||||
let sc = mem::replace(*sc, StreamChange::default());
|
let sc = mem::replace(*sc, StreamChange::default());
|
||||||
@ -918,10 +918,10 @@ impl LockedDatabase {
|
|||||||
}
|
}
|
||||||
if s.synced_recordings > 0 {
|
if s.synced_recordings > 0 {
|
||||||
new_ranges.entry(stream_id).or_insert(None);
|
new_ranges.entry(stream_id).or_insert(None);
|
||||||
stmt.execute_named(&[
|
stmt.execute_named(named_params!{
|
||||||
(":stream_id", &stream_id),
|
":stream_id": stream_id,
|
||||||
(":next_recording_id", &(s.next_recording_id + s.synced_recordings as i32)),
|
":next_recording_id": s.next_recording_id + s.synced_recordings as i32,
|
||||||
])?;
|
})?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process deletions.
|
// Process deletions.
|
||||||
@ -955,10 +955,10 @@ impl LockedDatabase {
|
|||||||
{
|
{
|
||||||
let mut stmt = tx.prepare_cached(
|
let mut stmt = tx.prepare_cached(
|
||||||
r"update open set duration_90k = ?, end_time_90k = ? where id = ?")?;
|
r"update open set duration_90k = ?, end_time_90k = ? where id = ?")?;
|
||||||
let rows = stmt.execute(&[
|
let rows = stmt.execute(params![
|
||||||
&(recording::Time::new(clocks.monotonic()) - self.open_monotonic).0 as &dyn ToSql,
|
(recording::Time::new(clocks.monotonic()) - self.open_monotonic).0,
|
||||||
&recording::Time::new(clocks.realtime()).0,
|
recording::Time::new(clocks.realtime()).0,
|
||||||
&o.id,
|
o.id,
|
||||||
])?;
|
])?;
|
||||||
if rows != 1 {
|
if rows != 1 {
|
||||||
bail!("unable to find current open {}", o.id);
|
bail!("unable to find current open {}", o.id);
|
||||||
@ -1107,7 +1107,7 @@ impl LockedDatabase {
|
|||||||
update sample_file_dir set last_complete_open_id = ? where id = ?
|
update sample_file_dir set last_complete_open_id = ? where id = ?
|
||||||
"#)?;
|
"#)?;
|
||||||
for &id in in_progress.keys() {
|
for &id in in_progress.keys() {
|
||||||
if stmt.execute(&[&o.id as &dyn ToSql, &id])? != 1 {
|
if stmt.execute(params![o.id, id])? != 1 {
|
||||||
bail!("unable to update dir {}", id);
|
bail!("unable to update dir {}", id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1302,7 +1302,7 @@ impl LockedDatabase {
|
|||||||
}
|
}
|
||||||
trace!("cache miss for recording {}", id);
|
trace!("cache miss for recording {}", id);
|
||||||
let mut stmt = self.conn.prepare_cached(GET_RECORDING_PLAYBACK_SQL)?;
|
let mut stmt = self.conn.prepare_cached(GET_RECORDING_PLAYBACK_SQL)?;
|
||||||
let mut rows = stmt.query_named(&[(":composite_id", &id.0)])?;
|
let mut rows = stmt.query_named(named_params!{":composite_id": id.0})?;
|
||||||
if let Some(row) = rows.next()? {
|
if let Some(row) = rows.next()? {
|
||||||
let video_index: VideoIndex = row.get(0)?;
|
let video_index: VideoIndex = row.get(0)?;
|
||||||
let result = f(&RecordingPlayback { video_index: &video_index.0[..] });
|
let result = f(&RecordingPlayback { video_index: &video_index.0[..] });
|
||||||
@ -1349,7 +1349,7 @@ impl LockedDatabase {
|
|||||||
from
|
from
|
||||||
video_sample_entry
|
video_sample_entry
|
||||||
"#)?;
|
"#)?;
|
||||||
let mut rows = stmt.query(&[] as &[&dyn ToSql])?;
|
let mut rows = stmt.query(params![])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let id = row.get(0)?;
|
let id = row.get(0)?;
|
||||||
let mut sha1 = [0u8; 20];
|
let mut sha1 = [0u8; 20];
|
||||||
@ -1388,7 +1388,7 @@ impl LockedDatabase {
|
|||||||
from
|
from
|
||||||
sample_file_dir d left join open o on (d.last_complete_open_id = o.id);
|
sample_file_dir d left join open o on (d.last_complete_open_id = o.id);
|
||||||
"#)?;
|
"#)?;
|
||||||
let mut rows = stmt.query(&[] as &[&dyn ToSql])?;
|
let mut rows = stmt.query(params![])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let id = row.get(0)?;
|
let id = row.get(0)?;
|
||||||
let dir_uuid: FromSqlUuid = row.get(2)?;
|
let dir_uuid: FromSqlUuid = row.get(2)?;
|
||||||
@ -1429,7 +1429,7 @@ impl LockedDatabase {
|
|||||||
from
|
from
|
||||||
camera;
|
camera;
|
||||||
"#)?;
|
"#)?;
|
||||||
let mut rows = stmt.query(&[] as &[&dyn ToSql])?;
|
let mut rows = stmt.query(params![])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let id = row.get(0)?;
|
let id = row.get(0)?;
|
||||||
let uuid: FromSqlUuid = row.get(1)?;
|
let uuid: FromSqlUuid = row.get(1)?;
|
||||||
@ -1467,7 +1467,7 @@ impl LockedDatabase {
|
|||||||
from
|
from
|
||||||
stream;
|
stream;
|
||||||
"#)?;
|
"#)?;
|
||||||
let mut rows = stmt.query(&[] as &[&dyn ToSql])?;
|
let mut rows = stmt.query(params![])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let id = row.get(0)?;
|
let id = row.get(0)?;
|
||||||
let type_: String = row.get(1)?;
|
let type_: String = row.get(1)?;
|
||||||
@ -1530,13 +1530,13 @@ impl LockedDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut stmt = self.conn.prepare_cached(INSERT_VIDEO_SAMPLE_ENTRY_SQL)?;
|
let mut stmt = self.conn.prepare_cached(INSERT_VIDEO_SAMPLE_ENTRY_SQL)?;
|
||||||
stmt.execute_named(&[
|
stmt.execute_named(named_params!{
|
||||||
(":sha1", &&sha1_bytes[..]),
|
":sha1": &sha1_bytes[..],
|
||||||
(":width", &(width as i64)),
|
":width": i32::from(width),
|
||||||
(":height", &(height as i64)),
|
":height": i32::from(height),
|
||||||
(":rfc6381_codec", &rfc6381_codec),
|
":rfc6381_codec": &rfc6381_codec,
|
||||||
(":data", &data),
|
":data": &data,
|
||||||
])?;
|
})?;
|
||||||
|
|
||||||
let id = self.conn.last_insert_rowid() as i32;
|
let id = self.conn.last_insert_rowid() as i32;
|
||||||
self.video_sample_entries_by_id.insert(id, Arc::new(VideoSampleEntry {
|
self.video_sample_entries_by_id.insert(id, Arc::new(VideoSampleEntry {
|
||||||
@ -1572,7 +1572,7 @@ impl LockedDatabase {
|
|||||||
self.conn.execute(r#"
|
self.conn.execute(r#"
|
||||||
insert into sample_file_dir (path, uuid, last_complete_open_id)
|
insert into sample_file_dir (path, uuid, last_complete_open_id)
|
||||||
values (?, ?, ?)
|
values (?, ?, ?)
|
||||||
"#, &[&path as &dyn ToSql, &uuid_bytes, &o.id])?;
|
"#, params![&path, uuid_bytes, o.id])?;
|
||||||
let id = self.conn.last_insert_rowid() as i32;
|
let id = self.conn.last_insert_rowid() as i32;
|
||||||
use ::std::collections::btree_map::Entry;
|
use ::std::collections::btree_map::Entry;
|
||||||
let e = self.sample_file_dirs_by_id.entry(id);
|
let e = self.sample_file_dirs_by_id.entry(id);
|
||||||
@ -1624,7 +1624,7 @@ impl LockedDatabase {
|
|||||||
meta.in_progress_open = mem::replace(&mut meta.last_complete_open,
|
meta.in_progress_open = mem::replace(&mut meta.last_complete_open,
|
||||||
::protobuf::SingularPtrField::none());
|
::protobuf::SingularPtrField::none());
|
||||||
dir.write_meta(&meta)?;
|
dir.write_meta(&meta)?;
|
||||||
if self.conn.execute("delete from sample_file_dir where id = ?", &[&dir_id])? != 1 {
|
if self.conn.execute("delete from sample_file_dir where id = ?", params![dir_id])? != 1 {
|
||||||
bail!("missing database row for dir {}", dir_id);
|
bail!("missing database row for dir {}", dir_id);
|
||||||
}
|
}
|
||||||
d.remove_entry();
|
d.remove_entry();
|
||||||
@ -1645,14 +1645,14 @@ impl LockedDatabase {
|
|||||||
values (:uuid, :short_name, :description, :onvif_host, :username,
|
values (:uuid, :short_name, :description, :onvif_host, :username,
|
||||||
:password)
|
:password)
|
||||||
"#)?;
|
"#)?;
|
||||||
stmt.execute_named(&[
|
stmt.execute_named(named_params!{
|
||||||
(":uuid", &uuid_bytes),
|
":uuid": uuid_bytes,
|
||||||
(":short_name", &camera.short_name),
|
":short_name": &camera.short_name,
|
||||||
(":description", &camera.description),
|
":description": &camera.description,
|
||||||
(":onvif_host", &camera.onvif_host),
|
":onvif_host": &camera.onvif_host,
|
||||||
(":username", &camera.username),
|
":username": &camera.username,
|
||||||
(":password", &camera.password),
|
":password": &camera.password,
|
||||||
])?;
|
})?;
|
||||||
camera_id = tx.last_insert_rowid() as i32;
|
camera_id = tx.last_insert_rowid() as i32;
|
||||||
streams = StreamStateChanger::new(&tx, camera_id, None, &self.streams_by_id,
|
streams = StreamStateChanger::new(&tx, camera_id, None, &self.streams_by_id,
|
||||||
&mut camera)?;
|
&mut camera)?;
|
||||||
@ -1694,14 +1694,14 @@ impl LockedDatabase {
|
|||||||
where
|
where
|
||||||
id = :id
|
id = :id
|
||||||
"#)?;
|
"#)?;
|
||||||
let rows = stmt.execute_named(&[
|
let rows = stmt.execute_named(named_params!{
|
||||||
(":id", &camera_id),
|
":id": camera_id,
|
||||||
(":short_name", &camera.short_name),
|
":short_name": &camera.short_name,
|
||||||
(":description", &camera.description),
|
":description": &camera.description,
|
||||||
(":onvif_host", &camera.onvif_host),
|
":onvif_host": &camera.onvif_host,
|
||||||
(":username", &camera.username),
|
":username": &camera.username,
|
||||||
(":password", &camera.password),
|
":password": &camera.password,
|
||||||
])?;
|
})?;
|
||||||
if rows != 1 {
|
if rows != 1 {
|
||||||
bail!("Camera {} missing from database", camera_id);
|
bail!("Camera {} missing from database", camera_id);
|
||||||
}
|
}
|
||||||
@ -1730,14 +1730,14 @@ impl LockedDatabase {
|
|||||||
if stream.range.is_some() {
|
if stream.range.is_some() {
|
||||||
bail!("Can't remove camera {}; has recordings.", id);
|
bail!("Can't remove camera {}; has recordings.", id);
|
||||||
}
|
}
|
||||||
let rows = stream_stmt.execute_named(&[(":id", stream_id)])?;
|
let rows = stream_stmt.execute_named(named_params!{":id": stream_id})?;
|
||||||
if rows != 1 {
|
if rows != 1 {
|
||||||
bail!("Stream {} missing from database", id);
|
bail!("Stream {} missing from database", id);
|
||||||
}
|
}
|
||||||
streams_to_delete.push(*stream_id);
|
streams_to_delete.push(*stream_id);
|
||||||
}
|
}
|
||||||
let mut cam_stmt = tx.prepare_cached(r"delete from camera where id = :id")?;
|
let mut cam_stmt = tx.prepare_cached(r"delete from camera where id = :id")?;
|
||||||
let rows = cam_stmt.execute_named(&[(":id", &id)])?;
|
let rows = cam_stmt.execute_named(named_params!{":id": id})?;
|
||||||
if rows != 1 {
|
if rows != 1 {
|
||||||
bail!("Camera {} missing from database", id);
|
bail!("Camera {} missing from database", id);
|
||||||
}
|
}
|
||||||
@ -1767,11 +1767,11 @@ impl LockedDatabase {
|
|||||||
bail!("can't set limit for stream {} to {}; must be >= 0",
|
bail!("can't set limit for stream {} to {}; must be >= 0",
|
||||||
c.stream_id, c.new_limit);
|
c.stream_id, c.new_limit);
|
||||||
}
|
}
|
||||||
let rows = stmt.execute_named(&[
|
let rows = stmt.execute_named(named_params!{
|
||||||
(":record", &c.new_record),
|
":record": c.new_record,
|
||||||
(":retain", &c.new_limit),
|
":retain": c.new_limit,
|
||||||
(":id", &c.stream_id),
|
":id": c.stream_id,
|
||||||
])?;
|
})?;
|
||||||
if rows != 1 {
|
if rows != 1 {
|
||||||
bail!("no such stream {}", c.stream_id);
|
bail!("no such stream {}", c.stream_id);
|
||||||
}
|
}
|
||||||
@ -1845,15 +1845,15 @@ impl LockedDatabase {
|
|||||||
/// Note this doesn't set journal options, so that it can be used on in-memory databases for
|
/// Note this doesn't set journal options, so that it can be used on in-memory databases for
|
||||||
/// test code.
|
/// test code.
|
||||||
pub fn init(conn: &mut rusqlite::Connection) -> Result<(), Error> {
|
pub fn init(conn: &mut rusqlite::Connection) -> Result<(), Error> {
|
||||||
conn.execute("pragma foreign_keys = on", &[] as &[&dyn ToSql])?;
|
conn.execute("pragma foreign_keys = on", params![])?;
|
||||||
conn.execute("pragma fullfsync = on", &[] as &[&dyn ToSql])?;
|
conn.execute("pragma fullfsync = on", params![])?;
|
||||||
conn.execute("pragma synchronous = 2", &[] as &[&dyn ToSql])?;
|
conn.execute("pragma synchronous = 2", params![])?;
|
||||||
let tx = conn.transaction()?;
|
let tx = conn.transaction()?;
|
||||||
tx.execute_batch(include_str!("schema.sql"))?;
|
tx.execute_batch(include_str!("schema.sql"))?;
|
||||||
{
|
{
|
||||||
let uuid = ::uuid::Uuid::new_v4();
|
let uuid = ::uuid::Uuid::new_v4();
|
||||||
let uuid_bytes = &uuid.as_bytes()[..];
|
let uuid_bytes = &uuid.as_bytes()[..];
|
||||||
tx.execute("insert into meta (uuid) values (?)", &[&uuid_bytes])?;
|
tx.execute("insert into meta (uuid) values (?)", params![uuid_bytes])?;
|
||||||
}
|
}
|
||||||
tx.commit()?;
|
tx.commit()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -1866,11 +1866,11 @@ pub fn init(conn: &mut rusqlite::Connection) -> Result<(), Error> {
|
|||||||
pub fn get_schema_version(conn: &rusqlite::Connection) -> Result<Option<i32>, Error> {
|
pub fn get_schema_version(conn: &rusqlite::Connection) -> Result<Option<i32>, Error> {
|
||||||
let ver_tables: i32 = conn.query_row_and_then(
|
let ver_tables: i32 = conn.query_row_and_then(
|
||||||
"select count(*) from sqlite_master where name = 'version'",
|
"select count(*) from sqlite_master where name = 'version'",
|
||||||
&[] as &[&dyn ToSql], |row| row.get(0))?;
|
params![], |row| row.get(0))?;
|
||||||
if ver_tables == 0 {
|
if ver_tables == 0 {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
Ok(Some(conn.query_row_and_then("select max(id) from version", &[] as &[&dyn ToSql],
|
Ok(Some(conn.query_row_and_then("select max(id) from version", params![],
|
||||||
|row| row.get(0))?))
|
|row| row.get(0))?))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1908,9 +1908,9 @@ impl<C: Clocks + Clone> Database<C> {
|
|||||||
/// Creates the database from a caller-supplied SQLite connection.
|
/// Creates the database from a caller-supplied SQLite connection.
|
||||||
pub fn new(clocks: C, conn: rusqlite::Connection,
|
pub fn new(clocks: C, conn: rusqlite::Connection,
|
||||||
read_write: bool) -> Result<Database<C>, Error> {
|
read_write: bool) -> Result<Database<C>, Error> {
|
||||||
conn.execute("pragma foreign_keys = on", &[] as &[&dyn ToSql])?;
|
conn.execute("pragma foreign_keys = on", params![])?;
|
||||||
conn.execute("pragma fullfsync = on", &[] as &[&dyn ToSql])?;
|
conn.execute("pragma fullfsync = on", params![])?;
|
||||||
conn.execute("pragma synchronous = 2", &[] as &[&dyn ToSql])?;
|
conn.execute("pragma synchronous = 2", params![])?;
|
||||||
{
|
{
|
||||||
let ver = get_schema_version(&conn)?.ok_or_else(|| format_err!(
|
let ver = get_schema_version(&conn)?.ok_or_else(|| format_err!(
|
||||||
"no such table: version. \
|
"no such table: version. \
|
||||||
@ -1940,7 +1940,7 @@ impl<C: Clocks + Clone> Database<C> {
|
|||||||
let mut stmt = conn.prepare(" insert into open (uuid, start_time_90k) values (?, ?)")?;
|
let mut stmt = conn.prepare(" insert into open (uuid, start_time_90k) values (?, ?)")?;
|
||||||
let uuid = Uuid::new_v4();
|
let uuid = Uuid::new_v4();
|
||||||
let uuid_bytes = &uuid.as_bytes()[..];
|
let uuid_bytes = &uuid.as_bytes()[..];
|
||||||
stmt.execute(&[&uuid_bytes as &dyn ToSql, &real.0])?;
|
stmt.execute(params![uuid_bytes, real.0])?;
|
||||||
Some(Open {
|
Some(Open {
|
||||||
id: conn.last_insert_rowid() as u32,
|
id: conn.last_insert_rowid() as u32,
|
||||||
uuid,
|
uuid,
|
||||||
|
97
db/raw.rs
97
db/raw.rs
@ -34,7 +34,7 @@ use crate::db::{self, CompositeId, FromSqlUuid};
|
|||||||
use failure::{Error, ResultExt, bail};
|
use failure::{Error, ResultExt, bail};
|
||||||
use fnv::FnvHashSet;
|
use fnv::FnvHashSet;
|
||||||
use crate::recording;
|
use crate::recording;
|
||||||
use rusqlite::types::ToSql;
|
use rusqlite::{named_params, params};
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@ -125,10 +125,11 @@ pub(crate) fn list_recordings_by_time(
|
|||||||
conn: &rusqlite::Connection, stream_id: i32, desired_time: Range<recording::Time>,
|
conn: &rusqlite::Connection, stream_id: i32, desired_time: Range<recording::Time>,
|
||||||
f: &mut dyn FnMut(db::ListRecordingsRow) -> Result<(), Error>) -> Result<(), Error> {
|
f: &mut dyn FnMut(db::ListRecordingsRow) -> Result<(), Error>) -> Result<(), Error> {
|
||||||
let mut stmt = conn.prepare_cached(LIST_RECORDINGS_BY_TIME_SQL)?;
|
let mut stmt = conn.prepare_cached(LIST_RECORDINGS_BY_TIME_SQL)?;
|
||||||
let rows = stmt.query_named(&[
|
let rows = stmt.query_named(named_params!{
|
||||||
(":stream_id", &stream_id),
|
":stream_id": stream_id,
|
||||||
(":start_time_90k", &desired_time.start.0),
|
":start_time_90k": desired_time.start.0,
|
||||||
(":end_time_90k", &desired_time.end.0)])?;
|
":end_time_90k": desired_time.end.0,
|
||||||
|
})?;
|
||||||
list_recordings_inner(rows, f)
|
list_recordings_inner(rows, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,10 +138,10 @@ pub(crate) fn list_recordings_by_id(
|
|||||||
conn: &rusqlite::Connection, stream_id: i32, desired_ids: Range<i32>,
|
conn: &rusqlite::Connection, stream_id: i32, desired_ids: Range<i32>,
|
||||||
f: &mut dyn FnMut(db::ListRecordingsRow) -> Result<(), Error>) -> Result<(), Error> {
|
f: &mut dyn FnMut(db::ListRecordingsRow) -> Result<(), Error>) -> Result<(), Error> {
|
||||||
let mut stmt = conn.prepare_cached(LIST_RECORDINGS_BY_ID_SQL)?;
|
let mut stmt = conn.prepare_cached(LIST_RECORDINGS_BY_ID_SQL)?;
|
||||||
let rows = stmt.query_named(&[
|
let rows = stmt.query_named(named_params!{
|
||||||
(":start", &CompositeId::new(stream_id, desired_ids.start).0),
|
":start": CompositeId::new(stream_id, desired_ids.start).0,
|
||||||
(":end", &CompositeId::new(stream_id, desired_ids.end).0),
|
":end": CompositeId::new(stream_id, desired_ids.end).0,
|
||||||
])?;
|
})?;
|
||||||
list_recordings_inner(rows, f)
|
list_recordings_inner(rows, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,7 +166,7 @@ fn list_recordings_inner(mut rows: rusqlite::Rows,
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_db_uuid(conn: &rusqlite::Connection) -> Result<Uuid, Error> {
|
pub(crate) fn get_db_uuid(conn: &rusqlite::Connection) -> Result<Uuid, Error> {
|
||||||
Ok(conn.query_row("select uuid from meta", &[] as &[&dyn ToSql], |row| -> rusqlite::Result<Uuid> {
|
Ok(conn.query_row("select uuid from meta", params![], |row| -> rusqlite::Result<Uuid> {
|
||||||
let uuid: FromSqlUuid = row.get(0)?;
|
let uuid: FromSqlUuid = row.get(0)?;
|
||||||
Ok(uuid.0)
|
Ok(uuid.0)
|
||||||
})?)
|
})?)
|
||||||
@ -183,19 +184,19 @@ pub(crate) fn insert_recording(tx: &rusqlite::Transaction, o: &db::Open, id: Com
|
|||||||
:video_samples, :video_sync_samples,
|
:video_samples, :video_sync_samples,
|
||||||
:video_sample_entry_id)
|
:video_sample_entry_id)
|
||||||
"#).with_context(|e| format!("can't prepare recording insert: {}", e))?;
|
"#).with_context(|e| format!("can't prepare recording insert: {}", e))?;
|
||||||
stmt.execute_named(&[
|
stmt.execute_named(named_params!{
|
||||||
(":composite_id", &id.0),
|
":composite_id": id.0,
|
||||||
(":stream_id", &(id.stream() as i64)),
|
":stream_id": i64::from(id.stream()),
|
||||||
(":open_id", &o.id),
|
":open_id": o.id,
|
||||||
(":run_offset", &r.run_offset),
|
":run_offset": r.run_offset,
|
||||||
(":flags", &r.flags),
|
":flags": r.flags,
|
||||||
(":sample_file_bytes", &r.sample_file_bytes),
|
":sample_file_bytes": r.sample_file_bytes,
|
||||||
(":start_time_90k", &r.start.0),
|
":start_time_90k": r.start.0,
|
||||||
(":duration_90k", &r.duration_90k),
|
":duration_90k": r.duration_90k,
|
||||||
(":video_samples", &r.video_samples),
|
":video_samples": r.video_samples,
|
||||||
(":video_sync_samples", &r.video_sync_samples),
|
":video_sync_samples": r.video_sync_samples,
|
||||||
(":video_sample_entry_id", &r.video_sample_entry_id),
|
":video_sample_entry_id": r.video_sample_entry_id,
|
||||||
]).with_context(|e| format!("unable to insert recording for recording {} {:#?}: {}",
|
}).with_context(|e| format!("unable to insert recording for recording {} {:#?}: {}",
|
||||||
id, r, e))?;
|
id, r, e))?;
|
||||||
|
|
||||||
let mut stmt = tx.prepare_cached(r#"
|
let mut stmt = tx.prepare_cached(r#"
|
||||||
@ -207,20 +208,20 @@ pub(crate) fn insert_recording(tx: &rusqlite::Transaction, o: &db::Open, id: Com
|
|||||||
0 => None,
|
0 => None,
|
||||||
_ => Some(r.local_time_delta.0),
|
_ => Some(r.local_time_delta.0),
|
||||||
};
|
};
|
||||||
stmt.execute_named(&[
|
stmt.execute_named(named_params!{
|
||||||
(":composite_id", &id.0),
|
":composite_id": id.0,
|
||||||
(":local_time_delta_90k", &delta),
|
":local_time_delta_90k": delta,
|
||||||
(":sample_file_sha1", &sha1),
|
":sample_file_sha1": sha1,
|
||||||
]).with_context(|e| format!("unable to insert recording_integrity for {:#?}: {}", r, e))?;
|
}).with_context(|e| format!("unable to insert recording_integrity for {:#?}: {}", r, e))?;
|
||||||
|
|
||||||
let mut stmt = tx.prepare_cached(r#"
|
let mut stmt = tx.prepare_cached(r#"
|
||||||
insert into recording_playback (composite_id, video_index)
|
insert into recording_playback (composite_id, video_index)
|
||||||
values (:composite_id, :video_index)
|
values (:composite_id, :video_index)
|
||||||
"#).with_context(|e| format!("can't prepare recording_playback insert: {}", e))?;
|
"#).with_context(|e| format!("can't prepare recording_playback insert: {}", e))?;
|
||||||
stmt.execute_named(&[
|
stmt.execute_named(named_params!{
|
||||||
(":composite_id", &id.0),
|
":composite_id": id.0,
|
||||||
(":video_index", &r.video_index),
|
":video_index": &r.video_index,
|
||||||
]).with_context(|e| format!("unable to insert recording_playback for {:#?}: {}", r, e))?;
|
}).with_context(|e| format!("unable to insert recording_playback for {:#?}: {}", r, e))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -261,15 +262,15 @@ pub(crate) fn delete_recordings(tx: &rusqlite::Transaction, sample_file_dir_id:
|
|||||||
:start <= composite_id and
|
:start <= composite_id and
|
||||||
composite_id < :end
|
composite_id < :end
|
||||||
"#)?;
|
"#)?;
|
||||||
let n = insert.execute_named(&[
|
let n = insert.execute_named(named_params!{
|
||||||
(":sample_file_dir_id", &sample_file_dir_id),
|
":sample_file_dir_id": sample_file_dir_id,
|
||||||
(":start", &ids.start.0),
|
":start": ids.start.0,
|
||||||
(":end", &ids.end.0),
|
":end": ids.end.0,
|
||||||
])?;
|
})?;
|
||||||
let p: &[(&str, &dyn rusqlite::types::ToSql)] = &[
|
let p = named_params!{
|
||||||
(":start", &ids.start.0),
|
":start": ids.start.0,
|
||||||
(":end", &ids.end.0),
|
":end": ids.end.0,
|
||||||
];
|
};
|
||||||
let n1 = del1.execute_named(p)?;
|
let n1 = del1.execute_named(p)?;
|
||||||
if n1 != n {
|
if n1 != n {
|
||||||
bail!("inserted {} garbage rows but deleted {} recording_playback rows!", n, n1);
|
bail!("inserted {} garbage rows but deleted {} recording_playback rows!", n, n1);
|
||||||
@ -292,7 +293,7 @@ pub(crate) fn mark_sample_files_deleted(tx: &rusqlite::Transaction, ids: &[Compo
|
|||||||
if ids.is_empty() { return Ok(()); }
|
if ids.is_empty() { return Ok(()); }
|
||||||
let mut stmt = tx.prepare_cached("delete from garbage where composite_id = ?")?;
|
let mut stmt = tx.prepare_cached("delete from garbage where composite_id = ?")?;
|
||||||
for &id in ids {
|
for &id in ids {
|
||||||
let changes = stmt.execute(&[&id.0])?;
|
let changes = stmt.execute(params![id.0])?;
|
||||||
if changes != 1 {
|
if changes != 1 {
|
||||||
// panic rather than return error. Errors get retried indefinitely, but there's no
|
// panic rather than return error. Errors get retried indefinitely, but there's no
|
||||||
// recovery from this condition.
|
// recovery from this condition.
|
||||||
@ -311,7 +312,7 @@ pub(crate) fn get_range(conn: &rusqlite::Connection, stream_id: i32)
|
|||||||
-> Result<Option<Range<recording::Time>>, Error> {
|
-> Result<Option<Range<recording::Time>>, Error> {
|
||||||
// The minimum is straightforward, taking advantage of the start_time_90k index.
|
// The minimum is straightforward, taking advantage of the start_time_90k index.
|
||||||
let mut stmt = conn.prepare_cached(STREAM_MIN_START_SQL)?;
|
let mut stmt = conn.prepare_cached(STREAM_MIN_START_SQL)?;
|
||||||
let mut rows = stmt.query_named(&[(":stream_id", &stream_id)])?;
|
let mut rows = stmt.query_named(named_params!{":stream_id": stream_id})?;
|
||||||
let min_start = match rows.next()? {
|
let min_start = match rows.next()? {
|
||||||
Some(row) => recording::Time(row.get(0)?),
|
Some(row) => recording::Time(row.get(0)?),
|
||||||
None => return Ok(None),
|
None => return Ok(None),
|
||||||
@ -322,7 +323,7 @@ pub(crate) fn get_range(conn: &rusqlite::Connection, stream_id: i32)
|
|||||||
// last MAX_RECORDING_DURATION must be examined in order to take advantage of the
|
// last MAX_RECORDING_DURATION must be examined in order to take advantage of the
|
||||||
// start_time_90k index.
|
// start_time_90k index.
|
||||||
let mut stmt = conn.prepare_cached(STREAM_MAX_START_SQL)?;
|
let mut stmt = conn.prepare_cached(STREAM_MAX_START_SQL)?;
|
||||||
let mut rows = stmt.query_named(&[(":stream_id", &stream_id)])?;
|
let mut rows = stmt.query_named(named_params!{":stream_id": stream_id})?;
|
||||||
let mut maxes_opt = None;
|
let mut maxes_opt = None;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let row_start = recording::Time(row.get(0)?);
|
let row_start = recording::Time(row.get(0)?);
|
||||||
@ -363,10 +364,10 @@ pub(crate) fn list_oldest_recordings(conn: &rusqlite::Connection, start: Composi
|
|||||||
f: &mut dyn FnMut(db::ListOldestRecordingsRow) -> bool)
|
f: &mut dyn FnMut(db::ListOldestRecordingsRow) -> bool)
|
||||||
-> Result<(), Error> {
|
-> Result<(), Error> {
|
||||||
let mut stmt = conn.prepare_cached(LIST_OLDEST_RECORDINGS_SQL)?;
|
let mut stmt = conn.prepare_cached(LIST_OLDEST_RECORDINGS_SQL)?;
|
||||||
let mut rows = stmt.query_named(&[
|
let mut rows = stmt.query_named(named_params!{
|
||||||
(":start", &start.0),
|
":start": start.0,
|
||||||
(":end", &CompositeId::new(start.stream() + 1, 0).0),
|
":end": CompositeId::new(start.stream() + 1, 0).0,
|
||||||
])?;
|
})?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let should_continue = f(db::ListOldestRecordingsRow {
|
let should_continue = f(db::ListOldestRecordingsRow {
|
||||||
id: CompositeId(row.get(0)?),
|
id: CompositeId(row.get(0)?),
|
||||||
|
@ -537,7 +537,7 @@ impl State {
|
|||||||
])?;
|
])?;
|
||||||
},
|
},
|
||||||
Entry::Vacant(_) => {
|
Entry::Vacant(_) => {
|
||||||
d_stmt.execute(&[t.0])?;
|
d_stmt.execute(params![t.0])?;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,7 @@ fn upgrade(args: &Args, target_ver: i32, conn: &mut rusqlite::Connection) -> Res
|
|||||||
tx.execute(r#"
|
tx.execute(r#"
|
||||||
insert into version (id, unix_time, notes)
|
insert into version (id, unix_time, notes)
|
||||||
values (?, cast(strftime('%s', 'now') as int32), ?)
|
values (?, cast(strftime('%s', 'now') as int32), ?)
|
||||||
"#, params![&(ver + 1), &UPGRADE_NOTES])?;
|
"#, params![ver + 1, UPGRADE_NOTES])?;
|
||||||
tx.commit()?;
|
tx.commit()?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ use failure::{Error, bail, format_err};
|
|||||||
use nix::fcntl::{FlockArg, OFlag};
|
use nix::fcntl::{FlockArg, OFlag};
|
||||||
use nix::sys::stat::Mode;
|
use nix::sys::stat::Mode;
|
||||||
use protobuf::prelude::MessageField;
|
use protobuf::prelude::MessageField;
|
||||||
use rusqlite::types::ToSql;
|
use rusqlite::params;
|
||||||
use crate::schema::DirMeta;
|
use crate::schema::DirMeta;
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@ -103,10 +103,10 @@ pub fn run(args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error>
|
|||||||
"#)?;
|
"#)?;
|
||||||
let db_uuid = ::uuid::Uuid::new_v4();
|
let db_uuid = ::uuid::Uuid::new_v4();
|
||||||
let db_uuid_bytes = &db_uuid.as_bytes()[..];
|
let db_uuid_bytes = &db_uuid.as_bytes()[..];
|
||||||
tx.execute("insert into meta (uuid) values (?)", &[&db_uuid_bytes])?;
|
tx.execute("insert into meta (uuid) values (?)", params![db_uuid_bytes])?;
|
||||||
let open_uuid = ::uuid::Uuid::new_v4();
|
let open_uuid = ::uuid::Uuid::new_v4();
|
||||||
let open_uuid_bytes = &open_uuid.as_bytes()[..];
|
let open_uuid_bytes = &open_uuid.as_bytes()[..];
|
||||||
tx.execute("insert into open (uuid) values (?)", &[&open_uuid_bytes])?;
|
tx.execute("insert into open (uuid) values (?)", params![open_uuid_bytes])?;
|
||||||
let open_id = tx.last_insert_rowid() as u32;
|
let open_id = tx.last_insert_rowid() as u32;
|
||||||
let dir_uuid = ::uuid::Uuid::new_v4();
|
let dir_uuid = ::uuid::Uuid::new_v4();
|
||||||
let dir_uuid_bytes = &dir_uuid.as_bytes()[..];
|
let dir_uuid_bytes = &dir_uuid.as_bytes()[..];
|
||||||
@ -125,7 +125,7 @@ pub fn run(args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error>
|
|||||||
tx.execute(r#"
|
tx.execute(r#"
|
||||||
insert into sample_file_dir (path, uuid, last_complete_open_id)
|
insert into sample_file_dir (path, uuid, last_complete_open_id)
|
||||||
values (?, ?, ?)
|
values (?, ?, ?)
|
||||||
"#, &[&sample_file_path as &dyn ToSql, &dir_uuid_bytes, &open_id])?;
|
"#, params![sample_file_path, dir_uuid_bytes, open_id])?;
|
||||||
|
|
||||||
tx.execute_batch(r#"
|
tx.execute_batch(r#"
|
||||||
drop table reserved_sample_files;
|
drop table reserved_sample_files;
|
||||||
@ -302,7 +302,7 @@ fn verify_dir_contents(sample_file_path: &str, dir: &mut nix::dir::Dir,
|
|||||||
from
|
from
|
||||||
(select count(*) as c from recording) a,
|
(select count(*) as c from recording) a,
|
||||||
(select count(*) as c from reserved_sample_files) b;
|
(select count(*) as c from reserved_sample_files) b;
|
||||||
"#, &[] as &[&dyn ToSql], |r| r.get(0))?;
|
"#, params![], |r| r.get(0))?;
|
||||||
let mut files = ::fnv::FnvHashSet::with_capacity_and_hasher(n as usize, Default::default());
|
let mut files = ::fnv::FnvHashSet::with_capacity_and_hasher(n as usize, Default::default());
|
||||||
for e in dir.iter() {
|
for e in dir.iter() {
|
||||||
let e = e?;
|
let e = e?;
|
||||||
@ -333,7 +333,7 @@ fn verify_dir_contents(sample_file_path: &str, dir: &mut nix::dir::Dir,
|
|||||||
// Iterate through the database and check that everything has a matching file.
|
// Iterate through the database and check that everything has a matching file.
|
||||||
{
|
{
|
||||||
let mut stmt = tx.prepare(r"select sample_file_uuid from recording_playback")?;
|
let mut stmt = tx.prepare(r"select sample_file_uuid from recording_playback")?;
|
||||||
let mut rows = stmt.query(&[] as &[&dyn ToSql])?;
|
let mut rows = stmt.query(params![])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let uuid: crate::db::FromSqlUuid = row.get(0)?;
|
let uuid: crate::db::FromSqlUuid = row.get(0)?;
|
||||||
if !files.remove(&uuid.0) {
|
if !files.remove(&uuid.0) {
|
||||||
@ -343,7 +343,7 @@ fn verify_dir_contents(sample_file_path: &str, dir: &mut nix::dir::Dir,
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut stmt = tx.prepare(r"select uuid from reserved_sample_files")?;
|
let mut stmt = tx.prepare(r"select uuid from reserved_sample_files")?;
|
||||||
let mut rows = stmt.query(&[] as &[&dyn ToSql])?;
|
let mut rows = stmt.query(params![])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let uuid: crate::db::FromSqlUuid = row.get(0)?;
|
let uuid: crate::db::FromSqlUuid = row.get(0)?;
|
||||||
if files.remove(&uuid.0) {
|
if files.remove(&uuid.0) {
|
||||||
@ -379,7 +379,7 @@ fn fix_video_sample_entry(tx: &rusqlite::Transaction) -> Result<(), Error> {
|
|||||||
let mut insert = tx.prepare(r#"
|
let mut insert = tx.prepare(r#"
|
||||||
insert into video_sample_entry values (:id, :sha1, :width, :height, :rfc6381_codec, :data)
|
insert into video_sample_entry values (:id, :sha1, :width, :height, :rfc6381_codec, :data)
|
||||||
"#)?;
|
"#)?;
|
||||||
let mut rows = select.query(&[] as &[&dyn ToSql])?;
|
let mut rows = select.query(params![])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let data: Vec<u8> = row.get(4)?;
|
let data: Vec<u8> = row.get(4)?;
|
||||||
insert.execute_named(&[
|
insert.execute_named(&[
|
||||||
|
@ -37,7 +37,7 @@ use crate::dir;
|
|||||||
use failure::Error;
|
use failure::Error;
|
||||||
use crate::schema;
|
use crate::schema;
|
||||||
use protobuf::prelude::MessageField;
|
use protobuf::prelude::MessageField;
|
||||||
use rusqlite::types::ToSql;
|
use rusqlite::params;
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ fn open_sample_file_dir(tx: &rusqlite::Transaction) -> Result<Arc<dir::SampleFil
|
|||||||
sample_file_dir s
|
sample_file_dir s
|
||||||
join open o on (s.last_complete_open_id = o.id)
|
join open o on (s.last_complete_open_id = o.id)
|
||||||
cross join meta m
|
cross join meta m
|
||||||
"#, &[] as &[&dyn ToSql], |row| {
|
"#, params![], |row| {
|
||||||
Ok((row.get(0)?,
|
Ok((row.get(0)?,
|
||||||
row.get(1)?,
|
row.get(1)?,
|
||||||
row.get(2)?,
|
row.get(2)?,
|
||||||
@ -82,7 +82,7 @@ pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error>
|
|||||||
from
|
from
|
||||||
recording_playback
|
recording_playback
|
||||||
"#)?;
|
"#)?;
|
||||||
let mut rows = stmt.query(&[] as &[&dyn ToSql])?;
|
let mut rows = stmt.query(params![])?;
|
||||||
while let Some(row) = rows.next()? {
|
while let Some(row) = rows.next()? {
|
||||||
let id = db::CompositeId(row.get(0)?);
|
let id = db::CompositeId(row.get(0)?);
|
||||||
let sample_file_uuid: FromSqlUuid = row.get(1)?;
|
let sample_file_uuid: FromSqlUuid = row.get(1)?;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user