0
|
1 |
|
|
2 |
# |
|
3 |
# See LICENCE for detail |
|
4 |
# Copyright (c) 2014 IRI |
|
5 |
# |
|
6 |
|
|
7 |
import datetime |
|
8 |
import json |
|
9 |
import uuid |
|
10 |
|
|
11 |
def get_table_create_stmt(): |
|
12 |
return ( |
|
13 |
"CREATE TABLE IF NOT EXISTS annotations ( " |
|
14 |
"id serial PRIMARY KEY, " |
|
15 |
"uuid uuid UNIQUE, " |
|
16 |
"created timestamp default (now() at time zone 'utc') NOT NULL, " |
|
17 |
"ts timestamptz NOT NULL, " |
|
18 |
"event varchar(255) NOT NULL, " |
|
19 |
"channel varchar(255) NOT NULL, " |
|
20 |
"content json);" |
|
21 |
) |
|
22 |
|
|
23 |
def insert_annot_async(params, conn): |
|
24 |
|
|
25 |
content = params.get('content', {}) |
|
26 |
if not isinstance(content, basestring): |
|
27 |
params['content'] = json.dumps(content) |
|
28 |
if 'uuid' not in params: |
|
29 |
params['uuid'] = uuid.uuid4() |
|
30 |
|
|
31 |
if 'ts' not in params: |
|
32 |
params['ts'] = datetime.utcnow() |
|
33 |
|
|
34 |
defer = conn.runOperation("INSERT INTO annotations (uuid, ts, event, channel, content) VALUES (%(uuid)s, %(ts)s, %(event)s, %(channel)s, %(content)s)", params) |
|
35 |
defer.addCallback(lambda _: params) |
|
36 |
|
|
37 |
return defer |