##// END OF EJS Templates
Simplify history retrieval code by moving query building and running into a common helper function.
Thomas Kluyver -
Show More
@@ -124,19 +124,42 b' class HistoryManager(Configurable):'
124 name='session_number'""", (self.session_number+1,))
124 name='session_number'""", (self.session_number+1,))
125 self.db.commit()
125 self.db.commit()
126
126
127 def get_hist_tail(self, n=10, raw=True, output=False):
127 def _get_hist_sql(self, sql, params, raw=True, output=False):
128 """Get the last n lines from the history database."""
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 toget = 'source_raw' if raw else 'source'
146 toget = 'source_raw' if raw else 'source'
130 sqlfrom = "history"
147 sqlfrom = "history"
131 if output:
148 if output:
132 sqlfrom = "history LEFT JOIN output_history USING (session, line)"
149 sqlfrom = "history LEFT JOIN output_history USING (session, line)"
133 toget = "history.%s, output_history.output" % toget
150 toget = "history.%s, output_history.output" % toget
134 cur = self.db.execute("SELECT session, line, " + toget +\
151 cur = self.db.execute("SELECT session, line, %s FROM %s " %\
135 " FROM "+sqlfrom+" ORDER BY session DESC, line DESC LIMIT ?", (n,))
152 (toget, sqlfrom) + sql, params)
136 hist = reversed(cur.fetchall())
153 if output: # Regroup into 3-tuples
137 if output:
154 return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur)
138 return ((ses, lin, (inp, out)) for ses, lin, inp, out in hist)
155 return cur
139 return hist
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 def get_hist_search(self, pattern="*", raw=True, output=False):
164 def get_hist_search(self, pattern="*", raw=True, output=False):
142 """Search the database using unix glob-style matching (wildcards * and
165 """Search the database using unix glob-style matching (wildcards * and
@@ -146,18 +169,11 b' class HistoryManager(Configurable):'
146 -------
169 -------
147 An iterator over tuples: (session, line_number, command)
170 An iterator over tuples: (session, line_number, command)
148 """
171 """
149 toget = "source_raw" if raw else "source"
172 tosearch = "source_raw" if raw else "source"
150 tosearch = toget
151 sqlfrom = "history"
152 if output:
173 if output:
153 sqlfrom = "history LEFT JOIN output_history USING (session, line)"
154 toget = "history.%s, output_history.output" % toget
155 tosearch = "history." + tosearch
174 tosearch = "history." + tosearch
156 hist = self.db.execute("SELECT session, line, " +toget+ \
175 return self._get_hist_sql("WHERE %s GLOB ?" % tosearch, (pattern,),
157 " FROM "+sqlfrom+" WHERE " +tosearch+ " GLOB ?", (pattern,))
176 raw=raw, output=output)
158 if output:
159 return ((ses, lin, (inp, out)) for ses, lin, inp, out in hist)
160 return hist
161
177
162 def _get_hist_session(self, start=1, stop=None, raw=True, output=False):
178 def _get_hist_session(self, start=1, stop=None, raw=True, output=False):
163 """Get input and output history from the current session. Called by
179 """Get input and output history from the current session. Called by
@@ -211,12 +227,6 b' class HistoryManager(Configurable):'
211 if session < 0:
227 if session < 0:
212 session += self.session_number
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 if stop:
230 if stop:
221 lineclause = "line >= ? AND line < ?"
231 lineclause = "line >= ? AND line < ?"
222 params = (session, start, stop)
232 params = (session, start, stop)
@@ -224,11 +234,8 b' class HistoryManager(Configurable):'
224 lineclause = "line>=?"
234 lineclause = "line>=?"
225 params = (session, start)
235 params = (session, start)
226
236
227 cur = self.db.execute("""SELECT session, line, %s FROM %s WHERE
237 return self._get_hist_sql("WHERE session==? AND %s""" % lineclause,
228 session==? AND %s""" %(toget, sqlfrom, lineclause), params)
238 params, raw=raw, output=output)
229 if output: # Regroup into 3-tuples
230 return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur)
231 return cur
232
239
233 def get_hist_from_rangestr(self, rangestr, raw=True, output=False):
240 def get_hist_from_rangestr(self, rangestr, raw=True, output=False):
234 """Get lines of history from a string of ranges, as used by magic
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