88impl Glue {
99 /// Only for `SELECT` queries.
1010 ///
11- /// Output is one big [String] of [JSON](https://json.org) , wrapped in a [Result] in case it fails .
11+ /// Output is one big [serde_json::Value] , wrapped in a [Result].
1212 ///
1313 /// Generally useful for webby interactions.
14- pub fn select_as_json ( & mut self , query : & str ) -> Result < String > {
14+ pub fn select_json ( & mut self , query : & str ) -> Result < JSONValue > {
1515 // TODO: Make this more efficient and not affect database if not select by converting earlier
1616 if let Payload :: Select { labels, rows } = self . execute ( query) ? {
17- let array = JSONValue :: Array (
17+ let rows = JSONValue :: Array (
1818 rows. into_iter ( )
1919 . map ( |row| {
2020 JSONValue :: Object (
@@ -27,49 +27,12 @@ impl Glue {
2727 } )
2828 . collect ( ) ,
2929 ) ;
30- Ok ( array . to_string ( ) )
30+ Ok ( rows )
3131 } else {
3232 Err ( ExecuteError :: QueryNotSupported . into ( ) )
3333 }
3434 }
3535
36- /// Only for `SELECT` queries.
37- ///
38- /// Output is one big [String] of [JSON](https://json.org), failures will be converted to json of `{error: [error]}`.
39- ///
40- /// Generally useful for webby interactions.
41- pub fn select_as_json_with_headers ( & mut self , query : & str ) -> String {
42- // TODO: Make this more efficient and not affect database if not select by converting earlier
43- let mut result = || -> Result < _ > {
44- if let Payload :: Select { labels, rows } = self . execute ( query) ? {
45- let array = JSONValue :: Array (
46- rows. into_iter ( )
47- . map ( |row| {
48- JSONValue :: Object (
49- row. 0
50- . into_iter ( )
51- . enumerate ( )
52- . map ( |( index, cell) | ( labels[ index] . clone ( ) , cell. into ( ) ) )
53- . collect :: < serde_json:: map:: Map < String , JSONValue > > ( ) ,
54- )
55- } )
56- . collect ( ) ,
57- ) ;
58- Ok ( json ! ( {
59- "labels" : JSONValue :: from( labels) ,
60- "data" : array
61- } ) )
62- } else {
63- Err ( ExecuteError :: QueryNotSupported . into ( ) )
64- }
65- } ;
66- match result ( ) {
67- Ok ( result) => result,
68- Err ( error) => json ! ( { "error" : error. to_string( ) } ) ,
69- }
70- . to_string ( )
71- }
72-
7336 /// Only for `SELECT` queries.
7437 pub fn select_as_string ( & mut self , query : & str ) -> Result < Vec < Vec < String > > > {
7538 // TODO: Make this more efficient and not affect database
0 commit comments