Formatting output from RubyDatamapper similar to SQL - ruby

Running a select statement in psql returns output nicely formatted in a table:
mydb=# SELECT * FROM users WHERE login = 'foo';
id | login | first_name | last_name
------+-------+------------+------------
1000 | foo | Dude | HowdyHeyYo
However, I often run my database queries in IRB, and use datamapper to connect to Postgres. What's the best way to get the output formatted similarly to the above table when running a query like this using Ruby and Datamapper?
>> User.all(:login => 'foo')

psql is formatting the output into the ASCII table layout. psql is the equivalent to IRB at that point, so you'd either have to write a method to call that would wrap your query and output formatted results, or use some IRB plug-ins.
Take a look at IRBTools and, in particular, hirb.

Related

Show runtime of a query on monetdb

I am testing monetdb for a colunmnar storage.
I already installed and run the server
but, when I connect to the client and run a query, the response does not show the time to execute the query.
I am connecting as:
mclient -u monetdb -d voc
I already tried to connect with interactive like:
mclient -u monetdb -d voc -i
Output example:
sql>select count(*) from voc.regions;
+---------+
| L3 |
+=========+
| 5570699 |
+---------+
1 tuple
As mkersten mentioned, I would read through the options of the mclient utility first.
To get server and client timing measurements I used --timer=performance option when starting mclient.
Inside mclient I would then disable the result output by setting \f trash to ignore the results when measuring only.
Prepend trace to your query and you get your results like this:
sql>\f trash
sql>trace select count(*) from categories;
sql:0.000 opt:0.266 run:1.713 clk:5.244 ms
sql:0.000 opt:0.266 run:2.002 clk:5.309 ms
The first of the two lines shows you the server timings, the second one the overall timing including passing the results back to the client.
If you use the latest version MonetDB-Mar18 you have good control over the performance timers, which includes parsing, optimization, and runtime at server. See mclient --help.

db2 drop few database as once

recently I started to work with db2, and created few databases.
To drop a single DB I should use db2 drop db demoDB, is there a way to drop all DBs at once?
Thanks
Taking into account the previous answer, this set of lines do the same without creating a script.
db2 list db directory | tail -n +6 | sed 'N;N;N;N;N;N;N;N;N;N;N;s/\n/ /g' | awk '$28 = /Indirect/ {print "db2 drop database "$7}' | source /dev/stdin
This filters the local databases, and executes the generated output.
(Only works in English environment)
first , i don't think there is any db2 nature way to do that. But I usually do the following thing. At start, the way to see all the databases on your instance is one of the following:
db2 list db directory
db2 list active active databases
Depends on your need ( all DBs or just the active DBs)
I'm sure there is more DBs lists you can get.(at DB2 user guide)
The way I usually drop all my DBs is by using shell script:
1. create new script by using 'vi db2_drop_all.sh' or some other way you want.
2. paste the code:
#!/bin/bash -x
for db_name in $(db2 list db directory | grep Database | \
grep name | cut -d= -f2); do
db2 drop db $db_name || true
done
exit 0
3. save changes
4. and just run the script (after you switched to your instance of course ) sh db2_drop_all.sh
Notice that in step 2 you can change the list of DBs as you wish. ( for example to db2 list active databases)
Hope it helped you. :)

squirrelsql doesn't like query_to_xml

I am using an SQL that includes query_to_xml:
select query_to_xml('select 1+1 answer', true, true, '') as_xml;
When any SQL with query_to_xml is executed in Squirrel SQL it will result in:
| as_xml |
+--------------------+
|<UnknownType (2009)>|
With the same JDBC driver, credentials and a Java class the SQL will result in the expected XML output:
| as_xml |
+------------------------------------------------------------+
| <row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">|
| |
| <answer>2</answer> |
| </row> |
I can execute other functions in Squirrel and they will respond, like select version().
Is this a known feature in Squirrel?
If you are using the latest squirrel client, Go to File, Global Preferences-> Data Type Controls, check the box under Unknown DataTypes section.
Rerun the query, it might work.
It seems to be an issue with the text option output for SQL results. It works if you change to the tabular output. Go to the Session menu and click "Session Properties". On the "General" tab under "Output" change "SQL Results" from Text to Table and rerun the query. You may need to close the existing results tabs first.
If this fixes it please add a bug report so that it can be fixed in the future.

Is it possible to count the number of partitions?

I am working on a test in which I must find out the number of partitions of a table and check if it is right. If I use show partitions TableName I get all the partitions by name, but I wish to get the number of partitions, like something along the lines show count(partitions) TableName (which retuns OK btw.. so it's not good) and get 12 (for ex.).
Is there any way to achieve this??
Using Hive CLI
$ hive --silent -e "show partitions <dbName>.<tableName>;" | wc -l
--silent is to enable silent mode
-e tells hive to execute quoted query string
You could use:
select count(distinct <partition key>) from <TableName>;
By using the below command, you will get the all partitions and also at the end it shows the number of fetched rows. That number of rows means number of partitions
SHOW PARTITIONS [db_name.]table_name [PARTITION(partition_spec)];
< failed pictoral example >
You can use the WebHCat interface to get information like this. This has the benefit that you can run the command from anywhere that the server is accessible. The result is JSON - use a JSON parser of your choice to process the results.
In this example of piping the WebHCat results to Python, only the number 24 is returned representing the number of partitions for this table. (Server name is the name node).
curl -s 'http://*myservername*:50111/templeton/v1/ddl/database/*mydatabasename*/table/*mytablename*/partition?user.name=*myusername*' | python -c 'import sys, json; print len(json.load(sys.stdin)["partitions"])'
24
In scala you can do following:
sql("show partitions <table_name>").count()
I used following.
beeline -silent --showHeader=false --outputformat=csv2 -e 'show partitions <dbname>.<tablename>' | wc -l
Use the following syntax:
show create table <table name>;

MySql queries from elisp?

How can I make SQL queries from within emacs scripts to MySql then print the result set?
#!/usr/bin/emacs --script
(setq sql "select id, name
from foobar
order by name
")
(princ sql)
If you need a really reliable way to use mysql I would suggest using python mysql libraries via pymacs.
If you need it just working use mysql comand line client? Mysql is quite easy to use in scripts.
Check out the example below
$ mysql -u root -p<password> -B <db> --execute="select * from projects"
p_id p_timestamp p_name p_manager_id p_status
1 2009-04-14 14:42:00 Test1 3 1
2 2009-04-14 14:42:13 Test2 3 1
The output should be easy to interpret as it looks like tab separated CSV. So it should be ok for most scenarios.
These may assist you:
http://www.emacswiki.org/emacs/SqlMode
http://atomized.org/2008/10/enhancing-emacs%E2%80%99-sql-mode/

Resources