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

1.1       parser      1: /** @file
                      2:        Parser: @b table class.
                      3: 
1.43    ! moko        4:        Copyright (c) 2001-2015 Art. Lebedev Studio (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: 
                      8: #include "pa_vtable.h"
                      9: #include "pa_vstring.h"
1.5       parser     10: #include "pa_vhash.h"
1.22      paf        11: #include "pa_vvoid.h"
1.1       parser     12: 
1.43    ! moko       13: volatile const char * IDENT_PA_VTABLE_C="$Id: pa_vtable.C,v 1.42 2015/10/15 18:32:03 moko Exp $" IDENT_PA_VTABLE_H;
1.39      moko       14: 
                     15: // limits
                     16: #define MAX_COLUMNS 20000 // equal to MAX_LOOPS
1.37      moko       17: 
1.40      moko       18: const String table_fields_name(TABLE_FIELDS_ELEMENT_NAME);
                     19: 
1.3       parser     20: #ifndef DOXYGEN
1.1       parser     21: struct Record_info {
1.22      paf        22:        Table* table;
                     23:        HashStringValue* hash;
1.1       parser     24: };
1.3       parser     25: #endif
1.29      misha      26: 
                     27: static void store_column_item_to_hash(const String* column_name, Record_info *info) {
                     28:        const String* column_item=info->table->item(*column_name);
                     29:        info->hash->put(*column_name, 
                     30:                (column_item && !column_item->is_empty())
                     31:                        ?new VString(*column_item)
                     32:                        :new VString()
                     33:        );
1.1       parser     34: }
1.29      misha      35: 
1.22      paf        36: Value* VTable::fields_element() {
1.29      misha      37:        Value& result=*new VHash;
1.22      paf        38:        Table& ltable=table();
1.29      misha      39:        if(!ltable.count())
                     40:                return &result;
                     41: 
                     42:        HashStringValue* hash=result.get_hash();
                     43: 
                     44:        if(Table::columns_type columns=ltable.columns()) { // named
                     45:                Record_info record_info={&ltable, hash};
1.1       parser     46:                columns->for_each(store_column_item_to_hash, &record_info);
1.29      misha      47:        } else { // nameless
                     48:                size_t row_size=ltable[ltable.current()]->count(); // number of columns in current row
                     49:                for(size_t index=0; index<row_size; index++){
                     50:                        const String* column_item=ltable.item(index);
                     51:                        hash->put(String::Body::Format(index), 
                     52:                                (column_item && !column_item->is_empty())
                     53:                                        ?new VString(*column_item)
                     54:                                        :new VString()
                     55:                        );
                     56:                }
1.1       parser     57:        }
1.29      misha      58: 
                     59:        return &result;
1.1       parser     60: }
                     61: 
1.31      misha      62: Value* VTable::get_element(const String& aname) {
1.40      moko       63: #ifdef FEATURE_GET_ELEMENT4CALL
1.1       parser     64:        // fields
1.40      moko       65:        if(aname==table_fields_name)
1.1       parser     66:                return fields_element();
                     67: 
1.40      moko       68:        // columns first
                     69:        if(ftable) {
                     70:                int index=ftable->column_name2index(aname, false);
                     71:                if(index>=0) // column aname|number valid
                     72:                {
                     73:                        const String* string=ftable->item(index); // there is such column
                     74:                        return new VString(string ? *string : String::Empty);
                     75:                }
                     76:        }
                     77: 
1.42      moko       78: #ifndef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
1.2       parser     79:        // methods
1.31      misha      80:        if(Value* result=VStateless_object::get_element(aname))
1.2       parser     81:                return result;
1.42      moko       82: #endif
1.2       parser     83: 
1.40      moko       84:        throw Exception(PARSER_RUNTIME, &aname, "column not found");
                     85: }
                     86: 
                     87: Value* VTable::get_element4call(const String& aname) {
                     88: #endif
                     89:        // fields
                     90:        if(aname==table_fields_name)
                     91:                return fields_element();
                     92: 
                     93:        // methods first
                     94:        if(Value* result=VStateless_object::get_element(aname))
                     95:                return result;
                     96: 
1.1       parser     97:        // columns
                     98:        if(ftable) {
1.16      paf        99:                int index=ftable->column_name2index(aname, false);
                    100:                if(index>=0) // column aname|number valid
1.32      pretende  101:                {
                    102:                        const String* string=ftable->item(index); // there is such column
                    103:                        return new VString(string ? *string : String::Empty);
                    104:                }
1.1       parser    105:        }
                    106: 
1.39      moko      107:        throw Exception(PARSER_RUNTIME, &aname, "column not found");
1.1       parser    108: }
1.33      misha     109: 
1.39      moko      110: const VJunction* VTable::put_element(const String& aname, Value* avalue) {
                    111:        if(ftable) {
                    112:                int index=ftable->column_name2index(aname, false);
                    113:                if(index>=0) // column aname|number valid
                    114:                {
                    115:                        if(index > MAX_COLUMNS)
                    116:                                throw Exception(PARSER_RUNTIME, &aname, "too big column number");
                    117:                        if(!avalue->is_string())
                    118:                                throw Exception(PARSER_RUNTIME, 0, "column value must be string");
                    119:                        ftable->put_item(index, avalue->get_string());
                    120:                        return PUT_ELEMENT_REPLACED_ELEMENT;
                    121:                }
                    122:        }
                    123: 
                    124:        throw Exception(PARSER_RUNTIME, &aname, "column not found");
                    125: }
1.36      moko      126: 
                    127: String& VTable::get_json_string_array(String& result, const char *indent) {
                    128:        // [
                    129:        //              ["c1",  "c2",  "c3"  ...] || null (for nameless),
                    130:        //              ["v11", "v12", "v13" ...],
                    131:        //              ["v21", "v22", "v23" ...],
                    132:        //              ...
                    133:        // ]
1.33      misha     134:        Table& ltable=table();
1.35      moko      135: 
1.36      moko      136:        // columns
                    137:        if(ltable.columns()){
                    138:                // named
                    139:                indent ? result << "\n\t" << indent << "[\"" : result << "\n[\"";
                    140: 
                    141:                bool need_delim=false;
                    142:                for(Array_iterator<const String*> c(*ltable.columns()); c.has_next(); ) {
                    143:                        if(need_delim)
                    144:                                result << "\",\"";
                    145:                        result.append(*c.next(), String::L_JSON, true/*forced lang*/);
                    146:                        need_delim=true;
                    147:                }
                    148:                result << "\"]";
                    149:        } else {
                    150:                // nameless
                    151:                indent ? result << "\n\t" << indent << "null" : result << "\nnull";
                    152:        }
                    153: 
                    154:        // data
                    155:        if(ltable.count()){
                    156:                result << ",";
                    157:                for(Array_iterator<ArrayString*> r(ltable); r.has_next(); ) {
                    158:                        indent ? result << "\n\t" << indent << "[\"" : result << "\n[\"";
1.33      misha     159:                        bool need_delim=false;
1.36      moko      160:                        for(Array_iterator<const String*> c(*r.next()); c.has_next(); ) {
1.33      misha     161:                                if(need_delim)
                    162:                                        result << "\",\"";
                    163:                                result.append(*c.next(), String::L_JSON, true/*forced lang*/);
                    164:                                need_delim=true;
                    165:                        }
1.36      moko      166:                        r.has_next() ? result << "\"]," : result << "\"]";
1.35      moko      167:                }
1.36      moko      168:        }
                    169: 
                    170:        result << "\n" << indent; 
                    171:        return result;
                    172: }
                    173: 
                    174: String& VTable::get_json_string_object(String& result, const char *indent) {
                    175:        // [
                    176:        //              {"c1":"v11", "c2":"v12", "c3":"v13"},
                    177:        //              {"c1":"v21", "c2":"v22", "c3":"v23"},
                    178:        //              ...
                    179:        // ]
                    180:        Table& ltable=table();
                    181:        ArrayString* columns=ltable.columns();
                    182:        size_t columns_count = (columns) ? columns->count() : 0;
                    183: 
                    184:        for(Array_iterator<ArrayString*> r(ltable); r.has_next(); ) {
                    185:                indent ? result << "\n\t" << indent << "{\"" : result << "\n{\"";
1.33      misha     186: 
1.36      moko      187:                ArrayString* row=r.next();
                    188:                for(size_t index=0; index<row->count(); index++){
                    189:                        if(index)
                    190:                                result << "\",\"";
                    191:                        result.append(index < columns_count ? *columns->get(index) : String(format(index, 0)), String::L_JSON, true/*forced lang*/);
                    192:                        result << "\":\"";
                    193:                        result.append(*row->get(index), String::L_JSON, true/*forced lang*/);
1.33      misha     194:                }
1.36      moko      195:                r.has_next() ? result << "\"}," : result << "\"}\n" << indent;
                    196:        }
                    197:        return result;
                    198: }
                    199: 
                    200: String& VTable::get_json_string_compact(String& result, const char *indent) {
                    201:        // [
                    202:        //              "v11",
                    203:        //              ["v21", "v22", "v23" ...],
                    204:        //              ...
                    205:        // ]
                    206:        Table& ltable=table();
                    207: 
                    208:        for(Array_iterator<ArrayString*> r(ltable); r.has_next(); ) {
                    209:                ArrayString& line=*r.next();
                    210:                if (line.count()==1){
                    211:                        indent ? result << "\n\t" << indent << "\"" : result << "\n\"";
1.33      misha     212: 
1.36      moko      213:                        result.append(*line[0], String::L_JSON, true/*forced lang*/);
                    214:                        r.has_next() ? result << "\"," : result << "\"\n" << indent;
                    215:                } else {
                    216:                        indent ? result << "\n\t" << indent << "[\"" :  result << "\n[\"";
1.33      misha     217: 
1.36      moko      218:                        bool need_delim=false;
                    219:                        for(Array_iterator<const String*> c(line); c.has_next(); ) {
                    220:                                if(need_delim)
1.33      misha     221:                                        result << "\",\"";
1.36      moko      222:                                result.append(*c.next(), String::L_JSON, true/*forced lang*/);
                    223:                                need_delim=true;
1.33      misha     224:                        }
1.36      moko      225:                        r.has_next() ? result << "\"]," : result  << "\"]\n" << indent;
                    226:                }
                    227:        }
                    228:        return result;
                    229: }
                    230: 
1.38      moko      231: const String* VTable::get_json_string(Json_options& options) {
1.36      moko      232:        String* result = new String("[", String::L_AS_IS);
1.33      misha     233: 
1.38      moko      234:        switch(options.table){
1.36      moko      235:        case Json_options::T_ARRAY:
1.38      moko      236:                result=&get_json_string_array(*result, options.indent);
1.36      moko      237:                break;
                    238:        case Json_options::T_OBJECT:
1.38      moko      239:                result=&get_json_string_object(*result, options.indent);
1.36      moko      240:                break;
                    241:        case Json_options::T_COMPACT:
1.38      moko      242:                result=&get_json_string_compact(*result, options.indent);
1.36      moko      243:                break;
1.33      misha     244:        }
                    245: 
1.36      moko      246:        *result << "]";
                    247:        return result;
                    248: }

E-mail: