Skip to content

Commit

Permalink
Interface workup
Browse files Browse the repository at this point in the history
  • Loading branch information
wave2 committed Jun 30, 2011
1 parent d9a988b commit b2256cd
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 75 deletions.
2 changes: 2 additions & 0 deletions mysql/pom.xml
Expand Up @@ -55,6 +55,8 @@
<Bundle-Vendor>Wave2 Limited</Bundle-Vendor> <Bundle-Vendor>Wave2 Limited</Bundle-Vendor>
<Bundle-Activator>org.dbinterrogator.mysql.Activator</Bundle-Activator> <Bundle-Activator>org.dbinterrogator.mysql.Activator</Bundle-Activator>
<Include-Resource>{maven-resources},META-INF/LICENSE=LICENSE</Include-Resource> <Include-Resource>{maven-resources},META-INF/LICENSE=LICENSE</Include-Resource>
<Export-Package>org.dbinterrogator.mysql.Instance</Export-Package>
<Private-Package>org.dbinterrogator.mysql.*</Private-Package>
</instructions> </instructions>
</configuration> </configuration>
</plugin> </plugin>
Expand Down
34 changes: 34 additions & 0 deletions mysql/src/main/java/org/dbinterrogator/mysql/Instance.java
Expand Up @@ -29,10 +29,44 @@
*/ */
package org.dbinterrogator.mysql; package org.dbinterrogator.mysql;


import java.io.BufferedReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

/** /**
* *
* @author Alan Snelson * @author Alan Snelson
*/ */
public interface Instance { public interface Instance {


public void connect(String hostname, int port, String username, String password, String db) throws SQLException;
public void connect(String hostname, String username, String password, String db) throws SQLException;
public void createSchema(String schema);
public void dropSchema(String schema);
public String getCreateDatabase(String database);
public String getCreateEvent(String event);
public String getCreateEvent(String schema, String event);
public String getCreateRoutine(String routine);
public String getCreateRoutine(String schema, String routine);
public String getCreateTable(String table);
public String getCreateTable(String schema, String table);
public String getCreateTrigger(String trigger);
public String getCreateTrigger(String schema, String trigger);
public String getCreateView(String view);
public String getCreateView(String schema, String view);
public Map<String, String> getGlobalVariables();
public String getVersion();
public String executeScript(String schema, BufferedReader script);
public ArrayList<String> listEvents(String schema);
public ArrayList<String> listGrantTables();
public ArrayList<String> listRoutines(String schema);
public ArrayList<String> listSchemata();
public ArrayList<String> listTables(String schema);
public ArrayList<String> listTriggers(String schema);
public ArrayList<String> listViews(String schema);
public String getSchema();
public void setSchema(String schema);
public int cleanup();

} }
85 changes: 43 additions & 42 deletions mysql/src/main/java/org/dbinterrogator/mysql/MySQLInstance.java
Expand Up @@ -31,19 +31,15 @@


import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.StringWriter;
import java.sql.*; import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;


/** /**
* *
Expand Down Expand Up @@ -122,6 +118,7 @@ public MySQLInstance(String host, String username, String password, String db) t
* @param password MySQL Password * @param password MySQL Password
* @param db Default database * @param db Default database
*/ */
@Override
public void connect(String hostname, int port, String username, String password, String db) throws SQLException { public void connect(String hostname, int port, String username, String password, String db) throws SQLException {
try { try {
Class.forName("com.mysql.jdbc.Driver").newInstance(); Class.forName("com.mysql.jdbc.Driver").newInstance();
Expand All @@ -147,6 +144,7 @@ public void connect(String hostname, int port, String username, String password,
* @param password MySQL Password * @param password MySQL Password
* @param db Default database * @param db Default database
*/ */
@Override
public final void connect(String hostname, String username, String password, String db) throws SQLException { public final void connect(String hostname, String username, String password, String db) throws SQLException {
connect(hostname, 3306, username, password, db); connect(hostname, 3306, username, password, db);
} }
Expand All @@ -156,6 +154,7 @@ public final void connect(String hostname, String username, String password, Str
* *
* @param schema Schema name * @param schema Schema name
*/ */
@Override
public void createSchema(String schema) { public void createSchema(String schema) {
//Ok lets see if the database exists - if not create it //Ok lets see if the database exists - if not create it
try { try {
Expand All @@ -180,6 +179,7 @@ public void createSchema(String schema) {
* *
* @param schema Schema name * @param schema Schema name
*/ */
@Override
public void dropSchema(String schema) { public void dropSchema(String schema) {
//Ok lets see if the database exists - if so drop it //Ok lets see if the database exists - if so drop it
try { try {
Expand All @@ -199,17 +199,14 @@ public void dropSchema(String schema) {
} }
} }


public File dumpAllDatabases() {
return null;
}

/** /**
* Get create database script * Get create database script
* *
* @param database Database name * @param database Database name
* @return Create database script * @return Create database script
*/ */
public String dumpCreateDatabase(String database) { @Override
public String getCreateDatabase(String database) {
String createDatabase = null; String createDatabase = null;
try { try {
Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
Expand All @@ -224,24 +221,15 @@ public String dumpCreateDatabase(String database) {
return createDatabase; return createDatabase;
} }


public File dumpDatabase(String database) {
//TODO Need to dump database structure and data
return null;
}

public File dumpAllTables(String database) {
//TODO Need to dump all tables within specified schema
return null;
}

/** /**
* Convenience method for objects created with schema - calls dumpCreateTable * Convenience method for objects created with schema - calls getCreateTable
* *
* @param event Event to dump * @param event Event to dump
* @return Create event definition * @return Create event definition
*/ */
public String dumpCreateEvent(String event) { @Override
return dumpCreateEvent(schema, event); public String getCreateEvent(String event) {
return getCreateEvent(schema, event);
} }


/** /**
Expand All @@ -251,7 +239,8 @@ public String dumpCreateEvent(String event) {
* @param event Event name * @param event Event name
* @return Create event definition * @return Create event definition
*/ */
public String dumpCreateEvent(String schema, String event) { @Override
public String getCreateEvent(String schema, String event) {
String createEvent = "--\n-- Event structure for event `" + event + "`\n--\n\n"; String createEvent = "--\n-- Event structure for event `" + event + "`\n--\n\n";
try { try {
Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
Expand All @@ -272,6 +261,7 @@ public String dumpCreateEvent(String schema, String event) {
* @param routine Routine to dump * @param routine Routine to dump
* @return Create routine definition * @return Create routine definition
*/ */
@Override
public String getCreateRoutine(String routine) { public String getCreateRoutine(String routine) {
return getCreateRoutine(schema, routine); return getCreateRoutine(schema, routine);
} }
Expand All @@ -283,6 +273,7 @@ public String getCreateRoutine(String routine) {
* @param routine Routine name * @param routine Routine name
* @return Create routine definition * @return Create routine definition
*/ */
@Override
public String getCreateRoutine(String schema, String routine) { public String getCreateRoutine(String schema, String routine) {
String createRoutine = "--\n-- Routine structure for routine `" + routine + "`\n--\n\n"; String createRoutine = "--\n-- Routine structure for routine `" + routine + "`\n--\n\n";
try { try {
Expand Down Expand Up @@ -310,6 +301,7 @@ public String getCreateRoutine(String schema, String routine) {
* @param table Table to dump * @param table Table to dump
* @return Create table definition * @return Create table definition
*/ */
@Override
public String getCreateTable(String table) { public String getCreateTable(String table) {
return getCreateTable(schema, table); return getCreateTable(schema, table);
} }
Expand All @@ -321,13 +313,13 @@ public String getCreateTable(String table) {
* @param table Table name * @param table Table name
* @return Create table definition * @return Create table definition
*/ */
@Override
public String getCreateTable(String schema, String table) { public String getCreateTable(String schema, String table) {
String createTable = "--\n-- Table structure for table `" + table + "`\n--\n\n"; String createTable = "--\n-- Table structure for table `" + table + "`\n--\n\n";
try { try {
PreparedStatement stmt = conn.prepareStatement(convertStreamToString(getClass().getResourceAsStream("getCreateTable.sql")),ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
stmt.setString(1, schema); stmt.executeQuery("SHOW CREATE TABLE `" + schema + "`.`" + table + "`");
stmt.setString(2, table); ResultSet rs = stmt.getResultSet();
ResultSet rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
createTable += rs.getString("Create Table") + ";"; createTable += rs.getString("Create Table") + ";";
} }
Expand All @@ -343,13 +335,14 @@ public String getCreateTable(String schema, String table) {
} }


/** /**
* Convenience method for objects created with schema - calls dumpCreateTrigger * Convenience method for objects created with schema - calls getCreateTrigger
* *
* @param trigger Trigger name * @param trigger Trigger name
* @return Create trigger definition * @return Create trigger definition
*/ */
public String dumpCreateTrigger(String trigger) { @Override
return dumpCreateTrigger(schema, trigger); public String getCreateTrigger(String trigger) {
return getCreateTrigger(schema, trigger);
} }


/** /**
Expand All @@ -359,7 +352,8 @@ public String dumpCreateTrigger(String trigger) {
* @param trigger Trigger name * @param trigger Trigger name
* @return Create trigger definition * @return Create trigger definition
*/ */
public String dumpCreateTrigger(String schema, String trigger) { @Override
public String getCreateTrigger(String schema, String trigger) {
String createTrigger = "--\n-- Trigger structure for trigger `" + trigger + "`\n--\n\n"; String createTrigger = "--\n-- Trigger structure for trigger `" + trigger + "`\n--\n\n";
try { try {
Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
Expand All @@ -381,6 +375,7 @@ public String dumpCreateTrigger(String schema, String trigger) {
* @param view View name * @param view View name
* @return Create view definition * @return Create view definition
*/ */
@Override
public String getCreateView(String view) { public String getCreateView(String view) {
return getCreateView(schema, view); return getCreateView(schema, view);
} }
Expand All @@ -392,6 +387,7 @@ public String getCreateView(String view) {
* @param table Table name * @param table Table name
* @return Create table definition * @return Create table definition
*/ */
@Override
public String getCreateView(String schema, String view) { public String getCreateView(String schema, String view) {
String createView = "--\n-- View definition for view `" + view + "`\n--\n\n"; String createView = "--\n-- View definition for view `" + view + "`\n--\n\n";
try { try {
Expand Down Expand Up @@ -470,7 +466,8 @@ public void dumpTable(BufferedWriter out, String table) {
} }
} }


public Map<String, String> dumpGlobalVariables() { @Override
public Map<String, String> getGlobalVariables() {
Map<String, String> variables = new TreeMap(); Map<String, String> variables = new TreeMap();
try { try {
Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
Expand All @@ -485,20 +482,13 @@ public Map<String, String> dumpGlobalVariables() {
return variables; return variables;
} }


public File dumpAllViews(String database) {
return null;
}

public File dumpView(String view) {
return null;
}

/** /**
* Execute SQL script * Execute SQL script
* *
* @param schema Schema name * @param schema Schema name
* @param script SQL Script * @param script SQL Script
*/ */
@Override
public String executeScript(String schema, BufferedReader script) { public String executeScript(String schema, BufferedReader script) {
String result = ""; String result = "";
String line; String line;
Expand Down Expand Up @@ -544,6 +534,7 @@ public String executeScript(String schema, BufferedReader script) {
* *
* @return MySQLInstance version * @return MySQLInstance version
*/ */
@Override
public String getVersion() { public String getVersion() {
return properties.getProperty("application.version"); return properties.getProperty("application.version");
} }
Expand All @@ -554,6 +545,7 @@ public String getVersion() {
* @param schema Schema name * @param schema Schema name
* @return List of events * @return List of events
*/ */
@Override
public ArrayList<String> listEvents(String schema) { public ArrayList<String> listEvents(String schema) {
ArrayList<String> events = new ArrayList(); ArrayList<String> events = new ArrayList();
try { try {
Expand All @@ -577,6 +569,7 @@ public ArrayList<String> listEvents(String schema) {
* *
* @return List of grant tables * @return List of grant tables
*/ */
@Override
public ArrayList<String> listGrantTables() { public ArrayList<String> listGrantTables() {
ArrayList<String> grantTables = new ArrayList(); ArrayList<String> grantTables = new ArrayList();
grantTables.add("user"); grantTables.add("user");
Expand All @@ -596,6 +589,7 @@ public ArrayList<String> listGrantTables() {
* @param schema Schema name * @param schema Schema name
* @return List of routines * @return List of routines
*/ */
@Override
public ArrayList<String> listRoutines(String schema) { public ArrayList<String> listRoutines(String schema) {
ArrayList<String> routines = new ArrayList(); ArrayList<String> routines = new ArrayList();
//Triggers were included beginning with MySQL 5.0.2 //Triggers were included beginning with MySQL 5.0.2
Expand All @@ -621,6 +615,7 @@ public ArrayList<String> listRoutines(String schema) {
* *
* @return List of schemata * @return List of schemata
*/ */
@Override
public ArrayList<String> listSchemata() { public ArrayList<String> listSchemata() {
ArrayList<String> schemata = new ArrayList(); ArrayList<String> schemata = new ArrayList();
try { try {
Expand All @@ -646,6 +641,7 @@ public ArrayList<String> listSchemata() {
* @param schema Schema name * @param schema Schema name
* @return List of tables * @return List of tables
*/ */
@Override
public ArrayList<String> listTables(String schema) { public ArrayList<String> listTables(String schema) {
ArrayList<String> tables = new ArrayList(); ArrayList<String> tables = new ArrayList();
try { try {
Expand Down Expand Up @@ -673,6 +669,7 @@ public ArrayList<String> listTables(String schema) {
* @param schema Schema name * @param schema Schema name
* @return List of triggers * @return List of triggers
*/ */
@Override
public ArrayList<String> listTriggers(String schema) { public ArrayList<String> listTriggers(String schema) {
ArrayList<String> triggers = new ArrayList(); ArrayList<String> triggers = new ArrayList();
//Triggers were included beginning with MySQL 5.0.2 //Triggers were included beginning with MySQL 5.0.2
Expand All @@ -699,6 +696,7 @@ public ArrayList<String> listTriggers(String schema) {
* @param schema Schema name * @param schema Schema name
* @return List of views * @return List of views
*/ */
@Override
public ArrayList<String> listViews(String schema) { public ArrayList<String> listViews(String schema) {
ArrayList<String> views = new ArrayList(); ArrayList<String> views = new ArrayList();
try { try {
Expand Down Expand Up @@ -726,6 +724,7 @@ public ArrayList<String> listViews(String schema) {
* *
* @return Currently set schema * @return Currently set schema
*/ */
@Override
public String getSchema() { public String getSchema() {
return schema; return schema;
} }
Expand All @@ -735,6 +734,7 @@ public String getSchema() {
* *
* @param schema Schema name * @param schema Schema name
*/ */
@Override
public void setSchema(String schema) { public void setSchema(String schema) {
this.schema = schema; this.schema = schema;
try { try {
Expand All @@ -750,7 +750,7 @@ public void setSchema(String schema) {
* @param bIn String to be converted to hex passed in as byte array * @param bIn String to be converted to hex passed in as byte array
* @return bOut MySQL compatible hex string * @return bOut MySQL compatible hex string
*/ */
public static String byteArrayToHexString(byte[] bIn) { private static String byteArrayToHexString(byte[] bIn) {
StringBuilder sb = new StringBuilder(bIn.length * 2); StringBuilder sb = new StringBuilder(bIn.length * 2);
for (int i = 0; i < bIn.length; i++) { for (int i = 0; i < bIn.length; i++) {
int v = bIn[i] & 0xff; int v = bIn[i] & 0xff;
Expand Down Expand Up @@ -836,7 +836,7 @@ private String getHeader() {
* *
* @return String * @return String
*/ */
public static String convertStreamToString(InputStream is) throws Exception { private static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is)); BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
String line = null; String line = null;
Expand All @@ -852,6 +852,7 @@ public static String convertStreamToString(InputStream is) throws Exception {
* *
* @return Application status code * @return Application status code
*/ */
@Override
public int cleanup() { public int cleanup() {
try { try {
conn.close(); conn.close();
Expand Down
Expand Up @@ -4,4 +4,4 @@
* @param table Table Name * @param table Table Name
* @returns * @returns
*/ */
SHOW CREATE TABLE ?.? SHOW CREATE TABLE `?`.`?`

0 comments on commit b2256cd

Please sign in to comment.