ray icon indicating copy to clipboard operation
ray copied to clipboard

Ray does not output query bindings when using DB::select() with bindings

Open mbaric opened this issue 1 month ago • 0 comments

Describe the bug Laravel code:

public static function GetMyInvestmentsActiveDeals(int $user_id): array
    {
        $totalDaysInMonth = "DATE_FORMAT( NOW(), '%e')";
        $daysInCurrentMonth = "DATE_FORMAT( LAST_DAY( NOW() ), '%e' )";
        $totalInvestmentDays = "DATEDIFF( l.loan_end_date, DATE_FORMAT(di.date_created, '%Y-%m-%d' ) )";
        $nowInvestmentDays = "DATEDIFF( NOW(), DATE_FORMAT(di.date_created, '%Y-%m-%d' ) ) + 1";
        $loanDoesNotEndThisMonth = "DATEDIFF(l.loan_end_date, NOW()) > 30";
        $daysLeft = "DATEDIFF( l.loan_end_date, NOW() )";

        $sql = "SELECT
		di.deal_id, di.user_id, di.deal_investment_id, di.date_created, di.flag_on_sale, di.flag_is_reserved, di.amount, l.loan_name, l.loan_end_date,
		d.deal_close_date, di.date_created, di.flag_on_sale , di.flag_capital_repaid, di.flag_bought,
		$daysLeft AS days_left, di.secondary_market_review,
		CASE WHEN tmp_rate_override.rate_of_return IS NOT NULL THEN  tmp_rate_override.rate_of_return ELSE di.rate_of_return END as `rate_of_return`,
		CASE WHEN tmp_rate_override.rate_of_return IS NOT NULL THEN  tmp_rate_override.rate_of_return*12/365 ELSE di.rate_of_return*12/365	END as `daily_interest`,
		CASE WHEN tmp_rate_override.rate_of_return IS NOT NULL THEN 1 ELSE 0 END as `is_enhanced?`,
		$totalDaysInMonth as total_days_in_month,		$daysInCurrentMonth as days_in_current_month,		$totalInvestmentDays as total_investment_days,		$nowInvestmentDays as now_investment_days,
		CASE WHEN l.loan_status != " . Loan::LOAN_SATISFACTORY_STATUS . " THEN 0 WHEN di.flag_is_reserved = 0 AND $loanDoesNotEndThisMonth  THEN 1 ELSE 0	END	AS 'flag_deal_investment_available_for_sale',
        CASE WHEN (tmp_has_secondary_market_history.deal_investment_id_old IS NULL) THEN 0 ELSE 1 END AS `has_secondary_market_history`,
        CASE WHEN (tmp_partially_repaid.deal_investment_id IS NULL) THEN 0 ELSE 1 END AS 'is_partially_repaid'
		FROM deal_investment as di
		LEFT JOIN deal as d ON d.deal_id = di.deal_id
		LEFT JOIN loan as l ON l.loan_id = d.loan_id
		LEFT JOIN (
			SELECT deal_id as `deal_id`, rate_of_return as `rate_of_return`			FROM user_rate_override			WHERE user_id = :user_id1		) as tmp_rate_override ON tmp_rate_override.deal_id = d.deal_id
        LEFT JOIN (
			SELECT deal_investment_id_old , count(*) as count_of_deal_investment_old            FROM secondary_market_audit_log             WHERE seller_user_id = :user_id2             GROUP BY deal_investment_id_old
        ) as tmp_has_secondary_market_history ON tmp_has_secondary_market_history.deal_investment_id_old = di.deal_investment_id
        LEFT JOIN (
        SELECT         di.deal_investment_id,        di.user_id        FROM		   deal_investment di INNER JOIN user_wallet_transaction uwt ON di.deal_investment_id=uwt.cash_transaction_id AND di.user_id=uwt.user_id AND di.flag_capital_repaid=0 AND di.flag_pool=0 AND uwt.cash_transaction_source_type=4 AND uwt.flag_is_reserved=0
        WHERE         di.user_id= :user_id3        GROUP BY di.deal_investment_id
		) AS tmp_partially_repaid ON tmp_partially_repaid.user_id=di.user_id AND tmp_partially_repaid.deal_investment_id=di.deal_investment_id
		WHERE di.user_id = :user_id4 AND d.flag_deleted = 0
		AND di.flag_ifisa = 0 AND di.flag_pool = 0
		ORDER BY di.created_at DESC;";
        
        
        $r = DB::select($sql, ['user_id1' => $user_id,'user_id2' => $user_id, 'user_id3' => $user_id, 'user_id4'  => $user_id,]);

        if (isset($r[0]) === true) {
            return $r;
        }

        return false;
    }

Ray outputs in it's window bindied variables: :user_id1, :user_id2, :user_id3, :user_id4 instead of real bindings so we can run the query in SQL.

SELECT
  di.deal_id,
  di.user_id,
  di.deal_investment_id,
  di.date_created,
  di.flag_on_sale,
  di.flag_is_reserved,
  di.amount,
  l.loan_name,
  l.loan_end_date,
  d.deal_close_date,
  di.date_created,
  di.flag_on_sale,
  di.flag_capital_repaid,
  di.flag_bought,
  DATEDIFF(l.loan_end_date, NOW()) AS days_left,
  di.secondary_market_review,
  CASE
    WHEN tmp_rate_override.rate_of_return IS NOT NULL THEN tmp_rate_override.rate_of_return
    ELSE di.rate_of_return
  END as `rate_of_return`,
  CASE
    WHEN tmp_rate_override.rate_of_return IS NOT NULL THEN tmp_rate_override.rate_of_return * 12 / 365
    ELSE di.rate_of_return * 12 / 365
  END as `daily_interest`,
  CASE
    WHEN tmp_rate_override.rate_of_return IS NOT NULL THEN 1
    ELSE 0
  END as `is_enhanced62`,
  DATE_FORMAT(NOW(), '%e') as total_days_in_month,
  DATE_FORMAT(LAST_DAY(NOW()), '%e') as days_in_current_month,
  DATEDIFF(
    l.loan_end_date,
    DATE_FORMAT(di.date_created, '%Y-%m-%d')
  ) as total_investment_days,
  DATEDIFF(
    NOW(),
    DATE_FORMAT(di.date_created, '%Y-%m-%d')
  ) + 1 as now_investment_days,
  CASE
    WHEN l.loan_status != 0 THEN 0
    WHEN di.flag_is_reserved = 0
    AND DATEDIFF(l.loan_end_date, NOW()) > 30 THEN 1
    ELSE 0
  END AS 'flag_deal_investment_available_for_sale',
  CASE
    WHEN (
      tmp_has_secondary_market_history.deal_investment_id_old IS NULL
    ) THEN 0
    ELSE 1
  END AS `has_secondary_market_history`,
  CASE
    WHEN (tmp_partially_repaid.deal_investment_id IS NULL) THEN 0
    ELSE 1
  END AS 'is_partially_repaid'
FROM
  deal_investment as di
  LEFT JOIN deal as d ON d.deal_id = di.deal_id
  LEFT JOIN loan as l ON l.loan_id = d.loan_id
  LEFT JOIN (
    SELECT
      deal_id as `deal_id`,
      rate_of_return as `rate_of_return`
    FROM
      user_rate_override
    WHERE
      user_id = :user_id1
  ) as tmp_rate_override ON tmp_rate_override.deal_id = d.deal_id
  LEFT JOIN (
    SELECT
      deal_investment_id_old,
      count(*) as count_of_deal_investment_old
    FROM
      secondary_market_audit_log
    WHERE
      seller_user_id = :user_id2
    GROUP BY
      deal_investment_id_old
  ) as tmp_has_secondary_market_history ON tmp_has_secondary_market_history.deal_investment_id_old = di.deal_investment_id
  LEFT JOIN (
    SELECT
      di.deal_investment_id,
      di.user_id
    FROM
      deal_investment di
      INNER JOIN user_wallet_transaction uwt ON di.deal_investment_id = uwt.cash_transaction_id
      AND di.user_id = uwt.user_id
      AND di.flag_capital_repaid = 0
      AND di.flag_pool = 0
      AND uwt.cash_transaction_source_type = 4
      AND uwt.flag_is_reserved = 0
    WHERE
      di.user_id = :user_id3
    GROUP BY
      di.deal_investment_id
  ) AS tmp_partially_repaid ON tmp_partially_repaid.user_id = di.user_id
  AND tmp_partially_repaid.deal_investment_id = di.deal_investment_id
WHERE
  di.user_id = :user_id4
  AND d.flag_deleted = 0
  AND di.flag_ifisa = 0
  AND di.flag_pool = 0
ORDER BY
  di.created_at DESC;
Image Image Image

Versions

  • Ray: 2.8.1
  • spatie/ray 1.43.1
  • spatie/laravel-ray 1.41.0
  • PHP version: 8.4.6
  • Laravel version: 12.38.1
  • OS: Ubuntu 22.04.5 LTS

To Reproduce Original code written above with the output

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information): Ubuntu 22.04.3 LTS

mbaric avatar Nov 21 '25 09:11 mbaric