Skip to content

Commit

Permalink
Reuse mysql_and_pgsql_insert_many function
Browse files Browse the repository at this point in the history
  • Loading branch information
arcusfelis committed May 14, 2024
1 parent 1c14567 commit f8f26af
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions src/rdbms/rdbms_queries.erl
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ prepared_upsert_fields(InsertFields, Updates, UniqueKeyFields) ->
case mongoose_rdbms:db_type() of
mssql ->
UniqueKeyFields ++ InsertFields ++ UpdateFields;
_ -> InsertFields ++ UpdateFields
_ ->
InsertFields ++ UpdateFields
end.

prepared_upsert_many_fields(RecordCount, InsertFields, Updates, _UniqueKeyFields) ->
Expand All @@ -192,7 +193,8 @@ prepared_upsert_many_fields(RecordCount, InsertFields, Updates, _UniqueKeyFields
case mongoose_rdbms:db_type() of
mssql ->
InsertFieldsMany;
_ -> InsertFieldsMany ++ UpdateFields
_ ->
InsertFieldsMany ++ UpdateFields
end.

-spec prepare_upsert_many(HostType :: mongooseim:host_type_or_global(),
Expand Down Expand Up @@ -232,16 +234,7 @@ upsert_query_many(HostType, RecordCount, Table, InsertFields, Updates, UniqueKey
NotSupported -> erlang:error({rdbms_not_supported, NotSupported})

Check warning on line 234 in src/rdbms/rdbms_queries.erl

View check run for this annotation

Codecov / codecov/patch

src/rdbms/rdbms_queries.erl#L234

Added line #L234 was not covered by tests
end.

mysql_and_pgsql_insert(Table, Columns) ->
JoinedFields = join(Columns, <<", ">>),
Placeholders = lists:duplicate(length(Columns), $?),
["INSERT INTO ", atom_to_binary(Table, utf8), " (",
JoinedFields,
") VALUES (",
join(Placeholders, ", "),
")"].

pgsql_insert_many(RecordCount, Table, Columns) ->
mysql_and_pgsql_insert_many(RecordCount, Table, Columns) ->
JoinedFields = join(Columns, <<", ">>),
Placeholders = lists:duplicate(length(Columns), $?),
Values = ["(", join(Placeholders, ", "), ")"],
Expand All @@ -250,12 +243,12 @@ pgsql_insert_many(RecordCount, Table, Columns) ->
JoinedFields, ") VALUES ", ManyValues].

upsert_mysql_query(Table, InsertFields, Updates, [Key | _], IncrementalField) ->
Insert = mysql_and_pgsql_insert(Table, InsertFields),
Insert = mysql_and_pgsql_insert_many(1, Table, InsertFields),
OnConflict = mysql_on_conflict(Table, Updates, Key, IncrementalField),
[Insert, OnConflict].

upsert_pgsql_query(Table, InsertFields, Updates, UniqueKeyFields, IncrementalField) ->
Insert = mysql_and_pgsql_insert(Table, InsertFields),
Insert = mysql_and_pgsql_insert_many(1, Table, InsertFields),
OnConflict = pgsql_on_conflict(Updates, UniqueKeyFields),
WhereIncrements = pgsql_ensure_increments(Table, IncrementalField),
[Insert, OnConflict, WhereIncrements].
Expand All @@ -270,7 +263,7 @@ upsert_many_mysql_query(RecordCount, Table, InsertFields) ->
" VALUES ", ManyValues].

upsert_many_pgsql_query(RecordCount, Table, InsertFields, Updates, UniqueKeyFields) ->
Insert = pgsql_insert_many(RecordCount, Table, InsertFields),
Insert = mysql_and_pgsql_insert_many(RecordCount, Table, InsertFields),
OnConflict = pgsql_on_conflict(Updates, UniqueKeyFields),
[Insert, OnConflict].

Expand Down Expand Up @@ -350,7 +343,6 @@ upsert_many_mssql_query(RecordCount, Table, InsertFields, Updates, UniqueKeyFiel
tgt_equals_new(Updates) ->
join([["tgt.", ColumnName, "=new.", ColumnName] || ColumnName <- Updates], ", ").


mssql_on_conflict([]) -> ";";
mssql_on_conflict(Updates) ->
[" WHEN MATCHED THEN UPDATE SET ", update_fields_on_conflict(Updates), ";"].
Expand Down

0 comments on commit f8f26af

Please sign in to comment.