Annotation of parser3/src/types/pa_vtable.C, revision 1.34
1.1 parser 1: /** @file
2: Parser: @b table class.
3:
1.28 misha 4: Copyright(c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)
1.11 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.14 paf 6: */
1.1 parser 7:
1.34 ! misha 8: static const char * const IDENT_VTABLE_C="$Date: 2010-09-16 23:33:52 $";
1.1 parser 9:
10: #include "pa_vtable.h"
11: #include "pa_vstring.h"
1.5 parser 12: #include "pa_vhash.h"
1.22 paf 13: #include "pa_vvoid.h"
1.1 parser 14:
1.3 parser 15: #ifndef DOXYGEN
1.1 parser 16: struct Record_info {
1.22 paf 17: Table* table;
18: HashStringValue* hash;
1.1 parser 19: };
1.3 parser 20: #endif
1.29 misha 21:
22: static void store_column_item_to_hash(const String* column_name, Record_info *info) {
23: const String* column_item=info->table->item(*column_name);
24: info->hash->put(*column_name,
25: (column_item && !column_item->is_empty())
26: ?new VString(*column_item)
27: :new VString()
28: );
1.1 parser 29: }
1.29 misha 30:
1.22 paf 31: Value* VTable::fields_element() {
1.29 misha 32: Value& result=*new VHash;
1.22 paf 33: Table& ltable=table();
1.29 misha 34: if(!ltable.count())
35: return &result;
36:
37: HashStringValue* hash=result.get_hash();
38:
39: if(Table::columns_type columns=ltable.columns()) { // named
40: Record_info record_info={<able, hash};
1.1 parser 41: columns->for_each(store_column_item_to_hash, &record_info);
1.29 misha 42: } else { // nameless
43: size_t row_size=ltable[ltable.current()]->count(); // number of columns in current row
44: for(size_t index=0; index<row_size; index++){
45: const String* column_item=ltable.item(index);
46: hash->put(String::Body::Format(index),
47: (column_item && !column_item->is_empty())
48: ?new VString(*column_item)
49: :new VString()
50: );
51: }
1.1 parser 52: }
1.29 misha 53:
54: return &result;
1.1 parser 55: }
56:
1.31 misha 57: Value* VTable::get_element(const String& aname) {
1.1 parser 58: // fields
1.16 paf 59: if(aname==TABLE_FIELDS_ELEMENT_NAME)
1.1 parser 60: return fields_element();
61:
1.2 parser 62: // methods
1.31 misha 63: if(Value* result=VStateless_object::get_element(aname))
1.2 parser 64: return result;
65:
1.1 parser 66: // columns
67: if(ftable) {
1.16 paf 68: int index=ftable->column_name2index(aname, false);
69: if(index>=0) // column aname|number valid
1.32 pretende 70: {
71: const String* string=ftable->item(index); // there is such column
72: return new VString(string ? *string : String::Empty);
73: }
1.1 parser 74: }
75:
1.27 misha 76: throw Exception(PARSER_RUNTIME,
1.16 paf 77: &aname,
1.1 parser 78: "column not found");
79: }
1.33 misha 80:
81: const String* VTable::get_json_string(Json_options* options) {
82: String& result = *new String("[");
83: Table& ltable=table();
84: if(options && options->table == Json_options::T_ARRAY){
85: // [
86: // ["c1", "c2", "c3" ...] || null (for nameless),
87: // ["v11", "v12", "v13" ...],
88: // ["v21", "v22", "v23" ...],
89: // ...
90: // ]
91:
92: // columns
93: if(ltable.columns()){
94: // named
1.34 ! misha 95: result << "\n[\"";
1.33 misha 96: bool need_delim=false;
97: for(Array_iterator<const String*> c(*ltable.columns()); c.has_next(); ) {
98: if(need_delim)
99: result << "\",\"";
100: result.append(*c.next(), String::L_JSON, true/*forced lang*/);
101: need_delim=true;
102: }
103: result << "\"]";
104: } else // nameless
1.34 ! misha 105: result << "\nnull";
1.33 misha 106:
107: // data
108: if(ltable.count()){
109: result << ",";
110: for(Array_iterator<ArrayString*> r(ltable); r.has_next(); ) {
1.34 ! misha 111: result << "\n[\"";
1.33 misha 112: bool need_delim=false;
113: for(Array_iterator<const String*> c(*r.next()); c.has_next(); ) {
114: if(need_delim)
115: result << "\",\"";
116: result.append(*c.next(), String::L_JSON, true/*forced lang*/);
117: need_delim=true;
118: }
119: result << (r.has_next() ? "\"]," : "\"]");
120: }
121: }
1.34 ! misha 122: result << "\n";
1.33 misha 123: } else {
124: // [
125: // {"c1":"v11", "c2":"v12", "c3":"v13"},
126: // {"c1":"v21", "c2":"v22", "c3":"v23"},
127: // ...
128: // ]
129: ArrayString* columns=ltable.columns();
130: size_t columns_count = (columns) ? columns->count() : 0;
131:
132: for(Array_iterator<ArrayString*> r(ltable); r.has_next(); ) {
133: ArrayString* row=r.next();
134:
1.34 ! misha 135: result << "\n{\"";
1.33 misha 136: for(size_t index=0; index<row->count(); index++){
137: if(index)
138: result << "\",\"";
139: result.append(index < columns_count ? *columns->get(index) : String(format(index, 0)), String::L_JSON, true/*forced lang*/);
140: result << "\":\"";
141: result.append(*row->get(index), String::L_JSON, true/*forced lang*/);
142: }
143: result << "\"}";
144:
1.34 ! misha 145: result << ((r.has_next()) ? "," : "\n");
1.33 misha 146: }
147: }
148:
149: result << "]";
150: return &result;
151: }
E-mail: