Loopers97

Loopers97

MSSQL stored procedures

Still really new to elixir, but I’m trying to get some experience with it. I’m working on trying to hook a new api up to an existing legacy mssql db using sso, that’s filled with stored procedures. I’m currently using ecto 3.4.6 and TDS 2.0. I’m able to pattern match a pid and start link, and query the db in iex.

I’m not quite sure what the code in the module is going to look like, and figured I’d ask for help from the community.

Do I have to do the
{:ok, pid} = Tds.start_link(“sso vars”)
Tds.query(pid, “[customer].gt_customer #{id}”, [])
Everytime I want to get something out of the db?

Does Ecto.Repo not do any of start link for me? Should I even be using Repo, if all my interactions with the data store are through stored procs?

Are there any examples anywhere of table type parameters to Tds.query()?

I’ve scoured the internet and not found much guidance, but I doubt I’m the only person that has had to deal with this. Is there some resources somewhere that’d help? I own ecto and absinthe graphql books. Their material primarily focuses on built in adapters.

Marked As Solved

Loopers97

Loopers97

For future people like me. I know this seems ridiculous, it felt ridiculous writing it. I welcome any advice anyone has to clean this up.

Put the start link in repo
{:ok, pid} = Tds.start_link([db connection string vars])
pid

Account module
{:ok, res} = Repo.query("[schema].[sp_name] #{id}")
res
|> result_to_maps #produce a list of maps
|> Enum.map(fn r -> Repo.load(MODULE, r) end) #Load list of maps into repo

def result_to_maps(%Tds.Result{columns: col_numbers, rows: rows}) do
Enum.map(rows, fn row -> row_to_map(col_numbers, row) end)
end

def row_to_map(col_numbers, values) do
Stream.zip(col_numbers, values)
|> Enum.into([], & &1)
end

Also I used embedded_schema because I don’t have access to the table and they’re not snake_casing because I ask them nicely.

embedded_schema do
field :first_name, :string, source: :FirstName
end

Also Liked

Loopers97

Loopers97

Thanks for the advice. I’ll try to reimplement using the libraries you suggested. I was hoping to get use out of Ecto.Changeset, but… you might be right to just skip over Ecto entirely.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement