| | @@ -2680,24 +2680,24 @@ |
| 2680 | 2680 | cson_double_t i = 0.0; |
| 2681 | 2681 | cson_value_fetch_double( val, &i ); |
| 2682 | 2682 | return i; |
| 2683 | 2683 | } |
| 2684 | 2684 | |
| 2685 | | -int cson_value_fetch_string( cson_value const * val, cson_string const ** dest ) |
| 2685 | +int cson_value_fetch_string( cson_value const * val, cson_string ** dest ) |
| 2686 | 2686 | { |
| 2687 | 2687 | if( ! val || ! dest ) return cson_rc.ArgError; |
| 2688 | 2688 | else if( ! cson_value_is_string(val) ) return cson_rc.TypeError; |
| 2689 | 2689 | else |
| 2690 | 2690 | { |
| 2691 | | - if( dest ) *dest = (cson_string const *)val->value; |
| 2691 | + if( dest ) *dest = CSON_STR(val); |
| 2692 | 2692 | return 0; |
| 2693 | 2693 | } |
| 2694 | 2694 | } |
| 2695 | 2695 | |
| 2696 | | -cson_string const * cson_value_get_string( cson_value const * val ) |
| 2696 | +cson_string * cson_value_get_string( cson_value const * val ) |
| 2697 | 2697 | { |
| 2698 | | - cson_string const * rc = NULL; |
| 2698 | + cson_string * rc = NULL; |
| 2699 | 2699 | cson_value_fetch_string( val, &rc ); |
| 2700 | 2700 | return rc; |
| 2701 | 2701 | } |
| 2702 | 2702 | |
| 2703 | 2703 | char const * cson_value_get_cstr( cson_value const * val ) |
| | @@ -5145,13 +5145,62 @@ |
| 5145 | 5145 | cson_value_free(aryV); |
| 5146 | 5146 | return NULL; |
| 5147 | 5147 | } |
| 5148 | 5148 | } |
| 5149 | 5149 | |
| 5150 | + |
| 5151 | +cson_value * cson_sqlite3_row_to_object2( sqlite3_stmt * st, |
| 5152 | + cson_array * colNames ) |
| 5153 | +{ |
| 5154 | + cson_value * rootV = NULL; |
| 5155 | + cson_object * root = NULL; |
| 5156 | + cson_string * colName = NULL; |
| 5157 | + int i = 0; |
| 5158 | + int rc = 0; |
| 5159 | + cson_value * currentValue = NULL; |
| 5160 | + int const colCount = sqlite3_column_count(st); |
| 5161 | + if( !colCount || (colCount>cson_array_length_get(colNames)) ) { |
| 5162 | + return NULL; |
| 5163 | + } |
| 5164 | + rootV = cson_value_new_object(); |
| 5165 | + if(!rootV) return NULL; |
| 5166 | + root = cson_value_get_object(rootV); |
| 5167 | + for( i = 0; i < colCount; ++i ) |
| 5168 | + { |
| 5169 | + colName = cson_value_get_string( cson_array_get( colNames, i ) ); |
| 5170 | + if( ! colName ) goto error; |
| 5171 | + currentValue = cson_sqlite3_column_to_value(st,i); |
| 5172 | + if( ! currentValue ) currentValue = cson_value_null(); |
| 5173 | + rc = cson_object_set_s( root, colName, currentValue ); |
| 5174 | + if( 0 != rc ) |
| 5175 | + { |
| 5176 | + cson_value_free( currentValue ); |
| 5177 | + goto error; |
| 5178 | + } |
| 5179 | + } |
| 5180 | + goto end; |
| 5181 | + error: |
| 5182 | + cson_value_free( rootV ); |
| 5183 | + rootV = NULL; |
| 5184 | + end: |
| 5185 | + return rootV; |
| 5186 | +} |
| 5187 | + |
| 5150 | 5188 | |
| 5151 | 5189 | cson_value * cson_sqlite3_row_to_object( sqlite3_stmt * st ) |
| 5152 | 5190 | { |
| 5191 | +#if 0 |
| 5192 | + cson_value * arV = cson_sqlite3_column_names(st); |
| 5193 | + cson_array * ar = NULL; |
| 5194 | + cson_value * rc = NULL; |
| 5195 | + if(!arV) return NULL; |
| 5196 | + ar = cson_value_get_array(arV); |
| 5197 | + assert( NULL != ar ); |
| 5198 | + rc = cson_sqlite3_row_to_object2(st, ar); |
| 5199 | + cson_value_free(arV); |
| 5200 | + return rc; |
| 5201 | +#else |
| 5153 | 5202 | cson_value * rootV = NULL; |
| 5154 | 5203 | cson_object * root = NULL; |
| 5155 | 5204 | char const * colName = NULL; |
| 5156 | 5205 | int i = 0; |
| 5157 | 5206 | int rc = 0; |
| | @@ -5178,10 +5227,11 @@ |
| 5178 | 5227 | error: |
| 5179 | 5228 | cson_value_free( rootV ); |
| 5180 | 5229 | rootV = NULL; |
| 5181 | 5230 | end: |
| 5182 | 5231 | return rootV; |
| 5232 | +#endif |
| 5183 | 5233 | } |
| 5184 | 5234 | |
| 5185 | 5235 | cson_value * cson_sqlite3_row_to_array( sqlite3_stmt * st ) |
| 5186 | 5236 | { |
| 5187 | 5237 | cson_value * aryV = NULL; |
| | @@ -5226,10 +5276,11 @@ |
| 5226 | 5276 | else |
| 5227 | 5277 | { |
| 5228 | 5278 | cson_value * rootV = NULL; |
| 5229 | 5279 | cson_object * root = NULL; |
| 5230 | 5280 | cson_value * colsV = NULL; |
| 5281 | + cson_array * cols = NULL; |
| 5231 | 5282 | cson_value * rowsV = NULL; |
| 5232 | 5283 | cson_array * rows = NULL; |
| 5233 | 5284 | cson_value * objV = NULL; |
| 5234 | 5285 | int rc = 0; |
| 5235 | 5286 | int const colCount = sqlite3_column_count(st); |
| | @@ -5240,18 +5291,19 @@ |
| 5240 | 5291 | if( ! colsV ) |
| 5241 | 5292 | { |
| 5242 | 5293 | cson_value_free( rootV ); |
| 5243 | 5294 | RETURN(cson_rc.AllocError); |
| 5244 | 5295 | } |
| 5296 | + cols = cson_value_get_array(colsV); |
| 5297 | + assert(NULL != cols); |
| 5245 | 5298 | root = cson_value_get_object(rootV); |
| 5246 | 5299 | rc = cson_object_set( root, "columns", colsV ); |
| 5247 | 5300 | if( rc ) |
| 5248 | 5301 | { |
| 5249 | 5302 | cson_value_free( colsV ); |
| 5250 | 5303 | RETURN(rc); |
| 5251 | 5304 | } |
| 5252 | | - colsV = NULL; |
| 5253 | 5305 | rowsV = cson_value_new_array(); |
| 5254 | 5306 | if( ! rowsV ) RETURN(cson_rc.AllocError); |
| 5255 | 5307 | rc = cson_object_set( root, "rows", rowsV ); |
| 5256 | 5308 | if( rc ) |
| 5257 | 5309 | { |
| | @@ -5260,11 +5312,11 @@ |
| 5260 | 5312 | } |
| 5261 | 5313 | rows = cson_value_get_array(rowsV); |
| 5262 | 5314 | assert(rows); |
| 5263 | 5315 | while( SQLITE_ROW == sqlite3_step(st) ) |
| 5264 | 5316 | { |
| 5265 | | - objV = cson_sqlite3_row_to_object(st); |
| 5317 | + objV = cson_sqlite3_row_to_object2(st, cols); |
| 5266 | 5318 | if( ! objV ) RETURN(cson_rc.UnknownError); |
| 5267 | 5319 | rc = cson_array_append( rows, objV ); |
| 5268 | 5320 | if( rc ) |
| 5269 | 5321 | { |
| 5270 | 5322 | cson_value_free( objV ); |
| 5271 | 5323 | |