Attachment 'mysql.py'

Download

   1 """
   2 
   3 Pre-release... sql, stuff doesn't work yet
   4 
   5 The user-name is hardcoded, configure mysql so it can't do any damage.
   6 
   7 Author: johannes@sipsolutions.net
   8 
   9 """
  10 # Imports
  11 from MoinMoin import config, user, wikiutil
  12 from MoinMoin.Page import Page
  13 import sys, re, MySQLdb
  14 
  15 Dependencies = []
  16 
  17 def execute(macro, text):
  18     _ = macro.request.getText
  19 
  20     # return immediately if getting links for the current page
  21     if macro.request.mode_getpagelinks:
  22         return ''
  23 
  24     if text is None: # macro call without parameters
  25         return ''
  26 
  27     query = text
  28     
  29     split = query.split(',', 1)
  30     if split[0] == 'sql':
  31         query = split[1]
  32         sql = True
  33     else:
  34         sql = False
  35     del split
  36     
  37     db = MySQLdb.connect(user="johannes-web")
  38     
  39     c = db.cursor()
  40     
  41     c.execute(query)
  42     
  43     formatter = macro.request.formatter
  44 
  45     if not sql:
  46       result = formatter.table(True)
  47       result += formatter.table_row(True)
  48       for stuff in c.description:
  49         result += formatter.table_cell(True)
  50         result += stuff[0]
  51         result += formatter.table_cell(False)
  52       result += formatter.table_row(False)
  53       for l in c.fetchall():
  54         if l is None: break
  55         result += formatter.table_row(True)
  56         for v in l:
  57           result += formatter.table_cell(True)
  58           result += str(v)
  59           result += formatter.table_cell(False)
  60         result += formatter.table_row(False)
  61       
  62       result += formatter.table(False)
  63     else:
  64       result = 'not yet implemented'
  65 #      for l in c.fetchall():
  66 #        if l is None: break
  67 #        result += l + "\n"
  68       import sha
  69       s = sha.new(query)
  70       id = s.hexdigest()
  71       result = formatter.code_area(True, id) + result + formatter.code_area(False, id)
  72     return result
  73 
  74 # EOF

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2004-10-05 10:34:58, 1.8 KB) [[attachment:mysql.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.