1 """
   2 SQLConnect.py -- Copyright 2004 William Waites
   3 
   4 This program is Free Software and is released
   5 under the terms of the GNU General Public License.
   6 Please see http://www.gnu.org/licenses/gpl for
   7 the full text of the terms and conditions.
   8 
   9 In order to use this macro you need
  10 to have pyPgSQL installed. You then
  11 must make a file, called moin_sql.py
  12 in the root directory of your wiki.
  13 This file should contain lines that
  14 look like:
  15 
  16 mydb = "dbhost:5432:dbname:username:password"
  17 
  18 You can then use this macro, SQLConnect
  19 in your wiki pages like this:
  20 
  21 [[SQLConnect(mydb)]]
  22 
  23 SQLConnect will make available a databse
  24 connection and a cursor in
  25 
  26 macro.request.sql_db
  27 macro.request.sql_cursor
  28 
  29 where you can call them from other
  30 macros.
  31 
  32 A generic example macro, SQLQuery is
  33 provided that you can use like this:
  34 
  35 [[SQLQuery(SELECT * FROM foo)]]
  36 
  37 and it will output a table of results.
  38 
  39 If you use this macro, you should always
  40 call
  41 
  42 [[SQLClose()]]
  43 
  44 as well in order to close the database
  45 connection and clean up any state stored
  46 in macro.result
  47 """
  48 
  49 def execute(macro, dbname):
  50 
  51     from pyPgSQL import PgSQL
  52 
  53     try:
  54         moin_sql = __import__("moin_sql", {}, {}, None)
  55     except ImportError, message:
  56         print message
  57         return
  58 
  59     try:
  60         dbstr = getattr(moin_sql, dbname)
  61     except AttributeError, message:
  62         print message
  63         return
  64 
  65     try:
  66         macro.request.sql_db = PgSQL.connect(dbstr)
  67     except Exception, message:
  68         print message
  69         return
  70 
  71     try:
  72         macro.request.sql_cursor = macro.request.sql_db.cursor()
  73     except Exception, message:
  74         print message
  75         return

MoinMoin: macro/SQLConnect.py (last edited 2007-10-29 19:10:15 by localhost)