合约的编写
基于springboot : https://github.com/FISCO-BCOS/spring-boot-starter
pragma solidity ^0.4.24;
contract TableFactory {
function openTable(string) public constant returns (Table); // 打开表
function createTable(string,string,string) public returns(int); // 创建表
}
// 查询条件
contract Condition {
//等于
function EQ(string, int) public;
function EQ(string, string) public;
//不等于
function NE(string, int) public;
function NE(string, string) public;
//大于
function GT(string, int) public;
//大于或等于
function GE(string, int) public;
//小于
function LT(string, int) public;
//小于或等于
function LE(string, int) public;
//限制返回记录条数
function limit(int) public;
function limit(int, int) public;
}
// 单条数据记录
contract Entry {
function getInt(string) public constant returns(int);
function getAddress(string) public constant returns(address);
function getBytes64(string) public constant returns(byte[64]);
function getBytes32(string) public constant returns(bytes32);
function getString(string) public constant returns(string);
function set(string, int) public;
function set(string, string) public;
function set(string, address) public;
}
// 数据记录集
contract Entries {
function get(int) public constant returns(Entry);
function size() public constant returns(int);
}
// Table主类
contract Table {
// 查询接口
function select(string, Condition) public constant returns(Entries);
// 插入接口
function insert(string, Entry) public returns(int);
// 更新接口
function update(string, Entry, Condition) public returns(int);
// 删除接口
function remove(string, Condition) public returns(int);
function newEntry() public constant returns(Entry);
function newCondition() public constant returns(Condition);
}
LibStrings.sol
/*
* @title String & slice utility library for Solidity contracts.
* @author Nick Johnson <arachnid@notdot.net>
*
* @dev Functionality in this library is largely implemented using an
* abstraction called a 'slice'. A slice represents a part of a string -
* anything from the entire string to a single character, or even no
* characters at all (a 0-length slice). Since a slice only has to specify
* an offset and a length, copying and manipulating slices is a lot less
* expensive than copying and manipulating the strings they reference.
*
* To further reduce gas costs, most functions on slice that need to return
* a slice modify the original one instead of allocating a new one; for
* instance, `s.split(".")` will return the text up to the first '.',
* modifying s to only contain the remainder of the string after the '.'.
* In situations where you do not want to modify the original slice, you
* can make a copy first with `.copy()`, for example:
* `s.copy().split(".")`. Try and avoid using this idiom in loops; since
* Solidity has no memory management, it will result in allocating many
* short-lived slices that are later discarded.
*
* Functions that return two slices come in two versions: a non-allocating
* version that takes the second slice as an argument, modifying it in
* place, and an allocating version that allocates and returns the second
* slice; see `nextRune` for example.
*
* Functions that have to copy string data will return strings rather than
* slices; these can be cast back to slices for further processing if
* required.
*
* For convenience, some functions are provided with non-modifying
* variants that create a new slice and return both; for instance,
* `s.splitNew('.')` leaves s unmodified, and returns two values
* corresponding to the left and right parts of the string.
*/
pragma solidity ^0.4.24;
library LibStrings {
struct slice {
uint _len;
uint _ptr;
}
function memcpy(uint dest, uint src, uint len) private pure {
// Copy word-length chunks while possible
for(; len >= 32; len -= 32) {
assembly {
mstore(dest, mload(src))
}
dest += 32;
src += 32;
}
// Copy remaining bytes
uint mask = 256 ** (32 - len) - 1;
assembly {
let srcpart := and(mload(src), not(mask))
let destpart := and(mload(dest), mask)
mstore(dest, or(destpart, srcpart))
}
}
/*
* @dev Returns a slice containing the entire string.
* @param self The string to make a slice from.
* @return A newly allocated slice containing the entire string.
*/
function toSlice(string memory self) internal pure returns (slice memory) {
uint ptr;
assembly {
ptr := add(self, 0x20)
}
return slice(bytes(self).length, ptr);
}
/*
* @dev Returns the length of a null-terminated bytes32 string.
* @param self The value to find the length of.
* @return The length of the string, from 0 to 32.
* TODO 此处将self改为uint(self)
*/
function len(bytes32 self) internal pure returns (uint) {
uint ret;
if (self == 0)
return 0;
if (uint(self) & 0xffffffffffffffffffffffffffffffff == 0) {
ret += 16;
self = bytes32(uint(self) / 0x100000000000000000000000000000000);
}
if (uint(self) & 0xffffffffffffffff == 0) {
ret += 8;
self = bytes32(uint(self) / 0x10000000000000000);
}
if (uint(self) & 0xffffffff == 0) {
ret += 4;
self = bytes32(uint(self) / 0x100000000);
}
if (uint(self) & 0xffff == 0) {
ret += 2;
self = bytes32(uint(self) / 0x10000);
}
if (uint(self) & 0xff == 0) {
ret += 1;
}
return 32 - ret;
}
/*
* @dev Returns a slice containing the entire bytes32, interpreted as a
* null-terminated utf-8 string.
* @param self The bytes32 value to convert to a slice.
* @return A new slice containing the value of the input argument up to the
* first null.
*/
function toSliceB32(bytes32 self) internal pure returns (slice memory ret) {
// Allocate space for `self` in memory, copy it there, and point ret at it
assembly {
let ptr := mload(0x40)
mstore(0x40, add(ptr, 0x20))
mstore(ptr, self)
mstore(add(ret, 0x20), ptr)
}
ret._len = len(self);
}
/*
* @dev Returns a new slice containing the same data as the current slice.
* @param self The slice to copy.
* @return A new slice containing the same data as `self`.
*/
function copy(slice memory self) internal pure returns (slice memory) {
return slice(self._len, self._ptr);
}
/*
* @dev Copies a slice to a new string.
* @param self The slice to copy.
* @return A newly allocated string containing the slice's text.
*/
function toString(slice memory self) internal pure returns (string memory) {
string memory ret = new string(self._len);
uint retptr;
assembly { retptr := add(ret, 32) }
memcpy(retptr, self._ptr, self._len);
return ret;
}
/*
* @dev Returns the length in runes of the slice. Note that this operation
* takes time proportional to the length of the slice; avoid using it
* in loops, and call `slice.empty()` if you only need to know whether
* the slice is empty or not.
* @param self The slice to operate on.
* @return The length of the slice in runes.
*/
function len(slice memory self) internal pure returns (uint l) {
// Starting at ptr-31 means the LSB will be the byte we care about
uint ptr = self._ptr - 31;
uint end = ptr + self._len;
for (l = 0; ptr < end; l++) {
uint8 b;
assembly { b := and(mload(ptr), 0xFF) }
if (b < 0x80) {
ptr += 1;
} else if(b < 0xE0) {
ptr += 2;
} else if(b < 0xF0) {
ptr += 3;
} else if(b < 0xF8) {
ptr += 4;
} else if(b < 0xFC) {
ptr += 5;
} else {
ptr += 6;
}
}
}
/*
* @dev Returns true if the slice is empty (has a length of 0).
* @param self The slice to operate on.
* @return True if the slice is empty, False otherwise.
*/
function empty(slice memory self) internal pure returns (bool) {
return self._len == 0;
}
/*
* @dev Returns a positive number if `other` comes lexicographically after
* `self`, a negative number if it comes before, or zero if the
* contents of the two slices are equal. Comparison is done per-rune,
* on unicode codepoints.
* @param self The first slice to compare.
* @param other The second slice to compare.
* @return The result of the comparison.
*/
function compare(slice memory self, slice memory other) internal pure returns (int) {
uint shortest = self._len;
if (other._len < self._len)
shortest = other._len;
uint selfptr = self._ptr;
uint otherptr = other._ptr;
for (uint idx = 0; idx < shortest; idx += 32) {
uint a;
uint b;
assembly {
a := mload(selfptr)
b := mload(otherptr)
}
if (a != b) {
// Mask out irrelevant bytes and check again
uint256 mask = uint256(-1); // 0xffff...
if(shortest < 32) {
mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
}
uint256 diff = (a & mask) - (b & mask);
if (diff != 0)
return int(diff);
}
selfptr += 32;
otherptr += 32;
}
return int(self._len) - int(other._len);
}
/*
* @dev Returns true if the two slices contain the same text.
* @param self The first slice to compare.
* @param self The second slice to compare.
* @return True if the slices are equal, false otherwise.
*/
function equals(slice memory self, slice memory other) internal pure returns (bool) {
return compare(self, other) == 0;
}
/*
* @dev Extracts the first rune in the slice into `rune`, advancing the
* slice to point to the next rune and returning `self`.
* @param self The slice to operate on.
* @param rune The slice that will contain the first rune.
* @return `rune`.
*/
function nextRune(slice memory self, slice memory rune) internal pure returns (slice memory) {
rune._ptr = self._ptr;
if (self._len == 0) {
rune._len = 0;
return rune;
}
uint l;
uint b;
// Load the first byte of the rune into the LSBs of b
assembly { b := and(mload(sub(mload(add(self, 32)), 31)), 0xFF) }
if (b < 0x80) {
l = 1;
} else if(b < 0xE0) {
l = 2;
} else if(b < 0xF0) {
l = 3;
} else {
l = 4;
}
// Check for truncated codepoints
if (l > self._len) {
rune._len = self._len;
self._ptr += self._len;
self._len = 0;
return rune;
}
self._ptr += l;
self._len -= l;
rune._len = l;
return rune;
}
/*
* @dev Returns the first rune in the slice, advancing the slice to point
* to the next rune.
* @param self The slice to operate on.
* @return A slice containing only the first rune from `self`.
*/
function nextRune(slice memory self) internal pure returns (slice memory ret) {
nextRune(self, ret);
}
/*
* @dev Returns the number of the first codepoint in the slice.
* @param self The slice to operate on.
* @return The number of the first codepoint in the slice.
*/
function ord(slice memory self) internal pure returns (uint ret) {
if (self._len == 0) {
return 0;
}
uint word;
uint length;
uint divisor = 2 ** 248;
// Load the rune into the MSBs of b
assembly { word:= mload(mload(add(self, 32))) }
uint b = word / divisor;
if (b < 0x80) {
ret = b;
length = 1;
} else if(b < 0xE0) {
ret = b & 0x1F;
length = 2;
} else if(b < 0xF0) {
ret = b & 0x0F;
length = 3;
} else {
ret = b & 0x07;
length = 4;
}
// Check for truncated codepoints
if (length > self._len) {
return 0;
}
for (uint i = 1; i < length; i++) {
divisor = divisor / 256;
b = (word / divisor) & 0xFF;
if (b & 0xC0 != 0x80) {
// Invalid UTF-8 sequence
return 0;
}
ret = (ret * 64) | (b & 0x3F);
}
return ret;
}
/*
* @dev Returns the keccak-256 hash of the slice.
* @param self The slice to hash.
* @return The hash of the slice.
*/
function keccak(slice memory self) internal pure returns (bytes32 ret) {
assembly {
ret := keccak256(mload(add(self, 32)), mload(self))
}
}
/*
* @dev Returns true if `self` starts with `needle`.
* @param self The slice to operate on.
* @param needle The slice to search for.
* @return True if the slice starts with the provided text, false otherwise.
*/
function startsWith(slice memory self, slice memory needle) internal pure returns (bool) {
if (self._len < needle._len) {
return false;
}
if (self._ptr == needle._ptr) {
return true;
}
bool equal;
assembly {
let length := mload(needle)
let selfptr := mload(add(self, 0x20))
let needleptr := mload(add(needle, 0x20))
equal := eq(keccak256(selfptr, length), keccak256(needleptr, length))
}
return equal;
}
/*
* @dev If `self` starts with `needle`, `needle` is removed from the
* beginning of `self`. Otherwise, `self` is unmodified.
* @param self The slice to operate on.
* @param needle The slice to search for.
* @return `self`
*/
function beyond(slice memory self, slice memory needle) internal pure returns (slice memory) {
if (self._len < needle._len) {
return self;
}
bool equal = true;
if (self._ptr != needle._ptr) {
assembly {
let length := mload(needle)
let selfptr := mload(add(self, 0x20))
let needleptr := mload(add(needle, 0x20))
equal := eq(keccak256(selfptr, length), keccak256(needleptr, length))
}
}
if (equal) {
self._len -= needle._len;
self._ptr += needle._len;
}
return self;
}
/*
* @dev Returns true if the slice ends with `needle`.
* @param self The slice to operate on.
* @param needle The slice to search for.
* @return True if the slice starts with the provided text, false otherwise.
*/
function endsWith(slice memory self, slice memory needle) internal pure returns (bool) {
if (self._len < needle._len) {
return false;
}
uint selfptr = self._ptr + self._len - needle._len;
if (selfptr == needle._ptr) {
return true;
}
bool equal;
assembly {
let length := mload(needle)
let needleptr := mload(add(needle, 0x20))
equal := eq(keccak256(selfptr, length), keccak256(needleptr, length))
}
return equal;
}
/*
* @dev If `self` ends with `needle`, `needle` is removed from the
* end of `self`. Otherwise, `self` is unmodified.
* @param self The slice to operate on.
* @param needle The slice to search for.
* @return `self`
*/
function until(slice memory self, slice memory needle) internal pure returns (slice memory) {
if (self._len < needle._len) {
return self;
}
uint selfptr = self._ptr + self._len - needle._len;
bool equal = true;
if (selfptr != needle._ptr) {
assembly {
let length := mload(needle)
let needleptr := mload(add(needle, 0x20))
equal := eq(keccak256(selfptr, length), keccak256(needleptr, length))
}
}
if (equal) {
self._len -= needle._len;
}
return self;
}
// Returns the memory address of the first byte of the first occurrence of
// `needle` in `self`, or the first byte after `self` if not found.
function findPtr(uint selflen, uint selfptr, uint needlelen, uint needleptr) private pure returns (uint) {
uint ptr = selfptr;
uint idx;
if (needlelen <= selflen) {
if (needlelen <= 32) {
bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
bytes32 needledata;
assembly { needledata := and(mload(needleptr), mask) }
uint end = selfptr + selflen - needlelen;
bytes32 ptrdata;
assembly { ptrdata := and(mload(ptr), mask) }
while (ptrdata != needledata) {
if (ptr >= end)
return selfptr + selflen;
ptr++;
assembly { ptrdata := and(mload(ptr), mask) }
}
return ptr;
} else {
// For long needles, use hashing
bytes32 hash;
assembly { hash := keccak256(needleptr, needlelen) }
for (idx = 0; idx <= selflen - needlelen; idx++) {
bytes32 testHash;
assembly { testHash := keccak256(ptr, needlelen) }
if (hash == testHash)
return ptr;
ptr += 1;
}
}
}
return selfptr + selflen;
}
// Returns the memory address of the first byte after the last occurrence of
// `needle` in `self`, or the address of `self` if not found.
function rfindPtr(uint selflen, uint selfptr, uint needlelen, uint needleptr) private pure returns (uint) {
uint ptr;
if (needlelen <= selflen) {
if (needlelen <= 32) {
bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
bytes32 needledata;
assembly { needledata := and(mload(needleptr), mask) }
ptr = selfptr + selflen - needlelen;
bytes32 ptrdata;
assembly { ptrdata := and(mload(ptr), mask) }
while (ptrdata != needledata) {
if (ptr <= selfptr)
return selfptr;
ptr--;
assembly { ptrdata := and(mload(ptr), mask) }
}
return ptr + needlelen;
} else {
// For long needles, use hashing
bytes32 hash;
assembly { hash := keccak256(needleptr, needlelen) }
ptr = selfptr + (selflen - needlelen);
while (ptr >= selfptr) {
bytes32 testHash;
assembly { testHash := keccak256(ptr, needlelen) }
if (hash == testHash)
return ptr + needlelen;
ptr -= 1;
}
}
}
return selfptr;
}
/*
* @dev Modifies `self` to contain everything from the first occurrence of
* `needle` to the end of the slice. `self` is set to the empty slice
* if `needle` is not found.
* @param self The slice to search and modify.
* @param needle The text to search for.
* @return `self`.
*/
function find(slice memory self, slice memory needle) internal pure returns (slice memory) {
uint ptr = findPtr(self._len, self._ptr, needle._len, needle._ptr);
self._len -= ptr - self._ptr;
self._ptr = ptr;
return self;
}
/*
* @dev Modifies `self` to contain the part of the string from the start of
* `self` to the end of the first occurrence of `needle`. If `needle`
* is not found, `self` is set to the empty slice.
* @param self The slice to search and modify.
* @param needle The text to search for.
* @return `self`.
*/
function rfind(slice memory self, slice memory needle) internal pure returns (slice memory) {
uint ptr = rfindPtr(self._len, self._ptr, needle._len, needle._ptr);
self._len = ptr - self._ptr;
return self;
}
/*
* @dev Splits the slice, setting `self` to everything after the first
* occurrence of `needle`, and `token` to everything before it. If
* `needle` does not occur in `self`, `self` is set to the empty slice,
* and `token` is set to the entirety of `self`.
* @param self The slice to split.
* @param needle The text to search for in `self`.
* @param token An output parameter to which the first token is written.
* @return `token`.
*/
function split(slice memory self, slice memory needle, slice memory token) internal pure returns (slice memory) {
uint ptr = findPtr(self._len, self._ptr, needle._len, needle._ptr);
token._ptr = self._ptr;
token._len = ptr - self._ptr;
if (ptr == self._ptr + self._len) {
// Not found
self._len = 0;
} else {
self._len -= token._len + needle._len;
self._ptr = ptr + needle._len;
}
return token;
}
/*
* @dev Splits the slice, setting `self` to everything after the first
* occurrence of `needle`, and returning everything before it. If
* `needle` does not occur in `self`, `self` is set to the empty slice,
* and the entirety of `self` is returned.
* @param self The slice to split.
* @param needle The text to search for in `self`.
* @return The part of `self` up to the first occurrence of `delim`.
*/
function split(slice memory self, slice memory needle) internal pure returns (slice memory token) {
split(self, needle, token);
}
/*
* @dev Splits the slice, setting `self` to everything before the last
* occurrence of `needle`, and `token` to everything after it. If
* `needle` does not occur in `self`, `self` is set to the empty slice,
* and `token` is set to the entirety of `self`.
* @param self The slice to split.
* @param needle The text to search for in `self`.
* @param token An output parameter to which the first token is written.
* @return `token`.
*/
function rsplit(slice memory self, slice memory needle, slice memory token) internal pure returns (slice memory) {
uint ptr = rfindPtr(self._len, self._ptr, needle._len, needle._ptr);
token._ptr = ptr;
token._len = self._len - (ptr - self._ptr);
if (ptr == self._ptr) {
// Not found
self._len = 0;
} else {
self._len -= token._len + needle._len;
}
return token;
}
/*
* @dev Splits the slice, setting `self` to everything before the last
* occurrence of `needle`, and returning everything after it. If
* `needle` does not occur in `self`, `self` is set to the empty slice,
* and the entirety of `self` is returned.
* @param self The slice to split.
* @param needle The text to search for in `self`.
* @return The part of `self` after the last occurrence of `delim`.
*/
function rsplit(slice memory self, slice memory needle) internal pure returns (slice memory token) {
rsplit(self, needle, token);
}
/*
* @dev Counts the number of nonoverlapping occurrences of `needle` in `self`.
* @param self The slice to search.
* @param needle The text to search for in `self`.
* @return The number of occurrences of `needle` found in `self`.
*/
function count(slice memory self, slice memory needle) internal pure returns (uint cnt) {
uint ptr = findPtr(self._len, self._ptr, needle._len, needle._ptr) + needle._len;
while (ptr <= self._ptr + self._len) {
cnt++;
ptr = findPtr(self._len - (ptr - self._ptr), ptr, needle._len, needle._ptr) + needle._len;
}
}
/*
* @dev Returns True if `self` contains `needle`.
* @param self The slice to search.
* @param needle The text to search for in `self`.
* @return True if `needle` is found in `self`, false otherwise.
*/
function contains(slice memory self, slice memory needle) internal pure returns (bool) {
return rfindPtr(self._len, self._ptr, needle._len, needle._ptr) != self._ptr;
}
/*
* @dev Returns a newly allocated string containing the concatenation of
* `self` and `other`.
* @param self The first slice to concatenate.
* @param other The second slice to concatenate.
* @return The concatenation of the two strings.
*/
function concat(slice memory self, slice memory other) internal pure returns (string memory) {
string memory ret = new string(self._len + other._len);
uint retptr;
assembly { retptr := add(ret, 32) }
memcpy(retptr, self._ptr, self._len);
memcpy(retptr + self._len, other._ptr, other._len);
return ret;
}
/*
* @dev Joins an array of slices, using `self` as a delimiter, returning a
* newly allocated string.
* @param self The delimiter to use.
* @param parts A list of slices to join.
* @return A newly allocated string containing all the slices in `parts`,
* joined with `self`.
*/
function join(slice memory self, slice[] memory parts) internal pure returns (string memory) {
if (parts.length == 0)
return "";
uint length = self._len * (parts.length - 1);
for(uint i = 0; i < parts.length; i++)
length += parts[i]._len;
string memory ret = new string(length);
uint retptr;
assembly { retptr := add(ret, 32) }
for(uint i1 = 0; i1 < parts.length; i1++) {
memcpy(retptr, parts[i1]._ptr, parts[i1]._len);
retptr += parts[i1]._len;
if (i < parts.length - 1) {
memcpy(retptr, self._ptr, self._len);
retptr += self._len;
}
}
return ret;
}
}
LibStringUtil.sol
pragma solidity ^0.4.24;
import "./Table.sol";
/**
@title 将Bean格式化为json
*/
library LibStringUtil {
function getEntry(string[] memory fields, Entry entry) internal view returns (string[] memory) {
string[] memory values = new string[](fields.length);
for (uint i = 0; i < fields.length; i++) {
values[i] = entry.getString(fields[i]);
}
return values;
}
function getJsonString(string[] memory fields, Entries entries) internal view returns (int, string memory) {
string memory detail;
if (0 == entries.size()) {
return (- 1, detail);
}
else {
// [{"index":"",{"key1":"","key2":""}}]
detail = "[";
// 获取Bean的值
for (uint i = 0; i < uint(entries.size()); i++) {
string[] memory values = getEntry(fields, entries.get(int(i)));
for (uint j = 0; j < values.length; j++) {
if (j == 0) {
detail = strConcat4(detail, "{\"index\":\"", values[0], "\",\"data\":{");
}
detail = strConcat6(detail, "\"", fields[j], "\":\"", values[j], "\"");
if (j == values.length - 1) {
detail = strConcat2(detail, "}}");
} else {
detail = strConcat2(detail, ",");
}
}
if (i != uint(entries.size()) - 1) {
detail = strConcat2(detail, ",");
}
}
detail = strConcat2(detail, "]");
return (0, detail);
}
}
function strConcat6(
string memory str1,
string memory str2,
string memory str3,
string memory str4,
string memory str5,
string memory str6
) internal pure returns (string memory) {
string[] memory strings = new string[](6);
strings[0] = str1;
strings[1] = str2;
strings[2] = str3;
strings[3] = str4;
strings[4] = str5;
strings[5] = str6;
return strConcat(strings);
}
function strConcat5(
string memory str1,
string memory str2,
string memory str3,
string memory str4,
string memory str5
) internal pure returns (string memory) {
string[] memory strings = new string[](5);
strings[0] = str1;
strings[1] = str2;
strings[2] = str3;
strings[3] = str4;
strings[4] = str5;
return strConcat(strings);
}
function strConcat4(
string memory str1,
string memory str2,
string memory str3,
string memory str4
) internal pure returns (string memory) {
string[] memory strings = new string[](4);
strings[0] = str1;
strings[1] = str2;
strings[2] = str3;
strings[3] = str4;
return strConcat(strings);
}
function strConcat3(
string memory str1,
string memory str2,
string memory str3
) internal pure returns (string memory) {
string[] memory strings = new string[](3);
strings[0] = str1;
strings[1] = str2;
strings[2] = str3;
return strConcat(strings);
}
function strConcat2(string memory str1, string memory str2) internal pure returns (string memory) {
string[] memory strings = new string[](2);
strings[0] = str1;
strings[1] = str2;
return strConcat(strings);
}
function strConcat(string[] memory strings) internal pure returns (string memory) {
// 计算字节长度
uint bLength = 0;
for (uint i = 0; i < strings.length; i++) {
bLength += bytes(strings[i]).length;
}
// 实例化字符串
string memory result = new string(bLength);
bytes memory bResult = bytes(result);
// 填充字符串
uint currLength = 0;
for (uint i1 = 0; i1 < strings.length; i1++) {
// 将当前字符串转换为字节数组
bytes memory bs = bytes(strings[i1]);
for (uint j = 0; j < bs.length; j++) {
bResult[currLength] = bs[j];
currLength++;
}
}
return string(bResult);
}
}
基于Table.sol 、LibStrings.sol、LibStringUtil.sol 实现返回例子
pragma solidity ^0.4.24;
pragma experimental ABIEncoderV2;
import "./Table.sol";
import "./LibStrings.sol";
import "./LibStringUtil.sol";
contract BudgetApp{
TableFactory tf = TableFactory(0X1001);
constructor() public{
tf.createTable("t_sichuan_bud","dwbm","dwmc,zcgnflbm,zcgnfl,zcjjflbm,zcjjfl,zfjjkmdm,zfjjfl,je,xm,jsonData");
}
function get(string dwbm,string[] yskey,string[] ystr,int startPage,int endPage)constant public returns(int, string memory){
Table table = tf.openTable("t_sichuan_bud");
// 条件为空表示不筛选 也可以根据需要使用条件筛选
Condition condition = table.newCondition();
for (uint i = 0; i < yskey.length; i++) {
condition.EQ(yskey[i],ystr[i]);
}
condition.limit(startPage,endPage);
Entries entries = table.select(dwbm, condition);
string[] memory info = new string[](11);
info[0] = "dwbm";
info[1] = "dwmc";
info[2] = "zcgnflbm";
info[3] = "zcgnfl";
info[4] = "zcjjflbm";
info[5] = "zcjjfl";
info[6] = "zfjjkmdm";
info[7] = "zfjjfl";
info[8] = "je";
info[9] = "xm";
info[10] = "jsonData";
return LibStringUtil.getJsonString(info, entries);
}
function set(string dwbm,string[] paystr,string jsonData) public returns(int){
Table tb = tf.openTable("t_sichuan_bud");
Entry entry = tb.newEntry();
entry.set("dwbm",dwbm);
entry.set("dwmc",paystr[1]);
entry.set("zcgnflbm",paystr[2]);
entry.set("zcgnfl",paystr[3]);
entry.set("zcjjflbm",paystr[4]);
entry.set("zcjjfl",paystr[5]);
entry.set("zfjjkmdm",paystr[6]);
entry.set("zfjjfl",paystr[7]);
entry.set("je",paystr[8]);
entry.set("xm",paystr[9]);
entry.set("jsonData",jsonData);
int count = tb.insert(dwbm,entry);
return count;
}
}
对外接口
public interface CommonService {
<T> String get(T t, int startPage, int endPage,String flag);
<T> boolean set(T t,String flag);
<T> TransactionInfo setData(T t, String flag);
}
@Service
public class CommonServiceImpl implements CommonService {
private Logger log = LoggerFactory.getLogger(CommonServiceImpl.class);
@Autowired
private Web3j web3j;
//BudgetApp
@Autowired
private BudgetApp budgetApp;
// DataSourIndi
@Autowired
private DataSourIndi dataSourIndi;
//
@Autowired
private TargetRele targetRele;
//
@Autowired
private TargetReleDown targetReleDown;
//
@Autowired
private IndexTobeDiv indexTobeDiv;
//
@Autowired
private ExecutableIndi executableIndi;
//
@Autowired
private DirectPayVoucher directPayVoucher;
//
@Autowired
private DirectPayVoucherItem vouitem;
//
@Autowired
private OneCardSolu oneCardSolu;
//
@Autowired
private AccountingBook account;
//
@Autowired
private Generalplan generalplan;
//直接支付申请
@Autowired
private DirectPayment directPayment;
//预警数据
@Autowired
private Earlywarning earlywarning;
/**
* 公共查询
* 查询时dwdm || yslydm 不能为空null 或"" 为空返回null
*
* @param t
* @param startPage 0
* @param endPage 10
* @param flag
* @param <T>
* @return
*/
@Override
public <T> String get(T t, int startPage, int endPage, String flag) {
log.info(">>>>>>>>>>>>>>get>>>>>>>>>>>>T" + JSONObject.toJSON(t).toString() + " startPage:" + startPage + " endPage" + endPage + " flag" + flag);
List<String> keylist = new ArrayList<>();
List<String> vallist = new ArrayList<>();
Field fields[] = t.getClass().getDeclaredFields();//t 是实体类名称
String[] name = new String[fields.length];
Object[] value = new Object[fields.length];
String dwdm = "";
String yslydm = "";
Tuple2<BigInteger, String> send = null;
try {
Field.setAccessible(fields, true);
for (int i = 0; i < name.length; i++) {
if (fields[i].get(t) != null && fields[i].get(t) != "") {
keylist.add(fields[i].getName());
vallist.add((String) fields[i].get(t));
}
if ("dwbm".equals(fields[i].getName()) || "dwdm".equals(fields[i].getName())) {
dwdm = (String) fields[i].get(t);
}
if ("yslydm".equals(fields[i].getName())) {
yslydm = (String) fields[i].get(t);
}
}
send = doGetDown(startPage, endPage, flag, keylist, vallist, dwdm, yslydm, send);
if (send != null) {
return send.getValue2();
} else {
return null;
}
} catch (Exception e) {
log.error(">>>>>>>>>>>>>set>>>>>>>>>errorinfo", e);
return null;
}
}
/**
* 公共添加
* dwdm || yslydm 不能为空null 或"" 为空返回false
*
* @param t
* @param flag
* @param <T>
* @return
*/
@Override
public <T> boolean set(T t, String flag) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>>>>>T" + JSONObject.toJSON(t).toString() + " flag" + flag);
List<String> keylist = new ArrayList<>();
List<String> vallist = new ArrayList<>();
Field fields[] = t.getClass().getDeclaredFields();//t 是实体类名称
String[] name = new String[fields.length];
Object[] value = new Object[fields.length];
String dwdm = "";
String yslydm = "";
String jsonData = "";
TransactionReceipt _temp = null;
try {
Field.setAccessible(fields, true);
for (int i = 0; i < name.length; i++) {
keylist.add(fields[i].getName());
if (fields[i].get(t) != null) {
vallist.add((String) fields[i].get(t));
} else {
vallist.add("");
}
if ("dwbm".equals(fields[i].getName()) || "dwdm".equals(fields[i].getName())) {
dwdm = (String) fields[i].get(t);
}
if ("yslydm".equals(fields[i].getName())) {
yslydm = (String) fields[i].get(t);
}
if ("jsonData".equals(fields[i].getName())) {
jsonData = (String) fields[i].get(t);
}
}
_temp = doSetDown(flag, vallist, dwdm, yslydm, jsonData, _temp);
if (_temp != null) {
return true;
} else {
return false;
}
} catch (Exception e) {
log.error(">>>>>>>>>>>>>set>>>>>>>>>errorinfo", e);
return false;
}
}
/**
* 公共添加
* dwdm || yslydm 不能为空null 或"" 为空返回 null
*
* @param <T>
* @param t
* @param flag
* @return
*/
@Override
public <T> TransactionInfo setData(T t, String flag) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>>>>>T" + JSONObject.toJSON(t).toString() + " flag" + flag);
List<String> keylist = new ArrayList<>();
List<String> vallist = new ArrayList<>();
Field fields[] = t.getClass().getDeclaredFields();//t 是实体类名称
String[] name = new String[fields.length];
Object[] value = new Object[fields.length];
String dwdm = "";
String yslydm = "";
String jsonData = "";
TransactionReceipt _temp = null;
TransactionInfo transactionInfo=new TransactionInfo();
try {
Field.setAccessible(fields, true);
for (int i = 0; i < name.length; i++) {
keylist.add(fields[i].getName());
if (fields[i].get(t) != null) {
vallist.add((String) fields[i].get(t));
} else {
vallist.add("");
}
if ("dwbm".equals(fields[i].getName()) || "dwdm".equals(fields[i].getName())) {
dwdm = (String) fields[i].get(t);
}
if ("yslydm".equals(fields[i].getName())) {
yslydm = (String) fields[i].get(t);
}
if ("jsonData".equals(fields[i].getName())) {
jsonData = (String) fields[i].get(t);
}
}
_temp = doSetDown(flag, vallist, dwdm, yslydm, jsonData, _temp);
if (_temp != null) {
transactionInfo.setTransactionHash(_temp.getTransactionHash());
transactionInfo.setBlockHash(_temp.getBlockHash());
transactionInfo.setBlockNumber(_temp.getBlockNumber());
return transactionInfo;
} else {
return null;
}
} catch (Exception e) {
log.error(">>>>>>>>>>>>>set>>>>>>>>>errorinfo", e);
return null;
}
}
/**
* get
*
* @param startPage
* @param endPage
* @param flag
* @param keylist
* @param vallist
* @param dwdm
* @param yslydm
* @param send
* @return
* @throws Exception
*/
private Tuple2<BigInteger, String> doGetDown(int startPage, int endPage, String flag, List<String> keylist, List<String> vallist, String dwdm, String yslydm, Tuple2<BigInteger, String> send) throws Exception {
if ("1".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
send = budgetApp.get(dwdm, keylist, vallist, BigInteger.valueOf(startPage), BigInteger.valueOf(endPage)).send();
log.info(">>>>>>>>>>>get>>>>>>>>>>>budgetApp:" + send);
} else {
log.info(">>>>>>>>>>>>>budgetApp dwdm is null.skip...>>>>>>>>");
}
} else if ("2".equals(flag)) {
if (StringUtils.isNotBlank(yslydm)) {
send = dataSourIndi.get(yslydm, keylist, vallist, BigInteger.valueOf(startPage), BigInteger.valueOf(endPage)).send();
log.info(">>>>>>>>>>>>get>>>>>>>>>>dataSourIndi:" + send);
} else {
log.info(">>>>>>>>>>>>>dataSourIndi yslydm is null.skip...>>>>>>>>");
}
} else if ("3".equals(flag)) {
if (StringUtils.isNotBlank(yslydm)) {
send = targetRele.get(yslydm, keylist, vallist, BigInteger.valueOf(startPage), BigInteger.valueOf(endPage)).send();
log.info(">>>>>>>>>>>>>get>>>>>>>>>targetRele:" + send);
} else {
log.info(">>>>>>>>>>>>>targetRele yslydm is null.skip...>>>>>>>>");
}
} else if ("4".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
send = targetReleDown.get(dwdm, keylist, vallist, BigInteger.valueOf(startPage), BigInteger.valueOf(endPage)).send();
log.info(">>>>>>>>>>>>>get>>>>>>>>>targetRele:" + send);
} else {
log.info(">>>>>>>>>>>>>targetReleDown dwdm is null.skip...>>>>>>>>");
}
} else if ("5".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
send = indexTobeDiv.get(dwdm, keylist, vallist, BigInteger.valueOf(startPage), BigInteger.valueOf(endPage)).send();
log.info(">>>>>>>>>>>>>get>>>>>>>>>indexTobeDiv:" + send);
} else {
log.info(">>>>>>>>>>>>>indexTobeDiv dwdm is null.skip...>>>>>>>>");
}
} else if ("6".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
send = executableIndi.get(dwdm, keylist, vallist, BigInteger.valueOf(startPage), BigInteger.valueOf(endPage)).send();
log.info(">>>>>>>>>>>>>get>>>>>>>>>executableIndi:" + send);
} else {
log.info(">>>>>>>>>>>>>executableIndi dwdm is null.skip...>>>>>>>>");
}
} else if ("7".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
send = directPayVoucher.get(dwdm, keylist, vallist, BigInteger.valueOf(startPage), BigInteger.valueOf(endPage)).send();
log.info(">>>>>>>>>>>>>get>>>>>>>>>directPayVoucher:" + send);
} else {
log.info(">>>>>>>>>>>>>directPayVoucher dwdm is null.skip...>>>>>>>>");
}
} else if ("8".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
send = vouitem.get(dwdm, keylist, vallist, BigInteger.valueOf(startPage), BigInteger.valueOf(endPage)).send();
log.info(">>>>>>>>>>>>>get>>>>>>>>>vouitem:" + send);
} else {
log.info(">>>>>>>>>>>>>vouitem dwdm is null.skip...>>>>>>>>");
}
} else if ("9".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
send = oneCardSolu.get(dwdm, keylist, vallist, BigInteger.valueOf(startPage), BigInteger.valueOf(endPage)).send();
log.info(">>>>>>>>>>>>>get>>>>>>>>>oneCardSolu:" + send);
} else {
log.info(">>>>>>>>>>>>>oneCardSolu dwdm is null.skip...>>>>>>>>");
}
} else if ("10".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
send = account.get(dwdm, keylist, vallist, BigInteger.valueOf(startPage), BigInteger.valueOf(endPage)).send();
log.info(">>>>>>>>>>>>>get>>>>>>>>>account:" + send);
} else {
log.info(">>>>>>>>>>>>>account dwdm is null.skip...>>>>>>>>");
}
} else if ("11".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
send = generalplan.get(dwdm, keylist, vallist, BigInteger.valueOf(startPage), BigInteger.valueOf(endPage)).send();
log.info(">>>>>>>>>>>>>get>>>>>>>>>generalplan:" + send);
} else {
log.info(">>>>>>>>>>>>>generalplan dwdm is null.skip...>>>>>>>>");
}
} else if ("12".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
send = directPayment.get(dwdm, keylist, vallist, BigInteger.valueOf(startPage), BigInteger.valueOf(endPage)).send();
log.info(">>>>>>>>>>>>>get>>>>>>>>>directPayment:" + send);
} else {
log.info(">>>>>>>>>>>>>directPayment dwdm is null.skip...>>>>>>>>");
}
} else if ("13".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
send = earlywarning.get(dwdm, keylist, vallist, BigInteger.valueOf(startPage), BigInteger.valueOf(endPage)).send();
log.info(">>>>>>>>>>>>>get>>>>>>>>>earlywarning:" + send);
} else {
log.info(">>>>>>>>>>>>>earlywarning dwdm is null.skip...>>>>>>>>");
}
}
return send;
}
/**
* doSetDown 添加方法提取
*
* @param flag
* @param vallist
* @param dwdm
* @param yslydm
* @param jsonData
* @param _temp
* @return
* @throws Exception
*/
private TransactionReceipt doSetDown(String flag, List<String> vallist, String dwdm, String yslydm, String jsonData, TransactionReceipt _temp) throws Exception {
if ("1".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>budgetApp:" + dwdm + " vallist:" + vallist + " jsonData:" + jsonData);
_temp = budgetApp.set(dwdm, vallist, jsonData).send();
// String strs = BudgetApp.getTransactionDecoder().decodeOutputReturnJson(_temp.getInput(), _temp.getOutput());
log.info(">>>>>>>>>>>>>set>>>>>>>>>BlockNumber: " + _temp.getBlockNumber() + " BlockHash: " + _temp.getBlockHash());
} else {
log.info(">>>>>>>>>>>>>budgetApp dwdm is null.skip...>>>>>>>>");
}
} else if ("2".equals(flag)) {
if (StringUtils.isNotBlank(yslydm)) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>dataSourIndi:" + yslydm + " vallist:" + vallist + " jsonData:" + jsonData);
_temp = dataSourIndi.set(yslydm, vallist, jsonData).send();
// String strs = DataSourIndi.getTransactionDecoder().decodeOutputReturnJson(_temp.getInput(), _temp.getOutput());
log.info(">>>>>>>>>>>>>set>>>>>>>>>BlockNumber: " + _temp.getBlockNumber() + " BlockHash: " + _temp.getBlockHash());
} else {
log.info(">>>>>>>>>>>>>dataSourIndi yslydm is null.skip...>>>>>>>>");
}
} else if ("3".equals(flag)) {
if (StringUtils.isNotBlank(yslydm)) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>targetRele:" + yslydm + " vallist:" + vallist + " jsonData:" + jsonData);
_temp = targetRele.set(yslydm, vallist, jsonData).send();
// String strs = TargetRele.getTransactionDecoder().decodeOutputReturnJson(_temp.getInput(), _temp.getOutput());
log.info(">>>>>>>>>>>>>set>>>>>>>>>BlockNumber: " + _temp.getBlockNumber() + " BlockHash: " + _temp.getBlockHash());
} else {
log.info(">>>>>>>>>>>>>targetRele yslydm is null.skip...>>>>>>>>");
}
} else if ("4".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>targetReleDown:" + dwdm + " vallist:" + vallist + " jsonData:" + jsonData);
_temp = targetReleDown.set(dwdm, vallist, jsonData).send();
// String strs = TargetReleDown.getTransactionDecoder().decodeOutputReturnJson(_temp.getInput(), _temp.getOutput());
log.info(">>>>>>>>>>>>>set>>>>>>>>>BlockNumber: " + _temp.getBlockNumber() + " BlockHash: " + _temp.getBlockHash());
} else {
log.info(">>>>>>>>>>>>>targetReleDown dwdm is null.skip...>>>>>>>>");
}
} else if ("5".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>indexTobeDiv:" + dwdm + " vallist:" + vallist + " jsonData:" + jsonData);
_temp = indexTobeDiv.set(dwdm, vallist, jsonData).send();
// String strs = IndexTobeDiv.getTransactionDecoder().decodeOutputReturnJson(_temp.getInput(), _temp.getOutput());
log.info(">>>>>>>>>>>>>set>>>>>>>>>BlockNumber: " + _temp.getBlockNumber() + " BlockHash: " + _temp.getBlockHash());
} else {
log.info(">>>>>>>>>>>>>indexTobeDiv dwdm is null.skip...>>>>>>>>");
}
} else if ("6".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>executableIndi:" + dwdm + " vallist:" + vallist + " jsonData:" + jsonData);
_temp = executableIndi.set(dwdm, vallist, jsonData).send();
// String strs = ExecutableIndi.getTransactionDecoder().decodeOutputReturnJson(_temp.getInput(), _temp.getOutput());
log.info(">>>>>>>>>>>>>set>>>>>>>>>BlockNumber: " + _temp.getBlockNumber() + " BlockHash: " + _temp.getBlockHash());
} else {
log.info(">>>>>>>>>>>>>executableIndi dwdm is null.skip...>>>>>>>>");
}
} else if ("7".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>directPayVoucher:" + dwdm + " vallist:" + vallist + " jsonData:" + jsonData);
_temp = directPayVoucher.set(dwdm, vallist, jsonData).send();
// String strs = DirectPayVoucher.getTransactionDecoder().decodeOutputReturnJson(_temp.getInput(), _temp.getOutput());
log.info(">>>>>>>>>>>>>set>>>>>>>>>BlockNumber: " + _temp.getBlockNumber() + " BlockHash: " + _temp.getBlockHash());
} else {
log.info(">>>>>>>>>>>>>directPayVoucher dwdm is null.skip...>>>>>>>>");
}
} else if ("8".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>vouitem:" + dwdm + " vallist:" + vallist + " jsonData:" + jsonData);
_temp = vouitem.set(dwdm, vallist, jsonData).send();
// String strs = DirectPayVoucherItem.getTransactionDecoder().decodeOutputReturnJson(_temp.getInput(), _temp.getOutput());
log.info(">>>>>>>>>>>>>set>>>>>>>>>BlockNumber: " + _temp.getBlockNumber() + " BlockHash: " + _temp.getBlockHash());
} else {
log.info(">>>>>>>>>>>>>vouitem dwdm is null.skip...>>>>>>>>");
}
} else if ("9".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>oneCardSolu:" + dwdm + " vallist:" + vallist + " jsonData:" + jsonData);
_temp = oneCardSolu.set(dwdm, vallist, jsonData).send();
// String strs = OneCardSolu.getTransactionDecoder().decodeOutputReturnJson(_temp.getInput(), _temp.getOutput());
log.info(">>>>>>>>>>>>>set>>>>>>>>>BlockNumber: " + _temp.getBlockNumber() + " BlockHash: " + _temp.getBlockHash());
} else {
log.info(">>>>>>>>>>>>>oneCardSolu dwdm is null.skip...>>>>>>>>");
}
} else if ("10".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>account:" + dwdm + " vallist:" + vallist + " jsonData:" + jsonData);
_temp = account.set(dwdm, vallist, jsonData).send();
// String strs = AccountingBook.getTransactionDecoder().decodeOutputReturnJson(_temp.getInput(), _temp.getOutput());
log.info(">>>>>>>>>>>>>set>>>>>>>>>BlockNumber: " + _temp.getBlockNumber() + " BlockHash: " + _temp.getBlockHash());
} else {
log.info(">>>>>>>>>>>>>account dwdm is null.skip...>>>>>>>>");
}
} else if ("11".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>generalplan:" + dwdm + " vallist:" + vallist + " jsonData:" + jsonData);
_temp = generalplan.set(dwdm, vallist, jsonData).send();
// String strs = Generalplan.getTransactionDecoder().decodeOutputReturnJson(_temp.getInput(), _temp.getOutput());
log.info(">>>>>>>>>>>>>set>>>>>>>>>BlockNumber: " + _temp.getBlockNumber() + " BlockHash: " + _temp.getBlockHash());
} else {
log.info(">>>>>>>>>>>>>generalplan dwdm is null.skip...>>>>>>>>");
}
}else if ("12".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>directPayment:" + dwdm + " vallist:" + vallist + " jsonData:" + jsonData);
_temp = directPayment.set(dwdm, vallist, jsonData).send();
// String strs = Generalplan.getTransactionDecoder().decodeOutputReturnJson(_temp.getInput(), _temp.getOutput());
log.info(">>>>>>>>>>>>>set>>>>>>>>>BlockNumber: " + _temp.getBlockNumber() + " BlockHash: " + _temp.getBlockHash());
} else {
log.info(">>>>>>>>>>>>>directPayment dwdm is null.skip...>>>>>>>>");
}
}else if ("13".equals(flag)) {
if (StringUtils.isNotBlank(dwdm)) {
log.info(">>>>>>>>>>>>>set>>>>>>>>>earlywarning:" + dwdm + " vallist:" + vallist + " jsonData:" + jsonData);
_temp = earlywarning.set(dwdm, vallist, jsonData).send();
// String strs = Generalplan.getTransactionDecoder().decodeOutputReturnJson(_temp.getInput(), _temp.getOutput());
log.info(">>>>>>>>>>>>>set>>>>>>>>>BlockNumber: " + _temp.getBlockNumber() + " BlockHash: " + _temp.getBlockHash());
} else {
log.info(">>>>>>>>>>>>>earlywarning dwdm is null.skip...>>>>>>>>");
}
}
return _temp;
}