@@ -46,36 +46,29 @@ if Code.ensure_loaded?(MyXQL) do
4646
4747 @ impl true
4848 def to_constraints ( % MyXQL.Error { mysql: % { name: :ER_DUP_ENTRY } , message: message } , opts ) do
49- case :binary . split ( message , " for key " ) do
50- [ _ , quoted ] -> [ unique: normalize_index_name ( quoted , opts [ :source ] ) ]
49+ with [ _ , quoted ] <- :binary . split ( message , " for key " ) ,
50+ [ _ , index | _ ] <- :binary . split ( quoted , "'" , [ :global ] ) do
51+ [ unique: strip_source ( index , opts [ :source ] ) ]
52+ else
5153 _ -> [ ]
5254 end
5355 end
56+
5457 def to_constraints ( % MyXQL.Error { mysql: % { name: name } , message: message } , _opts )
5558 when name in [ :ER_ROW_IS_REFERENCED_2 , :ER_NO_REFERENCED_ROW_2 ] do
56- case :binary . split ( message , [ " CONSTRAINT " , " FOREIGN KEY " ] , [ :global ] ) do
57- [ _ , quoted , _ ] -> [ foreign_key: strip_quotes ( quoted ) ]
59+ with [ _ , quoted ] <- :binary . split ( message , [ " CONSTRAINT " , " FOREIGN KEY " ] ) ,
60+ [ _ , index | _ ] <- :binary . split ( quoted , "'" , [ :global ] ) do
61+ [ foreign_key: index ]
62+ else
5863 _ -> [ ]
5964 end
6065 end
66+
6167 def to_constraints ( _ , _ ) ,
6268 do: [ ]
6369
64- defp strip_quotes ( quoted ) do
65- size = byte_size ( quoted ) - 2
66- << _ , unquoted :: binary - size ( size ) , _ >> = quoted
67- unquoted
68- end
69-
70- defp normalize_index_name ( quoted , source ) do
71- name = strip_quotes ( quoted )
72-
73- if source do
74- String . trim_leading ( name , "#{ source } ." )
75- else
76- name
77- end
78- end
70+ defp strip_source ( name , nil ) , do: name
71+ defp strip_source ( name , source ) , do: String . trim_leading ( name , "#{ source } ." )
7972
8073 ## Query
8174
0 commit comments