5
5
from collections import deque
6
6
from datetime import datetime
7
7
from functools import partial
8
- from io import StringIO
9
8
from json import dumps , loads
10
9
from operator import methodcaller
11
10
from os import environ , path
12
11
13
12
import numpy as np
14
13
import psycopg2 .sql
15
- from cdd .shared .pure_utils import identity , pp
14
+ from cdd .shared .pure_utils import identity
15
+ from pgcopy import CopyManager
16
16
from pyarrow .parquet import ParquetFile
17
17
from sqlalchemy import create_engine
18
18
@@ -70,6 +70,10 @@ def parse_col(col):
70
70
raise NotImplementedError (type (col ))
71
71
72
72
73
+ # {"\\"first item\\"","\\"second item\\"",
74
+ # "\\"third item with \\\\\\"quotes\\\\\\" inside\\"","\\"fourth with a \\\\\\\\ backslash\\""}
75
+
76
+
73
77
def maybe_quote_and_escape (s , ch = '"' ):
74
78
if isinstance (s , str ) and s .startswith ("{" ) and s .endswith ("}" ):
75
79
return "{ch}{}{ch}" .format (s .replace ('"' , '\\ "' ), ch = ch )
@@ -88,44 +92,17 @@ def psql_insert_copy(table, conn, keys, data_iter):
88
92
Column names
89
93
data_iter : Iterable that iterates the values to be inserted
90
94
"""
91
- # gets a DBAPI connection that can provide a cursor
92
- dbapi_conn = conn .connection
93
- with dbapi_conn .cursor () as cur :
94
- s_buf = StringIO ()
95
- s_buf .writelines (
96
- "\n " .join (
97
- map (lambda line : "|" .join (map (str , map (parse_col , line ))), data_iter )
98
- )
99
- )
100
- s_buf .seek (0 )
101
-
102
- sql = "COPY {} ({}) FROM STDIN WITH null as 'null' DELIMITER '|'" .format (
103
- psycopg2 .sql .Identifier (
104
- * (table .schema , table .name ) if table .schema else (table .name ,)
105
- ).as_string (cur ),
106
- psycopg2 .sql .SQL (", " )
107
- .join (
108
- map (
109
- psycopg2 .sql .Identifier ,
110
- keys [1 :] if keys and keys [0 ] == "index" else keys ,
111
- )
112
- )
113
- .as_string (cur ),
114
- )
115
- try :
116
- cur .copy_expert (sql = sql , file = s_buf )
117
- except :
118
- print (sql )
119
- s_buf .seek (0 )
120
- pp (
121
- dict (
122
- zip (
123
- keys [1 :] if keys and keys [0 ] == "index" else keys ,
124
- next (s_buf ).split ("|" ),
125
- )
126
- )
127
- )
128
- raise
95
+
96
+ with conn .connection .cursor () as cur :
97
+ table_name = psycopg2 .sql .Identifier (
98
+ * (table .schema , table .name ) if table .schema else (table .name ,)
99
+ ).as_string (cur )
100
+
101
+ mgr = CopyManager (
102
+ conn .connection , table .name , keys [1 :] if keys and keys [0 ] == "index" else keys
103
+ )
104
+ mgr .copy (data_iter )
105
+ conn .connection .commit ()
129
106
130
107
131
108
def csv_to_postgres_text (lines ):
0 commit comments