diff --git a/contracts/simpledb/simpledb.abi b/contracts/simpledb/simpledb.abi index 8ecd086d03df1c7cb6cc7767ca974d9d26bc6c6f..2279d7a256871368e4be73acc6346302529eb134 100644 --- a/contracts/simpledb/simpledb.abi +++ b/contracts/simpledb/simpledb.abi @@ -23,11 +23,25 @@ } }, { - "name": "KeyValuePair", + "name": "KeyValue1", "fields": { "key": "String", "value": "String" } + }, + { + "name": "ComplexType", + "fields": { + "name": "String", + "age": "UInt64" + } + }, + { + "name": "KeyValue2", + "fields": { + "key": "String", + "value": "ComplexType" + } } ], "actions": [ @@ -36,8 +50,8 @@ "type": "record1" }, { - "action": "insertalpha", - "type": "KeyValuePair" + "action": "insertkv1", + "type": "KeyValue1" }, { "action": "insert2", @@ -46,6 +60,10 @@ { "action": "insert3", "type": "record3" + }, + { + "action": "insertkv2", + "type": "KeyValue2" } ], "tables": [ @@ -89,9 +107,20 @@ ] }, { - "table": "record1alpha", - "type": "KeyValuePair", - "indextype": "i64", + "table": "keyvalue1", + "type": "KeyValue1", + "indextype": "str", + "keynames": [ + "key" + ], + "keytypes": [ + "String" + ] + }, + { + "table": "keyvalue2", + "type": "KeyValue2", + "indextype": "str", "keynames": [ "key" ], diff --git a/contracts/simpledb/simpledb.cpp b/contracts/simpledb/simpledb.cpp index fede3c250a51f172a9f8b8b7198d140016c0a963..487ae5c3a0a81827041438182dd993baa0e9096f 100644 --- a/contracts/simpledb/simpledb.cpp +++ b/contracts/simpledb/simpledb.cpp @@ -14,12 +14,30 @@ extern "C" { str[str_len] = x; } - //void printhex( char* data, uint32_t datalen ); - void apply( uint64_t code, uint64_t action ) { if( code == N(simpledb) ) { - if( action == N(insertalpha) ) { + if( action == N(insertkv1) ) { + // eosc push message simpledb insertkv1 '{"key":"a", "value":"aa"}' -S simpledb + // eosc get table simpledb simpledb keyvalue1 + char tmp[512]; + auto len = readMessage(tmp, 512); + + printhex(tmp, len); + + char *ptr = tmp; + uint32_t key_len = (uint32_t)*ptr; ptr+=1; + char* key = ptr; ptr+=key_len; + + hack_printstr(key, key_len); + + char *str = ptr; + hack_printstr(str+1, *str); + + uint32_t err = store_str( N(simpledb), N(keyvalue1), key, key_len, str, ((uint32_t)*str)+1); + } else if( action == N(insertkv2) ) { + // eosc push message simpledb insertkv2 '{"key":"a", "value":{"name":"aaa", "age":10}}' -S simpledb + // eosc get table simpledb simpledb keyvalue2 char tmp[512]; auto len = readMessage(tmp, 512); @@ -28,17 +46,16 @@ extern "C" { char *ptr = tmp; uint32_t key_len = (uint32_t)*ptr; ptr+=1; - char* key = ptr; ptr+=key_len; + char* key = ptr; ptr+=key_len; hack_printstr(key, key_len); - char value_len = (uint32_t)*ptr; ptr+=1; - char* value = ptr; ptr+=value_len; + char* complex_data = ptr; + uint32_t complex_data_len = len-(key_len+1); - hack_printstr(value, value_len); + uint32_t err = store_str( N(simpledb), N(keyvalue2), key, key_len, complex_data, complex_data_len ); - store_str( N(simpledb), N(record1alpha), key, key_len, value, value_len ); - } else if( action == N(insert1) ) { + } else if( action == N(insert1) ) { record1 tmp; readMessage(&tmp, sizeof(tmp)); store_i64( N(simpledb), N(record1), &tmp, sizeof(tmp) );