8000 Add GCF Python SQL samples · lesliepound/python-docs-samples@722b0fd · GitHub
[go: up one dir, main page]

Skip to content

Commit 722b0fd

Browse files
author
Ace Nassri
committed
Add GCF Python SQL samples
Change-Id: Ib60712a5f3808b1fb99088e5e637304ef21e98c7
1 parent de2cb86 commit 722b0fd

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

functions/sql/mysql_sample.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START functions_sql_mysql]
16+
from os import getenv
17+
18+
import pymysql
19+
20+
is_production = getenv('SUPERVISOR_HOSTNAME') is not None
21+
22+
# TODO(developer): specify SQL connection details
23+
mysql_config = {
24+
'user': getenv('SQL_USER'),
25+
'password': getenv('SQL_PASSWORD'),
26+
'db': getenv('SQL_DATABASE'),
27+
'charset': 'utf8mb4',
28+
'cursorclass': pymysql.cursors.DictCursor,
29+
'autocommit': True
30+
}
31+
32+
if is_production:
33+
mysql_config['unix_socket'] = \
34+
'/cloudsql/' + getenv('INSTANCE_CONNECTION_NAME')
35 8000 +
36+
# Create SQL connection globally to enable reuse
37+
mysql_connection = pymysql.connect(**mysql_config)
38+
39+
40+
def mysql_demo(request):
41+
with mysql_connection.cursor() as cursor:
42+
cursor.execute('SELECT NOW() as now')
43+
results = cursor.fetchone()
44+
return str(results['now'])
45+
# [END functions_sql_mysql]

functions/sql/mysql_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import re
17+
18+
import mock
19+
20+
env_vars = {
21+
'SQL_USER': os.getenv('MYSQL_USER'),
22+
'SQL_PASSWORD': os.getenv('MYSQL_PASSWORD'),
23+
'SQL_NAME': os.getenv('MYSQL_NAME'),
24+
'INSTANCE_CONNECTION_NAME':
25+
os.getenv('INSTANCE_CONNECTION_PREFIX') + '-mysql'
26+
}
27+
28+
date_regex = re.compile('\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}')
29+
30+
31+
@mock.patch.dict(os.environ, env_vars)
32+
def test_mysql():
33+
import mysql_sample
34+
results = mysql_sample.mysql_demo(None)
35+
assert date_regex.match(results)

functions/sql/postgres_sample.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START functions_sql_postgres]
16+
from os import getenv
17+
18+
import psycopg2
19+
20+
is_production = getenv('SUPERVISOR_HOSTNAME') is not None
21+
22+
# TODO(developer): specify SQL connection details
23+
pg_config = {
24+
'user': getenv('SQL_USER'),
25+
'password': getenv('SQL_PASSWORD'),
26+
'dbname': getenv('SQL_DATABASE'),
27+
}
28+
29+
if is_production:
30+
pg_config['host'] = '/cloudsql/' + getenv('INSTANCE_CONNECTION_NAME')
31+
else:
32+
pg_config['host'] = 'localhost'
33+
34+
# Create SQL connection globally to enable reuse
35+
pg_connection = psycopg2.connect(**pg_config)
36+
37+
38+
def postgres_demo(request):
39+
with pg_connection.cursor() as cursor:
40+
cursor.execute('SELECT NOW() as now')
41+
results = cursor.fetchone()
42+
return str(results[0])
43+
# [END functions_sql_postgres]

functions/sql/postgres_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import re
17+
18+
import mock
19+
20+
env_vars = {
21+
'SQL_USER': os.getenv('POSTGRES_USER'),
22+
'SQL_PASSWORD': os.getenv('POSTGRES_PASSWORD'),
23+
'SQL_NAME': os.getenv('POSTGRES_NAME'),
24+
'INSTANCE_CONNECTION_NAME':
25+
os.getenv('INSTANCE_CONNECTION_PREFIX') + '-mysql'
26+
}
27+
28+
date_regex = re.compile('\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}')
29+
30+
31+
@mock.patch.dict(os.environ, env_vars)
32+
def test_postgres():
33+
import postgres_sample
34+
results = postgres_sample.postgres_demo(None)
35+
assert date_regex.match(results)

functions/sql/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
psycopg2==2.7.5
2+
PyMySQL==0.9.2

0 commit comments

Comments
 (0)
0