##// END OF EJS Templates
Simplify history retrieval code by moving query building and running into a common helper function.
Thomas Kluyver -
Show More
@@ -123,20 +123,43 b' class HistoryManager(Configurable):'
123 123 self.db.execute("""UPDATE singletons SET value=? WHERE
124 124 name='session_number'""", (self.session_number+1,))
125 125 self.db.commit()
126
127 def get_hist_tail(self, n=10, raw=True, output=False):
128 """Get the last n lines from the history database."""
126
127 def _get_hist_sql(self, sql, params, raw=True, output=False):
128 """Prepares and runs an SQL query for the history database.
129
130 Parameters
131 ----------
132 sql : str
133 Any filtering expressions to go after SELECT ... FROM ...
134 params : tuple
135 Parameters passed to the SQL query (to replace "?")
136 raw : bool
137 If True, get raw input.
138 output :
139 If True, include output where available.
140
141 Returns
142 -------
143 An iterator over 3-tuples: (session, line_number, command), or if output
144 is True, (session, line_number, (command, output)).
145 """
129 146 toget = 'source_raw' if raw else 'source'
130 147 sqlfrom = "history"
131 148 if output:
132 149 sqlfrom = "history LEFT JOIN output_history USING (session, line)"
133 150 toget = "history.%s, output_history.output" % toget
134 cur = self.db.execute("SELECT session, line, " + toget +\
135 " FROM "+sqlfrom+" ORDER BY session DESC, line DESC LIMIT ?", (n,))
136 hist = reversed(cur.fetchall())
137 if output:
138 return ((ses, lin, (inp, out)) for ses, lin, inp, out in hist)
139 return hist
151 cur = self.db.execute("SELECT session, line, %s FROM %s " %\
152 (toget, sqlfrom) + sql, params)
153 if output: # Regroup into 3-tuples
154 return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur)
155 return cur
156
157
158 def get_hist_tail(self, n=10, raw=True, output=False):
159 """Get the last n lines from the history database."""
160 cur = self._get_hist_sql("ORDER BY session DESC, line DESC LIMIT ?",
161 (n,), raw=raw, output=output)
162 return reversed(list(cur))
140 163
141 164 def get_hist_search(self, pattern="*", raw=True, output=False):
142 165 """Search the database using unix glob-style matching (wildcards * and
@@ -146,18 +169,11 b' class HistoryManager(Configurable):'
146 169 -------
147 170 An iterator over tuples: (session, line_number, command)
148 171 """
149 toget = "source_raw" if raw else "source"
150 tosearch = toget
151 sqlfrom = "history"
172 tosearch = "source_raw" if raw else "source"
152 173 if output:
153 sqlfrom = "history LEFT JOIN output_history USING (session, line)"
154 toget = "history.%s, output_history.output" % toget
155 174 tosearch = "history." + tosearch
156 hist = self.db.execute("SELECT session, line, " +toget+ \
157 " FROM "+sqlfrom+" WHERE " +tosearch+ " GLOB ?", (pattern,))
158 if output:
159 return ((ses, lin, (inp, out)) for ses, lin, inp, out in hist)
160 return hist
175 return self._get_hist_sql("WHERE %s GLOB ?" % tosearch, (pattern,),
176 raw=raw, output=output)
161 177
162 178 def _get_hist_session(self, start=1, stop=None, raw=True, output=False):
163 179 """Get input and output history from the current session. Called by
@@ -211,12 +227,6 b' class HistoryManager(Configurable):'
211 227 if session < 0:
212 228 session += self.session_number
213 229
214 # Assemble the SQL query:
215 sqlfrom = "history"
216 toget = 'source_raw' if raw else 'source'
217 if output:
218 sqlfrom = "history LEFT JOIN output_history USING (session, line)"
219 toget = "history.%s, output_history.output" % toget
220 230 if stop:
221 231 lineclause = "line >= ? AND line < ?"
222 232 params = (session, start, stop)
@@ -224,11 +234,8 b' class HistoryManager(Configurable):'
224 234 lineclause = "line>=?"
225 235 params = (session, start)
226 236
227 cur = self.db.execute("""SELECT session, line, %s FROM %s WHERE
228 session==? AND %s""" %(toget, sqlfrom, lineclause), params)
229 if output: # Regroup into 3-tuples
230 return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur)
231 return cur
237 return self._get_hist_sql("WHERE session==? AND %s""" % lineclause,
238 params, raw=raw, output=output)
232 239
233 240 def get_hist_from_rangestr(self, rangestr, raw=True, output=False):
234 241 """Get lines of history from a string of ranges, as used by magic
General Comments 0
You need to be logged in to leave comments. Login now