Open kenshaw opened 3 years ago
\import
might be a bit confusing with \i
, how about \load
?
What you're thinking of as \import
(along with the corresponding \export
) is the \copy
functionality, as far as said functionality actually goes.
This issue was created before we added \copy
.
Then perhaps this one's best closed out so people don't think it's still a live issue.
Currently there is no easy way to import data into a database. While it is fairly straight forward to use
\copy
with acsvq
database, a more generic way of doing bulk imports from a file on disk should be supported, as similar functionality exists in almost every database's native command line client.usql
should match this native functionality with its own\import
command.Similarly, since the
csvq
driver may not be available, a separate\import
function should not rely on its presence and should instead use Go's standard library. Since it's already possible to export queries to disk in CSV and JSON formats, via\o
and\pset format <TYPE>
, a nativeusql
\import
should also support these formats.As such, we propose a
\import
function similar to the following:Where
TABLE
is the name of the table to import to,TYPE
is eithercsv
,json
or some other format to be supported in the future, andFILE
is a path to a file on disk.When
FILE
is not specified, then a file namedTABLE.TYPE
will be looked for in the current working directory. IfTYPE
is also not specified, then it will look for files with extensionscsv
and thenjson
. If a corresponding file is not found, then an error would be returned.If
TYPE
is not specified, then the command will attempt to first detect the import file's type via its extension, and failing that then via Go'shttp.DetectContentType
.The
\import
command would then process the data and prepare anINSERT
statement for each row found in thecsv
orjson
. It is a requirement that CSV files have a header containing the column names, and it is expected that JSON data have the same style output as generated by thetblfmt
's JSON encoder.As such, with a properly implemented
\import
command, the following would be possible: