-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsolidity-TableTxs.sol
More file actions
159 lines (143 loc) · 5.29 KB
/
solidity-TableTxs.sol
File metadata and controls
159 lines (143 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
pragma solidity ^0.4.4;
contract DBTest {
/*
* @param tableName eg: "test1"
* @param raw eg: "[{\"field\":\"id\", \"type\" : \"int\", \"length\" : 11, \"PK\" : 1, \"NN\" : 1, \"UQ\" : 1}, { \"field\":\"account\", \"type\" : \"varchar\" }, { \"field\":\"age\", \"type\" : \"int\" }]"
*/
function create(string tableName, string raw) public{
msg.sender.create(tableName, raw);
}
/*
* @param tableName eg: "test1"
*/
function drop(string tableName) public{
msg.sender.drop(tableName);
}
/*
* @param owner table's owner'
* @param tableName eg: "test1"
* @param raw eg: "[{\"account\":\"zU42yDW3fzFjGWosdeVjVasyPsF4YHj224\", \"id\":0}, {\"account\":\"zU42yDW3fzFjGWosdeVjVasyPsF4YHj224\", \"id\":1}, {\"account\":\"zU42yDW3fzFjGWosdeVjVasyPsF4YHj224\", \"id\":2}]"
*/
function insert(address owner, string tableName, string raw) public{
owner.insert(tableName, raw);
}
/*
* @param tableName eg: "test1"
* @param raw eg: "[{\"account\":\"zU42yDW3fzFjGWosdeVjVasyPsF4YHj224\", \"id\":0}, {\"account\":\"zU42yDW3fzFjGWosdeVjVasyPsF4YHj224\", \"id\":1}, {\"account\":\"zU42yDW3fzFjGWosdeVjVasyPsF4YHj224\", \"id\":2}]"
*/
function insert(string tableName, string raw) public {
msg.sender.insert(tableName, raw);
}
/*
* @param owner table's owner'
* @param tableName "test1"
* @param raw eg: "{\"id\":1}"
*/
function deletex(address owner, string tableName, string raw) public {
owner.deletex(tableName, raw);
}
/*
* @param tableName "test1"
* @param raw eg: "{\"id\":1}"
*/
function deletex(string tableName, string raw) public {
msg.sender.deletex(tableName, raw);
}
/*
* @param owner table's owner'
* @param tableName eg: "test1"
* @param rawUpdate eg: "{\"age\":15}"
* @param rawGet eg: "{\"id\": 2}"
*/
function update(address owner, string tableName, string rawUpdate, string rawGet) public{
owner.update(tableName, rawUpdate, rawGet);
}
/*
* @param tableName eg: "test1"
* @param rawUpdate eg: "{\"age\":15}"
* @param rawGet eg: "{\"id\": 2}"
*/
function update(string tableName, string rawUpdate, string rawGet) public{
msg.sender.update(tableName, rawUpdate, rawGet);
}
/*
* @param tableName eg: "test1"
* @param tableNameNew eg: "testNew1"
*/
function rename(string tableName, string tableNameNew) public{
msg.sender.rename(tableName, tableNameNew);
}
/*
* @param toWho ethereum address to be granted. need convert chainsql addr 2 ethereum addr .eg: "0xzzzzzzzzzzzzzzzzzzzzBZbvji"
* @param tableName eg: "test1"
* @param raw eg: "{\"insert\":false, \"delete\":false}"
*/
function grant(address toWho, string tableName, string raw) public{
return msg.sender.grant(toWho, tableName, raw);
}
function sqlTransaction(string tableName) public{
db.beginTrans();
msg.sender.create(tableName, "[{\"field\":\"id\", \"type\" : \"int\", \"length\" : 11, \"PK\" : 1, \"NN\" : 1, \"UQ\" : 1}, { \"field\":\"account\", \"type\" : \"varchar\" }, { \"field\":\"age\", \"type\" : \"int\" }]");
msg.sender.insert(tableName, "[{\"account\":\"zU42yDW3fzFjGWosdeVjVasyPsF4YHj224\", \"id\":1}, {\"account\":\"zU42yDW3fzFjGWosdeVjVasyPsF4YHj224\", \"id\":2}]");
msg.sender.deletex(tableName, "{\"id\":1}");
msg.sender.update(tableName, "{\"account\":\"id==2\"}", "{\"id\": 2}");
db.commit();
}
/*
* @param owner table's owner'
* @param tableName eg: "test1"
* @param raw eg: ""
*/
function get(address owner, string tableName, string raw) public view returns(string) {
uint256 handle = owner.get(tableName, raw);
require(handle != uint256(0), "Get table data failed,maybe user not authorized!");
uint row = db.getRowSize(handle);
uint col = db.getColSize(handle);
string memory xxx;
for(uint i=0; i<row; i++)
{
for(uint j=0; j<col; j++)
{
string memory y = (db.getValueByIndex(handle, i, j));
xxx = concat(xxx, y);
if(j != col - 1)
xxx = concat(xxx, ", ");
}
xxx = concat(xxx, ";\n");
}
return xxx;
}
/*
* @param owner table's owner'
* @param tableName eg: "test1"
* @param raw eg: ""
* @param field eg: "id"
*/
function get(address owner, string tableName, string raw, string field) public view returns(string) {
uint256 handle = owner.get(tableName, raw);
require(handle != uint256(0), "Get table data failed,maybe user not authorized!");
uint row = db.getRowSize(handle);
string memory xxx;
for(uint i=0; i<row; i++)
{
string memory y = (db.getValueByKey(handle, i, field));
xxx = concat(xxx, y);
xxx = concat(xxx, ";");
}
return xxx;
}
function concat(string _base, string _value) internal pure returns (string) {
bytes memory _baseBytes = bytes(_base);
bytes memory _valueBytes = bytes(_value);
string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length);
bytes memory _newValue = bytes(_tmpValue);
uint j = 0;
for(uint i=0; i<_baseBytes.length; i++) {
_newValue[j++] = _baseBytes[i];
}
for(uint i=0; i<_valueBytes.length; i++) {
_newValue[j++] = _valueBytes[i];
}
return string(_newValue);
}
}