Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically infer varchar column name #27

Open
wants to merge 1 commit into
base: version-4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 0 additions & 56 deletions apps/csv2sql/lib/csv2sql/data_transfer.ex

This file was deleted.

10 changes: 9 additions & 1 deletion apps/csv2sql/lib/csv2sql/database/database.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ defmodule Csv2sql.Database do
ProgressTracker.update_row_count(path, length(data_chunk))
end

@spec encode_binary(binary()) :: String.t()
def encode_binary(str) do
if Helpers.get_config(:remove_illegal_characters) do
# TODO: Check if utf8mb4 is supported https://github.com/tallakt/codepagex/issues/27
{:ok, str, replaced} =
Codepagex.to_string(str, :iso_8859_1, Codepagex.replace_nonexistent(""), 0)

# TODO: fix this can slow down things
# TODO: fix this it can slow down things
if replaced > 0,
do: Logger.warn("[#{Process.get(:file)}] Replaced #{replaced} characters in binary data")

Expand All @@ -116,6 +117,13 @@ defmodule Csv2sql.Database do
end
end

@spec string_column_type(non_neg_integer()) :: :text | {:varchar, non_neg_integer()}
def string_column_type(max_data_length) do
if max_data_length > varchar_limit(),
do: :text,
else: {:varchar, max_data_length}
end

# Callbacks to implement
@callback type_mapping(type_map()) :: String.t()

Expand Down
8 changes: 6 additions & 2 deletions apps/csv2sql/lib/csv2sql/database/mysql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ defmodule Csv2sql.Database.MySql do
type_map[:is_boolean] -> "BIT"
type_map[:is_integer] -> "INT"
type_map[:is_float] -> "DOUBLE"
type_map[:is_text] -> "LONGTEXT"
true -> "VARCHAR(#{varchar_limit()})"
true -> type_map[:max_data_length] |> string_column_type() |> get_string_column_type()
end
end

Expand Down Expand Up @@ -54,6 +53,7 @@ defmodule Csv2sql.Database.MySql do
@spec get_ordering_column_type :: String.t()
def get_ordering_column_type(), do: "INT UNSIGNED"

# Private helpers
defp to_date_string(%DateTime{} = datetime),
do: datetime |> DateTime.to_date() |> to_datetime_string()

Expand All @@ -63,4 +63,8 @@ defmodule Csv2sql.Database.MySql do
do: datetime |> DateTime.to_string() |> String.trim_trailing("Z")

defp to_datetime_string(val), do: val

defp get_string_column_type(:text), do: "LONGTEXT"
defp get_string_column_type({:varchar, 0}), do: "VARCHAR(#{varchar_limit()})"
defp get_string_column_type({:varchar, size}), do: "VARCHAR(#{size})"
end
8 changes: 6 additions & 2 deletions apps/csv2sql/lib/csv2sql/database/postgres.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ defmodule Csv2sql.Database.Postgres do
type_map[:is_boolean] -> "BOOLEAN"
type_map[:is_integer] -> "INT"
type_map[:is_float] -> "NUMERIC(1000, 100)"
type_map[:is_text] -> "TEXT"
true -> "VARCHAR(#{varchar_limit()})"
true -> type_map[:max_data_length] |> string_column_type() |> get_string_column_type()
end
end

Expand Down Expand Up @@ -71,4 +70,9 @@ defmodule Csv2sql.Database.Postgres do
@impl Csv2sql.Database
@spec get_ordering_column_type :: String.t()
def get_ordering_column_type(), do: "INT"

# Private helpers
defp get_string_column_type(:text), do: "TEXT"
defp get_string_column_type({:varchar, 0}), do: "VARCHAR(#{varchar_limit()})"
defp get_string_column_type({:varchar, size}), do: "VARCHAR(#{size})"
end
31 changes: 0 additions & 31 deletions apps/csv2sql/lib/csv2sql/db_worker.ex

This file was deleted.

15 changes: 0 additions & 15 deletions apps/csv2sql/lib/csv2sql/db_worker_supervisor.ex

This file was deleted.

55 changes: 0 additions & 55 deletions apps/csv2sql/lib/csv2sql/error_tracker.ex

This file was deleted.

139 changes: 0 additions & 139 deletions apps/csv2sql/lib/csv2sql/import_validator.ex

This file was deleted.

Loading