MCPcopy Create free account
hub / github.com/ActiveState/code / show_qry

Function show_qry

recipes/Python/578490_sql_the_SQLPlus_killer/recipe-578490.py:192–239  ·  view source on GitHub ↗
(sql)

Source from the content-addressed store, hash-verified

190 return results,desc
191
192def show_qry(sql):
193 if sql.strip().startswith('--'):
194 return
195 filename=fd=''
196 nr=reclen=0
197 fd, filename = mkstemp()
198 sql = sql.strip().strip(';')
199 #emulate pg/mysql limit
200 limit = sql.split('limit')
201 if len(limit) > 1:
202 if sql.lower().find('where') != -1:
203 sql=limit[0] + ' and rownum <= ' + limit[1]
204 else:
205 sql=limit[0] + ' where rownum <= ' + limit[1]
206 rs,desc = select_qry(sql)
207 if not rs:
208 return
209 title=[]
210 for row in desc:
211 title.append(row[0].lower())
212 righe = [ title ] + rs
213 leng = table(righe)
214 for j in leng.values():
215 reclen+=j
216 for row in righe:#print rows
217 for rc in range(len(row)):
218 if rc:
219 fmt = "| %%-%ss" % (leng[rc])
220 else:
221 fmt = "%%-%ss" % (leng[rc])
222 if row[rc] is None:
223 val='NULL'
224 else:
225 val=row[rc]
226 os.write(fd,(fmt%val))
227 os.write(fd,'\n')
228 if not nr: #print titles
229 for rc in range(len(row)):
230 if rc:
231 os.write(fd,'+ '+('-'*(leng[rc])))
232 else:
233 os.write(fd, ('-'*(leng[rc])))
234 os.write(fd,'\n')
235 nr+=1
236 if nr: nr-=1
237 os.write(fd, '(%s rows)\n' % nr )
238 os.close(fd)
239 pager(filename,reclen,len(rs))
240
241def pager(filename,reclen,totrec):
242 if totrec > int(LINES) or reclen > int(COLUMNS):

Callers 12

do_descMethod · 0.70
do_indexMethod · 0.70
do_constraintsMethod · 0.70
do_foreignsMethod · 0.70
do_setMethod · 0.70
do_settingsMethod · 0.70
do_tablesMethod · 0.70
do_sequencesMethod · 0.70
do_triggersMethod · 0.70
input_fileFunction · 0.70
settingsFunction · 0.70
oracle_mainFunction · 0.70

Calls 13

tableFunction · 0.85
rangeFunction · 0.85
startswithMethod · 0.80
select_qryFunction · 0.70
pagerFunction · 0.70
stripMethod · 0.45
splitMethod · 0.45
findMethod · 0.45
lowerMethod · 0.45
appendMethod · 0.45
valuesMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected