annotate SMART/Java/Python/mySql/MySqlTable.py @ 38:2c0c0a89fad7

Uploaded
author m-zytnicki
date Thu, 02 May 2013 09:56:47 -0400
parents 44d5973c188c
children 169d364ddd91
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
1 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
2 # Copyright INRA-URGI 2009-2010
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
3 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
4 # This software is governed by the CeCILL license under French law and
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
5 # abiding by the rules of distribution of free software. You can use,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
6 # modify and/ or redistribute the software under the terms of the CeCILL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
7 # license as circulated by CEA, CNRS and INRIA at the following URL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
8 # "http://www.cecill.info".
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
9 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
10 # As a counterpart to the access to the source code and rights to copy,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
11 # modify and redistribute granted by the license, users are provided only
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
12 # with a limited warranty and the software's author, the holder of the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
13 # economic rights, and the successive licensors have only limited
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
14 # liability.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
15 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
16 # In this respect, the user's attention is drawn to the risks associated
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
17 # with loading, using, modifying and/or developing or reproducing the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
18 # software by the user in light of its specific status of free software,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
19 # that may mean that it is complicated to manipulate, and that also
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
20 # therefore means that it is reserved for developers and experienced
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
21 # professionals having in-depth computer knowledge. Users are therefore
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
22 # encouraged to load and test the software's suitability as regards their
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
23 # requirements in conditions enabling the security of their systems and/or
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
24 # data to be ensured and, more generally, to use and operate it in the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
25 # same conditions as regards security.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
26 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
27 # The fact that you are presently reading this means that you have had
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
28 # knowledge of the CeCILL license and that you accept its terms.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
29 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
30 import re
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
31 import sys
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
32
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
33 class MySqlTable(object):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
34 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
35 Store a table of a mySQL database, used for transcripts or exons
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
36 Record a a name and a type (int, float, double) for each column
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
37 @ivar name: name of the table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
38 @type name: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
39 @ivar variables: name of the columns
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
40 @type variables: list of string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
41 @ivar types: type of the columns
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
42 @type types: dict of string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
43 @ivar mySqlConnection: connection to a database
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
44 @type mySqlConnection: class L{MySqlConnection<MySqlConnection>}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
45 @ivar nbLines: number of rows
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
46 @type nbLines: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
47 @ivar verbosity: verbosity
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
48 @type verbosity: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
49 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
50
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
51 def __init__(self, connection, name, verbosity = 0):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
52 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
53 Constructor
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
54 Possibly retrieve column names and types if table exists
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
55 @param mySqlConnection: connection to a databas
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
56 @type mySqlConnection: class L{MySqlConnection<MySqlConnection>}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
57 @param name: name of the table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
58 @type name: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
59 @param verbosity: verbosity
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
60 @type verbosity: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
61 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
62 self.name = name
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
63 self.variables = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
64 self.types = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
65 self.sizes = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
66 self.nbLines = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
67 self.verbosity = verbosity
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
68 self.mySqlConnection = connection
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
69 queryTables = self.mySqlConnection.executeQuery("SELECT name FROM sqlite_master WHERE type LIKE 'table' AND name LIKE '%s'" % (self.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
70 self.created = not queryTables.isEmpty()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
71 if self.created:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
72 queryFields = self.mySqlConnection.executeQuery("PRAGMA table_info('%s')" % (name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
73 for field in queryFields.getIterator():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
74 if field[1] != "id":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
75 self.variables.append(field[1])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
76 self.types[field[1]] = field[2]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
77 self.sizes[field[1]] = field[3]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
78
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
79
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
80 def getName(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
81 return self.name
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
82
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
83
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
84 def create(self, variables, types, sizes):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
85 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
86 Create a table using give column names and types
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
87 @param variables: names of the columns
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
88 @type variables: list of string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
89 @param types: types of the columns
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
90 @type types: dict of string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
91 @param sizes: sizes of the types
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
92 @type sizes: dict of int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
93 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
94 self.variables = variables
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
95 self.types = types
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
96 self.sizes = sizes
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
97 if self.created:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
98 self.remove()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
99 query = "CREATE TABLE '%s' (id INTEGER PRIMARY KEY" % (self.name)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
100 for variable in variables:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
101 query = "%s, %s %s(%d)" % (query, variable, types[variable], sizes[variable])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
102 query += ")"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
103 self.mySqlConnection.executeQuery(query)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
104 self.created = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
105
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
106
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
107 def insertMany(self, lines):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
108 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
109 Insert many lines
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
110 @param lines: the list of values
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
111 @type lines: list of lists
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
112 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
113 commands = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
114 for values in lines:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
115 commands.append("INSERT INTO '%s' (%s) VALUES (%s)" % (self.name, ", ".join(self.variables), ", ".join([MySqlTable.formatSql(values[variable], self.types[variable], self.sizes[variable]) for variable in self.variables])))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
116 self.mySqlConnection.executeManyQueries(commands)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
117
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
118
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
119 def insertManyFormatted(self, lines):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
120 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
121 Insert many lines
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
122 @param lines: the list of values
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
123 @type lines: list of lists
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
124 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
125 replacer = ["?"] * len(self.variables)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
126 command = "INSERT INTO '%s' (%s) VALUES (%s)" % (self.name, ", ".join(self.variables), ", ".join(replacer))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
127 values = [[line[variable] for variable in self.variables] for line in lines]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
128 self.mySqlConnection.executeManyFormattedQueries(command, values)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
129
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
130
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
131 def rename(self, name):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
132 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
133 Rename the table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
134 @param name: the new name
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
135 @type name: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
136 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
137 self.mySqlConnection.executeQuery("RENAME TABLE '%s' TO '%s'" % (self.name, name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
138 self.name = name
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
139
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
140
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
141 def copy(self, table):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
142 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
143 Copy the given table this one
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
144 @param table: the table to be copied
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
145 @type table: class L{MySqlTable<MySqlTable>}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
146 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
147 variables = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
148 types = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
149 sizes = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
150 fields = self.mySqlConnection.executeQuery("PRAGMA table_info(%s)" % (table.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
151 for field in fields.getIterator():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
152 if field[1] != "id":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
153 variables.append(field[1])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
154 m = re.search(r"(\w+)\((\d+)\)", field[2])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
155 if m == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
156 raise Exception("\nFormat %s in table %s is strange." % (field[2], table.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
157 types[field[1]] = m.group(1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
158 sizes[field[1]] = int(m.group(2))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
159 self.create(variables, types, sizes)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
160 self.mySqlConnection.executeQuery("INSERT INTO '%s' SELECT * FROM %s" % (self.name, table.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
161
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
162
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
163 def add(self, table):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
164 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
165 Add the content of a table to this one
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
166 @param table: the table to be added
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
167 @type table: class L{MySqlTable<MySqlTable>}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
168 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
169 self.mySqlConnection.executeQuery("INSERT INTO '%s' SELECT * FROM %s" % (self.name, table.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
170 self.created = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
171
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
172
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
173 def exists(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
174 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
175 Check if the table exists in mySQL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
176 @return: true if it exits
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
177 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
178 return self.created
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
179
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
180
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
181 def remove(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
182 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
183 Remove this table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
184 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
185 if self.exists():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
186 query = "DROP TABLE IF EXISTS '%s'" % (self.name)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
187 self.mySqlConnection.executeQuery(query)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
188 self.created = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
189
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
190
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
191 def clear(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
192 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
193 Clear the content of this table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
194 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
195 self.mySqlConnection.executeQuery("DELETE FROM '%s'" % (self.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
196
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
197
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
198 def getNbElements(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
199 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
200 Count the number of rows in the table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
201 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
202 command = "SELECT COUNT(id) FROM '%s'" % (self.name)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
203 query = self.mySqlConnection.executeQuery(command)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
204 return int(query.getLine()[0])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
205
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
206
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
207 def formatSql(self, value, type, size):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
208 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
209 Format a value using MySQL encapsulation
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
210 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
211 if type.find("int") != -1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
212 return "%d" % value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
213 if type.find("float") != -1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
214 return "%.10f" % value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
215 if type.find("double") != -1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
216 return "%.20f" % value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
217 if type.find("varchar") != -1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
218 if len(value) > size:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
219 return "'%s'" % value[0:size]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
220 return "'%s'" % value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
221 raise Exception("Do not understand type %s" % (type))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
222 formatSql = classmethod(formatSql)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
223
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
224
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
225 def addLine(self, values):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
226 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
227 Add a row to this table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
228 @param values: the values of the row
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
229 @type values: dict
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
230 @return: the id of the added row
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
231 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
232 sqlValues = [values[variable] for variable in self.variables]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
233 command = "INSERT INTO '%s' (%%s) VALUES (%s)" % (self.name, ", ".join(self.variables))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
234 id = self.mySqlConnection.executeFormattedQueryQuery(command, sqlValues, True)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
235 return id
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
236 sqlValues = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
237 for variable in self.variables:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
238 sqlValues.append(self.formatSql(values[variable], self.types[variable], self.sizes[variable]))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
239 command = "INSERT INTO '%s' (%s) VALUES (%s)" % (self.name, ", ".join(self.variables), ", ".join(sqlValues))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
240 id = self.mySqlConnection.executeQuery(command, True)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
241 return id
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
242
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
243
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
244 def retrieveFromId(self, id):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
245 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
246 Retrieve a row from its id
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
247 @param id: the id of the row
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
248 @type id: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
249 @return: the row
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
250 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
251 query = self.mySqlConnection.executeQuery("SELECT * FROM '%s' WHERE id = %d" % (self.name, id))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
252 result = query.getLine()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
253 if result == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
254 raise Exception("Error! Id %d is not in the table %s!" % (id, self.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
255 return result
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
256
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
257
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
258 def retrieveBulkFromId(self, ids):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
259 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
260 Retrieve a row from its id
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
261 @param id: the ids of the row
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
262 @type id: list of int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
263 @return: the row
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
264 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
265 if not ids:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
266 return []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
267 MAXSIZE = 1000
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
268 results = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
269 for batch in range(len(ids) / MAXSIZE + 1):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
270 theseIds = ids[batch * MAXSIZE : (batch+1) * MAXSIZE]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
271 if theseIds:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
272 query = self.mySqlConnection.executeQuery("SELECT * FROM '%s' WHERE id IN (%s)" % (self.name, ", ".join(["%d" % (id) for id in theseIds])))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
273 lines = query.getLines()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
274 if len(lines) != len(theseIds):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
275 raise Exception("Error! Some Ids of (%s) is are missing in the table '%s' (got %d instead of %d)!" % (", ".join(["%d" % (id) for id in theseIds]), self.name, len(lines)), len(theseIds))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
276 results.extend(lines)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
277 return results
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
278
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
279
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
280 def removeFromId(self, id):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
281 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
282 Remove a row from its id
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
283 @param id: the id of the row
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
284 @type id: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
285 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
286 self.mySqlConnection.executeQuery("DELETE FROM '%s' WHERE id = %d" % (self.name, id))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
287
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
288
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
289 def getIterator(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
290 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
291 Iterate on the content of table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
292 @return: iterator to the rows of the table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
293 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
294 if not self.created:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
295 return
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
296 MAXSIZE = 1000
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
297 query = self.mySqlConnection.executeQuery("SELECT count(id) FROM '%s'" % (self.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
298 nbRows = int(query.getLine()[0])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
299 for chunk in range((nbRows / MAXSIZE) + 1):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
300 query = self.mySqlConnection.executeQuery("SELECT * FROM '%s' LIMIT %d, %d" % (self.name, chunk * MAXSIZE, MAXSIZE))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
301 for line in query.getIterator():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
302 yield line
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
303
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
304
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
305 def createIndex(self, indexName, values, unique = False, fullText = False):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
306 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
307 Add an index on the table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
308 @param indexName: name of the index
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
309 @type indexName: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
310 @param values: values to be indexed
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
311 @type values: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
312 @param unique: if the index is unique
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
313 @type unique: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
314 @param fullText: whether full text should be indexed
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
315 @type fullText: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
316 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
317 self.mySqlConnection.executeQuery("CREATE %s%sINDEX '%s' ON '%s' (%s)" % ("UNIQUE " if unique else "", "FULLTEXT " if fullText else "", indexName, self.name, ", ".join(values)))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
318
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
319
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
320 def setDefaultTagValue(self, field, name, value):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
321 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
322 Add a tag value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
323 @param name: name of the tag
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
324 @type name: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
325 @param value: value of the tag
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
326 @type value: string or int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
327 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
328 newData = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
329 for line in MySqlTable.getIterator(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
330 id = line[0]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
331 tags = line[field]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
332 if tags == '':
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
333 newTag = "%s=%s" % (name, value)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
334 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
335 newTag = "%s;%s=%s" % (tags, name, value)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
336 if name not in [tag.split("=")[0] for tag in tags.split(";")]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
337 newData[id] = newTag
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
338 for id, tag in newData.iteritems():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
339 query = self.mySqlConnection.executeQuery("UPDATE '%s' SET tags = '%s' WHERE id = %i" % (self.name, tag, id))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
340
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
341
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
342
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
343 def show(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
344 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
345 Drop the content of the current table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
346 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
347 query = self.mySqlConnection.executeQuery("SELECT * FROM '%s'" % (self.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
348 print query.getLines()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
349