device.py 3.39 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
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
    @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,
                    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,)




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


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


    @staticmethod
    @with_psql
    def get(cur, appkey, dev_id):
116
        tn = 'devices_' +str(appkey)
117
        query = """
118
119
        SELECT * FROM 
            {}
120
121
122
        WHERE
            dev_id = %s
        """
123
124
        cur.execute(
            sql.SQL(query).format(sql.Identifier(tn)), [dev_id])
125
126
127
128
129
130
131
132
133
134
135
        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):
136
        tn = 'devices_' +str(appkey)
137
        query = """
138
139
        SELECT * FROM 
            {}
140
        """
141
142
        cur.execute(
            sql.SQL(query).format(sql.Identifier(tn)))
143
144
        return (True, cur.fetchall())