-
-
Save austindoeswork/9c2a79a2556bc6fa9d3dde79520568c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
if ARGV.length < 2 | |
puts "USAGE: vo <table_key> <id/name>..." | |
exit 1 | |
end | |
def compile_cmd(table, arg, id) | |
psql_string = "psql -h platform-prod.czdz20rx7adk.us-west-2.rds.amazonaws.com -U clover -d ebdb -c" | |
where = id ? table[:id_where] : table[:string_where] | |
query = sprintf("SELECT %s FROM %s WHERE %s", | |
table[:select], | |
table[:table_name], | |
sprintf(where, arg)) | |
sprintf('%s "%s"', psql_string, query) | |
end | |
tables = { | |
"t" => { | |
table_name: "teams", | |
select: "id, name, company_id", | |
id_where: "id = %s", | |
string_where: "name ILIKE '%%%s%%'", | |
}, | |
"team" => { | |
table_name: "teams", | |
select: "id, name, company_id", | |
id_where: "id = %s", | |
string_where: "name ILIKE '%%%s%%'", | |
}, | |
"teams" => { | |
table_name: "teams", | |
select: "id, name, company_id", | |
id_where: "id = %s", | |
string_where: "name ILIKE '%%%s%%'", | |
}, | |
"co" => { | |
table_name: "companies", | |
select: "id, name", | |
id_where: "id = %s", | |
string_where: "name ILIKE '%%%s%%'", | |
}, | |
"company" => { | |
table_name: "companies", | |
select: "id, name", | |
id_where: "id = %s", | |
string_where: "name ILIKE '%%%s%%'", | |
}, | |
"companies" => { | |
table_name: "companies", | |
select: "id, name", | |
id_where: "id = %s", | |
string_where: "name ILIKE '%%%s%%'", | |
}, | |
"ct" => { | |
table_name: "call_types", | |
select: "id, name, company_id", | |
id_where: "id = %s", | |
string_where: "name ILIKE '%%%s%%'", | |
}, | |
"call_type" => { | |
table_name: "call_types", | |
select: "id, name, company_id", | |
id_where: "id = %s", | |
string_where: "name ILIKE '%%%s%%'", | |
}, | |
"call_types" => { | |
table_name: "call_types", | |
select: "id, name, company_id", | |
id_where: "id = %s", | |
string_where: "name ILIKE '%%%s%%'", | |
}, | |
"u" => { | |
table_name: "users", | |
select: "id, first_name, last_name, email, company_id", | |
id_where: "id = %s", | |
string_where: "first_name ILIKE '%%%1$s%%' OR last_name ILIKE '%%%1$s%%' OR email ILIKE '%%%1$s%%'", | |
}, | |
"user" => { | |
table_name: "users", | |
select: "id, first_name, last_name, email, company_id", | |
id_where: "id = %s", | |
string_where: "first_name ILIKE '%%%1$s%%' OR last_name ILIKE '%%%1$s%%' OR email ILIKE '%%%1$s%%'", | |
}, | |
"users" => { | |
table_name: "users", | |
select: "id, first_name, last_name, email, company_id", | |
id_where: "id = %s", | |
string_where: "first_name ILIKE '%%%1$s%%' OR last_name ILIKE '%%%1$s%%' OR email ILIKE '%%%1$s%%'", | |
}, | |
} | |
arg0 = ARGV[0] | |
table = tables[arg0] | |
if table.nil? | |
puts "INVALID TABLE KEY: Possible keys" | |
puts tables.keys | |
exit 1 | |
end | |
id = (ARGV[1].to_i != 0) | |
cmd = compile_cmd(table, ARGV[1], id) | |
puts `#{cmd}` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment