remove duplicate (altercolumn was there at functions and now is duplicated at procedures)

This commit is contained in:
Carlos Ruiz 2013-02-08 10:52:01 -05:00
parent 2f4f806acf
commit 1aaf64fa7b
2 changed files with 22 additions and 141 deletions

View File

@ -1,111 +0,0 @@
create or replace function altercolumn(tablename name, columnname name, datatype name,
nullclause varchar, defaultclause varchar) returns void as $$
declare
command text;
viewtext text[];
viewname name[];
dropviews name[];
i int;
j int;
v record;
sqltype text;
sqltype_short text;
typename name;
begin
if datatype is not null then
select pg_type.typname, format_type(pg_type.oid, pg_attribute.atttypmod)
into typename, sqltype
from pg_class, pg_attribute, pg_type
where relname = lower(tablename)
and relkind = 'r'
and pg_class.oid = pg_attribute.attrelid
and attname = lower(columnname)
and atttypid = pg_type.oid;
sqltype_short := sqltype;
if typename = 'numeric' then
sqltype_short := replace(sqltype, ',0', '');
elsif strpos(sqltype,'character varying') = 1 then
sqltype_short := replace(sqltype, 'character varying', 'varchar');
elsif sqltype = 'timestamp without time zone' then
sqltype_short := 'timestamp';
end if;
if lower(datatype) <> sqltype and lower(datatype) <> sqltype_short then
i := 0;
for v in select a.relname, a.oid
from pg_class a, pg_depend b, pg_depend c, pg_class d, pg_attribute e
where a.oid = b.refobjid
and b.objid = c.objid
and b.refobjid <> c.refobjid
and b.deptype = 'n'
and c.refobjid = d.oid
and d.relname = lower(tablename)
and d.relkind = 'r'
and d.oid = e.attrelid
and e.attname = lower(columnname)
and c.refobjsubid = e.attnum
and a.relkind = 'v'
loop
i := i + 1;
viewtext[i] := pg_get_viewdef(v.oid);
viewname[i] := v.relname;
end loop;
if i > 0 then
begin
for j in 1 .. i loop
command := 'drop view ' || viewname[j];
execute command;
dropviews[j] := viewname[j];
end loop;
exception
when others then
i := array_upper(dropviews, 1);
if i > 0 then
for j in 1 .. i loop
command := 'create or replace view ' || dropviews[j] || ' as ' || viewtext[j];
execute command;
end loop;
end if;
raise exception 'Failed to recreate dependent view';
end;
end if;
command := 'alter table ' || lower(tablename) || ' alter column ' || lower(columnname) || ' type ' || lower(datatype);
execute command;
i := array_upper(dropviews, 1);
if i > 0 then
for j in 1 .. i loop
command := 'create or replace view ' || dropviews[j] || ' as ' || viewtext[j];
execute command;
end loop;
end if;
end if;
end if;
if defaultclause is not null then
if lower(defaultclause) = 'null' then
command := 'alter table ' || lower(tablename) || ' alter column ' || lower(columnname) || ' drop default ';
else
command := 'alter table ' || lower(tablename) || ' alter column ' || lower(columnname) || ' set default ''' || defaultclause || '''';
end if;
execute command;
end if;
if nullclause is not null then
if lower(nullclause) = 'not null' then
command := 'alter table ' || lower(tablename) || ' alter column ' || lower(columnname) || ' set not null';
execute command;
elsif lower(nullclause) = 'null' then
command := 'alter table ' || lower(tablename) || ' alter column ' || lower(columnname) || ' drop not null';
execute command;
end if;
end if;
end;
$$ language plpgsql;
/*
create table t_alter_column
( tablename name, columnname name, datatype name, nullclause varchar(10), defaultclause varchar(200));
create rule alter_column_rule as on insert to t_alter_column
do instead select altercolumn(new.tablename, new.columnname, new.datatype, new.nullclause,
new.defaultclause);
*/

View File

@ -104,30 +104,23 @@ public class MTable extends X_AD_Table
MTable retValue = null; MTable retValue = null;
String sql = "SELECT * FROM AD_Table WHERE UPPER(TableName)=?"; String sql = "SELECT * FROM AD_Table WHERE UPPER(TableName)=?";
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
pstmt = DB.prepareStatement (sql, null); pstmt = DB.prepareStatement (sql, null);
pstmt.setString(1, tableName.toUpperCase()); pstmt.setString(1, tableName.toUpperCase());
ResultSet rs = pstmt.executeQuery (); rs = pstmt.executeQuery ();
if (rs.next ()) if (rs.next())
retValue = new MTable (ctx, rs, null); retValue = new MTable (ctx, rs, null);
rs.close ();
pstmt.close ();
pstmt = null;
} }
catch (Exception e) catch (Exception e)
{ {
s_log.log(Level.SEVERE, sql, e); s_log.log(Level.SEVERE, sql, e);
} }
try finally
{ {
if (pstmt != null) DB.close(rs, pstmt);
pstmt.close (); rs = null; pstmt = null;
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
} }
if (retValue != null) if (retValue != null)
@ -229,37 +222,30 @@ public class MTable extends X_AD_Table
return m_columns; return m_columns;
m_columnNameMap = new HashMap<String, Integer>(); m_columnNameMap = new HashMap<String, Integer>();
m_columnIdMap = new HashMap<Integer, Integer>(); m_columnIdMap = new HashMap<Integer, Integer>();
String sql = "SELECT * FROM AD_Column WHERE AD_Table_ID=? ORDER BY ColumnName"; String sql = "SELECT * FROM AD_Column WHERE AD_Table_ID=? AND IsActive='Y' ORDER BY ColumnName";
ArrayList<MColumn> list = new ArrayList<MColumn>(); ArrayList<MColumn> list = new ArrayList<MColumn>();
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
pstmt = DB.prepareStatement (sql, get_TrxName()); pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt (1, getAD_Table_ID()); pstmt.setInt (1, getAD_Table_ID());
ResultSet rs = pstmt.executeQuery (); rs = pstmt.executeQuery ();
while (rs.next ()) { while (rs.next ()) {
MColumn column = new MColumn (getCtx(), rs, get_TrxName()); MColumn column = new MColumn (getCtx(), rs, get_TrxName());
list.add (column); list.add (column);
m_columnNameMap.put(column.getColumnName().toUpperCase(), list.size() - 1); m_columnNameMap.put(column.getColumnName().toUpperCase(), list.size() - 1);
m_columnIdMap.put(column.getAD_Column_ID(), list.size() - 1); m_columnIdMap.put(column.getAD_Column_ID(), list.size() - 1);
} }
rs.close ();
pstmt.close ();
pstmt = null;
} }
catch (Exception e) catch (Exception e)
{ {
log.log(Level.SEVERE, sql, e); log.log(Level.SEVERE, sql, e);
} }
try finally
{ {
if (pstmt != null) DB.close(rs, pstmt);
pstmt.close (); rs = null; pstmt = null;
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
} }
// //
m_columns = new MColumn[list.size ()]; m_columns = new MColumn[list.size ()];
@ -476,6 +462,7 @@ public class MTable extends X_AD_Table
finally finally
{ {
DB.close(rs, pstmt); DB.close(rs, pstmt);
rs = null; pstmt = null;
} }
return po; return po;
@ -581,21 +568,26 @@ public class MTable extends X_AD_Table
public static int getTable_ID(String tableName) { public static int getTable_ID(String tableName) {
int retValue = 0; int retValue = 0;
String SQL = "SELECT AD_Table_ID FROM AD_Table WHERE tablename = ?"; String SQL = "SELECT AD_Table_ID FROM AD_Table WHERE tablename = ?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
PreparedStatement pstmt = DB.prepareStatement(SQL, null); pstmt = DB.prepareStatement(SQL, null);
pstmt.setString(1, tableName); pstmt.setString(1, tableName);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) if (rs.next())
retValue = rs.getInt(1); retValue = rs.getInt(1);
rs.close();
pstmt.close();
} }
catch (Exception e) catch (Exception e)
{ {
s_log.log(Level.SEVERE, SQL, e); s_log.log(Level.SEVERE, SQL, e);
retValue = -1; retValue = -1;
} }
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
return retValue; return retValue;
} }