CLASS::TABLE

defined in header

template<name::raw TableName, typename T, typename PrimaryKeyType>
class table;

描述

table 是WaykiChain提供的访问数据库的C++接口。使智能合约能够读取和修改WaykiChain数据库中的数据

模版参数

参数 描述
TableName 表的名称
T 表对象的类型
PrimaryKeyType 主键的类型

example

TABLE currency_stats{
        asset supply;
        asset max_supply;
        regid issuer;

        uint64_t primary_key()const{ return supply.symbol.code().raw(); } 
    };
typedef wasm::table< "stat"_n, currency_stats, uint64_t > stats;

Member functions

函数 描述
table 构造函数
emplace 增加
modify 修改
erase 删除
get 查找

Detail


TableName

表的名称,最长12个字符,只能由小写字母、数字1-5、“.”组成。

T

表对象的类型,即表中“行(row)”的定义。

PrimaryKeyType

表主键的属性,即表中“列(line)”的定义。

table

构造函数 table( regid code, uint64_t scope )

paramters

参数名 描述
code 合约的id
scope 用户账户名下的区域。可以在code下定义多个scope,把属于不同scope的表隔离开

example

stats statstable(_self, sym.code().raw());

emplace

定义

往表中添加数据,模版函数

函数原型

template<typename Lambda>   
    void emplace( regid payer, Lambda&& constructor )

paramters

参数名 描述
player 账户regid
constructor 增加的数据(以数据结构的方式)

example

ACTION testTable::create(regid issuer, asset maximum_supply) {
    require_auth(issuer);
    auto sym = maximum_supply.symbol;
    stats statstable(_self, sym.code().raw());

    statstable.emplace(_self, [&](auto& s) {
        s.supply.symbol = sym;
        s.max_supply    = maximum_supply;
        s.issuer        = issuer;
    });
}

result

>> input  
 coind submittx 0-2  "393226-2" create '["0-2", "98.00000000 WICC"]'

<< output

coind wasm_gettable "393368-2" stat
{
    "rows" : [
        {
            "supply" : "0.00000000 WICC",
            "max_supply" : "98.00000000 WICC",
            "issuer" : "0-2",
            "key" : "02008009600000000000000000904dc657494343000000005749434300000000"
        }
    ],
    "more" : false
}

modify

定义

修改表中某条数据的信息,模版函数

函数原型

template<typename Lambda>
    void modify( const T& obj, regid payer, Lambda&& updater )

paramters

参数名 描述
obj 需要被修改的信息
player 修改的用户的regid, 默认可填 wasm::no_payer
updater 需要修改成的数据信息

example

ACTION testTable::add(asset supply){
    auto sym = supply.symbol;

    currency_stats st;
    stats statstable(_self, sym.code().raw());

    check( statstable.get( st, sym.code().raw() ), "token with symbol does not exist, create token before issue" );

    statstable.modify(st, wasm::no_payer, [&](auto& s)
    {
        s.supply += supply;
    });
}

result

>> input  
coind submittx 0-2  "393368-2" add '["98.00000000 WICC"]'

<< output  
coind wasm_gettable "393368-2" stat
{
    "rows" : [
        {
            "supply" : "98.00000000 WICC",
            "max_supply" : "98.00000000 WICC",
            "issuer" : "0-2",
            "key" : "02008009600000000000000000904dc657494343000000005749434300000000"
        }
    ],
    "more" : false
}

erase

定义

删除某条数据,模版函数

函数原型

void erase( const T& obj, regid payer)  

paramters

参数名 描述
obj 需要删除的数据
payer 操作的用户的regid, 默认为wasm::no_player

example

ACTION testTable::deletetest(symbol ds){
    currency_stats st;

    stats statstable(_self, ds.code().raw());

    check( statstable.get( st, ds.code().raw() ), "token with symbol does not exist, create token before issue" );

    statstable.erase(st, wasm::no_payer);
}

result

>> input  
coind submittx 0-2  "393368-2" deletetest '["wicc"]'

<< output  
coind wasm_gettable "393368-2" stat
{
    "rows" : [
    ],
    "more" : false
}

get

定义

通过主键获得这条数据的信息,模版函数

函数原型

bool get(T& t, const PrimaryKeyType& primary, const char* error_msg = "unable to find key" )

paramters

参数名 描述
t 通过key得到的数据
primary 主键key
error_msg 当不存在该数据时显示的信息,默认为unable to find key

example

stats statstable(_self, sym.code().raw());
statstable.get( st, sym.code().raw() ), "token with symbol does not exist, create token before issue" );

全局举例

testTable.hpp

#include <wasm.hpp>
#include <asset.hpp>
#include <table.hpp>


using namespace wasm;
using namespace std;

CONTRACT testTable : public contract {
   public:
      using contract::contract;

      ACTION create(regid issuer, asset maximum_supply);
      ACTION add(asset supply);
      ACTION deletetest(symbol ds);

   private:
      TABLE currency_stats{
         asset supply;
         asset max_supply;
         regid issuer;

         uint64_t primary_key()const{ return supply.symbol.code().raw(); } 
      };

   typedef wasm::table< "stat"_n, currency_stats, uint64_t > stats;
};

testTable.cpp

#include <testTable.hpp>

ACTION testTable::create(regid issuer, asset maximum_supply) {
    require_auth(issuer);

    auto sym = maximum_supply.symbol;

    currency_stats st;
    stats statstable(_self, sym.code().raw());

    statstable.emplace(_self, [&](auto& s) {
        s.supply.symbol = sym;
        s.max_supply    = maximum_supply;
        s.issuer        = issuer;
    });
}

ACTION testTable::add(asset supply){
    auto sym = supply.symbol;

    currency_stats st;
    stats statstable(_self, sym.code().raw());

    check( statstable.get( st, sym.code().raw() ), "token with symbol does not exist, create token before issue" );

    statstable.modify(st, wasm::no_payer, [&](auto& s)
    {
        s.supply += supply;
    });
}

ACTION testTable::deletetest(symbol ds){
    currency_stats st;

    stats statstable(_self, ds.code().raw());
    check( statstable.get( st, ds.code().raw() ), "token with symbol does not exist, create token before issue" );

    statstable.erase(st, wasm::no_payer);
}

WASM_DISPATCH( testTable, (create)(add)(deletetest))