LSSTApplications  11.0-24-g0a022a1,14.0+77,15.0,15.0+1
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__ (self, host, database, user, port=3306, password=None)
 
def createDb (self, database, options=['-vvv'])
 
def execStmt (self, stmt, stdout=sys.stdout, options=['-vvv'])
 
def execScript (self, script, options=['-vvv'])
 
def runQuery (self, query)
 
def isView (self, table)
 
def exists (self, table)
 
def getConn (self)
 

Public Attributes

 host
 
 port
 
 user
 
 database
 
 password
 
 mysqlCmd
 

Detailed Description

Definition at line 35 of file mysqlExecutor.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 37 of file mysqlExecutor.py.

37  def __init__(self, host, database, user, port=3306, password=None):
38  self.host = host
39  self.port = port
40  self.user = user
41  self.database = database
42  if password is None:
43  if self.host is not None and self.port is not None and \
44  DbAuth.available(self.host, str(self.port)):
45  self.user = DbAuth.username(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)
49  self.password = password
50  self.mysqlCmd = ['mysql']
51  if host is not None:
52  self.mysqlCmd += ['-h', self.host]
53  if port is not None:
54  self.mysqlCmd += ['-P', str(self.port)]
55  if user is not None:
56  self.mysqlCmd += ['-u', self.user]
57  if password is not None:
58  self.mysqlCmd += ['-p' + self.password]
59 
def __init__(self, needLockOnRead=True, data=None, cond=None)
Definition: SharedData.py:53

Member Function Documentation

◆ createDb()

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

Definition at line 60 of file mysqlExecutor.py.

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

◆ execScript()

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

Definition at line 82 of file mysqlExecutor.py.

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

◆ execStmt()

def lsst.datarel.mysqlExecutor.MysqlExecutor.execStmt (   self,
  stmt,
  stdout = sys.stdout,
  options = ['-vvv'] 
)

Definition at line 70 of file mysqlExecutor.py.

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')
73  cmd = list(self.mysqlCmd)
74  if self.database is not None:
75  cmd += ['-D', self.database]
76  cmd += options
77  cmd += ['-e', stmt]
78  subprocess.check_call(cmd, stdout=stdout, stderr=sys.stderr)
79  stdout.flush()
80  sys.stderr.flush()
81 

◆ exists()

def lsst.datarel.mysqlExecutor.MysqlExecutor.exists (   self,
  table 
)

Definition at line 116 of file mysqlExecutor.py.

116  def exists(self, table):
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',
121  (self.database, table))
122  return cursor.fetchone()[0] == 1
123 
def exists(env)
Definition: cuda.py:60

◆ getConn()

def lsst.datarel.mysqlExecutor.MysqlExecutor.getConn (   self)

Definition at line 124 of file mysqlExecutor.py.

124  def getConn(self):
125  kw = dict()
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
132  if self.database is not None:
133  kw['db'] = self.database
134  if self.password is not None:
135  kw['passwd'] = self.password
136  return sql.connect(**kw)
137 
138 

◆ isView()

def lsst.datarel.mysqlExecutor.MysqlExecutor.isView (   self,
  table 
)

Definition at line 108 of file mysqlExecutor.py.

108  def isView(self, table):
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
115 

◆ runQuery()

def lsst.datarel.mysqlExecutor.MysqlExecutor.runQuery (   self,
  query 
)

Definition at line 98 of file mysqlExecutor.py.

98  def runQuery(self, query):
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:
103  print(query)
104  sys.stdout.flush()
105  cursor.execute(query)
106  return cursor.fetchall()
107 

Member Data Documentation

◆ database

lsst.datarel.mysqlExecutor.MysqlExecutor.database

Definition at line 41 of file mysqlExecutor.py.

◆ host

lsst.datarel.mysqlExecutor.MysqlExecutor.host

Definition at line 38 of file mysqlExecutor.py.

◆ mysqlCmd

lsst.datarel.mysqlExecutor.MysqlExecutor.mysqlCmd

Definition at line 50 of file mysqlExecutor.py.

◆ password

lsst.datarel.mysqlExecutor.MysqlExecutor.password

Definition at line 49 of file mysqlExecutor.py.

◆ port

lsst.datarel.mysqlExecutor.MysqlExecutor.port

Definition at line 39 of file mysqlExecutor.py.

◆ user

lsst.datarel.mysqlExecutor.MysqlExecutor.user

Definition at line 40 of file mysqlExecutor.py.


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