Wednesday 5 June 2013

Sybase - grant select to all tables in a schema

The problem is to grant a permission (select in this case) to a particular user, on all tables/views in a database.

select distinct 'grant select on ' + user_name + '.' + name + ' to username go' command from sysobjects inner join SYSUSER on SYSUSER.user_id = sysobjects.uid where type in ('U' ,'V') and user_name='database_name'

Just copy and paste the results into a command line session.

Wednesday 20 February 2013

Calculate supernets to /16 in Oracle SQL

This bit of SQL calculates all the supernet addresses (to /16) for a given IP address (and mask) in Oracle. Supernet addresses are the 'containing' IP Addresses based on powers of 2, so a /30 address covers 2(32-30) addresses (or 4 IPs). A /26 address covers 2(32-26) addresses (64 IPs). This works on Oracle 9, 10 and 11. If your version does not support regexp_replace there are some commented fields that utilise some instr calls.
select 
lvl supernet_mask, 
ip, 
regexp_substr(ip,'^\d+\.\d+\.') || resl2 || '.' || resl supernet
from
(
select 
  lvl, ip, mask, q3,q4,
  case when lvl > 24 then
    null
  else 
    0 
  end supernet
  , 
  floor(q4/(power(2,32-lvl))) * power(2,32-lvl) resl 
  , 
  CASE WHEN lvl <= 24 then 
    floor(q3/(power(2,24-lvl))) * power(2,24-lvl) 
  ELSE 
    Q3
  END resl2
from
(
select lvl, ip, mask
, to_number(regexp_substr(ip,'\d+',1)) q1
, to_number(regexp_replace(ip,'(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})','\2')) q2
, to_number(regexp_replace(ip,'(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})','\3')) q3
, to_number(regexp_replace(ip,'(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})','\4')) q4
/* -- if your version does not support regexp_replace
, to_number(substr(ip,1,instr(ip,'.',1,1)-1)) q1
, to_number(substr(ip,instr(ip,'.',1,1)+1,instr(ip,'.',1,2 )-instr(ip,'.',1,1)-1 ))  q2
, to_number(substr(ip,instr(ip,'.',1,2)+1,instr(ip,'.',1,3 )-instr(ip,'.',1,2)-1 )) q3
, to_number(substr(ip,instr(ip,'.',1,3)+1)) q4
*/
from 
(
select 33 - level lvl,'203.63.104.108' ip,30 mask from dual connect by level <= 17 union
select 33 - level lvl,'203.63.104.12' ip,30 mask from dual connect by level <= 17 union
select 33 - level lvl,'203.63.104.124' ip,30 mask from dual connect by level <= 17 union
select 33 - level lvl,'203.63.104.144' ip,30 mask from dual connect by level <= 17 
) x
where mask >= lvl
) xx order by ip, lvl desc
) xxx

Sunday 3 February 2013

This Javascript function will filter a table according to a RegEx match with an input string. It does this by looping through each row of the table and hiding it if nothing matches. This is a pretty useful bit of code when you are presented report outputs as html tables, your user likes to chop and change conditions, and the query itself is a little slow.
//takes the tableid, and the filterstring and loops through the table
//doing a regex match using the filterString. if line matches, the <tr> is shown
// but if no match anywhere on the line, it is hidden.  
// Andrew Cave 04/02/2013
// andrew.cave.blogging@gmail.com
function filterTable(tableID,filterString){
t = document.getElementById(tableID);
if(typeof t=='undefined'){return;}//exit if the table hasn't been created yet
tbod = t.tBodies[0];
rows = tbod.rows;
rowct = rows.length;
filterString = trim(filterString);
filterString=(filterString.length==0)?'.*':filterString; //empty strings return all records
re = new RegExp(trim(filterString),"i");

for(i=0;i < rowct;i++){
cells=rows[i].cells;
cellct=cells.length;
hideme = 1; //start with hideme being true
for(j=0;j < cellct;j++){
	var td = cells[j].innerHTML;
	if(td.match(re)){ //checking for a match
		hideme=0;
		break;
	}
}
if(hideme){
	$(rows[i]).hide();
} else {
	$(rows[i]).show();
}
}
}

Thursday 10 January 2013

List of contrasting web-colors

a simple list of web-colours for backgrounds, with a contrasting foreground colour (based on RGB luminosity values):
background-colrcolor
#000000 #FFFFFF
#000080 #FFFFFF
#00008B #FFFFFF
#0000CD #FFFFFF
#0000FF #FFFFFF
#191970 #FFFFFF
#4B0082 #FFFFFF
#800000 #FFFFFF
#8B0000 #FFFFFF
#800080 #FFFFFF
#8B008B #FFFFFF
#006400 #FFFFFF
#9400D3 #FFFFFF
#2F4F4F #FFFFFF
#483D8B #FFFFFF
#008000 #FFFFFF
#B22222 #FFFFFF
#FF0000 #FFFFFF
#A52A2A #FFFFFF
#DC143C #FFFFFF
#8B4513 #FFFFFF
#C71585 #FFFFFF
#008080 #FFFFFF
#8A2BE2 #FFFFFF
#556B2F #FFFFFF
#228B22 #FFFFFF
#008B8B #FFFFFF
#9932CC #FFFFFF
#A0522D #FFFFFF
#FF1493 #FFFFFF
#FF00FF #FFFFFF
#2E8B57 #FFFFFF
#696969 #FFFFFF
#4169E1 #FFFFFF
#6A5ACD #FFFFFF
#808000 #FFFFFF
#FF4500 #FFFFFF
#4682B4 #FFFFFF
#6B8E23 #FFFFFF
#1E90FF #FFFFFF
#7B68EE #FFFFFF
#708090 #FFFFFF
#CD5C5C #FFFFFF
#808080 #FFFFFF
#D2691E #FFFFFF
#BA55D3 #FFFFFF
#20B2AA #FFFFFF
#778899 #FFFFFF
#9370D8 #FFFFFF
#B8860B #FFFFFF
#3CB371 #000000
#5F9EA0 #000000
#00BFFF #000000
#32CD32 #000000
#FF6347 #000000
#6495ED #000000
#00CED1 #000000
#D87093 #000000
#CD853F #000000
#00FF00 #000000
#DA70D6 #000000
#BC8F8F #000000
#FF69B4 #000000
#FF8C00 #000000
#FF7F50 #000000
#F08080 #000000
#FA8072 #000000
#00FA9A #000000
#00FF7F #000000
#DAA520 #000000
#48D1CC #000000
#A9A9A9 #000000
#8FBC8F #000000
#66CDAA #000000
#E9967A #000000
#9ACD32 #000000
#FFA500 #000000
#EE82EE #000000
#40E0D0 #000000
#BDB76B #000000
#00FFFF #000000
#F4A460 #000000
#FFA07A #000000
#DDA0DD #000000
#D2B48C #000000
#7CFC00 #000000
#87CEEB #000000
#7FFF00 #000000
#87CEFA #000000
#DEB887 #000000
#C0C0C0 #000000
#B0C4DE #000000
#90EE90 #000000
#D8BFD8 #000000
#FFD700 #000000
#ADD8E6 #000000
#FFB6C1 #000000
#ADFF2F #000000
#B0E0E6 #000000
#98FB98 #000000
#D3D3D3 #000000
#7FFFD4 #000000
#FFC0CB #000000
#AFEEEE #000000
#DCDCDC #000000
#F0E68C #000000
#F5DEB3 #000000
#FFDAB9 #000000
#FFDEAD #000000
#EEE8AA #000000
#FFFF00 #000000
#FFE4B5 #000000
#E6E6FA #000000
#FFE4C4 #000000
#FFE4E1 #000000
#FAEBD7 #000000
#FFEBCD #000000
#FFEFD5 #000000
#FAF0E6 #000000
#F5F5DC #000000
#FFF0F5 #000000
#F5F5F5 #000000
#FDF5E6 #000000
#E0FFFF #000000
#FAFAD2 #000000
#F0F8FF #000000
#FFF8DC #000000
#FFFACD #000000
#FFF5EE #000000
#F8F8FF #000000
#F0FFF0 #000000
#FFFAF0 #000000
#F0FFFF #000000
#F5FFFA #000000
#FFFFE0 #000000
#FFFAFA #000000
#FFFFF0 #000000
#FFFFFF #000000