3 # Copyright 2007 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
21 """Relational database API stub that uses the MySQLdb DB-API library.
23 Also see the rdbms module.
36 _POTENTIAL_SOCKET_LOCATIONS
= (
38 '/var/run/mysqld/mysqld.sock',
39 '/var/lib/mysql/mysql.sock',
40 '/var/run/mysql/mysql.sock',
41 '/var/mysql/mysql.sock',
52 def SetConnectKwargs(**kwargs
):
53 """Sets the keyword args (host, user, etc) to pass to MySQLdb.connect()."""
55 global _connect_kwargs
56 _connect_kwargs
= dict(kwargs
)
60 """Find the Unix socket for MySQL by scanning some known locations.
63 If found, the path to the Unix socket, otherwise, None.
65 for path
in _POTENTIAL_SOCKET_LOCATIONS
:
66 if os
.path
.exists(path
):
78 __import__('MySQLdb.constants', globals(), locals(), ['*'])
81 def connect(instance
=None, database
=None):
82 logging
.error('The rdbms API (Google Cloud SQL) is not available because '
83 'the MySQLdb library could not be loaded. Please see the SDK '
84 'documentation for installation instructions.')
86 raise NotImplementedError('Unable to find the MySQLdb library')
90 def connect(instance
=None, database
=None, **kwargs
):
91 merged_kwargs
= _connect_kwargs
.copy()
93 merged_kwargs
['db'] = database
94 merged_kwargs
.update(kwargs
)
95 if 'password' in merged_kwargs
:
96 merged_kwargs
['passwd'] = merged_kwargs
.pop('password')
97 host
= merged_kwargs
.get('host')
98 if ((not host
or host
== 'localhost') and
99 not merged_kwargs
.get('unix_socket') and
100 _OS_NAME
== 'posix'):
111 socket
= FindUnixSocket()
113 merged_kwargs
['unix_socket'] = socket
116 'Unable to find MySQL socket file. Use --mysql_socket to '
117 'specify its location manually.')
118 logging
.info('Connecting to MySQL with kwargs %r', merged_kwargs
)
120 return MySQLdb
.connect(**merged_kwargs
)
121 except MySQLdb
.Error
:
123 'MySQL connection failed! Ensure that you have provided correct '
124 'values for the --mysql_* flags when running dev_appserver.py')
128 def set_instance(instance
):
129 logging
.info('set_instance() is a noop in dev_appserver.')