annotate SMART/Java/Python/mySql/MySqlTable.py @ 34:529e3e6a0954

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