Skip to content

Instantly share code, notes, and snippets.

@Thiez
Last active December 16, 2015 12:59
Show Gist options
  • Save Thiez/5438985 to your computer and use it in GitHub Desktop.
Save Thiez/5438985 to your computer and use it in GitHub Desktop.
struct Car {
color: ~str,
miles: int,
}
impl Car {
fn new() -> ~Car { ~Car{color: ~"none", miles: 0,} }
}
macro_rules! simple_eq(( $ T : ident -> $ l : ident , $ r : expr ) =>
(
{ let temp = $ T :: new ( ) ; & temp . $ l == & temp .
$ l ; fmt ! ( "%s == %s" , stringify ! ( $ l ) , $ r )
} ) ;)
macro_rules! simple_neq(( $ T : ident -> $ l : ident , $ r : expr ) =>
(
{ let temp = $ T :: new ( ) ; & temp . $ l == & temp .
$ l ; fmt ! ( "%s != %s" , stringify ! ( $ l ) , $ r )
} ) ;)
pub struct Query {
filters: ~[~str],
}
impl Query {
fn new() -> Query { Query{filters: ~[],} }
fn Where(self, new_filter: ~str) -> Query {
let mut self = self;
self.filters.push(new_filter);
self
}
fn Execute() -> int {
// return row_count for now
0
}
fn ToString(&self) -> ~str {
use core::str::{append};
if self.filters.len() > 0 {
let res = append(~"SELECT FROM [FIX] WHERE ",self.filters[0]);
do self.filters.slice(1,self.filters.len()).foldr(res) |q,s| {
append(append(s, " AND "),*q)
}
} else {
~"SELECT FROM [FIX]"
}
}
}
fn main() {
let mut cars = Query::new();
let mut res =
cars.Where(simple_eq!(Car -> color ,
"red")).Where(simple_neq!(Car -> color ,
"blue")).ToString();
io::println(res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment