SQL Korma "count not supported on this type" error message - sqlkorma

Doing a very simple request rows from 1 table i.e. select * from users.
I am trying to pass the response to a selmer template to iterate over but i get the error message above and indeed i cant do the following
(count get-users)
so it looks like the response is not a Seq
so how do i convert the output of the below to a seq
(defn get-users []
(select users ))
and so use in the below selmer call
(defn home-page []
(layout/render
"users.html" {:users gw/get-users} ))
I am using 0.4.0 of korma
thanks

Im an idiot.
Should be
{:users (gw/get-users)}
What a newb

Related

Query I am using to grab data in JSON file not working?

I have a json file that looks like this:
MY_TABLE.HEADER
{"app"."hello","space"."123","profileid"."aacc","date"."2020-10-10"}
{"app"."island","space"."533","profileid"."xxyy","date"."2021-12-10"}
{"app"."city","space"."883","profileid"."bcee","date"."2021-12-11"}
I want to just grab the list of profileid. So something that looks like this:
aacc
xxyy
bcee
I am using this query:
SELECT x.profileid
FROM MY_TABLE a
LATERAL VIEW JSON_TUPLE (
a.header,
'profileid'
)x as profileid
This is not working and I am getting this error:
Error while compiling statement: FAILED: UDFArgumentException json_tuple()'s arguments have to be string type
Do you know how I can fix this issue?
Noticed that your json is not valid one, it contains dots instead of :. You can fix it using regaxp_replace:
lateral view json_tuple(regexp_replace(a.header, '"\\."','":"'), 'profileid') x as profileid

How to dump full SQL of failing Django queryset?

I am trying out a complex query on the django shell:
qs.annotate(rn=Window(expression=RowNumber(), order_by=F('date').desc(), partition_by=[F('name')]))
This is failing with:
ProgrammingError: syntax error at or near "DESC"
LINE 1: ...ion"."storage_name", ROW_NUMBER() OVER (ORDER BY DESC) OVER...
I need to debug this. I would like to see the full SQL, before it is even sent to Postgres (since it is failing). How can I do this?
From a working queryset, I would simply do:
In [60]: qs = Consumption.objects.values('name')
In [61]: print(qs.query)
SELECT "consumption_consumption"."name" FROM "consumption_consumption"

SQL injections in Rails 4 issue

I'm trying to learn about SQL injections and have tried to implement these, but when I put this code in my controller:
params[:username] = "johndoe') OR admin = 't' --"
#user_query = User.find(:first, :conditions => "username = '#{params[:username]}'")
I get the following error:
Couldn't find all Users with 'id': (first, {:conditions=>"username = 'johndoe') OR admin = 't' --'"}) (found 0 results, but was looking for 2)
I have created a User Model with the username "johndoe", but I am still getting no proper response. BTW I am using Rails 4.
You're using an ancient Rails syntax. Don't use
find(:first, :condition => <condition>) ...
Instead use
User.where(<condtion>).first
find accepts a list of IDs to lookup records for. You're giving it an ID of :first and an ID of condition: ..., which aren't going to match any records.
User.where(attr1: value, attr2: value2)
or for single items
User.find_by(attr1: value, attr2: value)
Bear in mind that while doing all this, it would be valuable to check what the actual sql statement is by adding "to_sql" to the end of the query method (From what I remember, find_by just does a LIMIT by 1)

Restricting select fields with Korma

I'm trying to restrict the columns returned from a select query to just one column, but Korma seems to just add the additional column to the default ones instead of using just this one:
=> (dry-run (select games (fields :white_id)))
dry run :: SELECT "games"."stones", "games"."white_id", "games"."black_id", "games"."white_id" FROM "games" :: []
For reference:
=> (dry-run (select games ))
dry run :: SELECT "games"."stones", "games"."white_id", "games"."black_id" FROM "games" :: []
What I'd like to see as the output is:
SELECT "games"."white_id" FROM "games";
Using latest Korma 0.4.0
How can I get that?
I've reported this upstream and it seems to be the expected behaviour (by the Korma developers) for the current version.
I expect the discussion to continue there instead: https://github.com/korma/Korma/issues/251
I checked following
(use :reload-all 'korma.core)
=> nil
(dry-run (select :users (fields :id)))
dry run :: SELECT "users"."id" FROM "users" :: []
=> [{nil 1}]
(dry-run (select :users))
dry run :: SELECT "users".* FROM "users" :: []
=> [{nil 1}]
i am using korma 0.3.1 and it's working fine for me.
check your korma version and reply back if you still have any issue or mention version number in reply

Protecting against sql injection using activerecord

Following on the question how can I use like query in ruby with sinatra? I have the following problem securing my sql from injection.Here is my method to make a query from the type string, it receives a v(alue) to search for and a k(ey) (=field) to look in.
After that the various selctions are joined by selection.join(' and ')
def string_selector(k, v)
case
when v[/\|/]
v.scan(/([^\|]+)(\|)([^\|]+)/).map {|p| "lower(#{k}) LIKE '%#{p.first.downcase}%' or lower(#{k}) LIKE '%#{p.last.downcase}%'"}
when v[/[<>=]/]
v.scan(/(<=?|>=?|=)([^<>=]+)/).map { |part| p part; "#{k} #{part.first} '#{part.last.strip}'"}
else
# "lower(#{k}) LIKE '%#{v.downcase}%'" #(works)
("lower(#{k}) LIKE ?", '%#{v.downcase}%') #doesn't work
end
end
But i get the error
selectors.rb:38: syntax error, unexpected keyword_end, expecting $end
from C:/../1.9.1/rubygems/core_ext/kernel_require.rb:55:in `require'
What could i be doing wrong ?
There's got to be a better way to do what you are trying to do if you are using ActiveRecord... However, if you need to support your string_selector functionality for some reason, I would at least use Arel:
def string_selector(k, v)
tbl = Arel::Table.new(:test) # your table, or you could pass this in...
condition = case v
when /\|/
vals = v.split(/\|/)
first = vals.shift
vals.inject(tbl[k].matches("%#{first.strip}%")) do |acc, val|
acc.or(tbl[k].matches("%#{val.strip}%"))
end
when /<>/
tbl[k].not_eq(v.gsub(/<>/, '').strip)
when /\=/
tbl[k].eq(v.gsub(/\=/, '').strip)
else
tbl[k].matches(v.strip)
end
tbl.where(condition).to_sql
end
Please note that matches will perform a case insensitive query for you (e.g., by using ILIKE in PostgreSQL).