Project

General

Profile

« Previous | Next » 

Revision 9508

lib/sql_io.py: append_csv(): support importing CSVs whose columns are a subset of the full table and/or in a different order. when the header exactly matches the columns, the explicit column list will still be omitted as an optimization. this uses code from r4927.

View differences:

lib/sql_io.py
233 233
row_num_col_def.type = 'integer'
234 234

  
235 235
def append_csv(db, table, reader, header):
236
    assert sql.table_col_names(db, table) == header
236
    def esc_name_(name): return sql.esc_name(db, name)
237 237
    
238 238
    def log(msg, level=1): db.log_debug(msg, level)
239 239
    
......
245 245
    dialect = stream.dialect # use default dialect
246 246
    
247 247
    # Create COPY FROM statement
248
    copy_from = ('COPY '+table.to_str(db)+' FROM STDIN DELIMITER '
248
    if header == sql.table_col_names(db, table): cols_str = ''
249
    else: cols_str =' ('+(', '.join(map(esc_name_, header)))+')'
250
    copy_from = ('COPY '+table.to_str(db)+cols_str+' FROM STDIN DELIMITER '
249 251
        +db.esc_value(dialect.delimiter)+' NULL '+db.esc_value(''))
250 252
    assert not csvs.is_tsv(dialect)
251 253
    copy_from += ' CSV'

Also available in: Unified diff