2010年9月30日木曜日

groovyとH2 Databaseでコンスタントを一覧表示する

groovyとH2 Databaseでコンスタントを一覧表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:h2:tcp://localhost/~/test",
"sa",
"",
"org.h2.Driver")

query = """
select
constant_schema,
constant_name,
sql
from
information_schema.constants
order by constant_name
"""

// constantを一覧表示する
sql.eachRow(query){
println("constant_schama:${it.constant_schema}")
println("constant_name:${it.constant_name}")
println("sql:${it.sql}")
println("-----")
}



動作環境
groovy 1.7.4, JDK6 Update 21, H2 Database 1.2.143 (2010-09-18)

2010年9月29日水曜日

groovyとH2 Databaseでドメインを一覧表示する

groovyとH2 Databaseでドメインを一覧表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:h2:tcp://localhost/~/test",
"sa",
"",
"org.h2.Driver")

query = """
select
domain_schema,
domain_name,
sql
from
information_schema.domains
order by domain_name
"""

// domainを一覧表示する
sql.eachRow(query){
println("domain_schama:${it.domain_schema}")
println("domain_name:${it.domain_name}")
println("sql:${it.sql}")
println("-----")
}



動作環境
groovy 1.7.4, JDK6 Update 21, H2 Database 1.2.143 (2010-09-18)

2010年9月28日火曜日

groovyとH2 Databaseでインデックスのカラムを一覧表示する

groovyとH2 Databaseでインデックスのカラムを一覧表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:h2:tcp://localhost/~/test",
"sa",
"",
"org.h2.Driver")

query = """
select
index_name,
ordinal_position,
column_name,
index_type_name
from
information_schema.indexes
order by
table_schema, table_name, index_name, ordinal_position
"""

// indexのカラムを一覧表示する
sql.eachRow(query){
println("index_name:${it.index_name}")
println("ordinal_position:${it.ordinal_position}")
println("column_name:${it.column_name}")
println("index_type_name:${it.index_type_name}")
println("-----")
}



動作環境
groovy 1.7.4, JDK6 Update 21, H2 Database 1.2.143 (2010-09-18)

2010年9月27日月曜日

groovyとH2 Databaseでロールを一覧表示する

groovyとH2 Databaseでロールを一覧表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:h2:tcp://localhost/~/test",
"sa",
"",
"org.h2.Driver")

query = """
select
name
from
information_schema.roles
order by name
"""

// Roleを一覧表示する
sql.eachRow(query){
println("name:${it.name}")
println("-----")
}



動作環境
groovy 1.7.4, JDK6 Update 21, H2 Database 1.2.143 (2010-09-18)

2010年9月26日日曜日

groovyとH2 Databaseでスキーマを一覧表示する

groovyとH2 Databaseでスキーマを一覧表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:h2:tcp://localhost/~/test",
"sa",
"",
"org.h2.Driver")

query = """
select
schema_name,
schema_owner,
is_default
from
information_schema.schemata
order by schema_name
"""

// Schemaを一覧表示する
sql.eachRow(query){
println("name:${it.schema_name}")
println("owner:${it.schema_owner}")
println("is default?:${it.is_default}")
println("-----")
}



動作環境
groovy 1.7.4, JDK6 Update 21, H2 Database 1.2.143 (2010-09-18)

2010年9月25日土曜日

groovyとH2 Databaseでシーケンスを一覧表示する

groovyとH2 Databaseでシーケンスを一覧表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:h2:tcp://localhost/~/test",
"sa",
"",
"org.h2.Driver")

query = """
select
sequence_schema,
sequence_name,
current_value,
increment
from
information_schema.sequences
order by sequence_name
"""

// Sequenceを一覧表示する
sql.eachRow(query){
println("schema:${it.sequence_schema}")
println("name:${it.sequence_name}")
println("current value:${it.current_value}")
println("increment:${it.increment}")
println("-----")
}



動作環境
groovy 1.7.4, JDK6 Update 21, H2 Database 1.2.143 (2010-09-18)

2010年9月24日金曜日

groovyとH2 Databaseでユーザを一覧表示する

groovyとH2 Databaseでユーザを一覧表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:h2:tcp://localhost/~/test",
"sa",
"",
"org.h2.Driver")

query = """
select
name, admin
from
information_schema.users
order by name
"""

// Userを一覧表示する
sql.eachRow(query){
println("name:${it.name}")
println("is admin?:${it.admin}")
println("-----")
}



動作環境
groovy 1.7.4, JDK6 Update 21, H2 Database 1.2.143 (2010-09-18)

groovyとH2 Databaseでビューを一覧表示する

groovyとH2 Databaseでビューを一覧表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:h2:tcp://localhost/~/test",
"sa",
"",
"org.h2.Driver")

query = """
select
table_schema,
table_name,
view_definition
from
information_schema.views
order by table_name
"""

// Viewを一覧表示する
sql.eachRow(query){
println("schema:${it.table_schema}")
println("view name:${it.table_name}")
println("view definition:${it.view_definition}")
println("-----")
}



動作環境
groovy 1.7.4, JDK6 Update 21, H2 Database 1.2.143 (2010-09-18)

2010年9月22日水曜日

groovyとH2 Databaseでテーブルのカラムを一覧表示する

groovyとH2 Databaseでテーブルのカラムを一覧表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:h2:tcp://localhost/~/test",
"sa",
"",
"org.h2.Driver")

tableSchema = "PUBLIC"
tableName = "TEST1"
query = """
select
cols.table_schema,
cols.table_name,
cols.column_name,
cols.type_name || '(' ||
case when (type_info.params = 'LENGTH' OR
type_info.params is null) then
cast(cols.character_maximum_length as varchar)
else
cols.numeric_precision || ', ' || cols.numeric_scale
end || ')' as coltype
from
information_schema.columns cols
join information_schema.type_info
on (cols.type_name = type_info.type_name)
where
cols.table_schema = ${tableSchema}
and cols.table_name = ${tableName}
order by cols.ordinal_position
"""

// カラムを一覧表示する
sql.eachRow(query){
print("${it.table_schema},${it.table_name},")
println("${it.column_name},${it.coltype}")
}


動作環境
groovy 1.7.4, JDK6 Update 21, H2 Database 1.2.143 (2010-09-18)

2010年9月21日火曜日

groovyとH2 Databaseでユーザテーブルを一覧表示する

groovyとH2 Databaseでユーザテーブルを一覧表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:h2:tcp://localhost/~/test",
"sa",
"",
"org.h2.Driver")

query = """
select table_schema, table_name
from information_schema.tables
where table_type = 'TABLE'
"""

// ユーザテーブルを一覧表示する
sql.eachRow(query){
println("${it.table_schema}.${it.table_name}")
}


動作環境
groovy 1.7.4, JDK6 Update 21, H2 Database 1.2.143 (2010-09-18)

2010年9月20日月曜日

groovyとApache Commons Compressでgzipファイルを解凍する

groovyとApache Commons Compressでgzipファイルを解凍するには、以下のコードを実行します。

import org.apache.commons.compress.compressors.gzip.*

// tgzファイルオープン
fn = "test.tgz"
fis = new FileInputStream(fn)
// 書き込みファイルオープン
fos = new FileOutputStream(
".\\out\\" +
GzipUtils.getUncompressedFilename(fn)
)
gcis = new GzipCompressorInputStream(fis)
// 解凍
buf = new byte[8192]
int rs = -1;
while( (rs = gcis.read(buf)) != -1 ){
fos.write(buf, 0, rs)
}
gcis.close()
fos.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月19日日曜日

groovyとApache Commons Compressでgzipファイルに圧縮する

groovyとApache Commons Compressでgzipファイルに圧縮するには、以下のコードを実行します。

import org.apache.commons.compress.compressors.gzip.*

// 読み込みファイルオープン
fn = "test.tar"
fis = new FileInputStream(fn)
// tgzファイルオープン
fos = new FileOutputStream(
GzipUtils.getCompressedFilename(fn)
)
gcos = new GzipCompressorOutputStream(fos)
// 圧縮
buf = new byte[8192]
int rs = -1;
while( (rs = fis.read(buf)) != -1 ){
gcos.write(buf, 0, rs)
}
gcos.flush()
gcos.close()
fos.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月18日土曜日

groovyとApache Commons Compressでbzip2ファイルを解凍する

groovyとApache Commons Compressでbzip2ファイルを解凍するには、以下のコードを実行します。

import org.apache.commons.compress.compressors.bzip2.*

// bz2ファイルオープン
fn = "test.tar.bz2"
fis = new FileInputStream(fn)
// 書き込みファイルオープン
fos = new FileOutputStream(
".\\out\\" +
BZip2Utils.getUncompressedFilename(fn)
)
bz2cis = new BZip2CompressorInputStream(fis)
// 解凍
buf = new byte[8192]
int rs = -1;
while( (rs = bz2cis.read(buf)) != -1 ){
fos.write(buf, 0, rs)
}
bz2cis.close()
fos.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月17日金曜日

groovyとApache Commons Compressでbzip2ファイルに圧縮する

groovyとApache Commons Compressでbzip2ファイルに圧縮するには、以下のコードを実行します。

import org.apache.commons.compress.compressors.bzip2.*

// 読み込みファイルオープン
fn = "test.tar"
fis = new FileInputStream(fn)
// bz2ファイルオープン
fos = new FileOutputStream(
BZip2Utils.getCompressedFilename(fn)
)
bz2cos = new BZip2CompressorOutputStream(fos, 8)
// 圧縮
buf = new byte[8192]
int rs = -1;
while( (rs = fis.read(buf)) != -1 ){
bz2cos.write(buf, 0, rs)
}
bz2cos.finish()
bz2cos.flush()
bz2cos.close()
fos.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月16日木曜日

groovyとoracleでサーバのホスト・マシン名を表示する

groovyとoracleでサーバのホスト・マシン名を表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger",
"oracle.jdbc.driver.OracleDriver")

query = """
select sys_context('USERENV', 'SERVER_HOST') as sc from dual
"""

// サーバのホスト・マシン名を表示する
sql.eachRow(query){
println("${it.sc}")
}


動作環境
groovy 1.7.4, JDK6 Update21, Oracle11g R2

2010年9月15日水曜日

groovyとApache Commons Compressでzipファイルを解凍する

groovyとApache Commons Compressでzipファイルを解凍するには、以下のコードを実行します。

import org.apache.commons.compress.archivers.zip.*
import org.apache.commons.compress.utils.*

is = new BufferedInputStream(new FileInputStream("test.zip"))
zais = new ZipArchiveInputStream(is, "Windows-31J", true)
dest = ".\\out\\"
while((ent = zais.getNextZipEntry()) != null){
println dest + ent.getName()
df = new File(dest + ent.getName())
if( ent.isDirectory() ){
df.mkdirs()
} else {
if( df.getParentFile().exists() == false ){
df.getParentFile().mkdirs()
}
OutputStream out = new FileOutputStream(df)
IOUtils.copy(zais, out, 8192)
out.close()
}
}
zais.close()
is.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月14日火曜日

groovyとApache Commons Compressでzipファイルに圧縮する

groovyとApache Commons Compressでzipファイルに圧縮するには、以下のコードを実行します。

import org.apache.commons.compress.archivers.zip.*
import org.apache.commons.compress.utils.*

def writeEntries = { aos, base, path ->
if( path.isDirectory() ){
def children = path.listFiles()
if( children.length == 0 ){
// ディレクトリ追加
def fn = path.getAbsolutePath().substring(
base.getAbsolutePath().length()+1).
replace(File.separator,'/')
println("creating...:" + fn)
ent = new ZipArchiveEntry(path, fn)
aos.putArchiveEntry(ent)
aos.closeArchiveEntry()
} else {
for(child in path.listFiles()){
call(aos, base, child)
}
}
} else {
// ファイル追加
def fn = path.getAbsolutePath().substring(
base.getAbsolutePath().length()+1).
replace(File.separator,'/')
println("adding...:" + fn)
ent = aos.createArchiveEntry(path, fn)
aos.putArchiveEntry(ent)
IOUtils.copy(new FileInputStream(path), aos, 8192)
aos.closeArchiveEntry()
}
}

zaos = new ZipArchiveOutputStream(new File("test.zip"))
zaos.setEncoding("Windows-31J")
basedir = new File("testdir")
writeEntries(zaos, basedir, basedir)
zaos.finish()
zaos.flush()
zaos.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月13日月曜日

groovyとoracleでインスタンス名を表示する

groovyとoracleでインスタンス名を表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger",
"oracle.jdbc.driver.OracleDriver")

query = """
select sys_context('USERENV', 'INSTANCE_NAME') as sc from dual
"""

// インスタンス名を表示する
sql.eachRow(query){
println("${it.sc}")
}


動作環境
groovy 1.7.4, JDK6 Update21, Oracle11g R2

2010年9月12日日曜日

groovyとoracleでクライアントのホスト・マシン名を表示する

groovyとoracleでクライアントのホスト・マシン名を表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger",
"oracle.jdbc.driver.OracleDriver")

query = """
select sys_context('USERENV', 'HOST') as sc from dual
"""

// クライアントのホスト・マシン名を表示する
sql.eachRow(query){
println("${it.sc}")
}


動作環境
groovy 1.7.4, JDK6 Update21, Oracle11g R2

2010年9月11日土曜日

groovyとApache Commons Compressでtarファイルを解凍する

groovyとApache Commons Compressでtarファイルを解凍するには、以下のコードを実行します。

import org.apache.commons.compress.archivers.tar.*
import org.apache.commons.compress.utils.*

is = new BufferedInputStream(new FileInputStream("test.tar"))
tais = new TarArchiveInputStream(is, 8192)
dest = ".\\out\\"
while((ent = tais.getNextTarEntry()) != null){
println dest + ent.getName()
df = new File(dest + ent.getName())
if( ent.isDirectory() ){
df.mkdirs()
} else {
if( df.getParentFile().exists() == false ){
df.getParentFile().mkdirs()
}
OutputStream out = new FileOutputStream(df)
IOUtils.copy(tais, out, 8192)
out.close()
}
}
tais.close()
is.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月10日金曜日

groovyとApache Commons Compressでtarファイルに圧縮する

groovyとApache Commons Compressでtarファイルに圧縮するには、以下のコードを実行します。

import org.apache.commons.compress.archivers.tar.*
import org.apache.commons.compress.utils.*

def writeEntries = { aos, base, path ->
if( path.isDirectory() ){
def children = path.listFiles()
if( children.length == 0 ){
// ディレクトリ追加
def fn = path.getAbsolutePath().substring(
base.getAbsolutePath().length()+1).
replace(File.separator,'/')
println("creating...:" + fn)
ent = new TarArchiveEntry(path, fn)
aos.putArchiveEntry(ent)
aos.closeArchiveEntry()
} else {
for(child in path.listFiles()){
call(aos, base, child)
}
}
} else {
// ファイル追加
def fn = path.getAbsolutePath().substring(
base.getAbsolutePath().length()+1).
replace(File.separator,'/')
println("adding...:" + fn)
ent = aos.createArchiveEntry(path, fn)
aos.putArchiveEntry(ent)
IOUtils.copy(new FileInputStream(path), aos, 8192)
aos.closeArchiveEntry()
}
}

taos = new TarArchiveOutputStream(
new FileOutputStream("test.tar"), 8192)
basedir = new File("testdir")
writeEntries(taos, basedir, basedir)
taos.finish()
taos.flush()
taos.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月9日木曜日

groovyとPostgreSQLでテーブルの統計を最後に取得した日時を表示する

groovyとPostgreSQLでテーブルの統計を最後に取得した日時を表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:postgresql://localhost:5432/liferay",
"postgres",
"postgres",
"org.postgresql.Driver")

query = """
select
relname,
last_analyze
from
pg_stat_all_tables
where
schemaname = 'public'
order by relname
"""

sql.eachRow(query){
// テーブル名と最後に統計を取得した日時を表示
println "${it.relname},${it.last_analyze}"
}


動作環境
groovy 1.7.4, JDK6 Update21, PostgreSQL8.4.4

2010年9月8日水曜日

groovyとoracleで現在のデフォルトスキーマを表示する

groovyとoracleで現在のデフォルトスキーマを表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger",
"oracle.jdbc.driver.OracleDriver")

query = """
select sys_context('USERENV', 'CURRENT_SCHEMA') as cs from dual
"""

// 現在のデフォルトスキーマを表示する
sql.eachRow(query){
println("${it.cs}")
}


動作環境
groovy 1.7.4, JDK6 Update21, Oracle11g R2

2010年9月7日火曜日

groovyとoracleでセッション統計を表示する

groovyとoracleでセッション統計を表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:oracle:thin:@localhost:1521:orcl",
"system",
"system",
"oracle.jdbc.driver.OracleDriver")

query = """
select
st.sid as sid,
nm.name,
st.value
from
v\$sesstat st
join v\$statname nm
on (st.statistic# = nm.statistic#)
where
st.sid = userenv('SID')
order by nm.name
"""

// セッション統計を表示する
sql.eachRow(query){
println("${it.name}, ${it.value}")
}


動作環境
groovy 1.7.4, JDK6 Update21, Oracle11g R2

2010年9月6日月曜日

groovyとoracleでデータファイル毎の使用率を調べる

groovyとoracleでデータファイル毎の使用率を調べるには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:oracle:thin:@localhost:1521:orcl",
"system",
"system",
"oracle.jdbc.driver.OracleDriver")

query = """
select ddf.file_name as filename,
round(ddf.bytes/1024/1024, 2) as filesize,
round(dfs.bytes/1024/1024, 2) as freesize,
round((ddf.bytes - dfs.bytes)/ddf.bytes, 2)*100 as used
from dba_data_files ddf
join (select file_id, sum(bytes) as bytes
from dba_free_space
group by file_id) dfs
on (dfs.file_id = ddf.file_id)
order by ddf.file_name
"""

// データファイル毎の使用率
sql.eachRow(query){
println("${it.filename}, ${it.filesize}MB, \
${it.freesize}MB, ${it.used}%")
}


動作環境
groovy 1.7.4, JDK6 Update21, Oracle11g R2

2010年9月5日日曜日

groovyとoracleでユーザ・セグメントタイプ毎の割り当てサイズを調べる

groovyとoracleでユーザ・セグメントタイプ毎の割り当てサイズを調べるには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:oracle:thin:@localhost:1521:orcl",
"system",
"system",
"oracle.jdbc.driver.OracleDriver")

query = """
select
owner,
segment_type,
round(sum(bytes)/1024/1024) as MBytes
from
dba_segments
group by
owner,
segment_type
order by
owner,
segment_type
"""

// ユーザ・セグメントタイプ毎のオブジェクトの割り当てサイズを調べる
sql.eachRow(query){
println("${it.owner},${it.segment_type},${it.MBytes}")
}


動作環境
groovy 1.7.4, JDK6 Update21, Oracle11g R2

2010年9月4日土曜日

groovyとPostgreSQLでテーブルのサイズを一覧表示する

groovyとPostgreSQLで上位30位のテーブルのサイズを一覧表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:postgresql://localhost:5432/yourdb",
"postgres",
"postgres",
"org.postgresql.Driver")

query = """
select
pc.relname table_name,
round(pg_total_relation_size(pc.oid)/1024) as KBytes
from
pg_class pc
where
pc.relkind = 'r'
order by
2 desc
limit 30
"""

sql.eachRow(query){
// テーブル名とサイズ
println "${it.table_name},${it.KBytes}(KBytes)"
}


動作環境
groovy 1.7.4, JDK6 Update21, PostgreSQL8.4.4