/
DatabaseOp.py
executable file
·260 lines (211 loc) · 8.29 KB
/
DatabaseOp.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/local/bin/python
##
# Project: pHOG
# Module: DatabaseOp
# Desc.: This module imports data stored in the db, creates tables, inserts
# entries according to the passed values
# Date: 18.02,02
# Modification history
# 19.02,02
# There are several issues not addressed:
# 1. No real error handeling mechanisms
# 2. The table creation method has no idea if the table exisited
# 3. The insert method doesn't check if the record intended for insertion is
# redundant.
# 10.03.02
# Implement the second connect method, reading configuration from a file.
# 25.03.02
# Deal with password input from config file
##
import pg;
import _pg;
import sys;
# GLOBAL
connection = "";
##
# Read configuration file and return a dictionary. The ending "\n" is omitted.
##
def get_config(configFile,operation):
configLines = open(configFile,"r").readlines()
config = {}
inOperation = 0
for i in configLines:
#allow documentation and empty string, match operation
#print operation,i[:-1]
if i[0] == ">":
if i[1:-1] != operation:
if inOperation:
break
else:
continue
elif i[1:-1] == operation:
inOperation = 1
continue
#assign values to dict
if inOperation and i[0] != "#" and i != "" and i != "\n":
if i[-1] == "\n":
config[i[:i.find(" ")]] = i[i.find(" ")+1:-1]
else:
config[i[:i.find(" ")]] = i[i.find(" ")+1:]
return config
##
# Establish connection, need 4 parameters for it to work.
#
# @param config - the dictionary with connection configuration
##
def connect(config):
global connection;
print ("Establish connection...")
connection = pg.connect(dbname = config["dbname"],
host = config["host"],
port = config["port"],
user = config["user"])
print (" Host = "+ connection.host)
print (" DB = "+ connection.db)
#print " Port = %i" % connection.port
#print " Status = %i\n" % connection.status
return connection;
##
# In some situations, only dbname is passed.
##
def qconnect(config):
global connection
print ("Establish quick connection...")
connection = pg.connect(dbname = config["dbname"])
print (" Host :",connection.host )
print (" Database:",connection.db)
return connection
##
# Establish connection. The config is in a file instead of a dictionary passed
# like above. The config file contains <config_name> <space> <value>
#
# @param configFile - the file with configuration information
# @param operation - read config from a particular operation
# @return config - the config dictionary
##
def configConnect(configFile, operation):
global connection
print ("Establish connection..")
configLines = open(configFile,"r").readlines()
#store lines into a dict
config = {}
inOperation = 0
for i in configLines:
#allow documentation and empty string, match operation
#print operation,i[:-1]
if i[0] == ">":
if i[1:-1] != operation:
if inOperation:
break
else:
continue
elif i[1:-1] == operation:
inOperation = 1
continue
#assign values to dict
if inOperation and i[0] != "#" and i != "" and i != "\n":
if i[-1] == "\n":
config[i[:i.find(" ")]] = i[i.find(" ")+1:-1]
else:
config[i[:i.find(" ")]] = i[i.find(" ")+1:]
if config.has_key("pass"):
connection = pg.connect(dbname = config["dbname"],
host = config["host"],
port = int(config["port"]),
user = config["user"],
passwd = config["pass"])
else:
connection = pg.connect(dbname = config["dbname"],
host = config["host"],
port = int(config["port"]),
user = config["user"])
print (" Host = "+ connection.host)
print (" DB = "+ connection.db)
#print " Port = %i" % connection.port
#print " Status = %i\n" % connection.status
return config
##
# Close connection
##
def close():
connection.close();
##
# Will send query to database according the passed argument. The sql command
# connect take the following arguments
# connect([dbname], [host], [port], [opt], [tty], [user], [passwd])
#
# @param queryStr - the query string
# @return qTuple - the tuple returned from database query
##
def select(queryStr,flag=0):
if flag:
print (queryStr)
qTuple = (connection.query(queryStr)).getresult()
return qTuple
def select2(fields,table,flag=0):
queryStr = "SELECT %s FROM %s" % (fields,table)
if flag:
print (queryStr)
qTuple = (connection.query(queryStr)).getresult()
return qTuple
##
# Send query
##
def query(queryStr):
#print "'%s'" % queryStr
connection.query(queryStr)
##
# Creates a database with the specified name and fields
#
# @param tbName - name of the table to be created
# @param fields - a nested list contains the attributes and corresponding types
##
def createTable(tbName, fields):
queryStr = "CREATE TABLE %s (" % tbName
for i in fields:
for j in i:
queryStr = queryStr+j+" "
queryStr = queryStr[:-1]+","
queryStr = queryStr[:-1]+");"
print (queryStr)
try:
connection.query(queryStr)
print ("%s created successfully\n" % tbName)
return 1 #success
except:
print ("%s already exist\n" % tbName)
##
# Adding multiple entries to a database
# @param dbName name of the database to be inserted
#
# @param attrs a string contains the attributes in the same order as the
# values to be inserted.
# @param values a nested list contains the attribute values, each element
# contains one entry (with multiple values) to be inserted
# @param flag print debug string or not
##
def insert(tbName, attrs, values, flag = 0):
for i in values:
if flag:
print (i)
queryStr = "INSERT INTO %s (%s) VALUES (" % (tbName, attrs)
for j in i:
queryStr = queryStr+j+",";
queryStr = queryStr[:-1]+");";
if flag:
print (" ",queryStr)
connection.query(queryStr);
return 1 #success
##
# Not tested yet
##
def dump(table):
qTuple = self.dbtask.select("SELECT * FROM %s"% self.config[table])
oup = open(self.config[table]+".dump","w")
for i in qTuple:
out_str = ""
for j in i:
if type(j) == type(1):
j = str(j)
out_str = out_str + j + "\t"
oup.write(out_str[:-1]+"\n")