From 7a6e3d85e158d33fe18dd15f8b97711c5cbaafbb Mon Sep 17 00:00:00 2001 From: Archie Wood <58074498+archiewood@users.noreply.github.com> Date: Thu, 24 Oct 2024 23:28:57 -0400 Subject: [PATCH 1/3] working but mandatory --- src/gsheets_copy.cpp | 24 ++++++------------------ src/include/gsheets_copy.hpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/gsheets_copy.cpp b/src/gsheets_copy.cpp index 38bf147..fd0052a 100644 --- a/src/gsheets_copy.cpp +++ b/src/gsheets_copy.cpp @@ -21,26 +21,12 @@ namespace duckdb copy_to_sink = GSheetWriteSink; } - struct GSheetCopyGlobalState : public GlobalFunctionData - { - explicit GSheetCopyGlobalState(ClientContext &context, const string &sheet_id, const string &token, const string &sheet_name) - : sheet_id(sheet_id), token(token), sheet_name(sheet_name) - { - } - - public: - string sheet_id; - string token; - string sheet_name; - }; - - struct GSheetWriteBindData : public TableFunctionData - { - }; unique_ptr GSheetCopyFunction::GSheetWriteBind(ClientContext &context, CopyFunctionBindInput &input, const vector &names, const vector &sql_types) { - return make_uniq(); + string file_path = input.info.file_path; + + return make_uniq(file_path, sql_types, names); } unique_ptr GSheetCopyFunction::GSheetWriteInitializeGlobal(ClientContext &context, FunctionData &bind_data, const string &file_path) @@ -96,9 +82,11 @@ namespace duckdb sheet_data["range"] = "Sheet1"; sheet_data["majorDimension"] = "ROWS"; - // TODO: Add column headers + vector headers = bind_data_p.Cast().options.name_list; vector> values; + values.push_back(headers); + for (idx_t r = 0; r < input.size(); r++) { vector row; diff --git a/src/include/gsheets_copy.hpp b/src/include/gsheets_copy.hpp index d762b08..2803939 100644 --- a/src/include/gsheets_copy.hpp +++ b/src/include/gsheets_copy.hpp @@ -6,6 +6,37 @@ namespace duckdb { + struct GSheetCopyGlobalState : public GlobalFunctionData + { + explicit GSheetCopyGlobalState(ClientContext &context, const string &sheet_id, const string &token, const string &sheet_name) + : sheet_id(sheet_id), token(token), sheet_name(sheet_name) + { + } + + public: + string sheet_id; + string token; + string sheet_name; + }; + + struct GSheetWriteOptions + { + vector name_list; + }; + + struct GSheetWriteBindData : public TableFunctionData + { + vector files; + GSheetWriteOptions options; + vector sql_types; + + GSheetWriteBindData(string file_path, vector sql_types, vector names) + : sql_types(std::move(sql_types)) + { + files.push_back(std::move(file_path)); + options.name_list = std::move(names); + } + }; class GSheetCopyFunction : public CopyFunction { From ff565c82362e2bdde212010be110ec247d6da8e1 Mon Sep 17 00:00:00 2001 From: Archie Wood <58074498+archiewood@users.noreply.github.com> Date: Fri, 25 Oct 2024 00:11:14 -0400 Subject: [PATCH 2/3] add test --- test/sql/copyto.test | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 test/sql/copyto.test diff --git a/test/sql/copyto.test b/test/sql/copyto.test new file mode 100644 index 0000000..3f4b867 --- /dev/null +++ b/test/sql/copyto.test @@ -0,0 +1,35 @@ +# name: test/sql/copyto.test +# description: test COPY TO function +# group: [gsheets] + +require-env TOKEN + +require gsheets + +# Create a secret NB must substitute a token, do not commit! +statement ok +create secret test_secret (type gsheet, token '${TOKEN}'); + +# Create a table to copy to Google Sheet +statement ok +create table spreadsheets as +select 'Microsoft' as company, 'Excel' as product, 1985 as year_founded +union all +select 'Google', 'Google Sheets', 2006 +union all +select 'Apple', 'Numbers', 1984 +union all +select 'LibreOffice', 'Calc', 2000; + +# Copy the table to Google Sheet +statement ok +copy spreadsheets to 'https://docs.google.com/spreadsheets/d/11QdEasMWbETbFVxry-SsD8jVcdYIT1zBQszcF84MdE8/edit#gid=0' (format gsheet); + +# Read the table from Google Sheet +query III +from read_gsheet('https://docs.google.com/spreadsheets/d/11QdEasMWbETbFVxry-SsD8jVcdYIT1zBQszcF84MdE8/edit#gid=0'); +---- +Microsoft Excel 1985 +Google Google Sheets 2006 +Apple Numbers 1984 +LibreOffice Calc 2000 From 5a7281b5152c19c96ce0fa8bd78a44a9be070865 Mon Sep 17 00:00:00 2001 From: Archie Wood <58074498+archiewood@users.noreply.github.com> Date: Fri, 25 Oct 2024 00:12:12 -0400 Subject: [PATCH 3/3] update todos --- TODO.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index 49dad11..ebadc16 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,7 @@ # TODO ## Copy to -- [ ] header +- [x] header - [ ] sheet - [ ] types - [ ] implicit copy to when it sees a gsheets url