comparison SMART/Java/Python/mySql/MySqlConnection.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents 769e306b7933
children
comparison
equal deleted inserted replaced
17:b0e8584489e6 18:94ab73e8a190
37 class MySqlConnection(object): 37 class MySqlConnection(object):
38 """Connection to a database""" 38 """Connection to a database"""
39 39
40 def __init__(self, verbosity = 0): 40 def __init__(self, verbosity = 0):
41 self.verbosity = verbosity 41 self.verbosity = verbosity
42 self.databaseName = "%s%ssmartdb%d" % (os.environ.get("SMARTTMPPATH", "."), os.sep, random.randint(0, 100000)) 42 self.databaseName = os.path.join(os.environ.get("SMARTTMPPATH", "."), "smartdb%d" % random.randint(0, 100000))
43 self.connection = sqlite3.connect(self.databaseName) 43 self.connection = sqlite3.connect(self.databaseName)
44 self.executeQuery("PRAGMA journal_mode = OFF") 44 self.executeQuery("PRAGMA journal_mode = OFF")
45 self.executeQuery("PRAGMA synchronous = 0") 45 self.executeQuery("PRAGMA synchronous = 0")
46 self.executeQuery("PRAGMA locking_mode = EXCLUSIVE") 46 self.executeQuery("PRAGMA locking_mode = EXCLUSIVE")
47 self.executeQuery("PRAGMA count_change = OFF") 47 self.executeQuery("PRAGMA count_change = OFF")
86 for cpt, command in enumerate(commands): 86 for cpt, command in enumerate(commands):
87 query.execute(command) 87 query.execute(command)
88 self.connection.commit() 88 self.connection.commit()
89 89
90 90
91 def executeManyFormattedQueries(self, command, lines, insertion = False):
92 cursor = self.connection.cursor()
93 query = MySqlQuery(cursor, self.verbosity)
94 for line in lines:
95 result = query.executeFormat(command, line)
96 self.connection.commit()
97 if insertion:
98 return result
99 else:
100 return query
101
102
91 def executeManyQueriesIterator(self, table): 103 def executeManyQueriesIterator(self, table):
92 cursor = self.connection.cursor() 104 cursor = self.connection.cursor()
93 query = MySqlQuery(cursor, self.verbosity) 105 query = MySqlQuery(cursor, self.verbosity)
94 try: 106 try:
95 for command in table.getIterator(): 107 for command in table.getIterator():
99 for command in table.getIterator(): 111 for command in table.getIterator():
100 query.execute(command) 112 query.execute(command)
101 self.connection.commit() 113 self.connection.commit()
102 114
103 115
104 def executeFormattedQuery(self, command, *parameters): 116 def executeManyFormattedQueriesIterator(self, table):
105 cursor = self.connection.cursor() 117 cursor = self.connection.cursor()
106 query = MySqlQuery(cursor, self.verbosity) 118 query = MySqlQuery(cursor, self.verbosity)
107 query.executeFormat(command, parameters) 119 try:
120 for command, values in table.getIterator():
121 query.executeFormat(command, values)
122 self.connection.commit()
123 except:
124 for command, values in table.getIterator():
125 query.execute(command, values)
126 self.connection.commit()
127
128
129 def executeFormattedQuery(self, command, parameters, insertion = False):
130 cursor = self.connection.cursor()
131 query = MySqlQuery(cursor, self.verbosity)
132 result = query.executeFormat(command, parameters)
108 self.connection.commit() 133 self.connection.commit()
109 return query 134 if insertion:
135 return result
136 else:
137 return query