--- parser3/src/classes/table.C 2002/09/17 08:44:45 1.161 +++ parser3/src/classes/table.C 2002/09/17 14:34:53 1.164 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_TABLE_C="$Date: 2002/09/17 08:44:45 $"; +static const char* IDENT_TABLE_C="$Date: 2002/09/17 14:34:53 $"; #include "classes.h" #include "pa_common.h" @@ -42,7 +42,9 @@ static void get_copy_options(Request& r, Value& voptions=params->as_no_junction(param_index, "options must be hash, not code"); if(!voptions.is_string()) { if(Hash *options=voptions.get_hash(&method_name)) { - if(Value *voffset=(Value *)options->get(*sql_offset_name)) + int valid_options=0; + if(Value *voffset=(Value *)options->get(*sql_offset_name)) { + valid_options++; if(voffset->is_string()) { const String& soffset=*voffset->get_string(); if(soffset == "cur") @@ -53,8 +55,15 @@ static void get_copy_options(Request& r, "must be 'cur' string or expression"); } else offset=r.process_to_value(*voffset).as_int(); - if(Value *vlimit=(Value *)options->get(*sql_limit_name)) + } + if(Value *vlimit=(Value *)options->get(*sql_limit_name)) { + valid_options++; limit=r.process_to_value(*vlimit).as_int(); + } + if(valid_options!=options->size()) + throw Exception("parser.runtime", + &method_name, + "called with invalid option"); } else throw Exception("parser.runtime", &method_name, @@ -299,6 +308,7 @@ struct Row_info { int key_field; Array *value_fields; Hash *hash; + bool distinct; }; #endif static void table_row_to_hash(Array::Item *value, void *info) { @@ -317,7 +327,12 @@ static void table_row_to_hash(Array::Ite new(pool) VString(*row.get_string(value_field))); } - ri.hash->put(*row.get_string(ri.key_field), &result); + const String& key=*row.get_string(ri.key_field); + if(ri.hash->put_dont_replace(key, &result)) // put. existed? + if(!ri.distinct) + throw Exception("parser.runtime", + &key, + "duplicate key"); } } static void _hash(Request& r, const String& method_name, MethodParams *params) { @@ -330,9 +345,29 @@ static void _hash(Request& r, const Stri "key field name must not be code").as_string(); int key_field=self_table.column_name2index(key_field_name, true); + bool distinct=false; + int param_index=params->size()-1; + if(Hash *options= + params->as_no_junction(param_index, "param must not be code").get_hash(0)) { + --param_index; + int valid_options=0; + if(Value *vdistinct=(Value *)options->get(*sql_distinct_name)) { + valid_options++; + distinct=r.process_to_value(*vdistinct).as_bool(); + } + if(valid_options!=options->size()) + throw Exception("parser.runtime", + &method_name, + "called with invalid option"); + } + if(param_index==2) // bad options param type + throw Exception("parser.runtime", + &method_name, + "options must be hash"); + Array value_fields(pool); - if(params->size()>1) { - Value& value_fields_param=params->as_no_junction(1, "value field(s) must not be code"); + if(param_index>0) { + Value& value_fields_param=params->as_no_junction(param_index, "value field(s) must not be code"); if(value_fields_param.is_string()) { value_fields+=self_table.column_name2index(value_fields_param.as_string(), true); } else if(Table *value_fields_table=value_fields_param.get_table()) { @@ -352,7 +387,7 @@ static void _hash(Request& r, const Stri } // integers: key_field & value_fields - Row_info row_info={&self_table, key_field, &value_fields, result.get_hash(0)}; + Row_info row_info={&self_table, key_field, &value_fields, result.get_hash(0), distinct}; self_table.for_each(table_row_to_hash, &row_info); } r.write_no_lang(result); @@ -600,10 +635,19 @@ static void _sql(Request& r, const Strin Value& voptions=params->as_no_junction(1, "options must be hash, not code"); if(!voptions.is_string()) if(Hash *options=voptions.get_hash(&method_name)) { - if(Value *vlimit=(Value *)options->get(*sql_limit_name)) + int valid_options=0; + if(Value *vlimit=(Value *)options->get(*sql_limit_name)) { + valid_options++; limit=(ulong)r.process_to_value(*vlimit).as_double(); - if(Value *voffset=(Value *)options->get(*sql_offset_name)) + } + if(Value *voffset=(Value *)options->get(*sql_offset_name)) { + valid_options++; offset=(ulong)r.process_to_value(*voffset).as_double(); + } + if(valid_options!=options->size()) + throw Exception("parser.runtime", + &method_name, + "called with invalid option"); } else throw Exception("parser.runtime", &method_name, @@ -728,7 +772,7 @@ MTable::MTable(Pool& apool) : Methoded(a // ^table:hash[key field name] // ^table:hash[key field name][value field name(s) string/table] - add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 2); + add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 3); // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[desc|asc] // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[desc|asc]