-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlo.py
More file actions
executable file
·44 lines (34 loc) · 1.01 KB
/
Copy pathlo.py
File metadata and controls
executable file
·44 lines (34 loc) · 1.01 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
#!/usr/local/bin/python3 -bb
#import sys
#import postgresql
#import ftnconfig
#db=ftnconfig.connectdb()
#lo=sys.argv[1]
#cmd=sys.argv[2]
def lo_save(db, lo, f):
db.execute("begin")
lo_h=db.prepare("select lo_open($1, 131072)").first(lo)
l=0
while True:
d = db.prepare("select loread($1, 1024*1024)").first(lo_h)
if not d:
break
print (len(d), "bytes read")
print(f.write(d), "bytes written")
l+=len(d)
db.prepare("select lo_close($1)").first(lo_h)
db.execute("commit")
return l
import io
class LOIOReader(io.RawIOBase):
def __init__(self, db, lo_id):
self.db = db.clone() # lo functions require own transaction context
self.lo_id = lo_id
self.db.execute("begin")
self.lo_h = self.db.prepare("select lo_open($1, 131072)").first(self.lo_id)
def read(self, n):
return self.db.prepare("select loread($1, $2)").first(self.lo_h, n)
def __del__(self):
self.db.prepare("select lo_close($1)").first(self.lo_h)
self.db.execute("commit")
self.db.close()