forked from fernandojunior/python-sqlite-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorm.py
More file actions
140 lines (107 loc) · 4.51 KB
/
orm.py
File metadata and controls
140 lines (107 loc) · 4.51 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
'''
A Python object relational mapper for SQLite.
Author: Fernando Felix do Nascimento Junior
License: MIT License
Homepage: https://github.com/fernandojunior/python-sqlite-orm
'''
import sqlite3
def cut_attrs(obj, keys):
return dict(i for i in vars(obj).items() if i[0] not in keys)
def render_schema(model): # factory method to create table schemas for models
schema = 'create table {table} (id integer primary key autoincrement, {columns});' # noqa
datatypes = {str: 'text', int: 'integer', float: 'real'}
iscol = lambda key, value: key[0] is not '_' and value in datatypes.keys()
colrender = lambda key, value: '%s %s' % (key, datatypes[value])
cols = [colrender(*i) for i in vars(model).items() if iscol(*i)]
values = {'table': model.__name__, 'columns': ', '.join(cols)}
return schema.format(**values)
class Database(object): # proxy class to access sqlite3.connect method
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
self.connected = False
self.Model = type('Model%s' % str(self), (Model,), {'db': self})
@property
def connection(self):
if self.connected:
return self._connection
self._connection = sqlite3.connect(*self.args, **self.kwargs)
self._connection.row_factory = sqlite3.Row
self.connected = True
return self._connection
def close(self):
if self.connected:
self.connection.close()
self.connected = False
def commit(self):
self.connection.commit()
def execute(self, sql, *args):
return self.connection.execute(sql, args)
def executescript(self, script):
self.connection.cursor().executescript(script)
self.commit()
class Manager(object): # data mapper interface (generic repository) for models
def __init__(self, db, model):
self.db = db
self.model = model
self.tablename = model.__name__
if not self._hastable():
self.db.executescript(render_schema(self.model))
def all(self):
cursor = self.db.execute('select * from %s' % self.tablename)
return (self.create(**row) for row in cursor.fetchall())
def create(self, **kwargs):
obj = object.__new__(self.model)
obj.__dict__ = kwargs
return obj
def delete(self, obj):
sql = 'DELETE from %s WHERE id = ?'
self.db.execute(sql % self.tablename, obj.id)
def get(self, id):
sql = 'select * from %s where id = ?' % self.tablename
cursor = self.db.execute(sql, id)
row = cursor.fetchone()
if not row:
msg = 'Object%s with id does not exist: %s' % (self.model, id)
raise ValueError(msg)
return self.create(**row)
def has(self, id):
sql = 'select id from %s where id = ?' % self.tablename
cursor = self.db.execute(sql, id)
return True if cursor.fetchall() else False
def save(self, obj):
if hasattr(obj, 'id') and self.has(obj.id):
msg = 'Object%s id already registred: %s' % (self.model, obj.id)
raise ValueError(msg)
copy_ = cut_attrs(obj, 'id')
keys = '(%s)' % ', '.join(copy_.keys()) # (key1, ...)
refs = '(%s)' % ', '.join('?' for i in range(len(copy_))) # (?, ...)
sql = 'insert into %s %s values %s' % (self.tablename, keys, refs)
cursor = self.db.execute(sql, *copy_.values())
obj.id = cursor.lastrowid
return obj
def update(self, obj):
copy_ = cut_attrs(obj, 'id')
keys = '= ?, '.join(copy_.keys()) + '= ?' # key1 = ?, ...
sql = 'UPDATE %s SET %s WHERE id = ?' % (self.tablename, keys)
self.db.execute(sql, *(list(copy_.values()) + [obj.id]))
def _hastable(self):
sql = 'select name len FROM sqlite_master where type = ? AND name = ?'
cursor = self.db.execute(sql, 'table', self.tablename)
return True if cursor.fetchall() else False
class Model(object): # abstract entity model with an active record interface
db = None
def delete(self):
return self.__class__.manager().delete(self)
def save(self):
return self.__class__.manager().save(self)
def update(self):
return self.__class__.manager().update(self)
@property
def public(self):
return dict(i for i in vars(self).items() if i[0][0] is not '_')
def __repr__(self):
return str(self.public)
@classmethod
def manager(cls, db=None):
return Manager(db if db else cls.db, cls)