Skip to content

Instantly share code, notes, and snippets.

@finsterthecat
finsterthecat / sql-batch.sql
Created November 17, 2010 15:53
Driver script to run a series of scripts (sql, sqlldr or otherwise) within oracle sqlplus
-- cross platform! Works on unix and windows. Notice ability to run host programs?!
accept _usr prompt 'UserName: (app) ' default 'app'
accept _pwd prompt 'Password: (&_usr) ' default &_usr hide
accept _tns prompt 'TNSalias: (SUE2DEV) ' default 'SUE2DEV'
whenever sqlerror exit
connect &_usr/&_pwd@&_tns
whenever sqlerror continue
@finsterthecat
finsterthecat / drop_all.sql
Created January 25, 2011 23:01
Oracle script: Drop everything for your user. Handy when you can't just delete your user.
declare
stringa varchar2(100);
cursor cur is
select *
from user_objects;
begin
for c in cur loop
begin
@finsterthecat
finsterthecat / simple_oracle_sqlplus_report.sql
Created January 25, 2011 23:22
Shows formatting, column width settings to print out a simple report based on an oracle view
SET LINESIZE 200
SET PAGESIZE 60
COLUMN FIRST_NAME HEADING 'FIRST NAME' FORMAT A15 WRAP
COLUMN LAST_NAME HEADING 'LAST NAME' FORMAT A20 WRAP
COLUMN BUSINESS FORMAT A20 WRAP
COLUMN PHONE FORMAT A15 WRAP
COLUMN EMAIL FORMAT A30 WRAP
COLUMN CREATED FORMAT A16
COLUMN MODIFIED FORMAT A16
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI';
@finsterthecat
finsterthecat / modify_sql_ddl_script.py
Created January 31, 2011 18:42
Drops certain options and qualifiers from create table statements. Could this be any more complicated?
import re, sys, getopt
from collections import deque
DEFAULT_STATEMENT_TERMINATOR = ';'
OPTION_PATTERN = re.compile('''^--<ScriptOptions (.*)/>''', re.I)
TERM_OPTION_PATTERN = re.compile('''^--<ScriptOptions.*statementTerminator="(.*)"/>''', re.I)
TERM_OPTION = '''--<ScriptOptions statementTerminator="%s"/>'''
LOGGING_PATTERN = re.compile('''(.*)^\s+LOGGING\s*$(.*)''', re.I + re.S + re.M)
@finsterthecat
finsterthecat / update_duplicates.sql
Created February 22, 2011 18:23
Fix duplicates. Useful for when you want to add an alternate key to fields that currently have dups.
update searchable s1
set datasource_guid = datasource_guid || ' @@DUPLICATE@@ ' || searchable_id,
published_flag = 'N'
where s1.searchable_id >
(
select min(s2.searchable_id)
from searchable s2
where s1.datasource_id = s2.datasource_id
and s1.datasource_guid = s2.datasource_guid
);
@finsterthecat
finsterthecat / CatgManufacturingMachine.rb
Created March 18, 2011 15:11
State machine to read spreadsheet contain classes, categories and subcategories and produce sql inserts to populate relational db.
require 'CSV'
class CatgManufacturingMachine
attr_accessor :main_class, :catg_class
attr_reader :cur_catg
# Base state - always reset on blank line
class StateLook
def blank_line(o)
o.reset
@finsterthecat
finsterthecat / uncomment.sh
Created April 14, 2011 15:17
Remove all comment lines. Shows how to invoke ruby as line filter using regex
#Remove all line comments. Backup may be important...
ruby -pi.bak -e '$_.sub!(/^#.*/, "")' /work/test.txt
@finsterthecat
finsterthecat / naics_synonym_listener.rb
Created May 25, 2011 20:53
Simple REXML-SAX stream listener example
require 'rubygems'
require 'rexml/document'
require 'rexml/streamlistener'
include REXML
class NaicsSynonymListener
include StreamListener
def initialize(f)
@outfile = File.open(f, 'w')
@finsterthecat
finsterthecat / euler3.rb
Created June 18, 2011 21:40
Euler Problem #3 in Ruby - Find largest prime factor
def big_prime(x)
prime = x
(2..Math.sqrt(x).to_i).each do |i|
break if prime <= i
prime /= i while (prime > i && prime % i == 0)
end
prime
end
s = Time.new
@finsterthecat
finsterthecat / gist:1175976
Last active September 26, 2015 23:27
Euler Problem #5 in Ruby - Smallest number divisible by each number in 1..20
factors = []
(1..20).each do |x|
rem = x
factors.each do |f|
rem /= f if rem % f == 0
end
factors << rem if rem > 1
end
puts "The answer is... " + factors.inject(1) {|prod, f| prod *= f}.to_s