Annotation of parser3/src/types/pa_vtable.C, revision 1.35

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.35    ! moko        8: static const char * const IDENT_VTABLE_C="$Date: 2010-09-20 02:24:32 $";
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={&ltable, 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) {
1.35    ! moko       82:        String& result = *new String("[", String::L_AS_IS);
1.33      misha      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.35    ! moko       95:                        if (options->indent){
        !            96:                                result << "\n\t" << options->indent << "[\"";
        !            97:                        } else {
        !            98:                                result << "\n[\"";
        !            99:                        }
        !           100: 
1.33      misha     101:                        bool need_delim=false;
                    102:                        for(Array_iterator<const String*> c(*ltable.columns()); c.has_next(); ) {
                    103:                                if(need_delim)
                    104:                                        result << "\",\"";
                    105:                                result.append(*c.next(), String::L_JSON, true/*forced lang*/);
                    106:                                need_delim=true;
                    107:                        }
                    108:                        result << "\"]";
1.35    ! moko      109:                } else {
        !           110:                        // nameless
        !           111:                        if (options->indent){
        !           112:                                result << "\n\t" << options->indent << "null";
        !           113:                        } else {
        !           114:                                result << "\nnull";
        !           115:                        }
        !           116:                }
1.33      misha     117: 
                    118:                // data
                    119:                if(ltable.count()){
                    120:                        result << ",";
                    121:                        for(Array_iterator<ArrayString*> r(ltable); r.has_next(); ) {
1.35    ! moko      122:                                if (options->indent){
        !           123:                                        result << "\n\t" << options->indent << "[\"";
        !           124:                                } else {
        !           125:                                        result << "\n[\"";
        !           126:                                }
1.33      misha     127:                                bool need_delim=false;
                    128:                                for(Array_iterator<const String*> c(*r.next()); c.has_next(); ) {
                    129:                                        if(need_delim)
                    130:                                                result << "\",\"";
                    131:                                        result.append(*c.next(), String::L_JSON, true/*forced lang*/);
                    132:                                        need_delim=true;
                    133:                                }
                    134:                                result << (r.has_next() ? "\"]," : "\"]");
                    135:                        }
                    136:                }
1.34      misha     137:                result << "\n";
1.35    ! moko      138:                result << options->indent;
1.33      misha     139:        } else {
                    140:                // [
                    141:                //              {"c1":"v11", "c2":"v12", "c3":"v13"},
                    142:                //              {"c1":"v21", "c2":"v22", "c3":"v23"},
                    143:                //              ...
                    144:                // ]
                    145:                ArrayString* columns=ltable.columns();
                    146:                size_t columns_count = (columns) ? columns->count() : 0;
                    147: 
                    148:                for(Array_iterator<ArrayString*> r(ltable); r.has_next(); ) {
                    149:                        ArrayString* row=r.next();
                    150: 
1.35    ! moko      151:                        if (options->indent){
        !           152:                                result << "\n\t" << options->indent << "{\"";
        !           153:                        } else {
        !           154:                                result << "\n{\"";
        !           155:                        }
1.33      misha     156:                        for(size_t index=0; index<row->count(); index++){
                    157:                                if(index)
                    158:                                        result << "\",\"";
                    159:                                result.append(index < columns_count ? *columns->get(index) : String(format(index, 0)), String::L_JSON, true/*forced lang*/);
                    160:                                result << "\":\"";
                    161:                                result.append(*row->get(index), String::L_JSON, true/*forced lang*/);
                    162:                        }
                    163:                        result << "\"}";
                    164: 
1.35    ! moko      165:                        if (r.has_next()){
        !           166:                                result << ",";
        !           167:                        } else {
        !           168:                                result << "\n";
        !           169:                                result << options->indent;
        !           170:                        }
1.33      misha     171:                }
                    172:        }
                    173: 
                    174:        result << "]";
                    175:        return &result;
                    176: }

E-mail: