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.
- velocity/__init__.py +3 -0
- velocity/aws/__init__.py +24 -0
- velocity/aws/context.py +0 -0
- velocity/aws/handlers/__init__.py +2 -0
- velocity/aws/handlers/lambda_handler.py +123 -0
- velocity/aws/handlers/sqs_handler.py +48 -0
- velocity/db/__init__.py +5 -0
- velocity/db/core/__init__.py +0 -0
- velocity/db/core/column.py +202 -0
- velocity/db/core/database.py +65 -0
- velocity/db/core/decorators.py +81 -0
- velocity/db/core/engine.py +367 -0
- velocity/db/core/exceptions.py +31 -0
- velocity/db/core/result.py +137 -0
- velocity/db/core/row.py +191 -0
- velocity/db/core/sequence.py +33 -0
- velocity/db/core/table.py +448 -0
- velocity/db/core/transaction.py +178 -0
- velocity/db/servers/__init__.py +0 -0
- velocity/db/servers/mysql.py +575 -0
- velocity/db/servers/postgres.py +1275 -0
- velocity/db/servers/sql.py +558 -0
- velocity/db/servers/sqlite.py +899 -0
- velocity/db/servers/sqlserver.py +821 -0
- velocity/misc/__init__.py +0 -0
- velocity/misc/conv.py +420 -0
- velocity/misc/db.py +85 -0
- velocity/misc/export.py +147 -0
- velocity/misc/format.py +81 -0
- velocity/misc/mail.py +67 -0
- velocity/misc/timer.py +27 -0
- velocity_python-0.0.1.dist-info/LICENSE +19 -0
- velocity_python-0.0.1.dist-info/METADATA +181 -0
- velocity_python-0.0.1.dist-info/RECORD +36 -0
- velocity_python-0.0.1.dist-info/WHEEL +5 -0
- velocity_python-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -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
|