Krug.ResultSetHandler (Krug v2.0.27) View Source

Module to handle the resultset returned by Krug.BaseEctoDAO (extended modules) load function.

Link to this section Summary

Functions

Concat the value of column col of all rows of resultset. Return a string with values delimited with comma char.

Obtain the value of a column col of a row row of resultset.

Link to this section Functions

Link to this function

concat_cols_of_result_rows(resultset, col \\ 0)

View Source

Concat the value of column col of all rows of resultset. Return a string with values delimited with comma char.

  • Supose that was load a result as follow:
    %MyXQL.Result{
    columns: ["id", "name", "email", "age", "address"],
    connection_id: 1515,
    last_insert_id: 0,
    num_rows: 1,
    num_warnings: 0,
    rows: [
      [1,"Johannes Backend","johannes@backend.com",54,"404 street"],
      [2,"Peter Druggking","peter@thedrugger.com",23,"404 street"],
      [3,"Josephine Frontfull","josephine@staline.com",34,"404 street"]
    ]
    }
iex > Krug.ResultSetHandler.concat_cols_of_result_rows(resultset,0)
"1,2,3"
iex > Krug.ResultSetHandler.concat_cols_of_result_rows(resultset,1)
"Johannes Backend,Peter Druggking,Josephine Frontfull"
iex > Krug.ResultSetHandler.concat_cols_of_result_rows(resultset,2)
"johannes@backend.com,peter@thedrugger.com,josephine@staline.com"
iex > Krug.ResultSetHandler.concat_cols_of_result_rows(resultset,3)
"54,23,34"
Link to this function

get_column_value(resultset, row, col \\ 0, skip_verification \\ false)

View Source

Obtain the value of a column col of a row row of resultset.

Return a empty string if value is nil.

You could use "skip_verification" parameter as true, if you are sure that values already were verified, this will improve performance.

  • Supose that was load a result as follow:
    %MyXQL.Result{
    columns: ["id", "name", "email", "age", "popularitypercent", "address"],
    connection_id: 1515,
    last_insert_id: 0,
    num_rows: 1,
    num_warnings: 0,
    rows: [
      [1,"Johannes Backend","johannes@backend.com",54,95.7,"404 street"],
      [2,"Peter Druggking","peter@thedrugger.com",23,66.6,"404 street"],
      [3,"Josephine Frontfull","josephine@staline.com",34,17.3,"404 street"]
    ]
    }

And you want transform it to a pretty map array as [%{},%{},%{}], then you could iterate recursively the resultset, making some as this for each row:

%{
  id: Krug.ResultSetHandler.get_column_value(resultset,row,0) |> Krug.NumberUtil.to_integer(),
  name: Krug.ResultSetHandler.get_column_value(resultset,row,1),
  email: Krug.ResultSetHandler.get_column_value(resultset,row,2),
  age: Krug.ResultSetHandler.get_column_value(resultset,row,3) |> Krug.NumberUtil.to_integer(),
  popularitypercent: Krug.ResultSetHandler.get_column_value(resultset,row,4) |> Krug.NumberUtil.to_float(),
  address: Krug.ResultSetHandler.get_column_value(resultset,row,5)
}