-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththechaindb.sql
273 lines (190 loc) · 6.36 KB
/
thechaindb.sql
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
DROP DATABASE IF EXISTS thechain;
CREATE DATABASE thechain TEMPLATE template0;
\c thechain;
create table userbase (
id serial primary key,
username varchar(40) unique not null,
passphrase varchar(200),
address varchar(200),
ethaddress varchar(200),
btcaddress varchar(200),
creation_date timestamp,
body jsonb,
organization jsonb
);
-- ALTER TABLE userbase ADD COLUMN btcaddress varchar(200);
CREATE TABLE txbase (
id serial primary key,
transactionid varchar(200),
body jsonb,
creation_date timestamp
);
CREATE TABLE organizationbase (
id serial primary key,
username varchar(40) unique not null,
name varchar(200),
owners jsonb,
members jsonb,
body jsonb,
creation_date timestamp
);
CREATE TABLE contractbase (
id serial primary key,
creator varchar(40),
username1 varchar(200),
username2 varchar(200),
address1 varchar(200),
address2 varchar(200),
contractaddress varchar(200),
transactionhash varchar(200),
body jsonb,
contract jsonb,
creation_date timestamp
);
CREATE TABLE contractbase (
id serial primary key,
body varchar(200),
creation_date timestamp
);
CREATE TABLE contractlog (
id serial primary key,
body jsonb
)
CREATE TABLE realtime(
id serial not null primary key,
sender varchar(40),
reciever varchar(40),
title varchar(200),
message text,
actions jsonb,
confirmations jsonb,
creation_date timestamp
);
INSERT INTO realtime(sender,reciever,title,message,creation_date)
VALUES ('testuser','dalio','invitation','invitation testorg',current_timestamp)
CREATE FUNCTION notify_realtime() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM pg_notify('addedrecord', TG_TABLE_NAME || ',id,' || NEW.id||',' || NEW.sender||',' || NEW.reciever||',' || NEW.title||',' || NEW.message ||',' || NEW.actions ||',' || NEW.confirmations );
RETURN NULL;
END;
$$;
CREATE trigger updated_realtime_trigger AFTER INSERT ON realtime
FOR EACH ROW EXECUTE PROCEDURE notify_realtime()
-------------------------------------------------------------------------
CREATE TABLE realtime(
id SERIAL NOT NULL PRIMARY KEY,
title character varying(128)
);
CREATE FUNCTION notify_realtime() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM pg_notify('addedrecord', NEW.title);
RETURN NULL;
END;
$$;
CREATE TRIGGER updated_realtime_trigger AFTER INSERT ON realtime
FOR EACH ROW EXECUTE PROCEDURE notify_realtime();
-------------------------------------------------------------------------
-- INSERT INTO userbase(btcaddress) VALUES ("n4dt26oCnDWeUexhGjxTHHwFJBVdGawngD") WHERE username='suero';
UPDATE userbase SET btcaddress = 'mr3wnUgRxJpEZPiHik2WHJdGDgqWtvuESn' WHERE username='serio';
INSERT INTO contractbase(creator,organization,parties,body,creation_date)
VALUES ('test')
INSERT INTO organizationbase(name,username,owners,members,body,creation_date)
VALUES ( )
INSERT INTO userbase(username,passphrase,address,creation_date,body)
VALUES ('testuser',
'U2FsdGVkX1+wkQMWy7VKFsOzMmv2by7yZa1+TFaPklgb/uXPQBL1VcnZ1guxlDdH1WoLVYacUd/I4F1qfyM7+Ges86olA0yTg1roffLXQLg/vnHfdQ6qPvETCDsXGnYIO8o3iNPqwYBmpJJs1kM6UzRqNgmdMpoDrMs1frs4vU0=',
'3MzMwvrenZgDAoCkHqVcvgRZDi8yRYzcQvm',
current_timestamp,
'{"username":"testuser","account_config":{"email":"[email protected]","searchable":"false"}}'
);
SELECT members->>''
-- INSERT INTO userbase(username,passphrase,creation_date,body)
-- VALUES (username,encrypted,current_timestamp, fdfds);
SELECT data->>'name' AS name FROM cards
SELECT body->>'passphrase' AS passphrase FROM userbase
SELECT passphrase FROM userbase WHERE username = 'testuser';
UPDATE userbase SET body->>'email' = 'Mora@mi' WHERE username='testuser';
UPDATE userbase
SET body = jsonb_set(body, '{email}', '"Mora@mi"', true),
body = jsonb_set(body, '{username}', '"fu"', true)
WHERE username='testuser';
UPDATE organizationbase
SET members = jsonb_set(
members::jsonb,
array['invited'],
(members->'invited')::jsonb || '["gordo"]'::jsonb)
WHERE username = 'misia_cinco';
-- ---------------------------------------------------------
To add the value use the JSON array append opperator (||)
UPDATE jsontesting
SET jsondata = jsondata || '["newString"]'::jsonb
WHERE id = 7;
Removing the value looks like this
UPDATE jsontesting
SET jsondata = jsondata - "newString"
WHERE id = 7;
Concatenating to a nested field looks like this
UPDATE jsontesting
SET jsondata = jsonb_set(
jsondata::jsonb,
array['nestedfield'],
(jsondata->'nestedfield')::jsonb || '["newString"]'::jsonb)
WHERE id = 7;
-- ---------------------------------------------------------
UPDATE jsontesting SET jsondata = (
CASE
WHEN jsondata IS NULL THEN '[]'::JSONB
ELSE jsondata
END
) || '["newString"]'::JSONB WHERE id = 7;
UPDATE userbase SET organization = (
CASE
WHEN organization IS NULL THEN '[]'::JSONB
ELSE organization
END
) || '["newString"]'::JSONB WHERE username = 'dalio';
UPDATE userbase SET organization = (CASE WHEN organization IS NULL THEN '[]'::JSONB ELSE organization END) || '["newString"]'::JSONB WHERE username = 'dalio';
UPDATE userbase
SET organization = organization - 'newString2'
WHERE username = 'dalio';
UPDATE organizationbase
SET members = jsonb_set(
members::jsonb,
array['invited'],
(members->'invited')::jsonb || '["gordo"]'::jsonb)
WHERE username = 'misia_cinco';
UPDATE organizationbase SET members = jsonb_set(members::jsonb,array['invited'],(members->'invited')::jsonb ||'["gordo"]'::jsonb) WHERE username = 'misia_cinco';
update userbase set
body = body || '{"city": "ottawa", "phone": "phonenum", "prefix": "prefixedName"}'
where number = 1;
SELECT usernma
SELECT *
FROM comments
WHERE session_id=1
AND user_id in (10,11,12,13,14,15,16);
SELECT username
FROM userbase
WHERE address in ('3MzMwvrenZgDAoCkHqVcvgRZDi8yRYzcQvm',
'3N2yczgEYMcMkuMCs52iBZfkCrPnSTRAoYw',
'3MqWgNDKeJhA8poSfAgXvhEhKTSzYVwkqx9',
'3MujfJGxKzRkDZ17BZ7KqSo3UhAxvNFyQfq' );
CREATE TABLE epictable
(
mytable_key serial primary key,
moobars VARCHAR(40) not null,
foobars DATE
);
CREATE TABLE pups (
ID SERIAL PRIMARY KEY,
name VARCHAR,
breed VARCHAR,
age INTEGER,
sex VARCHAR
);
INSERT INTO pups (name, breed, age, sex)
VALUES ('Tyler', 'Retrieved', 3, 'M');
'select 1 from (select username as username from userbase union all select username from organizationbase ) a where username = \''+user_input+'\';'