Forum Discussion
import groovy.sql.Sql
import java.sql.SQLException
// Get basic info to connect
def hostname = hostProps.get("system.hostname")
def user = hostProps.get("sybase.user")
def pass = hostProps.get("sybase.pass")
def port = 21000
// Construct an SQL instance with a URL and a driver
def url = "jdbc:sybase:Tds:$hostname:$port"
def driver = "com.sybase.jdbc4.jdbc.SybDriver"
def sql = Sql.newInstance(url, user, pass, driver)
def conn = sql.getConnection()
try {
def stmt = conn.createStatement()
// Retrieve database names
def dbRs = stmt.executeQuery("select name from master..sysdatabases")
while (dbRs.next()) {
def dbName = dbRs.getString("name")
// Construct a new SQL instance for each database
def dbUrl = "jdbc:sybase:Tds:$hostname:$port?ServiceName=$dbName"
def dbSql = Sql.newInstance(dbUrl, user, pass, driver)
def dbConn = dbSql.getConnection()
try {
def dbStmt = dbConn.createStatement()
// Retrieve segment names for the current database
def segmentRs = dbStmt.executeQuery("select name from syssegments")
while (segmentRs.next()) {
def segmentName = segmentRs.getString("name")
// Process segment information and create instance
def instanceName = "${dbName}_${segmentName}_segment"
def instanceConfig = [
"segment": segmentName,
"description": "Instance for segment: ${segmentName}",
"prop1name": "prop1value",
"prop2name": "prop2value"
]
createInstance(instanceName, instanceConfig)
}
segmentRs.close()
dbStmt.close()
} catch (SQLException e) {
// Handle SQL exception
e.printStackTrace()
} finally {
if (dbConn != null) {
dbConn.close()
}
}
}
dbRs.close()
stmt.close()
} catch (SQLException e) {
// Handle SQL exception
e.printStackTrace()
} finally {
if (conn != null) {
conn.close()
}
}
def createInstance(name, config) {
def description = config.description
def prop1 = config.prop1name
def prop2 = config.prop2name
println("${name}##${name}##${description}####prop1name=${prop1}&prop2name=${prop2}")
}
i got the out put as desired
the problem here is how to pass this instance as wild value in the second script. say if my second script is below
import groovy.sql.Sql
//@Grab('org.hsqldb:hsqldb:2.7.1:jdk8')
//@GrabConfig(systemClassLoader=true)
Class.forName("com.sybase.jdbc4.jdbc.SybDriver")
// Get basic info to connect
def hostname = hostProps.get("system.hostname")
def user = hostProps.get("sybase.user")
def pass = hostProps.get("sybase.pass")
def port = 21000
def wildvalue = instanceProps.get("wildvalue")
def segmentWildcard = "${wildvalue}"
// Construct an SQL instance with a url and a driver
def url = "jdbc:sybase:Tds:${hostname}:${port}"
def sql = Sql.newInstance(url, user, pass, "com.sybase.jdbc4.jdbc.SybDriver")
sql.eachRow("sp_helpsegment '${segmentWildcard}'") { row ->
println row
}
sql.close()
Related Content
- 2 years ago
- 3 days ago
- 2 years ago