fengda 发表于 2018-10-19 11:06:43

OSS重磅推出OSS Select——使用SQL选取文件的内容

  [前几天翻译了一篇关于EOS智能合约数据库的内容,今天来演示一下数据库的使用方法。
  目录
  增
  查
  改
  删
  新增
  新增内容往往用到emplace构造函数,来进行数据库对象的新增。
  .cpp

  void test_da::create(account_name user, string>  {
  require_auth( user ); //验证权限
  das datable( _self, user); //定义数据库对象
  datable.emplace(user, [&]( da & d){

  d.title =>  d.content = content;
  d.post_id = datable.available_primary_key();
  d.poster = user;
  }); //数据库内容创建
  }
  这里需要注意的是:
  定义数据库对象, 其中第一个参数是合约的拥有者_self,第二个变量就是数据库的payer,也就是数据库是谁的,数据库存储在谁的账户下。
  emplace函数接收两个参数,一个payer,和一个lamada表达式。这个结构是固定的。
  那么我们现在来看一下我们的test_da类是怎么定义的。
  .hpp 
  class test_da : public contract {
  public:
  test_da( account_name self ):contract(self){}
  

       // @abi action
  void create(account_name user, string>  

  private:
  // @abi table data i64
  struct da {
  uint64_t   post_id;
  account_name poster;

  string      >  string       content;
  

  uint64_t primary_key()const { return post_id; }
  account_name get_poster() const { return poster; }
  

  EOSLIB_SERIALIZE(da, (post_id)(poster)(title)(content))
  };
  typedef eosio::multi_index das;
  

  };
  } /// namespace eosio
  所有的智能合约都继承自contract合约。test_da( account_name self ):contract(self){}是test_da合约的构造函数。
  下面是对create函数的声明。
  接下来是对数据字段的定义。这里我们定义了数据结构da.
  primary_key函数是定义主键的函数。 
  接下来我们定义了辅助主键返回poster。
  EOSLIB_SERIALIZE宏的第一个参数是数据结构,其他参数是数据结构中的数据成员。
  typedef我们在这里定义了一个名字为das的类型,它用来定义数据库对象。这里我们定义的是一个具有主键及一个辅助键的数据库对象。
  .abi 
  abi 非常重要,错误的abi会导致合约执行失败。
  {
  "types": [],
  "structs": [{
  "name": "da",
  "base": "",
  "fields": [{
  "name": "post_id",
  "type": "uint64"
  },{
  "name": "poster",
  "type": "account_name"
  },{
  "name": "title",
  "type": "string"
  },{
  "name": "content",
  "type": "string"
  }
  ]
  },{
  "name": "create",
  "base": "",
  "fields": [{
  "name": "user",
  "type": "account_name"
  },{
  "name": "title",
  "type": "string"
  },{
  "name": "content",
  "type": "string"
  }
  ]
  }}
  ],
  "actions": [{
  "name": "create",
  "type": "create",
  }
  ],
  "tables": [{
  "name": "data",
  "index_type": "i64",
  "key_names": [
  "post_id"
  ],
  "key_types": [
  "uint64"
  ],
  "type": "da"
  }
  ],
  "ricardian_clauses": []
  }
  接下来来演示一下:
  发布合约
  cleos set contract eosio test_da
  Reading WAST/WASM from test_da/test_da.wasm...
  Using already assembled WASM...
  Publishing contract...
  executed transaction: 3d6f04278617d3807fe876a33057f1155acf9c9e5a392ac6ed8ad51e795060096752 bytes24679 us

eosio content.c_str(), " post_id:", pos->post_id, ">  }
  获取二级索引并获取数据,这里我只用了find查询,其他查询就不在一一介绍。
  执行getd操作:
  cleos push action eosio getd '{"post_id":2,"user": "eostea"}' -p eosio
  executed transaction: 2370e1fb1ee8a581f7321f02fb40645e51269e579d183c33ef470dba0b3afdbc200 bytes5403 us

eosioPost_id: 2Post_Tile: eostea first Content: eostea create a first onecontent:eostea create a first one post_id:2>  数据库中数据如下:

  cleos get table eosio eosio data
  {
  "rows": [{
  "post_id": 0,
  "poster": "eosio",
  "title": "first",
  "content": "eostea create a first one"
  },{
  "post_id": 1,
  "poster": "eosio",
  "title": "first",
  "content": "eostea create a first one"
  },{
  "post_id": 2,
  "poster": "eostea",
  "title": "eostea first",
  "content": "eostea create a first one"
  }
  ],
  "more": false
  }
  更改
  更改数据库内容。
  这里我们先看一下目前的数据库内容。
  cleos get table eosio eosio data
  {
  "rows": [{
  "post_id": 0,
  "poster": "eosio",
  "title": "first",
  "content": "eostea create a first one"
  },{
  "post_id": 1,
  "poster": "eosio",
  "title": "first",
  "content": "eostea create a first one"
  },{
  "post_id": 2,
  "poster": "eostea",
  "title": "eostea first",
  "content": "eostea create a first one"
  }
  ],
  "more": false
  }
  写一个更改数据的action代码如下:

  void test_da::change(account_name user, uint64_t post_id, string>  {
  require_auth(user);
  das datable( _self, user);
  auto post = datable.find(post_id);
  eosio_assert(post->poster == user, "yonghucuowu");
  datable.modify(post, user, [&](auto& p){
  if (title != "")

  p.title =>  if (content != "")
  p.content = content;
  });
  }
  1. 前几行代码已经之前讲解过,现在直接说modify方法,他的第一个参数是你查询出的要更改的对象,第二个参数是payer,其他的不用多说。
  下面我们执行一下命令:
  cleos push action eosio change '{"user":"eosio","post_id":1,"title":"change","content":"change action"}' -p eosio
  executed transaction: 8cb561a712f2741560118651aefd49efd161e3d73c56f6d24cf1d699c265e2dc224 bytes2130 us

eosio title.c_str());
  

    eosio_assert(post->poster == user, "yonghucuowu");  datable.erase(post);
  
}
  

  这里调用了erase方法删除数据,参数为一个数据对象。下面我们来看一下执行结果:
  cleos push action eosio dele '{"user":"eosio","post_id":1}' -p eosioexecuted transaction: 3affbbbbd1da328ddcf37753f1f2f6c5ecc36cd81a0e12fea0c789e75b59714e200 bytes2383 us

eosio
页: [1]
查看完整版本: OSS重磅推出OSS Select——使用SQL选取文件的内容