Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add samples #55

Merged
merged 40 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
d8b56a2
feat: ass samples for create and drop table
HemangChothani Apr 19, 2021
a36749b
feat: add sample for insert and fetch rows
HemangChothani Apr 20, 2021
4bfac00
feat: add row related samples
HemangChothani Apr 21, 2021
8005a89
feat: add delete and orderby test
HemangChothani Apr 22, 2021
02eb091
Merge branch 'main' of https://github.com/cloudspannerecosystem/pytho…
HemangChothani Apr 29, 2021
a65b226
feat: remove CLI from samples
HemangChothani Apr 29, 2021
7ab99c2
feat: remove ununsed import and add filter like sample
HemangChothani May 1, 2021
d269c0e
feat: add more samples of startswith and endswith
HemangChothani May 3, 2021
550571d
feat: add samples for get table column primary key
HemangChothani May 11, 2021
b77539f
Merge branch 'main' of https://github.com/cloudspannerecosystem/pytho…
HemangChothani May 11, 2021
9388d91
Merge branch 'main' of https://github.com/cloudspannerecosystem/pytho…
HemangChothani May 12, 2021
585ea3a
feat: ass foreign key sample
HemangChothani May 12, 2021
0cf649b
feat: add get index sample
HemangChothani May 18, 2021
e14891d
feat: add create unique index sample
HemangChothani May 19, 2021
26632db
Merge branch 'main' of https://github.com/cloudspannerecosystem/pytho…
HemangChothani May 19, 2021
f69d070
feat: add limit and offset samples
HemangChothani May 19, 2021
dc21c74
feat: move all tests and added in nox
HemangChothani May 20, 2021
0a493f8
feat: nits
HemangChothani May 21, 2021
1a2643d
Merge branch 'main' of https://github.com/cloudspannerecosystem/pytho…
HemangChothani May 21, 2021
3c292f9
Merge branch 'main' of https://github.com/cloudspannerecosystem/pytho…
HemangChothani May 25, 2021
a4e3775
Merge branch 'main' of https://github.com/cloudspannerecosystem/pytho…
HemangChothani May 25, 2021
0fb8081
feat: open db connection explicitly
HemangChothani May 25, 2021
19e12b9
Merge branch 'main' of https://github.com/cloudspannerecosystem/pytho…
HemangChothani May 31, 2021
06c353e
feat: resolve conflict
HemangChothani Jun 8, 2021
67c16e8
Merge branch 'main' of https://github.com/cloudspannerecosystem/pytho…
HemangChothani Jun 17, 2021
21a3e00
Merge branch 'main' of https://github.com/cloudspannerecosystem/pytho…
HemangChothani Jun 17, 2021
8857441
Merge branch 'main' into demo_samples
AVaksman Jun 17, 2021
fbe3e79
WIP: update the SQLAlchemy samples
Sep 7, 2021
6bc37ee
Merge branch 'main' into demo_samples
skuruppu Sep 8, 2021
c8879fb
update samples
Sep 9, 2021
938320a
Merge branch 'demo_samples' of https://github.com/cloudspannerecosyst…
Sep 9, 2021
3143b0c
Merge branch 'main' into demo_samples
Sep 9, 2021
49f2ac8
del excess with statements
Sep 9, 2021
9de08a8
Merge branch 'demo_samples' of https://github.com/cloudspannerecosyst…
Sep 9, 2021
d52e2c8
change tags prefixes
Sep 9, 2021
b9b47ec
update contributing.md
Sep 10, 2021
228c261
Apply suggestions from code review
larkee Sep 16, 2021
fa96c05
Merge branch 'main' into demo_samples
Sep 16, 2021
87ec881
Merge branch 'demo_samples' of https://github.com/cloudspannerecosyst…
Sep 16, 2021
701f7a8
erase excess whitespaces
Sep 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions samples/autocommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def enable_autocommit_mode(url):
"""Enables autocommit mode."""
# [START sqlalchemy_spanner_autocmmit_on]
# [START sqlalchemy_spanner_autocommit_on]

conn = create_engine(url).connect()
level = conn.get_isolation_level()
Expand All @@ -21,5 +21,5 @@ def enable_autocommit_mode(url):
print("Connection autocommit mode is {}".format(level))
print("Spanner DBAPI autocommit mode is {}".format(conn.connection.connection.autocommit))

# [END sqlalchemy_spanner_autocmmit_on]
# [END sqlalchemy_spanner_autocommit_on]
return conn
2 changes: 1 addition & 1 deletion samples/get_table_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def get_table_names(url):

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down
26 changes: 26 additions & 0 deletions samples/table_create_unique_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2021 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

from sqlalchemy import Index


def create_unique_indexes(table_id):
"""Create unique index"""

# TODO(developer): Create the table
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
# table = Table(
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
# )
# table.create()

# [START sqlalchemy_spanner_create_unique_indexes]
index = Index("some_index", table_id.c.user_name, unique=True)
index.create()
print("Index created successfully")
# [END sqlalchemy_spanner_create_unique_indexes]
2 changes: 1 addition & 1 deletion samples/table_delete_all_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def delete_all_rows(table):

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down
2 changes: 1 addition & 1 deletion samples/table_delete_row_with_where_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def delete_row_with_where_condition(table):

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down
2 changes: 1 addition & 1 deletion samples/table_exists.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def table_exists(table):

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down
2 changes: 1 addition & 1 deletion samples/table_fetch_row_with_where_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def fetch_row_with_where_condition(table):

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down
4 changes: 2 additions & 2 deletions samples/table_fetch_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


def fetch_rows(table):
"""fetch all rows from the table"""
"""Fetch all rows from the table"""

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down
31 changes: 31 additions & 0 deletions samples/table_fetch_rows_with_limit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2021 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd


def fetch_rows_with_limit(table):
"""Fetch rows from the table with limit"""

# TODO(developer): Create the table
# table = Table(
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
# )
# table.create()

table.insert().execute([
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
{"user_id": 1, "user_name": 'GEH'},
{"user_id": 2, "user_name": 'DEF'},
{"user_id": 3, "user_name": 'HIJ'},
{"user_id": 4, "user_name": 'ABC'}
])

# [START sqlalchemy_spanner_fetch_rows_with_limit]
result = list(table.select().limit(2).execute())
print("The rows are:", result)
# [END sqlalchemy_spanner_fetch_rows_with_limit]
return result
31 changes: 31 additions & 0 deletions samples/table_fetch_rows_with_limit_offset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2021 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd


def fetch_rows_with_offset(table):
"""Fetch rows from the table with limit and offset"""

# TODO(developer): Create the table
# table = Table(
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
# )
# table.create()

table.insert().execute([
{"user_id": 1, "user_name": 'GEH'},
{"user_id": 2, "user_name": 'DEF'},
{"user_id": 3, "user_name": 'HIJ'},
{"user_id": 4, "user_name": 'ABC'}
])

# [START sqlalchemy_spanner_fetch_rows_with_limit_offset]
result = list(table.select().limit(2).offset(1).execute())
print("The rows are:", result)
# [END sqlalchemy_spanner_fetch_rows_with_limit_offset]
return result
31 changes: 31 additions & 0 deletions samples/table_fetch_rows_with_offset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2021 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd


def fetch_rows_with_offset(table):
"""Fetch rows from the table with offset"""

# TODO(developer): Create the table
# table = Table(
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
# )
# table.create()

table.insert().execute([
{"user_id": 1, "user_name": 'GEH'},
{"user_id": 2, "user_name": 'DEF'},
{"user_id": 3, "user_name": 'HIJ'},
{"user_id": 4, "user_name": 'ABC'}
])

# [START sqlalchemy_spanner_fetch_rows_with_offset]
result = list(table.select().offset(1).execute())
print("The rows are:", result)
# [END sqlalchemy_spanner_fetch_rows_with_offset]
return result
4 changes: 2 additions & 2 deletions samples/table_fetch_rows_with_order_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


def fetch_rows_with_order_by(table):
"""fetch all rows from the table in order"""
"""Fetch all rows from the table in order"""

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down
4 changes: 2 additions & 2 deletions samples/table_filter_data_endswith.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


def filter_data_endswith(table):
"""filter data with endswith from the table"""
"""Filter data with endswith from the table"""

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down
4 changes: 2 additions & 2 deletions samples/table_filter_data_startswith.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


def filter_data_startswith(table):
"""filter data with startswith from the table"""
"""Filter data with startswith from the table"""

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down
4 changes: 2 additions & 2 deletions samples/table_filter_data_with_contains.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


def filter_data_with_contains(table):
"""filter data with contains from the table"""
"""Filter data with contains from the table"""

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down
4 changes: 2 additions & 2 deletions samples/table_filter_data_with_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


def filter_data_with_like(table):
"""filter data with like from the table"""
"""Filter data with like from the table"""

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down
2 changes: 1 addition & 1 deletion samples/table_get_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def get_table_columns(url, table_id):

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down
43 changes: 43 additions & 0 deletions samples/table_get_foreign_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2021 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

from sqlalchemy import create_engine, inspect


def get_table_foreign_key(url, table_id):
"""Retrieve the Foreign key of the table"""

# TODO(developer): Create the table
# table1 = Table(
# table1_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
# )
# table1.create()

# TODO(developer): Create the table with foreign key
# table2 = Table(
# table2_name,
# metadata,
# Column("id", Integer, primary_key=True),
# Column("name", String(16), nullable=False),
# Column(
# "table1_id",
# Integer,
# ForeignKey("table1.id", name="table1id")
# ),
# )
# table2.create()

# [START sqlalchemy_spanner_get_foreign_key]
engine = create_engine(url)
insp = inspect(engine)
f_key = insp.get_foreign_keys(table_id.name)

print("Foreign key is:", f_key)
# [END sqlalchemy_spanner_get_foreign_key]
return f_key
33 changes: 33 additions & 0 deletions samples/table_get_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2021 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

from sqlalchemy import create_engine, inspect, Index


def get_table_indexes(url, table_id):
"""Retrieve the Indexes of the table"""

# TODO(developer): Create the table
# table = Table(
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
# )
# table.create()

# Create Index
index = Index("some_index", table_id.c.user_name)
index.create()

# [START sqlalchemy_spanner_get_indexes]
engine = create_engine(url)
insp = inspect(engine)
indexes = insp.get_indexes(table_id.name)

print("Indexes are:", indexes)
# [END sqlalchemy_spanner_get_indexes]
return indexes
4 changes: 2 additions & 2 deletions samples/table_get_primary_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@


def get_table_primary_key(url, table_id):
"""Retrieve the primary key of the table"""
"""Retrieve the Primary key of the table"""

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down
2 changes: 1 addition & 1 deletion samples/table_insert_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def insert_row(table):

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down
2 changes: 1 addition & 1 deletion samples/table_update_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def update_row(table):

# TODO(developer): Create the table
# table = Table(
# table_id,
# table_name,
# metadata,
# Column("user_id", Integer, primary_key=True),
# Column("user_name", String(16), nullable=False),
Expand Down