velocity-python 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of velocity-python might be problematic. Click here for more details.

@@ -0,0 +1,178 @@
1
+ import re
2
+ from velocity.db.core.row import Row
3
+ from velocity.db.core.table import Table
4
+ from velocity.db.core.result import Result
5
+ from velocity.db.core.column import Column
6
+ from velocity.db.core.database import Database
7
+ from velocity.db.core.sequence import Sequence
8
+ from velocity.misc.db import randomword
9
+
10
+
11
+ debug = False
12
+
13
+ class Transaction(object):
14
+
15
+ def __init__(self, engine, connection=None):
16
+ self.engine = engine
17
+ self.connection = connection
18
+
19
+ def __str__(self):
20
+ c = self.engine.config
21
+ server = c.get('host',c.get('server'))
22
+ database = c.get('database')
23
+ return "{}.transaction({}:{})".format(
24
+ self.engine.sql.server,
25
+ server,
26
+ database)
27
+
28
+ def __enter__(self):
29
+ return self
30
+
31
+ def __exit__(self, exc_type, exc_val, exc_tb):
32
+ if exc_type:
33
+ self.rollback()
34
+ self.close()
35
+
36
+ def cursor(self):
37
+ if not self.connection:
38
+ if debug:
39
+ print('>>> {} open connection to {database}'.format(id(self), **self.engine.config))
40
+ self.connection = self.engine.connect()
41
+ if debug:
42
+ print("*** {} open {database}.transaction.connection.cursor".format(id(self), **self.engine.config))
43
+ return self.connection.cursor()
44
+
45
+ def close(self):
46
+ if self.connection:
47
+ self.commit()
48
+ if debug:
49
+ print('<<< {} close connection to {database}'.format(id(self), **self.engine.config))
50
+ self.connection.close()
51
+
52
+ def execute(self, sql, parms=None, single=False, cursor=None):
53
+ return self._execute(sql, parms=parms, single=single, cursor=cursor)
54
+
55
+ def _execute(self, sql, parms=None, single=False, cursor=None):
56
+ if single:
57
+ cursor = None
58
+ if not self.connection:
59
+ if debug:
60
+ print('>>> {} open connection to {database}'.format(id(self), **self.engine.config))
61
+ self.connection = self.engine.connect()
62
+ action = re.search(r'(\w+)', sql, re.I)
63
+ if action:
64
+ action = action.group().lower()
65
+ else:
66
+ action = 'None'
67
+ if debug:
68
+ print(action)
69
+ print(id(self), '------------>', sql, '::', parms)
70
+ print()
71
+ if single:
72
+ self.commit()
73
+ self.connection.autocommit = True
74
+ if not cursor:
75
+ cursor = self.cursor()
76
+ try:
77
+ if parms:
78
+ cursor.execute(sql, parms)
79
+ else:
80
+ cursor.execute(sql)
81
+ except:
82
+ self.engine.ProcessError(sql,parms)
83
+ if single:
84
+ self.connection.autocommit = False
85
+ return Result(cursor)
86
+
87
+ def server_execute(self, sql, parms=None):
88
+ return self._execute(sql, parms, cursor=self.cursor())
89
+
90
+ def commit(self):
91
+ if self.connection:
92
+ if debug:
93
+ print('{} --- connection commit {database}'.format(id(self), **self.engine.config))
94
+ self.connection.commit()
95
+
96
+ def rollback(self):
97
+ if self.connection:
98
+ if debug:
99
+ print('{} --- connection rollback {database}'.format(id(self), **self.engine.config))
100
+ self.connection.rollback()
101
+
102
+ def create_savepoint(self, sp=None, cursor=None):
103
+ if not sp:
104
+ sp = randomword()
105
+ sql, vals = self.engine.sql.create_savepoint(sp)
106
+ if sql:
107
+ self._execute(sql, vals, cursor=cursor)
108
+ return sp
109
+
110
+ def release_savepoint(self, sp=None, cursor=None):
111
+ sql, vals = self.engine.sql.release_savepoint(sp)
112
+ if sql:
113
+ self._execute(sql, vals, cursor=cursor)
114
+
115
+ def rollback_savepoint(self, sp=None, cursor=None):
116
+ sql, vals = self.engine.sql.rollback_savepoint(sp)
117
+ if sql:
118
+ self._execute(sql, vals, cursor=cursor)
119
+
120
+ def database(self, name=None):
121
+ return Database(self, name)
122
+
123
+ def table(self, tablename):
124
+ return Table(self, tablename)
125
+
126
+ def sequence(self, name):
127
+ return Sequence(self, name)
128
+
129
+ def row(self, tablename, pk):
130
+ """
131
+ Returns exactly one row based on primary key.
132
+ raise exception if primary key not provided.
133
+ """
134
+ return Row(self.table(tablename), pk)
135
+
136
+ def get(self, tablename, where):
137
+ """
138
+ Search for row. return row if 1 found.
139
+ raise exception if duplicates found.
140
+ return new if not found. (creates new/use find otherwise)
141
+ """
142
+ return self.table(tablename).get(where)
143
+
144
+ def find(self, tablename, where):
145
+ """
146
+ Search for row. return row if 1 found.
147
+ raise exception if duplicates found.
148
+ return {} if not found. (Does not create new)
149
+ """
150
+ return self.table(tablename).find(where)
151
+
152
+ def column(self, tablename, colname):
153
+ return Column(self.table(tablename), colname)
154
+
155
+ def current_database(self):
156
+ sql, vals = self.engine.sql.current_database()
157
+ return self.execute(sql, vals).scalar()
158
+
159
+ def vacuum(self, analyze=True, full=False, reindex=True):
160
+ self.connection = self.engine.connect()
161
+ old_isolation_level = self.connection.isolation_level
162
+ self.connection.set_isolation_level(0)
163
+ sql = ["VACUUM"]
164
+ if full:
165
+ sql.append('FULL')
166
+ if analyze:
167
+ sql.append('ANALYZE')
168
+ self.execute(' '.join(sql))
169
+ print(self.current_database())
170
+ if reindex:
171
+ database = self.engine.config.get('database')
172
+ self.execute("REINDEX DATABASE {}".format(database))
173
+ self.connection.set_isolation_level(old_isolation_level)
174
+
175
+ def tables(self):
176
+ sql, vals = self.engine.sql.tables()
177
+ result = self.execute(sql, vals)
178
+ return ["%s.%s" % x for x in result.as_tuple()]
File without changes