23 from __future__
import with_statement
24 from __future__
import print_function
25 from contextlib
import closing
37 def __init__(self, host, database, user, port=3306, password=None):
43 if self.
host is not None and self.
port is not None and \
44 DbAuth.available(self.
host, str(self.
port)):
46 password = DbAuth.password(self.
host, str(self.
port))
47 elif not os.path.exists(os.path.join(os.environ[
'HOME'],
".my.cnf")):
48 password = getpass.getpass(
"%s's MySQL password: " % user)
57 if password
is not None:
60 def createDb(self, database, options=['-vvv']):
61 if not isinstance(database, basestring):
62 raise TypeError(
'database name is not a string')
65 cmd += [
'-e',
'CREATE DATABASE %s;' % database]
66 subprocess.check_call(cmd, stdout=sys.stdout, stderr=sys.stderr)
70 def execStmt(self, stmt, stdout=sys.stdout, options=['-vvv']):
71 if not isinstance(stmt, basestring):
72 raise TypeError(
'SQL statement is not a string')
78 subprocess.check_call(cmd, stdout=stdout, stderr=sys.stderr)
83 if not isinstance(script, basestring):
84 raise TypeError(
'Script file name is not a string')
85 if not os.path.isfile(script):
87 'Script %s does not exist or is not a file' % script)
88 with open(script,
'rb')
as f:
93 subprocess.check_call(cmd, stdin=f,
94 stdout=sys.stdout, stderr=sys.stderr)
99 if not isinstance(query, basestring):
100 raise TypeError(
'Query is not a string')
101 with closing(self.
getConn())
as conn:
102 with closing(conn.cursor())
as cursor:
105 cursor.execute(query)
106 return cursor.fetchall()
109 with closing(self.
getConn())
as conn:
110 with closing(conn.cursor())
as cursor:
111 cursor.execute(
'SELECT COUNT(*) FROM information_schema.tables ' 112 'WHERE table_schema=%s AND table_name=%s AND ' 113 'table_type=\'VIEW\'', (self.
database, table))
114 return cursor.fetchone()[0] == 1
117 with closing(self.
getConn())
as conn:
118 with closing(conn.cursor())
as cursor:
119 cursor.execute(
'SELECT COUNT(*) FROM information_schema.tables ' 120 'WHERE table_schema=%s AND table_name=%s',
122 return cursor.fetchone()[0] == 1
126 if self.
host is not None:
127 kw[
'host'] = self.
host 128 if self.
port is not None:
129 kw[
'port'] = self.
port 130 if self.
user is not None:
131 kw[
'user'] = self.
user 136 return sql.connect(**kw)
140 if not isinstance(parser, argparse.ArgumentParser):
141 raise TypeError(
'Expecting an argparse.ArgumentParser')
142 defUser = (
'USER' in os.environ
and os.environ[
'USER'])
or None 144 "--user", default=defUser, dest=
"user",
145 help=
"MySQL database user name (%(default)s).")
147 "--host", default=
"lsst10.ncsa.uiuc.edu", dest=
"host",
148 help=
"MySQL database server hostname (%(default)s).")
150 "--port", default=3306, type=int, dest=
"port",
151 help=
"MySQL database server port (%(default)d).")
def runQuery(self, query)
def createDb(self, database, options=['-vvv'])
def execScript(self, script, options=['-vvv'])
def __init__(self, host, database, user, port=3306, password=None)
def execStmt(self, stmt, stdout=sys.stdout, options=['-vvv'])