Use queryparams for server queries. Fixes #44.

This commit is contained in:
Dessalines 2019-02-07 08:32:06 -08:00
parent d956011969
commit 90329ebae3
1 changed files with 15 additions and 23 deletions

View File

@ -10,7 +10,7 @@ use actix_web::{fs, fs::NamedFile, http, server, App, HttpRequest, HttpResponse,
use std::env; use std::env;
use std::ops::Deref; use std::ops::Deref;
use rusqlite::{Connection, NO_PARAMS}; use rusqlite::{Connection};
fn main() { fn main() {
println!("Access me at http://localhost:8080"); println!("Access me at http://localhost:8080");
@ -87,18 +87,15 @@ struct Torrent {
} }
fn torrent_search(query: &str, size: usize, offset: usize) -> Vec<Torrent> { fn torrent_search(query: &str, size: usize, offset: usize) -> Vec<Torrent> {
let stmt_str = format!( let stmt_str = "select * from torrents where name like '%' || ?1 || '%' limit ?2 offset ?3";
"select * from torrents where name like '%{}%' limit {} offset {}",
query.replace(" ", "%").replace("\'","''"),
size,
offset
);
let conn = Connection::open(torrents_db_file()).unwrap(); let conn = Connection::open(torrents_db_file()).unwrap();
let mut stmt = conn.prepare(&stmt_str).unwrap(); let mut stmt = conn.prepare(&stmt_str).unwrap();
let torrent_iter = stmt let torrent_iter = stmt
.query_map(NO_PARAMS, |row| Torrent { .query_map(&[
query.replace(" ", "%"),
size.to_string(),
offset.to_string(),
], |row| Torrent {
infohash: row.get(0), infohash: row.get(0),
name: row.get(1), name: row.get(1),
size_bytes: row.get(2), size_bytes: row.get(2),
@ -107,8 +104,7 @@ fn torrent_search(query: &str, size: usize, offset: usize) -> Vec<Torrent> {
leechers: row.get(5), leechers: row.get(5),
completed: row.get(6), completed: row.get(6),
scraped_date: row.get(7), scraped_date: row.get(7),
}) }).unwrap();
.unwrap();
let mut torrents = Vec::new(); let mut torrents = Vec::new();
for torrent in torrent_iter { for torrent in torrent_iter {
@ -131,18 +127,15 @@ struct File {
} }
fn torrent_file_search(query: &str, size: usize, offset: usize) -> Vec<File> { fn torrent_file_search(query: &str, size: usize, offset: usize) -> Vec<File> {
let stmt_str = format!( let stmt_str = "select * from files where path like '%' || ?1 || '%' limit ?2 offset ?3";
"select * from files where path like '%{}%' limit {} offset {}",
query.replace(" ", "%").replace("\'","''"),
size,
offset
);
let conn = Connection::open(torrents_db_file()).unwrap(); let conn = Connection::open(torrents_db_file()).unwrap();
let mut stmt = conn.prepare(&stmt_str).unwrap(); let mut stmt = conn.prepare(&stmt_str).unwrap();
let file_iter = stmt let file_iter = stmt
.query_map(NO_PARAMS, |row| File { .query_map(&[
query.replace(" ", "%"),
size.to_string(),
offset.to_string(),
], |row| File {
infohash: row.get(0), infohash: row.get(0),
index_: row.get(1), index_: row.get(1),
path: row.get(2), path: row.get(2),
@ -152,8 +145,7 @@ fn torrent_file_search(query: &str, size: usize, offset: usize) -> Vec<File> {
leechers: row.get(6), leechers: row.get(6),
completed: row.get(7), completed: row.get(7),
scraped_date: row.get(8), scraped_date: row.get(8),
}) }).unwrap();
.unwrap();
let mut files = Vec::new(); let mut files = Vec::new();
for file in file_iter { for file in file_iter {