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 4 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
Empty file added samples/__init__.py
Empty file.
44 changes: 44 additions & 0 deletions samples/autocommit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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

import argparse

from sqlalchemy import create_engine


def enable_autocommit_mode(url):
"""Enables autocommit mode."""
# [START sqlalchemy_spanner_autocmmit_on]
larkee marked this conversation as resolved.
Show resolved Hide resolved

conn = create_engine(url).connect()
level = conn.get_isolation_level()

print("Connection autocommit default mode is {}".format(level))
print("Spanner DBAPI default autocommit mode is {}".format(conn.connection.connection.autocommit))

conn.execution_options(isolation_level="AUTOCOMMIT")
print("Connection autocommit mode is {}".format(level))
print("Spanner DBAPI autocommit mode is {}".format(conn.connection.connection.autocommit))

# [END sqlalchemy_spanner_autocmmit_on]
larkee marked this conversation as resolved.
Show resolved Hide resolved
return conn


if __name__ == "__main__":
larkee marked this conversation as resolved.
Show resolved Hide resolved
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("url", help="Your Cloud Spanner url which contains "
"project-id, instance-id, databas-id.")

subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("enable_autocommit_mode", help=enable_autocommit_mode.__doc__)
args = parser.parse_args()
if args.command == "enable_autocommit_mode":
enable_autocommit_mode(args.url)
else:
print(f"Command {args.command} did not match expected commands.")
57 changes: 57 additions & 0 deletions samples/create_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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

import argparse

from sqlalchemy import (
Column,
Integer,
MetaData,
String,
Table,
create_engine,
)


def create_table(url, random_table_id):
"""Create the table"""
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
# [START sqlalchemy_spanner_create_table]

engine = create_engine(url)
metadata = MetaData(bind=engine)

table = Table(
random_table_id,
metadata,
Column("user_id", Integer, primary_key=True),
Column("user_name", String(16), nullable=False),
)
table.create()

print("Table {} is created successfully".format(table.name))
# [END sqlalchemy_spanner_create_table]
return table


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("url", help="Your Cloud Spanner url which contains "
"project-id, instance-id, databas-id.")
parser.add_argument(
"--table-id",
help="Your Cloud Spanner table ID.",
default="example_table",
)
subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("create_table", help=create_table.__doc__)
args = parser.parse_args()
if args.command == "create_table":
create_table(args.url, args.table-id)
else:
print(f"Command {args.command} did not match expected commands.")
60 changes: 60 additions & 0 deletions samples/drop_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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

import argparse

from sqlalchemy import (
Column,
Integer,
MetaData,
String,
Table,
create_engine,
)


def drop_table(url, random_table_id):
"""Drop the table"""
# [START sqlalchemy_spanner_drop_table]

engine = create_engine(url)
metadata = MetaData(bind=engine)

table = Table(
random_table_id,
metadata,
Column("user_id", Integer, primary_key=True),
Column("user_name", String(16), nullable=False),
)

table.create()

table.drop()
print("Table {} is dropped successfully".format(table.name))
# [END sqlalchemy_spanner_drop_table]
return table


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("url", help="Your Cloud Spanner url which contains "
"project-id, instance-id, databas-id.")
parser.add_argument(
"--table-id",
help="Your Cloud Spanner table ID.",
default="example_table",
)
subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("drop_table", help=drop_table.__doc__)
args = parser.parse_args()
if args.command == "drop_table":
drop_table(args.url, args.table-id)
else:
print(f"Command {args.command} did not match expected commands.")

54 changes: 54 additions & 0 deletions samples/table_delete_all_rows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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

import argparse


def delete_all_rows(table):
"""Delete all rows from the table"""

# TODO(developer): Create the table
# table = Table(
# table_id,
# 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": 'ABC'},
{"user_id": 2, "user_name": 'DEF'}
)

result = table.select().execute().fetchall()
print("Total inserted rows:", len(result))

# [START sqlalchemy_spanner_delete_all_rows]
table.delete().execute()

result = table.select().execute().fetchall()
print("Total rows:", len(result))
# [END sqlalchemy_spanner_delete_all_rows]


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)

parser.add_argument(
"--table",
help="Your sqlalchemy table object.",
)
subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("delete_all_rows", help=delete_all_rows.__doc__)
args = parser.parse_args()
if args.command == "delete_all_rows":
delete_all_rows(args.table)
else:
print(f"Command {args.command} did not match expected commands.")
54 changes: 54 additions & 0 deletions samples/table_delete_row_with_where_condition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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

import argparse


def delete_row_with_where_condition(table):
"""Delete selected row from the table"""

# TODO(developer): Create the table
# table = Table(
# table_id,
# 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": 'ABC'},
{"user_id": 2, "user_name": 'DEF'}
)

result = table.select().execute().fetchall()
print("Total inserted rows:", len(result))

# [START sqlalchemy_spanner_delete_row_with_where_condition]
table.delete().where(table.c.user_id == 1).execute()

result = table.select().execute().fetchall()
print("Total rows:", len(result))
# [END sqlalchemy_spanner_delete_row_with_where_condition]


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)

parser.add_argument(
"--table",
help="Your sqlalchemy table object.",
)
subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("delete_row_with_where_condition", help=delete_row_with_where_condition.__doc__)
args = parser.parse_args()
if args.command == "delete_row_with_where_condition":
delete_row_with_where_condition(args.table)
else:
print(f"Command {args.command} did not match expected commands.")
44 changes: 44 additions & 0 deletions samples/table_exists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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

import argparse


def table_exists(table):
"""Check the table exists"""
# [START sqlalchemy_spanner_table_exists]

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

result = table.exists()
print("Table exists:", result)
# [END sqlalchemy_spanner_table_exists]


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)

parser.add_argument(
"--table",
help="Your sqlalchemy table object.",
)
subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("table_exists", help=table_exists.__doc__)
args = parser.parse_args()
if args.command == "table_exists":
table_exists(args.table)
else:
print(f"Command {args.command} did not match expected commands.")
51 changes: 51 additions & 0 deletions samples/table_fetch_row_with_where_condition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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

import argparse


def fetch_row_with_where_condition(table):
"""Fetch row with where condition from the table"""

# TODO(developer): Create the table
# table = Table(
# table_id,
# 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": 'ABC'},
{"user_id": 2, "user_name": 'DEF'}
])

# [START sqlalchemy_spanner_fetch_row_with_where_condition]
result = list(table.select().where(table.c.user_id == 1).execute())

print("Output is :", result)
# [END sqlalchemy_spanner_fetch_row_with_where_condition]
return result


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)

parser.add_argument(
"--table",
help="Your sqlalchemy table object.",
)
subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("fetch_row_with_where_condition", help=fetch_row_with_where_condition.__doc__)
args = parser.parse_args()
if args.command == "fetch_row_with_where_condition":
fetch_row_with_where_condition(args.table)
else:
print(f"Command {args.command} did not match expected commands.")