atquery 2.14.2__tar.gz → 2.15.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. {atquery-2.14.2 → atquery-2.15.0}/PKG-INFO +1 -1
  2. {atquery-2.14.2 → atquery-2.15.0}/pyproject.toml +1 -1
  3. {atquery-2.14.2 → atquery-2.15.0}/pyproject.toml.orig +1 -1
  4. atquery-2.15.0/src/atquery/cli/csv.py +105 -0
  5. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/cli/main.py +41 -1
  6. {atquery-2.14.2 → atquery-2.15.0}/LICENSE +0 -0
  7. {atquery-2.14.2 → atquery-2.15.0}/README.md +0 -0
  8. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/__init__.py +0 -0
  9. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/aio/__init__.py +0 -0
  10. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/aio/engine.py +0 -0
  11. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/aio/query.py +0 -0
  12. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/api_v2.py +0 -0
  13. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/atquery2.py +0 -0
  14. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/cli/__init__.py +0 -0
  15. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/cli/columns.py +0 -0
  16. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/cli/ddl.py +0 -0
  17. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/cli/diff.py +0 -0
  18. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/cli/ecd.py +0 -0
  19. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/cli/migrate.py +0 -0
  20. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/cli/routines.py +0 -0
  21. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/cli/scenario.py +0 -0
  22. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/cli/schemas.py +0 -0
  23. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/cli/tables.py +0 -0
  24. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/dialect/__init__.py +0 -0
  25. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/dialect/duckdb.py +0 -0
  26. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/dialect/mysql/__init__.py +0 -0
  27. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/dialect/mysql/aio.py +0 -0
  28. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/dialect/mysql/core.py +0 -0
  29. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/dialect/mysql/dump/__init__.py +0 -0
  30. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/dialect/mysql/dump/__main__.py +0 -0
  31. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/dialect/mysql/dump/core.py +0 -0
  32. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/dialect/mysql/meta.py +0 -0
  33. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/dialect/mysql/pooling.py +0 -0
  34. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/dialect/mysql/restore/__init__.py +0 -0
  35. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/dialect/mysql/restore/__main__.py +0 -0
  36. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/dialect/mysql/restore/core.py +0 -0
  37. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/dialect/sqlite.py +0 -0
  38. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/diff/__init__.py +0 -0
  39. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/diff/comparator.py +0 -0
  40. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/diff/generator.py +0 -0
  41. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/diff/git_integration.py +0 -0
  42. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/diff/models.py +0 -0
  43. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/diff/parser.py +0 -0
  44. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/ecd/__init__.py +0 -0
  45. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/ecd/_toml.py +0 -0
  46. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/ecd/catalog.py +0 -0
  47. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/ecd/ddl.py +0 -0
  48. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/ecd/diff.py +0 -0
  49. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/ecd/erd.py +0 -0
  50. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/ecd/errors.py +0 -0
  51. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/ecd/model.py +0 -0
  52. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/ecd/parser.py +0 -0
  53. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/ecd/resolver.py +0 -0
  54. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/ecd/seed.py +0 -0
  55. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/ecd/skeleton.py +0 -0
  56. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/engine.py +0 -0
  57. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/factory.py +0 -0
  58. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/migration/__init__.py +0 -0
  59. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/migration/_migrator.py +0 -0
  60. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/migration/_protocol.py +0 -0
  61. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/migration/_standard.py +0 -0
  62. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/migration/_types.py +0 -0
  63. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/py.typed +0 -0
  64. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/testing/__init__.py +0 -0
  65. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/testing/creator.py +0 -0
  66. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/testing/loader.py +0 -0
  67. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/testing/schema.py +0 -0
  68. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/testing/validator.py +0 -0
  69. {atquery-2.14.2 → atquery-2.15.0}/src/atquery/type.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: atquery
3
- Version: 2.14.2
3
+ Version: 2.15.0
4
4
  Summary: A Python module for simplifying DBAPI usage with one-query-per-file SQL management.
5
5
  Keywords: sql,database,dbapi,mysql,sqlite,duckdb
6
6
  Author: alpine
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "atquery"
7
- version = "2.14.2"
7
+ version = "2.15.0"
8
8
  description = "A Python module for simplifying DBAPI usage with one-query-per-file SQL management."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "atquery"
7
- version = "2.14.2"
7
+ version = "2.15.0"
8
8
  description = "A Python module for simplifying DBAPI usage with one-query-per-file SQL management."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -0,0 +1,105 @@
1
+ """atquery csv サブコマンド - CSVファイルのインポート・エクスポート"""
2
+ import argparse
3
+ import sys
4
+
5
+ from atquery.cli.main import (
6
+ EXIT_ERROR,
7
+ EXIT_SUCCESS,
8
+ format_error_for_cli,
9
+ resolve_datasource_uri,
10
+ )
11
+ from atquery.dialect import create_datasource
12
+
13
+
14
+ def _add_common_args(parser: argparse.ArgumentParser) -> None:
15
+ """insert/upsert/export共通のオプションを追加する
16
+
17
+ default=argparse.SUPPRESS: ルートパーサの -d/-s (例: `atquery -d ... csv insert`) が
18
+ このサブパーサの未指定引数で上書き(Noneでリセット)されるのを防ぐ。
19
+ """
20
+ parser.add_argument(
21
+ "-d",
22
+ "--datasource",
23
+ required=False,
24
+ default=argparse.SUPPRESS,
25
+ help="Database connection string (or set ATQUERY_DATASOURCE env var)",
26
+ )
27
+ parser.add_argument(
28
+ "-s",
29
+ "--schema",
30
+ required=False,
31
+ default=argparse.SUPPRESS,
32
+ help="Override the schema/database segment of the resolved connection URI",
33
+ )
34
+ parser.add_argument("-t", "--table", required=True, help="Target table name")
35
+ parser.add_argument("-f", "--file", required=True, help="CSV file path")
36
+
37
+
38
+ def setup_parser(parser: argparse.ArgumentParser) -> None:
39
+ """csv サブコマンドのパーサーを設定する。"""
40
+ parser.formatter_class = argparse.RawDescriptionHelpFormatter
41
+ parser.epilog = """
42
+ Supported dialects: mysql, sqlite
43
+
44
+ Examples:
45
+ # Insert rows from a CSV file (rows with an existing PK are skipped)
46
+ atquery csv insert -t users -f users.csv -d sqlite:///data.db
47
+
48
+ # Insert or update rows from a CSV file (rows with an existing PK are overwritten)
49
+ atquery csv upsert -t users -f users.csv -d mysql://root@localhost/mydb
50
+
51
+ # Export a table to a CSV file
52
+ atquery csv export -t users -f users.csv -d sqlite:///data.db
53
+ """
54
+
55
+ sub = parser.add_subparsers(dest="csv_command", help="csv commands")
56
+
57
+ insert_parser = sub.add_parser("insert", help="Insert CSV rows into a table (skip rows with an existing PK)")
58
+ _add_common_args(insert_parser)
59
+
60
+ upsert_parser = sub.add_parser("upsert", help="Insert or update CSV rows into a table (overwrite rows with an existing PK)")
61
+ _add_common_args(upsert_parser)
62
+
63
+ export_parser = sub.add_parser("export", help="Export a table to a CSV file")
64
+ _add_common_args(export_parser)
65
+
66
+
67
+ def execute_csv(args: argparse.Namespace) -> int:
68
+ """csv サブコマンドをディスパッチする。"""
69
+ if not args.csv_command:
70
+ print("Usage: atquery csv <insert|upsert|export> -t TABLE -f FILE ...", file=sys.stderr)
71
+ print(" atquery csv --help", file=sys.stderr)
72
+ return EXIT_ERROR
73
+
74
+ database_uri = resolve_datasource_uri(getattr(args, "datasource", None), getattr(args, "schema", None))
75
+ if not database_uri:
76
+ print(
77
+ "Error: the following arguments are required: -d/--datasource or ATQUERY_DATASOURCE environment variable",
78
+ file=sys.stderr,
79
+ )
80
+ return EXIT_ERROR
81
+
82
+ import atquery
83
+
84
+ try:
85
+ datasource = create_datasource(database_uri)
86
+
87
+ if args.csv_command == "export":
88
+ with datasource.connect() as conn:
89
+ count = atquery.export_csv(conn, args.table, args.file)
90
+ print(f"Exported {count} row(s) from '{args.table}' to {args.file}", file=sys.stderr)
91
+ return EXIT_SUCCESS
92
+
93
+ func = atquery.insert_csv if args.csv_command == "insert" else atquery.upsert_csv
94
+ verb = "Inserted" if args.csv_command == "insert" else "Upserted"
95
+ with datasource.begin() as conn:
96
+ count = func(conn, args.table, args.file)
97
+ print(f"{verb} {count} row(s) from {args.file} into '{args.table}'", file=sys.stderr)
98
+ return EXIT_SUCCESS
99
+
100
+ except FileNotFoundError as e:
101
+ print(f"Error: {format_error_for_cli(e)}", file=sys.stderr)
102
+ return EXIT_ERROR
103
+ except Exception as e:
104
+ print(f"Error: {format_error_for_cli(e)}", file=sys.stderr)
105
+ return EXIT_ERROR
@@ -309,7 +309,10 @@ class OutputWriter:
309
309
  if output_format in ("csv", "tsv"):
310
310
  delimiter = "\t" if output_format == "tsv" else ","
311
311
  self.csv_output = StringIO()
312
- self.csv_writer = csv.writer(self.csv_output, delimiter=delimiter)
312
+ # lineterminatorは"\n"に固定する。標準出力はテキストモードで開かれており、
313
+ # print()時に"\n"はos.linesep(Windowsでは"\r\n")へ自動変換される。
314
+ # ここでデフォルトの"\r\n"のまま渡すと、Windows環境で"\r\r\n"に二重変換されてしまう。
315
+ self.csv_writer = csv.writer(self.csv_output, delimiter=delimiter, lineterminator="\n")
313
316
 
314
317
  def _parse_json_columns(self, row_dict: dict) -> dict:
315
318
  """JSON文字列カラムをパースする"""
@@ -605,9 +608,17 @@ Examples:
605
608
  atquery -f query.sql --ndjson
606
609
  atquery -f query.sql --format json --with-types
607
610
 
611
+ # Force LF line endings for CSV output on Windows
612
+ atquery -f query.sql --eol lf > out.csv
613
+
608
614
  # Retrieve database structure
609
615
  atquery columns -d sqlite:///data.db
610
616
  atquery routines -d mysql://root:@localhost/mydb
617
+
618
+ # Import/export CSV files
619
+ atquery csv insert -t users -f users.csv -d sqlite:///data.db
620
+ atquery csv upsert -t users -f users.csv -d mysql://root:@localhost/mydb
621
+ atquery csv export -t users -f users.csv -d sqlite:///data.db
611
622
  """,
612
623
  )
613
624
 
@@ -659,6 +670,15 @@ Examples:
659
670
  )
660
671
  setup_schemas_parser(schemas_parser)
661
672
 
673
+ # csv サブコマンドの追加
674
+ from atquery.cli.csv import setup_parser as setup_csv_parser
675
+ csv_parser = subparsers.add_parser(
676
+ "csv",
677
+ help="Import/export CSV files (insert/upsert/export)",
678
+ description="Import/export CSV files (insert/upsert/export)",
679
+ )
680
+ setup_csv_parser(csv_parser)
681
+
662
682
  # scenario サブコマンドの追加
663
683
  from atquery.cli.scenario import setup_parser as setup_scenario_parser
664
684
  scenario_parser = subparsers.add_parser(
@@ -735,6 +755,15 @@ Examples:
735
755
 
736
756
  add_output_format_arguments(parser)
737
757
 
758
+ parser.add_argument(
759
+ "--eol",
760
+ choices=["auto", "lf", "crlf"],
761
+ default="auto",
762
+ help="Line ending for stdout output (csv/tsv/plain text): "
763
+ "auto=OS native (default; CRLF on Windows, LF elsewhere), "
764
+ "lf=force LF even on Windows, crlf=force CRLF even on non-Windows",
765
+ )
766
+
738
767
  parser.add_argument("--limit", type=int, help="Limit number of results")
739
768
 
740
769
  parser.add_argument(
@@ -772,6 +801,13 @@ Examples:
772
801
 
773
802
  args = parser.parse_args()
774
803
 
804
+ # --eol で明示指定された場合のみ改行変換を上書きする。
805
+ # "auto"(デフォルト)は最初のreconfigure(encoding="utf-8")で設定済みのOSネイティブ変換のまま。
806
+ if args.eol == "lf":
807
+ cast(io.TextIOWrapper, sys.stdout).reconfigure(newline="\n")
808
+ elif args.eol == "crlf":
809
+ cast(io.TextIOWrapper, sys.stdout).reconfigure(newline="\r\n")
810
+
775
811
  # 引数なし(かつ標準入力がパイプでない)場合はヘルプを表示
776
812
  if len(sys.argv) == 1 and sys.stdin.isatty():
777
813
  parser.print_help()
@@ -798,6 +834,10 @@ Examples:
798
834
  from atquery.cli.schemas import execute_schemas
799
835
  return execute_schemas(args)
800
836
 
837
+ if args.subcommand == "csv":
838
+ from atquery.cli.csv import execute_csv
839
+ return execute_csv(args)
840
+
801
841
  if args.subcommand == "scenario":
802
842
  from atquery.cli.scenario import execute_scenario
803
843
  return execute_scenario(args)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes