annotate SMART/Java/Python/mySql/MySqlTable.py @ 58:5f5c9b74c2dd

Uploaded
author m-zytnicki
date Fri, 07 Feb 2014 11:53:36 -0500
parents 169d364ddd91
children
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 rename(self, name):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
120 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
121 Rename the table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
122 @param name: the new name
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
123 @type name: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
124 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
125 self.mySqlConnection.executeQuery("RENAME TABLE '%s' TO '%s'" % (self.name, name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
126 self.name = name
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
127
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
128
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
129 def copy(self, table):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
130 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
131 Copy the given table this one
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
132 @param table: the table to be copied
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
133 @type table: class L{MySqlTable<MySqlTable>}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
134 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
135 variables = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
136 types = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
137 sizes = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
138 fields = self.mySqlConnection.executeQuery("PRAGMA table_info(%s)" % (table.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
139 for field in fields.getIterator():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
140 if field[1] != "id":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
141 variables.append(field[1])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
142 m = re.search(r"(\w+)\((\d+)\)", field[2])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
143 if m == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
144 raise Exception("\nFormat %s in table %s is strange." % (field[2], table.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
145 types[field[1]] = m.group(1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
146 sizes[field[1]] = int(m.group(2))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
147 self.create(variables, types, sizes)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
148 self.mySqlConnection.executeQuery("INSERT INTO '%s' SELECT * FROM %s" % (self.name, table.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
149
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
150
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
151 def add(self, table):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
152 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
153 Add the content of a table to this one
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
154 @param table: the table to be added
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
155 @type table: class L{MySqlTable<MySqlTable>}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
156 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
157 self.mySqlConnection.executeQuery("INSERT INTO '%s' SELECT * FROM %s" % (self.name, table.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
158 self.created = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
159
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
160
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
161 def exists(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
162 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
163 Check if the table exists in mySQL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
164 @return: true if it exits
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
165 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
166 return self.created
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
167
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
168
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
169 def remove(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
170 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
171 Remove this table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
172 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
173 if self.exists():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
174 query = "DROP TABLE IF EXISTS '%s'" % (self.name)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
175 self.mySqlConnection.executeQuery(query)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
176 self.created = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
177
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
178
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
179 def clear(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
180 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
181 Clear the content of this table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
182 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
183 self.mySqlConnection.executeQuery("DELETE FROM '%s'" % (self.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
184
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
185
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
186 def getNbElements(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
187 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
188 Count the number of rows in the table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
189 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
190 command = "SELECT COUNT(id) FROM '%s'" % (self.name)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
191 query = self.mySqlConnection.executeQuery(command)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
192 return int(query.getLine()[0])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
193
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
194
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
195 def formatSql(self, value, type, size):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
196 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
197 Format a value using MySQL encapsulation
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
198 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
199 if type.find("int") != -1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
200 return "%d" % value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
201 if type.find("float") != -1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
202 return "%.10f" % value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
203 if type.find("double") != -1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
204 return "%.20f" % value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
205 if type.find("varchar") != -1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
206 if len(value) > size:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
207 return "'%s'" % value[0:size]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
208 return "'%s'" % value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
209 raise Exception("Do not understand type %s" % (type))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
210 formatSql = classmethod(formatSql)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
211
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
212
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
213 def addLine(self, values):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
214 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
215 Add a row to this table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
216 @param values: the values of the row
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
217 @type values: dict
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
218 @return: the id of the added row
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
219 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
220 sqlValues = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
221 for variable in self.variables:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
222 sqlValues.append(self.formatSql(values[variable], self.types[variable], self.sizes[variable]))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
223 command = "INSERT INTO '%s' (%s) VALUES (%s)" % (self.name, ", ".join(self.variables), ", ".join(sqlValues))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
224 id = self.mySqlConnection.executeQuery(command, True)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
225 return id
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
226
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
227
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
228 def retrieveFromId(self, id):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
229 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
230 Retrieve a row from its id
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
231 @param id: the id of the row
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
232 @type id: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
233 @return: the row
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
234 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
235 query = self.mySqlConnection.executeQuery("SELECT * FROM '%s' WHERE id = %d" % (self.name, id))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
236 result = query.getLine()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
237 if result == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
238 raise Exception("Error! Id %d is not in the table %s!" % (id, self.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
239 return result
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
240
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
241
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
242 def retrieveBulkFromId(self, ids):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
243 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
244 Retrieve a row from its id
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
245 @param id: the ids of the row
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
246 @type id: list of int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
247 @return: the row
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
248 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
249 if not ids:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
250 return []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
251 MAXSIZE = 1000
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
252 results = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
253 for batch in range(len(ids) / MAXSIZE + 1):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
254 theseIds = ids[batch * MAXSIZE : (batch+1) * MAXSIZE]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
255 if theseIds:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
256 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
257 lines = query.getLines()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
258 if len(lines) != len(theseIds):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
259 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
260 results.extend(lines)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
261 return results
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
262
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
263
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
264 def removeFromId(self, id):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
265 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
266 Remove a row from its id
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
267 @param id: the id of the row
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
268 @type id: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
269 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
270 self.mySqlConnection.executeQuery("DELETE FROM '%s' WHERE id = %d" % (self.name, id))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
271
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
272
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
273 def getIterator(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
274 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
275 Iterate on the content of table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
276 @return: iterator to the rows of the table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
277 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
278 if not self.created:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
279 return
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
280 MAXSIZE = 1000
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
281 query = self.mySqlConnection.executeQuery("SELECT count(id) FROM '%s'" % (self.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
282 nbRows = int(query.getLine()[0])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
283 for chunk in range((nbRows / MAXSIZE) + 1):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
284 query = self.mySqlConnection.executeQuery("SELECT * FROM '%s' LIMIT %d, %d" % (self.name, chunk * MAXSIZE, MAXSIZE))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
285 for line in query.getIterator():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
286 yield line
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 createIndex(self, indexName, values, unique = False, fullText = False):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
290 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
291 Add an index on the table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
292 @param indexName: name of the index
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
293 @type indexName: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
294 @param values: values to be indexed
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
295 @type values: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
296 @param unique: if the index is unique
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
297 @type unique: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
298 @param fullText: whether full text should be indexed
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
299 @type fullText: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
300 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
301 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
302
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
303
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
304 def setDefaultTagValue(self, field, name, value):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
305 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
306 Add a tag value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
307 @param name: name of the tag
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
308 @type name: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
309 @param value: value of the tag
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
310 @type value: string or int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
311 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
312 newData = {}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
313 for line in MySqlTable.getIterator(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
314 id = line[0]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
315 tags = line[field]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
316 if tags == '':
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
317 newTag = "%s=%s" % (name, value)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
318 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
319 newTag = "%s;%s=%s" % (tags, name, value)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
320 if name not in [tag.split("=")[0] for tag in tags.split(";")]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
321 newData[id] = newTag
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
322 for id, tag in newData.iteritems():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
323 query = self.mySqlConnection.executeQuery("UPDATE '%s' SET tags = '%s' WHERE id = %i" % (self.name, tag, id))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
324
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
325
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
326
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
327 def show(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
328 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
329 Drop the content of the current table
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
330 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
331 query = self.mySqlConnection.executeQuery("SELECT * FROM '%s'" % (self.name))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
332 print query.getLines()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
333
46
169d364ddd91 Uploaded
m-zytnicki
parents: 36
diff changeset
334