# $Id: CreateTestDatabase.py,v 1.1 2002/05/15 17:21:13 graham Exp $ # # Create a new MySQL database to be used as an RDF model store # # TODO: # - Add command line option for database URL and name to create. # Also username/password values, etc. # # Under SuSE Linux, set environment variable: # PYTHONPATH=/usr/lib/python-2.0:/usr/lib/python-2.1/site-packages # #--------+---------+---------+---------+---------+---------+---------+---------+ # # Copyright (c) 2002, G. KLYNE # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # #--------+---------+---------+---------+---------+---------+---------+---------+ # $Source: /file/cvsdev/PythonN3/CreateTestDatabase.py,v $ # $Author: graham $ # $Revision: 1.1 $ $Date: 2002/05/15 17:21:13 $ #--------+---------+---------+---------+---------+---------+---------+---------+ # 1 2 3 4 5 6 7 8 # """ Script to initialize a MySQL database for N3ModelJenaRdb testing """ import os import sys import re import string import MySQLdb class InitDatabase: # Construct instance to process a given tree to a named file def __init__( self, host, db, user, password ): if host: self.db = MySQLdb.connect( host=host, db=db, user=user, passwd=password ) else: self.db = MySQLdb.connect( db=db, user=user, passwd=password ) outname = None if outname: self.outfile = open( outname, 'w' ) else: self.outfile = sys.stdout return # Method to execute a supplied command and return a result def execute( self, command, values ): c = self.db.cursor() c.execute( command, values ) return c.fetchall() def permitAll( self, host, user, passwd ): command = """ REPLACE INTO user ( Host, User, Password, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_priv, Shutdown_priv, File_priv, Grant_priv, Index_priv, Alter_priv, Process_priv, References_priv ) VALUES ( %s, %s, password(%s), 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y' ) ;""" res = self.execute( command, (host,user,passwd) ) return def permitWrite( self, host, user, passwd ): command = """ REPLACE INTO user ( Host, User, Password, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_priv, Shutdown_priv, File_priv, Grant_priv, Index_priv, Alter_priv, Process_priv, References_priv ) VALUES ( %s, %s, password(%s), 'Y', 'Y', 'Y', 'Y', 'Y', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N' ) ;""" res = self.execute( command, (host,user,passwd) ) return def permitRead( self, host, user, passwd ): command = """ REPLACE INTO user ( Host, User, Password, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_priv, Shutdown_priv, File_priv, Grant_priv, Index_priv, Alter_priv, Process_priv, References_priv ) VALUES ( %s, %s, password(%s), 'Y', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N' ) ;""" res = self.execute( command, (host,user,passwd) ) return def removeAll( self, host, user ): command = """ DELETE FROM user WHERE Host=%s AND User=%s """ res = self.execute( command, (host,user) ) return # Write line of data def putLine( self, line ): self.outfile.write( `line` ) self.outfile.write( "\n" ) # End of InitDatabase if __name__ == '__main__': db = InitDatabase( "Luggage", "mysql", "graham", "graham" ) db.putLine( "CreateTestDatabase here" ) db.putLine( "Deleting TestN3ModelJenaRdb..." ) db.execute( "DROP DATABASE TestN3ModelJenaRdb ;", None ) db.putLine( "Creating TestN3ModelJenaRdb..." ) db.execute( "CREATE DATABASE TestN3ModelJenaRdb ;", None ) db.putLine( "CreateTestDatabase finished" ) #--------+---------+---------+---------+---------+---------+---------+---------+ # $Log: CreateTestDatabase.py,v $ # Revision 1.1 2002/05/15 17:21:13 graham # N3ModelJenaRdb.py, with supporting code, # works with a Jena RDB model store. # Tested against a MySQL server. # #