exquisite icon indicating copy to clipboard operation
exquisite copied to clipboard

Select full elixir object?

Open AndrewDryga opened this issue 9 years ago • 4 comments

Hello,

How can I select whole elixir object as match result? Eg.:

SellOffer.where(sell_offer.id >= 0, select: :"$_", qualified: true, limit: 1)

Results:

%Amnesia.Table.Select{coerce: Matcher.SellOffer, continuation: :"$end_of_table",
 values: [{Matcher.SellOffer, 1, "my_loan_1", "vivus.lv", #Decimal<100.5>,
   #<DateTime(2016-09-26T16:35:51.022265Z Etc/UTC)>,
   #<DateTime(2016-09-26T16:35:51.022272Z Etc/UTC)>, #Decimal<1.1345>,
   #Decimal<0.15>, false, true,
   [%{amount: #Decimal<100.5>,
      date: #<DateTime(2016-09-26T16:35:51.022306Z Etc/UTC)>}],
   %{borrower_parameters: %{age_group: "18-22", delinquent_payments: 0,
       income_amount: #Decimal<1000>, max_dpd: 3},
     loan_parameters: %{apr: #Decimal<36.5>, currency: "EUR",
       duration_group: "ANY", extended: false, is_additional_withdrawal: false,
       outstanding_principal: #Decimal<105>, product_type: "PDL",
       risk_class: "A", risk_subclass: "A", status: "NEW"}}},
  {Matcher.SellOffer, 2, "my_loan_1", "vivus.lv", #Decimal<100.5>,
   #<DateTime(2016-09-26T16:35:51.022265Z Etc/UTC)>,
   #<DateTime(2016-09-26T16:35:51.022272Z Etc/UTC)>, #Decimal<1.1345>,
   #Decimal<0.15>, false, true,
   [%{amount: #Decimal<100.5>,
      date: #<DateTime(2016-09-26T16:35:51.022306Z Etc/UTC)>}],
   %{borrower_parameters: %{age_group: "18-22", delinquent_payments: 0,
       income_amount: #Decimal<1000>, max_dpd: 3},
     loan_parameters: %{apr: #Decimal<36.5>, currency: "EUR",
       duration_group: "ANY", extended: false, is_additional_withdrawal: false,
       outstanding_principal: #Decimal<105>, product_type: "PDL",
       risk_class: "A", risk_subclass: "A", status: "NEW"}}}]}
[%Matcher.SellOffer{book_date: #<DateTime(2016-09-26T16:35:51.022265Z Etc/UTC)>,
  book_value: #Decimal<100.5>,
  criteria: %{borrower_parameters: %{age_group: "18-22", delinquent_payments: 0,
      income_amount: #Decimal<1000>, max_dpd: 3},
    loan_parameters: %{apr: #Decimal<36.5>, currency: "EUR",
      duration_group: "ANY", extended: false, is_additional_withdrawal: false,
      outstanding_principal: #Decimal<105>, product_type: "PDL",
      risk_class: "A", risk_subclass: "A", status: "NEW"}}, dividable: false,
  end_date: #<DateTime(2016-09-26T16:35:51.022272Z Etc/UTC)>, guaranteed: true,
  id: 1, loan_id: "my_loan_1", max_shared_apr: #Decimal<0.15>,
  min_price_rate: #Decimal<1.1345>,
  relative_flows: [%{amount: #Decimal<100.5>,
     date: #<DateTime(2016-09-26T16:35:51.022306Z Etc/UTC)>}],
  trader: "vivus.lv"},
 %Matcher.SellOffer{book_date: #<DateTime(2016-09-26T16:35:51.022265Z Etc/UTC)>,
  book_value: #Decimal<100.5>,
  criteria: %{borrower_parameters: %{age_group: "18-22", delinquent_payments: 0,
      income_amount: #Decimal<1000>, max_dpd: 3},
    loan_parameters: %{apr: #Decimal<36.5>, currency: "EUR",
      duration_group: "ANY", extended: false, is_additional_withdrawal: false,
      outstanding_principal: #Decimal<105>, product_type: "PDL",
      risk_class: "A", risk_subclass: "A", status: "NEW"}}, dividable: false,
  end_date: #<DateTime(2016-09-26T16:35:51.022272Z Etc/UTC)>, guaranteed: true,
  id: 2, loan_id: "my_loan_1", max_shared_apr: #Decimal<0.15>,
  min_price_rate: #Decimal<1.1345>,
  relative_flows: [%{amount: #Decimal<100.5>,
     date: #<DateTime(2016-09-26T16:35:51.022306Z Etc/UTC)>}],
  trader: "vivus.lv"}]

Will return list of all objects, but I want to iterate them 1 by 1. I've tried this code:

SellOffer.where(sell_offer.id >= 0, select: sell_offer, qualified: true, limit: 1)

But if throws AgrumentError exception:

   ** (ArgumentError) argument error
     stacktrace:
       (stdlib) :ets.match_spec_compile([{{Matcher.SellOffer, :"$1", :"$2", :"$3", :"$4", :"$5", :"$6", :"$7", :"$8", :"$9", :"$10", :"$11", :"$12"}, [{:>=, :"$1", 0}], [{Matcher.SellOffer, :"$1", :"$2", :"$3", :"$4", :"$5", :"$6", :"$7", :"$8", :"$9", :"$10", :"$11", :"$12"}]}])
       (mnesia) mnesia.erl:1382: :mnesia.fun_select/10
       lib/amnesia/table.ex:627: Amnesia.Table.select/3
       test/unit/models/sell_offer_test.exs:62: anonymous fn/0 in SellOfferTest.test selects sell offers/1
       (mnesia) mnesia_tm.erl:835: :mnesia_tm.apply_fun/3
       (mnesia) mnesia_tm.erl:810: :mnesia_tm.execute_transaction/5
       test/unit/models/sell_offer_test.exs:56: (test)

AndrewDryga avatar Sep 26 '16 16:09 AndrewDryga

do you want something like this ? :) https://github.com/gasparch/amnesia/commit/2dc9370245b56a51ed46a6f32a9d2a92ae089779

I did not made PR yet.

gasparch avatar Nov 07 '16 16:11 gasparch

Almost, I expect that it will be wrapped in same %User{} model :).

By the way, why don't you create an Ecto adapter?

AndrewDryga avatar Nov 07 '16 16:11 AndrewDryga

I do not need databases other than mnesia :) And actually mnesia was also too slow for my tasks, so switched to ets with some data integrity compromises.

gasparch avatar Nov 07 '16 16:11 gasparch

Anyway, if you will have free time, we will appreciate help (we don't have much experience with Mnesia) - https://github.com/Nebo15/ecto_mnesia. Jose promised to review our repo when we will complete it.

AndrewDryga avatar Nov 07 '16 16:11 AndrewDryga