lib/sequel/adapters/shared/mysql_prepared_statements.rb in sequel-3.44.0 vs lib/sequel/adapters/shared/mysql_prepared_statements.rb in sequel-3.45.0

- old
+ new

@@ -42,10 +42,26 @@ attr_accessor :prepared_statements end conn.prepared_statements = {} end + # Stupid MySQL doesn't use SQLState error codes correctly, mapping + # all constraint violations to 23000 even though it recognizes + # different types. + def database_specific_error_class(exception, opts) + case exception.errno + when 1048 + NotNullConstraintViolation + when 1062 + UniqueConstraintViolation + when 1451, 1452 + ForeignKeyConstraintViolation + else + super + end + end + # Executes a prepared statement on an available connection. If the # prepared statement already exists for the connection and has the same # SQL, reuse it, otherwise, prepare the new statement. Because of the # usual MySQL stupidity, we are forced to name arguments via separate # SET queries. Use @sequel_arg_N (for N starting at 1) for these @@ -63,11 +79,11 @@ _execute(conn, "SET " + args.map {|arg| "@sequel_arg_#{i+=1} = #{literal(arg)}"}.join(", "), opts) unless args.empty? opts = opts.merge(:log_sql=>" (#{sql})") if ps.log_sql _execute(conn, "EXECUTE #{ps_name}#{" USING #{(1..i).map{|j| "@sequel_arg_#{j}"}.join(', ')}" unless i == 0}", opts, &block) end end - end + module DatasetMethods include Sequel::Dataset::StoredProcedures # Methods to add to MySQL prepared statement calls without using a # real database prepared statement and bound variables.