device.py 3.44 KB
Newer Older
1
import psycopg2
2
from psycopg2 import sql
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
import bcrypt


class DeviceDao:
    
    def __init__(self):
        pass

    # decorator implementation
    def with_psql(f):
        def _with_psql(*args, **kwargs):
            conn = psycopg2.connect('dbname=gateway')
            cur = conn.cursor()

            try:
                res = f(cur, *args, **kwargs)
            except (Exception, psycopg2.DatabaseError) as error:
                conn.rollback()
                res = (False, error)
            else:
                conn.commit()
            finally:
                cur.close()
                conn.close()
    
            return res
        return _with_psql

31
32
33
34
35
36
37
38
    @staticmethod
    @with_psql
    def create_datatable(cur, appkey, dev_id):
        tn = 'dev_' +str(appkey)+ '_' +str(dev_id)
        cur.execute(
            sql.SQL(
                """CREATE TABLE {} (
                    utc NUMERIC(10) NOT NULL,
Vladislav Rykov's avatar
Vladislav Rykov committed
39
                    timedate VARCHAR(100) NOT NULL,
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
                    data json NOT NULL
                )"""
            ).format(sql.Identifier(tn)))
        return (True,)

    
    @staticmethod
    @with_psql
    def delete_datatable(cur, appkey, dev_id):
        tn = 'dev_' +str(appkey)+ '_' +str(dev_id)
        cur.execute(
            psycopg2.sql.SQL(
                "DROP TABLE {}"
            ).format(sql.Identifier(tn)))
        return (True,)

    @staticmethod
    @with_psql
    def create_table(cur, appkey):
        tn = 'devices_' +str(appkey)
        cur.execute(
            sql.SQL(
                """CREATE TABLE {} (
                    name VARCHAR(30) NOT NULL,
                    dev_id NUMERIC(3) PRIMARY KEY,
                    description VARCHAR(200)
                )"""
            ).format(sql.Identifier(tn)))
        return (True,)

    
    @staticmethod
    @with_psql
    def delete_table(cur, appkey):
        tn = 'devices_' +str(appkey)
        cur.execute(
            psycopg2.sql.SQL(
                "DROP TABLE {}"
            ).format(sql.Identifier(tn)))
        return (True,)




84
85
86
    @staticmethod
    @with_psql
    def create(cur, name, dev_id, appkey, desc):
87
        tn = 'devices_' +str(appkey)
88
89
        query = """
        INSERT INTO 
90
            {}
91
        VALUES
92
            (%s, %s, %s)
93
        """
94
95
        cur.execute(
            sql.SQL(query).format(sql.Identifier(tn)), [name, dev_id, desc])
96
97
98
99
100
101
        return (True,)


    @staticmethod
    @with_psql
    def delete(cur, appkey, dev_id):
102
        tn = 'devices_' +str(appkey)
103
104
        query = """
        DELETE FROM 
105
            {}
106
107
108
        WHERE
            dev_id = %s
        """
109
110
        cur.execute(
            sql.SQL(query).format(sql.Identifier(tn)), [dev_id])
111
112
113
114
115
116
        return (True,)


    @staticmethod
    @with_psql
    def get(cur, appkey, dev_id):
117
        tn = 'devices_' +str(appkey)
118
        query = """
119
120
        SELECT * FROM 
            {}
121
122
123
        WHERE
            dev_id = %s
        """
124
125
        cur.execute(
            sql.SQL(query).format(sql.Identifier(tn)), [dev_id])
126
127
128
129
130
131
132
133
134
135
136
        dev = cur.fetchone()
        
        if (dev is None):
            return (False, 'There is no device with dev_id = {}'.format(dev_id))
        else:
            return (True, dev)


    @staticmethod
    @with_psql
    def get_list(cur, appkey):
137
        tn = 'devices_' +str(appkey)
138
        query = """
139
140
        SELECT * FROM 
            {}
141
        """
142
143
        cur.execute(
            sql.SQL(query).format(sql.Identifier(tn)))
144
145
        return (True, cur.fetchall())