LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Member Functions | Public Attributes | List of all members
lsst.datarel.mysqlExecutor.MysqlExecutor Class Reference
Inheritance diagram for lsst.datarel.mysqlExecutor.MysqlExecutor:

Public Member Functions

def __init__
 
def createDb
 
def execStmt
 
def execScript
 
def runQuery
 
def isView
 
def exists
 
def getConn
 

Public Attributes

 host
 
 port
 
 user
 
 database
 
 password
 
 mysqlCmd
 

Detailed Description

Definition at line 34 of file mysqlExecutor.py.

Constructor & Destructor Documentation

def lsst.datarel.mysqlExecutor.MysqlExecutor.__init__ (   self,
  host,
  database,
  user,
  port = 3306,
  password = None 
)

Definition at line 35 of file mysqlExecutor.py.

35 
36  def __init__(self, host, database, user, port=3306, password=None):
37  self.host = host
38  self.port = port
39  self.user = user
40  self.database = database
41  if password == None:
42  if self.host is not None and self.port is not None and \
43  DbAuth.available(self.host, str(self.port)):
44  self.user = DbAuth.username(self.host, str(self.port))
45  password = DbAuth.password(self.host, str(self.port))
46  elif not os.path.exists(os.path.join(os.environ['HOME'], ".my.cnf")):
47  password = getpass.getpass("%s's MySQL password: " % user)
48  self.password = password
49  self.mysqlCmd = ['mysql']
50  if host is not None:
51  self.mysqlCmd += ['-h', self.host]
52  if port is not None:
53  self.mysqlCmd += ['-P', str(self.port)]
54  if user is not None:
55  self.mysqlCmd += ['-u', self.user]
56  if password is not None:
57  self.mysqlCmd += ['-p' + self.password]

Member Function Documentation

def lsst.datarel.mysqlExecutor.MysqlExecutor.createDb (   self,
  database,
  options = ['-vvv'] 
)

Definition at line 58 of file mysqlExecutor.py.

58 
59  def createDb(self, database, options=['-vvv']):
60  if not isinstance(database, basestring):
61  raise TypeError('database name is not a string')
62  cmd = list(self.mysqlCmd)
63  cmd += options
64  cmd += ['-e', 'CREATE DATABASE %s;' % database]
65  subprocess.check_call(cmd, stdout=sys.stdout, stderr=sys.stderr)
66  sys.stdout.flush()
67  sys.stderr.flush()
def lsst.datarel.mysqlExecutor.MysqlExecutor.execScript (   self,
  script,
  options = ['-vvv'] 
)

Definition at line 80 of file mysqlExecutor.py.

80 
81  def execScript(self, script, options=['-vvv']):
82  if not isinstance(script, basestring):
83  raise TypeError('Script file name is not a string')
84  if not os.path.isfile(script):
85  raise RuntimeError(
86  'Script %s does not exist or is not a file' % script)
87  with open(script, 'rb') as f:
88  cmd = list(self.mysqlCmd)
89  if self.database is not None:
90  cmd += ['-D', self.database]
91  cmd += options
92  subprocess.check_call(cmd, stdin=f,
93  stdout=sys.stdout, stderr=sys.stderr)
94  sys.stdout.flush()
95  sys.stderr.flush()
def lsst.datarel.mysqlExecutor.MysqlExecutor.execStmt (   self,
  stmt,
  stdout = sys.stdout,
  options = ['-vvv'] 
)

Definition at line 68 of file mysqlExecutor.py.

68 
69  def execStmt(self, stmt, stdout=sys.stdout, options=['-vvv']):
70  if not isinstance(stmt, basestring):
71  raise TypeError('SQL statement is not a string')
72  cmd = list(self.mysqlCmd)
73  if self.database is not None:
74  cmd += ['-D', self.database]
75  cmd += options
76  cmd += ['-e', stmt]
77  subprocess.check_call(cmd, stdout=stdout, stderr=sys.stderr)
78  stdout.flush()
79  sys.stderr.flush()
def lsst.datarel.mysqlExecutor.MysqlExecutor.exists (   self,
  table 
)

Definition at line 114 of file mysqlExecutor.py.

115  def exists(self, table):
116  with closing(self.getConn()) as conn:
117  with closing(conn.cursor()) as cursor:
118  cursor.execute('SELECT COUNT(*) FROM information_schema.tables '
119  'WHERE table_schema=%s AND table_name=%s',
120  (self.database, table))
121  return cursor.fetchone()[0] == 1
def lsst.datarel.mysqlExecutor.MysqlExecutor.getConn (   self)

Definition at line 122 of file mysqlExecutor.py.

123  def getConn(self):
124  kw = dict()
125  if self.host is not None:
126  kw['host'] = self.host
127  if self.port is not None:
128  kw['port'] = self.port
129  if self.user is not None:
130  kw['user'] = self.user
131  if self.database is not None:
132  kw['db'] = self.database
133  if self.password is not None:
134  kw['passwd'] = self.password
135  return sql.connect(**kw)
136 
def lsst.datarel.mysqlExecutor.MysqlExecutor.isView (   self,
  table 
)

Definition at line 106 of file mysqlExecutor.py.

107  def isView(self, table):
108  with closing(self.getConn()) as conn:
109  with closing(conn.cursor()) as cursor:
110  cursor.execute('SELECT COUNT(*) FROM information_schema.tables '
111  'WHERE table_schema=%s AND table_name=%s AND '
112  'table_type=\'VIEW\'', (self.database, table))
113  return cursor.fetchone()[0] == 1
def lsst.datarel.mysqlExecutor.MysqlExecutor.runQuery (   self,
  query 
)

Definition at line 96 of file mysqlExecutor.py.

96 
97  def runQuery(self, query):
98  if not isinstance(query, basestring):
99  raise TypeError('Query is not a string')
100  with closing(self.getConn()) as conn:
101  with closing(conn.cursor()) as cursor:
102  print query
103  sys.stdout.flush()
104  cursor.execute(query)
105  return cursor.fetchall()

Member Data Documentation

lsst.datarel.mysqlExecutor.MysqlExecutor.database

Definition at line 39 of file mysqlExecutor.py.

lsst.datarel.mysqlExecutor.MysqlExecutor.host

Definition at line 36 of file mysqlExecutor.py.

lsst.datarel.mysqlExecutor.MysqlExecutor.mysqlCmd

Definition at line 48 of file mysqlExecutor.py.

lsst.datarel.mysqlExecutor.MysqlExecutor.password

Definition at line 47 of file mysqlExecutor.py.

lsst.datarel.mysqlExecutor.MysqlExecutor.port

Definition at line 37 of file mysqlExecutor.py.

lsst.datarel.mysqlExecutor.MysqlExecutor.user

Definition at line 38 of file mysqlExecutor.py.


The documentation for this class was generated from the following file: