forked from nicholas-leonard/hypero
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgres.lua
More file actions
Latest commit
110 lines (102 loc) · 3.57 KB
/
Copy pathPostgres.lua
File metadata and controls
110 lines (102 loc) · 3.57 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
------------------------------------------------------------------------
--[[ Postgres ]]--
-- Simplified PostgreSQL database connection handler.
-- Uses the ~/.pgpass file for authentication
-- see (http://wiki.postgresql.org/wiki/Pgpass)
------------------------------------------------------------------------
localPostgres=torch.class("hypero.Postgres")
functionPostgres:__init(config)
config=configor {}
localargs, database, user, host, env, autocommit
=xlua.unpack(
{config},
'Postgres', 'Default is to get the connection string from an ' ..
'environment variable. For security reasons, and to allow ' ..
'for its serialization, no password is accepted. The password ' ..
'should be set in the ~/.pgpass file.',
{arg='database', type='string', help='name of postgres database'},
{arg='user', type='string', help='username used to connect to db'},
{arg='host', type='string', help='hostname/IP address of database'},
{arg='env', type='string', default='HYPER_PG_CONN'},
{arg='autocommit', type='boolean', default=true}
)
ifnot (databaseoruserorhost) then
self.connStr=os.getenv(env)
assert(self.connStr, "Environment variable HYPER_PG_CONN not set")
else
self.connStr=""
ifdatabasethenself.connStr=self.connStr.."dbname="..database.."" end
ifuserthenself.connStr=self.connStr.."user="..user.."" end
ifhostthenself.connStr=self.connStr.."host="..host.."" end
end
localenv=require('luasql.postgres'):postgres()
self.conn=assert(env:connect(self.connStr))
self.conn:setautocommit(autocommit)
self.autocommit=autocommit
end
functionPostgres:executeMany(command, param_list)
localresults, errs= {}, {}
localresult, err
fori, paramsinpairs (param_list) do
result, err=self:execute(command, params)
table.insert(results, result)
table.insert(errs, err)
end
returnresults, errs
end
functionPostgres:execute(command, params)
localresult, err
ifparamsthen
params=torch.type(params) =='table' andparamsor {params}
result, err=self.conn:execute(string.format(command, unpack(params)))
else
result, err=self.conn:execute(command)
end
returnresult, err
end
--mode : 'n' returns rows as array, 'a' returns them as key-value
functionPostgres:fetch(command, params, mode)
mode=modeor'n'
localcur, err=self:execute(command, params)
ifcurthen
localcoltypes=cur:getcoltypes()
localcolnames=cur:getcolnames()
localrow=cur:fetch({}, mode)
localrows= {}
whilerowdo
table.insert(rows, row)
row=cur:fetch({}, mode)
end
cur:close()
returnrows, coltypes, colnames
else
returnfalse, err
end
end
functionPostgres:fetchOne(command, params, mode)
mode=modeor'n'
localcur, err=self:execute(command, params)
ifcurthen
localcoltypes=cur:getcoltypes()
localcolname=cur:getcolnames()
localrow=cur:fetch({}, mode)
cur:close()
returnrow, coltypes, colnames
else
returnfalse, err
end
end
functionPostgres:close()
self.conn:close()
end
-- These two methods allow for (de)serialization of Postgres objects:
functionPostgres:write(file)
file:writeObject(self.connStr)
file:writeObject(self.autocommit)
end
functionPostgres:read(file, version)
self.connStr=file:readObject()
self.autocommit=file:readObject()
localenv=require('luasql.postgres'):postgres()
self.conn=assert(env:connect(self.connStr))
end