@@ -115,6 +115,18 @@ impl Category {
115
115
116
116
Ok ( invalid_categories)
117
117
}
118
+
119
+ pub fn count_toplevel ( conn : & GenericConnection ) -> CargoResult < i64 > {
120
+ let sql = format ! ( "\
121
+ SELECT COUNT(*) \
122
+ FROM {} \
123
+ WHERE category NOT LIKE '%::%'",
124
+ Model :: table_name( None :: <Self >
125
+ ) ) ;
126
+ let stmt = try!( conn. prepare ( & sql) ) ;
127
+ let rows = try!( stmt. query ( & [ ] ) ) ;
128
+ Ok ( rows. iter ( ) . next ( ) . unwrap ( ) . get ( "count" ) )
129
+ }
118
130
}
119
131
120
132
impl Model for Category {
@@ -141,10 +153,13 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
141
153
_ => "ORDER BY category ASC" ,
142
154
} ;
143
155
144
- // Collect all the categories
145
- let stmt = try!( conn. prepare ( & format ! ( "SELECT * FROM categories {} \
146
- LIMIT $1 OFFSET $2",
147
- sort_sql) ) ) ;
156
+ // Collect all the top-level categories
157
+ let stmt = try!( conn. prepare ( & format ! (
158
+ "SELECT * FROM categories \
159
+ WHERE category NOT LIKE '%::%' {} \
160
+ LIMIT $1 OFFSET $2",
161
+ sort_sql
162
+ ) ) ) ;
148
163
149
164
let categories: Vec < _ > = try!( stmt. query ( & [ & limit, & offset] ) )
150
165
. iter ( )
@@ -155,7 +170,7 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
155
170
. collect ( ) ;
156
171
157
172
// Query for the total count of categories
158
- let total = try!( Category :: count ( conn) ) ;
173
+ let total = try!( Category :: count_toplevel ( conn) ) ;
159
174
160
175
#[ derive( RustcEncodable ) ]
161
176
struct R { categories : Vec < EncodableCategory > , meta : Meta }
0 commit comments