Wednesday, 11 June 2014

SQLDeveloper: Handy user report to generate column names and data types for arbitrary query

I've already covered the tough question on converting arbitrary query to clob delimited text (Take 1 and Take 2). The resulting function used a code snippet that produced a list of column names and their datatypes in it's intestines. Based on that snippet here's a SQLDeveloper user reports that produces a list of column names and their datatypes for arbitrary query. A result of a report is ready to be used in create (temporary) table statement or insert statement (for target columns) and in many other ways.
Here's the text of a report:
/* http://stackoverflow.com/questions/6544922/column-names-in-an-empty-oracle-ref-cursor */
DECLARE
  v_ref_cur SYS_REFCURSOR;
  v_cur_handle NUMBER;
  v_count NUMBER;
  v_desc_tab dbms_sql.desc_tab;
  v_statement clob := :a_statement;
  v_print_data_type pls_integer := :a_print_data_type;
  DELIMITER constant varchar2( 5 char ) := chr( 13 ) || chr( 10 ) || chr( 9 ) || ', ';
  PROCEDURE print_desc_tab( a_desc_tab IN sys.dbms_sql.desc_tab, a_print_data_type in pls_integer ) as
    v_data_type VARCHAR2(30);
    v_delimiter varchar2( 30 char ) := '';  
  BEGIN
    dbms_output.put_line( '<pre>' );
    FOR i IN 1 .. a_desc_tab.count LOOP
      SELECT DECODE( to_char( a_desc_tab( i ).col_type ), 1, 'VARCHAR2', 2, 'NUMBER', 12, 'DATE' )
      INTO v_data_type
      FROM dual
      ;
      dbms_output.put( v_delimiter || a_desc_tab( i ).col_name );
      if ( 1 = a_print_data_type ) then
        dbms_output.put( ' ' || v_data_type);  
        case a_desc_tab( i ).col_type
          when 1 then
            dbms_output.put( '(' || to_char( a_desc_tab( i ).col_max_len ) || ' char)' );  
          when 2 then
            if ( 0 != a_desc_tab( i ).col_precision ) then
              dbms_output.put( 
                '(' || to_char( a_desc_tab( i ).col_precision ) || ', ' || to_char( a_desc_tab( i ).col_scale ) || ')'  
              );  
            end if;
          else
          
            null;
        end case;
      end if;
      v_delimiter := DELIMITER;
    END LOOP;
    dbms_output.new_line;
    dbms_output.put_line( '</pre>' );
  END print_desc_tab;
  
BEGIN
  OPEN v_ref_cur FOR v_statement;

  v_cur_handle := dbms_sql.to_cursor_number( v_ref_cur );
  dbms_sql.describe_columns( v_cur_handle, v_count, v_desc_tab );
  
  print_desc_tab( v_desc_tab, v_print_data_type );
  dbms_sql.close_cursor( v_cur_handle );
END;
And here's the link for user report (hosted on Google drive). Once downloaded it can be imported into SQLDeveloper.

Wednesday, 14 May 2014

Delphi: Most useful keyboard shortcuts

Here are most useful keyboard shortcuts for Delphi IDE, that I extracted from this wiki (some of these I did not know before).
ShortcutDescription
TabIn Object Inspector activates incremental search for properties. Press again Tab to move focus to property value
Ctr+EIncremental search (search is an undoable action)
Ctrl+Shift+IIndent the current selected block
Ctrl+K I
Ctrl+Shift+UUnindent the current selected block
Ctrl+K U
Alt+[Go to matching paranthesis
Alt+]
Alt+LeftBrowse backward (hotlink history)
Alt+RightBrowse forward (hotlink history)
Alt+UpBrowse to symbol under editor cursor (invoke a hotlink and add it to the hotlink history)
Ctrl+F5Add watch
Alt+GGo to line number in editor
Ctrl+SpaceInvoke code completition
Ctrl+Shift+SpaceInvoce code parameter hints
Ctrl+EnterOpen file at cursor
Ctrl+Shift+EnterFind all references
Ctrl+Shift+UpNavigate to method implementation/declaration
Ctrl+Shift+Down
Ctrl+Alt+PActivate the Tool Palette in filtering mode (start typing, press Enter to drop component)
Ctrl + /Comment line/selected block
Ctrl + Shift + TAdd TODO
F11Invoke Object Inspector window
Shift+Alt+F11Invoke Structure window
Ctrl+Alt+BInvoke Breakpoints window
By the way, I made this cool looking table with emacs Org mode ;-)

Tuesday, 6 May 2014

VBA: These blog posts cover hard topics on working with array formulas in Excel

Many thanks to two great blogs RAD Excel and Daily Dose of Excel for covering hard topic on working with array formulas and setting long array formulas for the Range objects in Excel.

WP: How to change WPs default domain

This WordPress Codex page fully covers the topic.

Monday, 28 April 2014

WP: Redirect back to custom page outside of wp directory after comment

Finally added product reviews for A-Rada.com. Used existing WP installation and it's comment system to show reviews on product page (will cover the topic on showing WP content on custom pages outside of it's default directory in following posts).
The problem rose with the default WP "after comment" redirect. By default after user posts it's valuable review he is redirected to default page view (say a-rada.com/blog/?page_id=32), but what we wanted is to redirect him back to appropriate product page.
With an info from wonderful Wordpress.org Support pages and with the simple code in functions.php the desired behavior was achieved.
add_filter( 'comment_post_redirect', 'my_comment_post_redirect' );
function my_comment_post_redirect( $location ){
 if (preg_match("/page_id=([0-9]*)/", $location, $match)) {
  $pageID = $match[1] + 0;
  $page_title = get_the_title( $pageID ); 

  return "http://" . $_SERVER['HTTP_HOST'] . "/product.php?" . $page_title . "#reviews";
 }
 return $location;
}
As you may see, all pages are bound to concrete products, while all posts are actually blog posts, but as I said that's another story (short one) :)

Monday, 17 March 2014

Batch files: easy way of iterating over arguments

Here's the ease way of iterating over argument list passed to batch files (got it from stackoverlow topic). Used it to invoke ImageMagic's commands that involve their own elaborate interpretation of % operator.
rem im.bat
@echo off
setlocal enableDelayedExpansion
set argCount=0
for %%x in (%*) do set /A argCount+=1
set /a arg=0
for /l %%x in (1, 1, %argCount%) do (
 set /a arg=!arg!+1
 call im_impl.bat %%!arg!
)
endlocal
rem im_impl.bat
rem ImageMagic commands
convert %1_%%d.jpg[1-3] -resize 170x130 -set filename:f %%t_also.jpg %%[filename:f]
convert %1_%%d.jpg[1-4] -resize x109 -crop 124x109+3+0 +append %1_4small.jpg
Now it's possible to run im_impl.bat multiple times with different arguments just calling im.bat:
>im.bat clippy_small chandler_mojito

Wednesday, 26 February 2014