show postgres query text with params - ruby

I have trouble with query
con = PG::Connection.open(mydatabase)
result = con.exec_params(sql,[params1, params2, params3])
How I can get all text sending query, to investigate it?

I'm not sure you can get it on the client side with using PG and exec_params command. As just mentioned above in the comments you can try to log all queries on the Postgres side (postgresql.conf).
I think the closest approximation you can to achieve here is to install and use pg-connection-general_log gem.
It's pretty simple in installation.
And you should get the general log of all generated queries in the following format:
SQL (0000.78ms) SELECT * FROM users WHERE name = $1 ["param1"] /path/to/test.rb:13:in `block in <main>'

Related

How do I use the azure_table_service.query_entities result and token in Ruby?

I'm trying to us the Ruby Azure SDK to query an Azure Table. I can get the call to work, and if I look at the wireshark it's returning tons of results. But I can't figure out how to iterate through them.
query = {:filter => "Timestamp ge datetime'2015-01-01T00:00:00Z'", :select => ["FileName"]}
result, token = azure_table_service.query_entities("ActivityTable", query)
p result
p token
Shows this as the output.
#<Azure::Table::Entity:0xb8f74fdc #properties={"FileName"=>"LOCKINFO.DAT"}, #table="ActivityTable", #updated=2015-01-06 20:22:14 UTC, #etag=nil>
#<Azure::Table::Entity:0xb8f74f3c #properties={"FileName"=>"Scan000.pdf"}, #table="ActivityTable", #updated=2015-01-06 20:22:14 UTC, #etag=nil>
I tried result.count, result.pop, and others. The documentation really sucks too, https://github.com/Azure/azure-sdk-for-ruby/blob/master/lib/azure/table/table_service.rb. I looks like I'm getting an array of EnumerationResults back but none of the array calls work.
I also can't figure out how to use the token to get the next set of results but that's after I can figure out how to use the ones I have.
-Update-
p result.class
p token.class
Shows that both are Azure::Table::Entity
Okay! I found an issue with the documentation I guess. I shouldn't use their example since
result = azure_table_service.query_entities("XASActivityTable", query)
returns an array of the expected values. Adding that Token variable seems to cause some type of Ruby magic where the first and second values are put into the variables and the rest are dumped.
You can get the status by using below statement.
status = result.properties['status']

How to use Bigquery streaming insertall on Ruby Rails

EDIT: Fixed - for ruby use "insert_all" instead of "insertAll" like the api specifies. The api for ruby needs updating.
Im using v 0.6.4 of the google-api-client gem and trying to create a streaming insert, but keep getting the following error:
google_bigquery.rb:233:in undefined method `insertAll' for #<Google::APIClient::Resource:0xcbc974 NAME:tabledata> (NoMethodError)
My code is as follows:
def streaming_insert_data_in_table(table, dataset=DATASET)
body = {"rows"=>[
{"json"=> {"person_id"=>1, "name"=>"john"}},
{"json"=> {"person_id"=>2, "name"=>"doe"}},
]}
result = #client.execute(
:api_method=> #bigquery.tabledata.insert_all,
:parameters=> {
:projectId=> #project_id.to_s,
:datasetId=> dataset,
:tableId=>table},
:body_object=>body,
)
puts result.body
end
Could someone tell me if the insetAll has been created for the google-api-client gem? I have tried 'insert' as that is what table, dataset etc use and get the same error as well.. I can however run tabledata.list perfectly fine.. I've tried digging throught the gem source code and didn't get anywhere with that.
Is the body object that I created correct or do I need to alter it?
Any help is much appreciated.
Thanks in advance and have a great day.
Ok. So fixed it and updated the code in the question. For ruby: the method is called "insert_all". Also note that the table & schema must be created BEFORE the insert_all. This id different when compared to the the "jobs.insert" method which will create the table if it doesn't exist

Fusion Tables: Why do I keep getting a "400 Bad Request" error when trying to update a table style via Ruby's RestClient gem

I'm trying to update a style for one of my Fusion Tables by using the Ruby gem RestClient.
Here's my code:
require 'rest_client'
tableId = '<STRING CONTAINING TABLEID>'
styleId = '<STRING CONTAINING STYLEID>'
key = '<STRING CONTAINING MY FUSION TABLES API KEY>'
table_url = "https://www.googleapis.com/fusiontables/v1/tables/#{tableId}/styles/#{styleId}?key=#{key}"
update = '{"polygonOptions": {"strokeColor":"#ffffff"}}'
token = 'STRING CONTAINING AUTHORIZATION TOKEN'
RestClient.put table_url,update,{"Authorization" => "Bearer #{token}"}
When I run that code, I get this error:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/abstract_response.rb:48:in `return!': 400 Bad Request (RestClient::BadRequest)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:230:in `process_result'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:178:in `block in transmit'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:745:in `start'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient.rb:80:in `put'
When I input the update code into Google's official Style request PUT maker thingie, the update works. But it does not work when I run my Ruby code.
Does anyone know what I am doing wrong?
EDIT: Extra output I get from adding in RestClient.log = logger
RestClient.put "https://www.googleapis.com/fusiontables/v1/tables/<MY TABLE ID HERE>/styles/4?key=<AND HERE'S WHERE MY FUSION TABLE API KEY GOES>", "{\"polygonOptions\":{\"strokeColor\":\"#ffffff\"}}", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Authorization"=>"Bearer <THIS CONTAINS THE BEARER STRING>", "Content-Length"=>"44"
# => 400 BadRequest | application/json 255 bytes
You really should be using the google-api-ruby-client library instead of building your own REST calls. The library abstracts a lot of the OAuth stuff and formatting of the parameters for you.
Having said that, can you enable debugging for your RestClient and post the output of the RestClient call along with the output from Google official PUT maker thingie (I like your technical jargon there)? Comparing the two should show how they differ and what Google doesn't like with yours.
I think the problem is that you are not setting the content-type to application/json. Try to do something like this:
RestClient.put(table_url, update, {"Authorization" => "Bearer #{token}", "Content-type" => "application/json"})
The payload in this case needs to be json so you can either use your json-string from your example or run to_json on you data structure.
In answer to your question about setting up a logger without Rails, in the comments to #Jay Lee's answer…
Here is a logger set up to output to standard out:
logger = Logger.new STDOUT
logger.level = Logger::WARN # INFO/DEBUG… whatever level you find is needed
logger.datetime_format = '%a %d-%m-%Y %H%M '
Then put the rest of your code into a console (e.g. IRB) and before the last line add:
RestClient.log = logger
and you should get some helpful information output to the terminal. See the documents for the Logger class for more information on the levels available.
It could be because your hash here
update = '{"polygonOptions": {"strokeColor":"#ffffff"}}'
should probably be
update = {"polygonOptions" => {"strokeColor" => "#ffffff"}}
Good luck!
FYI,
Alternatives to try:
1) Remove HTTPS request in project settings and token regeneration.
2) Try to use SSL in that case.
3) Sometimes this error occurs when the values exceeds more than 255 characters that may be the possibility here with you. The same problem occurred sometime back with someone and was resolved after debugging. Check the link for more information.
I was having the same 400 Bad Request problem, specifically when posting styles. I was able to solve the problem by making sure any values for "kind" in the style were namespaced, which the examples in the docs don't always get right -- for example:
{
...
"kind": "fusiontables#buckets"
...
}
Instead of just "buckets".

How to fire raw MongoDB queries directly in Ruby

Is there any way that I can fire a raw mongo query directly in Ruby instead of converting them to the native Ruby objects?
I went through Ruby Mongo Tutorial, but I cannot find such a method anywhere.
If it were mysql, I would have fired a query something like this.
ActiveRecord::Base.connection.execute("Select * from foo")
My mongo query is a bit large and it is properly executing in the MongoDB console. What I want is to directly execute the same inside Ruby code.
Here's a (possibly) better mini-tutorial on how to get directly into the guts of your MongoDB. This might not solve your specific problem but it should get you as far as the MongoDB version of SELECT * FROM table.
First of all, you'll want a Mongo::Connection object. If
you're using MongoMapper then you can call the connection
class method on any of your MongoMapper models to get a connection
or ask MongoMapper for it directly:
connection = YourMongoModel.connection
connection = MongoMapper.connection
Otherwise I guess you'd use the from_uri constructor to build
your own connection.
Then you need to get your hands on a database, you can do this
using the array access notation, the db method, or get
the current one straight from MongoMapper:
db = connection['database_name'] # This does not support options.
db = connection.db('database_name') # This does support options.
db = MongoMapper.database # This should be configured like
# the rest of your app.
Now you have a nice shiny Mongo::DB instance in your hands.
But, you probably want a Collection to do anything interesting
and you can get that using either array access notation or the
collection method:
collection = db['collection_name']
collection = db.collection('collection_name')
Now you have something that behaves sort of like an SQL table so
you can count how many things it has or query it using find:
cursor = collection.find(:key => 'value')
cursor = collection.find({:key => 'value'}, :fields => ['just', 'these', 'fields'])
# etc.
And now you have what you're really after: a hot out of the oven Mongo::Cursor
that points at the data you're interested in. Mongo::Cursor is
an Enumerable so you have access to all your usual iterating
friends such as each, first, map, and one of my personal
favorites, each_with_object:
a = cursor.each_with_object([]) { |x, a| a.push(mangle(x)) }
There are also command and eval methods on Mongo::DB that might do what you want.
In case you are using mongoid you will find the answer to your question here.
If you're using Mongoid 3, it provides easy access to its MongoDB driver: Moped. Here's an example of accessing some raw data without using Models to access the data:
db = Mongoid::Sessions.default
# inserting a new document
collection = db[:collection_name]
collection.insert(name: 'my new document')
# finding a document
doc = collection.find(name: 'my new document').first
# "select * from collection"
collection.find.each do |document|
puts document.inspect
end

Run Doctrine 2 CLI Tools Tasks from a script run from the browser

A little background information: I am working on integrating Doctrine into a CodeIgniter application. I have it working, but I would like to be able to run the Doctrine command line (CLI) tasks from the browser, i.e. not from the command line script.
The reason I desire this is because I will be running Doctrine and CodeIgniter on a shared hosting package where I will not have command line access.
This seems like a very basic feature, but is not readily available with Doctrine 2.
My last-ditch effort will be going into the command line tool and figuring out how the tasks are being executed then duplicating that code in a CodeIgniter controller.
If there is any simpler way to do this, please let me know.
Thanks!
Unanswered duplicate posted a while back.
For the following
$doctrine = \Zend_Registry::get('doctrine');
$em = $doctrine->getEntityManager();
$tool = new \Doctrine\ORM\Tools\SchemaTool($em);
Get the SQL to update the current schema:
$sqlArray = $tool->getUpdateSchemaSql($em->getMetadataFactory()->getAllMetadata());
Update the schema with the current metadata
$res = $tool->updateSchema($em->getMetadataFactory()->getAllMetadata());
Create the schema.
$res = $tool->createSchema($em->getMetadataFactory()->getAllMetadata());
This belongs in an install script. Just create and verify the db connection
$conn = $doctrine->getConnection();
$sql = "SELECT * FROM users";
try {
$stmt = $conn->query($sql); // Simple (too simple?)
die('Already installed');
} catch (Exception $e) {
// Table not found, continue
}
Then create your schema.
You probably don't want to try to run the command-line tools without a command-line.
However, you can do it yourself in scripts pretty simply. For instance, if you wanted to do things that orm:schema-tool:* does, you'd start here

Resources