Last active
August 29, 2015 14:00
-
-
Save jromero/d11253eb97949278ce16 to your computer and use it in GitHub Desktop.
Basic SQL Driver implementation used in Connect2SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.net.Socket; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import me.jromero.connect2sql.sql.ConnectionDetails; | |
public abstract class BaseDriver { | |
public static final int STATUS_IDLE = 0; | |
public static final int STATUS_CONNECTING = 1; | |
public static final int STATUS_CONNECTED = 2; | |
/** | |
* Keeps track of details such as tables, columns, etc | |
*/ | |
private ConnectionDetails mConnectionDetails; | |
/** | |
* This is the main connection (stream) | |
*/ | |
protected Connection mConnection; | |
/** | |
* Current connection information | |
*/ | |
protected ConnectionInfo mConnectionInfo; | |
/** | |
* Current status of driver | |
*/ | |
public int status = 0; | |
/** | |
* The driver path as required by JDBC | |
*/ | |
protected final String mDriverPath; | |
private OnDisconnectListener mOnDisconnectListener; | |
/** | |
* Default constructor | |
* | |
* @param path | |
* - Driver path for class include | |
*/ | |
public BaseDriver(String path) { | |
mDriverPath = path; | |
} | |
/** | |
* Initiate connection to SQL server | |
* | |
* @param mConnectionInfo | |
* @return Connection | |
* @throws SQLException | |
*/ | |
public Connection connect(ConnectionInfo connectionInfo) | |
throws SQLException { | |
try { | |
// import database driver | |
EzLogger.d("Importing database driver: " + mDriverPath); | |
Class.forName(mDriverPath); | |
// build our connection path | |
String connectionPath = getConnectionString(connectionInfo); | |
// connect | |
EzLogger.d("Connecting to: " + connectionPath); | |
status = STATUS_CONNECTING; | |
DriverManager.setLoginTimeout(10); | |
mConnection = DriverManager.getConnection(connectionPath, | |
connectionInfo.getUsername(), connectionInfo.getPassword()); | |
if (mConnection == null) { | |
status = STATUS_IDLE; | |
} else { | |
status = STATUS_CONNECTED; | |
// save our current connection data | |
mConnectionInfo = connectionInfo; | |
// create new connection detail | |
mConnectionDetails = new ConnectionDetails(); | |
} | |
} catch (SQLException e) { | |
throw new SQLException(e.getMessage()); | |
} catch (ClassNotFoundException e) { | |
throw new SQLException("Class not found: " + e.getMessage()); | |
} catch (Exception e) { | |
throw new SQLException(e.getMessage()); | |
} | |
return mConnection; | |
} | |
/** | |
* Wrapper method for connection.createStatement | |
* | |
* @return {@link Statement} | |
* @throws SQLException | |
*/ | |
public Statement createStatement() throws SQLException { | |
Statement stmt = null; | |
stmt = mConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, | |
ResultSet.CONCUR_READ_ONLY); | |
return stmt; | |
} | |
/** | |
* Current connection stream | |
* | |
* @return | |
*/ | |
public Connection getConnection() { | |
return mConnection; | |
} | |
protected abstract String getConnectionString(ConnectionInfo connectionInfo); | |
/** | |
* Check if connection is still open | |
* | |
* @return Boolean | |
* @throws SQLException | |
*/ | |
public boolean isConnected() { | |
if (mConnection != null) { | |
try { | |
if (!mConnection.isClosed()) { | |
Statement stmt = mConnection.createStatement(); | |
stmt.execute("SELECT 1"); | |
// FIXME: Fix in API level 9 | |
// } else if (!mConnection.isValid(10)) { | |
// return false; | |
// } | |
// by now it should be connected | |
return true; | |
} | |
} catch (SQLException e) { | |
return false; | |
} | |
} | |
return false; | |
} | |
/** | |
* Close current connection | |
*/ | |
public void close() { | |
try { | |
if (!mConnection.isClosed()) { | |
try { | |
mConnection.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} finally { | |
status = STATUS_IDLE; | |
mConnection = null; | |
if (mOnDisconnectListener != null) { | |
mOnDisconnectListener.onDisconnect(mConnectionInfo); | |
} | |
mConnectionInfo = null; | |
} | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static interface OnDisconnectListener { | |
void onDisconnect(ConnectionInfo connectionInfo); | |
} | |
public void setOnDisconnectListener(OnDisconnectListener listener) { | |
mOnDisconnectListener = listener; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class BaseJtdsDriver extends BaseDriver { | |
public BaseJtdsDriver() { | |
super("net.sourceforge.jtds.jdbc.Driver"); | |
} | |
public abstract String getServerType(); | |
@Override | |
protected String getConnectionString(ConnectionInfo connectionInfo) { | |
String connectionPath = "jdbc:jtds:" + getServerType() + "://" + connectionInfo.getHost(); | |
connectionPath += ":" + connectionInfo.getPort(); | |
String database = connectionInfo.getDatabase(); | |
if (!TextUtils.isEmpty(database)) { | |
connectionPath += "/" + database; | |
} | |
connectionPath += ";"; | |
String instance = connectionInfo.getOption(ConnectionInfo.OPTION_INSTANCE); | |
if (!TextUtils.isEmpty(instance)) { | |
connectionPath += "instance=" + instance + ";"; | |
} | |
String domain = connectionInfo.getOption(ConnectionInfo.OPTION_DOMAIN); | |
if (!TextUtils.isEmpty(domain)) { | |
connectionPath += "domain=" + domain + ";"; | |
} | |
String ssl = connectionInfo.getOption(ConnectionInfo.OPTION_SSL); | |
if (!TextUtils.isEmpty(ssl)) { | |
connectionPath += "ssl=" + ssl + ";"; | |
} | |
return connectionPath; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.jromero.connect2sql.sql; | |
import java.util.ArrayList; | |
import java.util.List; | |
import me.jromero.connect2sql.log.EzLogger; | |
public class ConnectionDetails { | |
private Database mCurrentDatabase; | |
private Table mCurrentTable; | |
private List<Database> mDatabases = new ArrayList<Database>(); | |
public ConnectionDetails() { | |
} | |
public List<Database> getDatabases() { | |
return mDatabases; | |
} | |
public void setDatabases(List<Database> databases) { | |
mDatabases.clear(); | |
mDatabases.addAll(databases); | |
} | |
public Database getCurrentDatabase() { | |
return mCurrentDatabase; | |
} | |
public void setCurrentDatabase(Database database) throws Exception { | |
if (!mDatabases.contains(database)) { | |
throw new Exception("Database object is not in our known list of databases!"); | |
} | |
mCurrentDatabase = database; | |
// clear table selection | |
mCurrentTable = null; | |
} | |
public Table getCurrentTable() { | |
return mCurrentTable; | |
} | |
public void setCurrentTable(Table table) throws Exception { | |
if (mCurrentDatabase == null) { | |
throw new Exception("No database is currently selected."); | |
} | |
if (!mCurrentDatabase.getTables().contains(table)) { | |
throw new Exception("Table " + table.getName() + " does not exist " | |
+ "in current database: " + getCurrentDatabase().getName()); | |
} | |
mCurrentTable = table; | |
} | |
public void setCurrentDatabaseByName(String databaseName) throws Exception { | |
int i = 0; | |
for (Database d : mDatabases) { | |
EzLogger.i("setCurrentDatabaseByName: " + i + " of " | |
+ getDatabases().size()); | |
if (d.getName().equals(databaseName)) { | |
setCurrentDatabase(d); | |
break; | |
} | |
i++; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MsSQLDriver extends BaseJtdsDriver { | |
/* (non-Javadoc) | |
* @see me.jromero.connect2sql.sql.driver.BaseJtdsDriver#getServerType() | |
*/ | |
@Override | |
public String getServerType() { | |
return "sqlserver"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment