--- parser3/src/classes/hash.C 2024/09/13 04:01:22 1.158 +++ parser3/src/classes/hash.C 2024/09/28 02:05:19 1.162 @@ -17,7 +17,7 @@ #include "pa_vbool.h" #include "pa_vmethod_frame.h" -volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.158 2024/09/13 04:01:22 moko Exp $"; +volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.162 2024/09/28 02:05:19 moko Exp $"; // class @@ -38,20 +38,19 @@ DECLARE_CLASS_VAR(hash, new MHash); #ifndef DOXYGEN class Hash_sql_event_handlers: public SQL_Driver_query_event_handlers { bool distinct; - HashStringValue& rows_hash; + HashStringValue& result; Value* row_value; int column_index; ArrayString& columns; bool one_bool_column; - static VBool only_one_column_value; Table2hash_value_type value_type; int columns_count; public: Table* empty; public: - Hash_sql_event_handlers(bool adistinct, HashStringValue& arows_hash, Table2hash_value_type avalue_type): + Hash_sql_event_handlers(bool adistinct, HashStringValue& aresult, Table2hash_value_type avalue_type): distinct(adistinct), - rows_hash(arows_hash), + result(aresult), row_value(0), column_index(0), columns(*new ArrayString), @@ -75,20 +74,22 @@ public: error=SQL_Error("no columns"); return true; } - switch(value_type){ - case C_STRING: { - if(columns.count()>2){ - error=SQL_Error("only 2 columns allowed for $.type[string]."); - return true; + if(columns.count()==1) { + one_bool_column=true; + } else { + switch(value_type){ + case C_STRING: { + if(columns.count()>2){ + error=SQL_Error("only 2 columns allowed for $.type[string]."); + return true; + } + break; + } + case C_TABLE: { + // create empty table which we'll copy later + empty=new Table(&columns); + columns_count=columns.count(); } - } - case C_TABLE: { - // create empty table which we'll copy later - empty=new Table(&columns); - columns_count=columns.count(); - } - case C_HASH: { - one_bool_column=columns.count()==1; } } return false; @@ -105,42 +106,39 @@ public: bool duplicate=false; if(one_bool_column) { - duplicate=rows_hash.put_dont_replace(cell, &only_one_column_value); // put. existed? + duplicate=result.put_dont_replace(cell, &VBool::get(true)); // put. existed? } else if(column_index==0) { switch(value_type){ case C_HASH: { VHash* row_vhash=new VHash; row_value=row_vhash; - duplicate=rows_hash.put_dont_replace(cell, row_vhash); // put. existed? + duplicate=result.put_dont_replace(cell, row_vhash); // put. existed? break; } case C_STRING: { VString* row_vstring=new VString(); row_value=row_vstring; - duplicate=rows_hash.put_dont_replace(cell, row_vstring); // put. existed? + duplicate=result.put_dont_replace(cell, row_vstring); // put. existed? break; } case C_TABLE: { - VTable* vtable=(VTable*)rows_hash.get(cell); - Table* table; + VTable* vtable=(VTable*)result.get(cell); if(vtable) { // table with this key exist? if(!distinct) { duplicate=true; break; } - table=vtable->get_table(); } else { // no? creating table of same structure as source Table::Action_options table_options(0, 0); - table=new Table(*empty, table_options/*no rows, just structure*/); - vtable=new VTable(table); - rows_hash.put(cell, vtable); // put + vtable=new VTable(new Table(*empty, table_options/*no rows, just structure*/)); + result.put(cell, vtable); // put } ArrayString* row=new ArrayString(columns_count); row_value=(Value*)row; *row+=&cell; - *table+=row; + *vtable->get_table()+=row; break; } } @@ -177,7 +175,6 @@ public: } }; -VBool Hash_sql_event_handlers::only_one_column_value(true); #endif @@ -317,25 +314,25 @@ static void _sql(Request& r, MethodParam if(params.count()>1) if(HashStringValue* options=params.as_hash(1, "sql options")) { int valid_options=0; - if(Value* vbind=options->get(sql_bind_name)) { - valid_options++; - bind=vbind->get_hash(); - } - if(Value* vlimit=options->get(sql_limit_name)) { - valid_options++; - limit=(ulong)r.process(*vlimit).as_double(); - } - if(Value* voffset=options->get(sql_offset_name)) { - valid_options++; - offset=(ulong)r.process(*voffset).as_double(); - } - if(Value* vdistinct=options->get(sql_distinct_name)) { - valid_options++; - distinct=r.process(*vdistinct).as_bool(); - } - if(Value* vvalue_type=options->get(sql_value_type_name)) { - valid_options++; - value_type=get_value_type(r.process(*vvalue_type)); + for(HashStringValue::Iterator i(*options); i; i.next() ){ + String::Body key=i.key(); + Value* value=i.value(); + if(key == sql_bind_name) { + bind=value->get_hash(); + valid_options++; + } else if(key == sql_limit_name) { + limit=(ulong)r.process(*value).as_double(); + valid_options++; + } else if(key == sql_offset_name) { + offset=(ulong)r.process(*value).as_double(); + valid_options++; + } else if (key == sql_distinct_name) { + distinct=r.process(*value).as_bool(); + valid_options++; + } else if (key == sql_value_type_name) { + value_type=get_value_type(r.process(*value)); + valid_options++; + } } if(valid_options!=options->count()) throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);