Prepared statements everywhere
This commit is contained in:
@ -9,6 +9,8 @@ require "pg"
|
||||
|
||||
USER = File.read("/var/openfaas/secrets/nombres-user").strip
|
||||
PASS = File.read("/var/openfaas/secrets/nombres-pass").strip
|
||||
DB_URL = "postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres"
|
||||
|
||||
|
||||
class Handler
|
||||
def format_buffer(buffer, canvas_name, title = "")
|
||||
@ -41,14 +43,14 @@ class Handler
|
||||
html.gsub("gnuplot_canvas", canvas_name)
|
||||
end
|
||||
|
||||
def query(sql)
|
||||
def query(sql, nombre)
|
||||
# Runs a SQL query against the database.
|
||||
#
|
||||
# Returns an array of values [[Year,Count]...]
|
||||
# Or nil if there are no results
|
||||
|
||||
DB.open("postgres://#{USER}:#{PASS}@10.61.0.1:5432/nombres") do |cursor|
|
||||
cursor.query sql do |result_set|
|
||||
DB.open(DB_URL) do |cursor|
|
||||
cursor.query(sql, nombre) do |result_set|
|
||||
result = [] of Tuple(Int32, Int32)
|
||||
result_set.each do
|
||||
year = result_set.read(Int32)
|
||||
@ -96,10 +98,9 @@ class Handler
|
||||
show_key(true)
|
||||
xrange(1922..2015)
|
||||
nombres.map { |nombre|
|
||||
sql = "SELECT anio::integer, contador::integer FROM nombres WHERE nombre = '#{nombre}' ORDER BY anio"
|
||||
x = Array(Int32).new
|
||||
y = Array(Int32).new
|
||||
results = query(sql)
|
||||
results = query("SELECT anio::integer, contador::integer FROM nombres WHERE nombre = $1 ORDER BY anio", nombre)
|
||||
if results.nil? # No results, all 0s
|
||||
x = (1922..2015).to_a
|
||||
y = x.map { |_| 0 }
|
||||
|
Reference in New Issue
Block a user