Skip to content

Commit

Permalink
More reduced string creation for SQL library.
Browse files Browse the repository at this point in the history
  • Loading branch information
tberthel committed May 17, 2015
1 parent 6c00869 commit 76ed91f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 44 deletions.
Expand Up @@ -272,24 +272,24 @@ public void insert(Vector values)
{
StringBuffer stringBuffer = new StringBuffer();

stringBuffer.append("INSERT INTO ");
stringBuffer.append(this.sqlStrings.INSERT_INTO);
stringBuffer.append(this.getTableName());
stringBuffer.append(" VALUES ('");
stringBuffer.append(this.sqlStrings.VALUES);

try
{
//Iterator iter = values.iterator();
for (int i = 0; i < values.size() - 1; i++)
{
String value = this.getValue((String) values.get(i));
value = new Replace("\\", "\\\\").all(value);
value = new Replace(this.sqlStrings.ESCAPE, this.sqlStrings.DOUBLE_ESCAPE).all(value);

stringBuffer.append(value);
stringBuffer.append("','");
stringBuffer.append(this.sqlStrings.SINGLE_QUOTE_COMMA_SEP);
}

String value = this.getValue((String) values.lastElement());
value = new Replace("\\", "\\\\").all(value);
value = new Replace(this.sqlStrings.ESCAPE, this.sqlStrings.DOUBLE_ESCAPE).all(value);

stringBuffer.append(value);
stringBuffer.append("')");
Expand All @@ -314,8 +314,7 @@ public HashMap getRow(HashMap keysAndValues)
{
StringBuffer stringBuffer = new StringBuffer();

stringBuffer.append(this.sqlStrings.SELECT_ALL);
stringBuffer.append(sqlStrings.FROM);
stringBuffer.append(this.sqlStrings.SELECT_ALL_FROM);
stringBuffer.append(this.getTableName());
stringBuffer.append(sqlStrings.WHERE);

Expand Down Expand Up @@ -391,8 +390,7 @@ public Vector getRows(HashMap keysAndValues)
{
StringBuffer stringBuffer = new StringBuffer();

stringBuffer.append(sqlStrings.SELECT_ALL);
stringBuffer.append(sqlStrings.FROM);
stringBuffer.append(this.sqlStrings.SELECT_ALL_FROM);
stringBuffer.append(this.getTableName());
stringBuffer.append(sqlStrings.WHERE);

Expand Down Expand Up @@ -463,8 +461,7 @@ public Vector getAllRows()
{
StringBuffer stringBuffer = new StringBuffer();

stringBuffer.append(this.sqlStrings.SELECT_ALL);
stringBuffer.append(sqlStrings.FROM);
stringBuffer.append(this.sqlStrings.SELECT_ALL_FROM);
stringBuffer.append(this.getTableName());

try
Expand Down Expand Up @@ -514,7 +511,7 @@ public Vector getRowsWhereLike(HashMap keysAndValues, HashMap likeKeysAndValues)
{
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("SELECT *");
stringBuffer.append(this.sqlStrings.SELECT_ALL);
stringBuffer.append(sqlStrings.FROM);
stringBuffer.append(this.getTableName());
stringBuffer.append(sqlStrings.WHERE);
Expand Down Expand Up @@ -551,7 +548,7 @@ public Vector getRowsWhereLike(HashMap keysAndValues, HashMap likeKeysAndValues)
String value = new String((String) likeKeysAndValues.get(key));
stringBuffer.append(key);
stringBuffer.append(" LIKE \"");
stringBuffer.append(this.sqlStrings.LIKE_QUOTE);
stringBuffer.append(this.getValue(value));
stringBuffer.append(sqlStrings.CLOSE_QUOTE);
Expand All @@ -564,7 +561,7 @@ public Vector getRowsWhereLike(HashMap keysAndValues, HashMap likeKeysAndValues)
String sqlStatement = stringBuffer.toString();
if (abcs.logic.communication.log.config.type.LogConfigTypes.LOGGING.contains(abcs.logic.communication.log.config.type.LogConfigType.SQLLOGGING))
if (org.allbinary.logic.communication.log.config.type.LogConfigTypes.LOGGING.contains(org.allbinary.logic.communication.log.config.type.LogConfigType.SQLLOGGING))
{
LogUtil.put(LogFactory.getInstance(sqlStrings.SQL_STATEMENT_LABEL + sqlStatement, this, "getRowsWhereLike"));
}
Expand All @@ -584,7 +581,7 @@ public Vector getRowsWhereLike(HashMap keysAndValues, HashMap likeKeysAndValues)
result.put(columnName, field);
}
if (abcs.logic.communication.log.config.type.LogConfigTypes.LOGGING.contains(abcs.logic.communication.log.config.type.LogConfigType.SQLLOGGING))
if (org.allbinary.logic.communication.log.config.type.LogConfigTypes.LOGGING.contains(org.allbinary.logic.communication.log.config.type.LogConfigType.SQLLOGGING))
{
LogUtil.put(LogFactory.getInstance("\nRow Value: " + result.toString(), this, "getRowsWhereLike"));
}
Expand All @@ -593,7 +590,7 @@ public Vector getRowsWhereLike(HashMap keysAndValues, HashMap likeKeysAndValues)
return rows;
} catch (Exception e)
{
if (abcs.logic.communication.log.config.type.LogConfigTypes.LOGGING.contains(abcs.logic.communication.log.config.type.LogConfigType.SQLLOGGINGERROR))
if (org.allbinary.logic.communication.log.config.type.LogConfigTypes.LOGGING.contains(org.allbinary.logic.communication.log.config.type.LogConfigType.SQLLOGGINGERROR))
{
LogUtil.put(LogFactory.getInstance(this.FAILED_SQL_STATEMENT + stringBuffer, this, "getRowsWhereLike", e));
}
Expand All @@ -607,22 +604,21 @@ public Vector getRowsWhereBetween(HashMap whereKeyValuePairs,
{
StringBuffer stringBuffer = new StringBuffer();

stringBuffer.append(this.sqlStrings.SELECT_ALL);
stringBuffer.append(this.sqlStrings.FROM);
stringBuffer.append(this.sqlStrings.SELECT_ALL_FROM);
stringBuffer.append(this.getTableName());

try
{
Vector rows = new Vector();
stringBuffer.append(sqlStrings.WHERE);

Set set = whereKeyValuePairs.keySet();
Iterator whereIter = set.iterator();

String key;
String value;
while (whereIter.hasNext())
{
String key = (String) whereIter.next();
String value = (String) whereKeyValuePairs.get(key);
key = (String) whereIter.next();
value = (String) whereKeyValuePairs.get(key);

stringBuffer.append(key);
stringBuffer.append(sqlStrings.EQUAL_QUOTE);
Expand All @@ -632,12 +628,12 @@ public Vector getRowsWhereBetween(HashMap whereKeyValuePairs,
}

stringBuffer.append(betweenColumn);
stringBuffer.append(" > \"");
stringBuffer.append(this.sqlStrings.MORE_THAN_QUOTE);
stringBuffer.append(smallest);
stringBuffer.append(sqlStrings.CLOSE_QUOTE);
stringBuffer.append(sqlStrings.AND);
stringBuffer.append(betweenColumn);
stringBuffer.append(" < \"");
stringBuffer.append(this.sqlStrings.LESS_THAN_QUOTE);
stringBuffer.append(largest);
stringBuffer.append(sqlStrings.CLOSE_QUOTE);

Expand All @@ -651,15 +647,21 @@ public Vector getRowsWhereBetween(HashMap whereKeyValuePairs,
ResultSet rset = this.executeSQLStatement(sqlStatement);
ResultSetMetaData resultSetMetaData = rset.getMetaData();

HashMap result;
Vector columnNames;
int columnCount;
String columnName;
String field;

while (rset.next())
{
HashMap result = new HashMap();
Vector columnNames = new Vector();
int columnCount = resultSetMetaData.getColumnCount();
result = new HashMap();
columnNames = new Vector();
columnCount = resultSetMetaData.getColumnCount();
for (int index = 1; index <= columnCount; index++)
{
String columnName = resultSetMetaData.getColumnName(index);
String field = rset.getString(columnName);
columnName = resultSetMetaData.getColumnName(index);
field = rset.getString(columnName);
result.put(columnName, field);
}

Expand All @@ -684,21 +686,20 @@ public Vector getRowsWhereBetween(String betweenColumn, String smallest, String
{
StringBuffer stringBuffer = new StringBuffer();

stringBuffer.append(this.sqlStrings.SELECT_ALL);
stringBuffer.append(this.sqlStrings.FROM);
stringBuffer.append(this.sqlStrings.SELECT_ALL_FROM);
stringBuffer.append(this.getTableName());

try
{
Vector rows = new Vector();
stringBuffer.append(sqlStrings.WHERE);
stringBuffer.append(betweenColumn);
stringBuffer.append(" > \"");
stringBuffer.append(this.sqlStrings.MORE_THAN_QUOTE);
stringBuffer.append(smallest);
stringBuffer.append(sqlStrings.CLOSE_QUOTE);
stringBuffer.append(sqlStrings.AND);
stringBuffer.append(betweenColumn);
stringBuffer.append(" < \"");
stringBuffer.append(this.sqlStrings.LESS_THAN_QUOTE);
stringBuffer.append(largest);
stringBuffer.append(sqlStrings.CLOSE_QUOTE);

Expand All @@ -712,15 +713,20 @@ public Vector getRowsWhereBetween(String betweenColumn, String smallest, String
ResultSet rset = this.executeSQLStatement(sqlStatement);
ResultSetMetaData resultSetMetaData = rset.getMetaData();

String columnName;
String field;
HashMap result;
Vector columnNames;
int columnCount;
while (rset.next())
{
HashMap result = new HashMap();
Vector columnNames = new Vector();
int columnCount = resultSetMetaData.getColumnCount();
result = new HashMap();
columnNames = new Vector();
columnCount = resultSetMetaData.getColumnCount();
for (int index = 1; index <= columnCount; index++)
{
String columnName = resultSetMetaData.getColumnName(index);
String field = rset.getString(columnName);
columnName = resultSetMetaData.getColumnName(index);
field = rset.getString(columnName);
result.put(columnName, field);
}

Expand Down
Expand Up @@ -27,7 +27,6 @@
import org.allbinary.logic.basic.io.file.directory.Directory;
import org.allbinary.logic.basic.path.AbPath;
import org.allbinary.logic.basic.path.AbPathData;
import org.allbinary.logic.basic.string.StringUtil;
import org.allbinary.logic.communication.log.LogFactory;
import org.allbinary.logic.communication.log.LogUtil;
import org.allbinary.globals.PATH_GLOBALS;
Expand All @@ -39,6 +38,9 @@ public class AbSqlTable extends AbSqlBasic
private String tableName;
private static String EXTENSION = ".adb";

private final String SAVING_BACKUP_PATH = "Saving Backup: Path: ";
private final String FILE_LABEL = " File: ";

public AbSqlTable(DbConnectionInfo databaseConnectionInfoInterface)
{
super(databaseConnectionInfoInterface);
Expand Down Expand Up @@ -154,9 +156,9 @@ private synchronized boolean backupFile(AbPath path, String backupPath, String t
{
stringBuffer.delete(0, stringBuffer.length());

stringBuffer.append("Saving Backup: Path: ");
stringBuffer.append(SAVING_BACKUP_PATH);
stringBuffer.append(backupAbPath.toFileSystemString());
stringBuffer.append(" File: ");
stringBuffer.append(FILE_LABEL);
stringBuffer.append(fileName);

LogUtil.put(LogFactory.getInstance(stringBuffer.toString(), this, "backupFile()"));
Expand Down Expand Up @@ -190,6 +192,8 @@ private String convertNewLines(String value)
'\n', '\f', '\r'
};

final String NEW_LINE = "\\n";

int index = 0;
int lastIndex = 0;
while (index < value.length())
Expand All @@ -198,7 +202,8 @@ private String convertNewLines(String value)
if (index != -1)
{
String nextLine = value.substring(lastIndex, index - 1);
stringBuffer.append(nextLine + "\\n");
stringBuffer.append(nextLine);
stringBuffer.append(NEW_LINE);
lastIndex = index + 1;
} else
{
Expand All @@ -223,7 +228,7 @@ public synchronized String backupTable()
{
int count = 0;

String sqlStatement = "SELECT * FROM " + tableName;
String sqlStatement = this.sqlStrings.SELECT_ALL_FROM + tableName;
String path = org.allbinary.globals.URLGLOBALS.getMainPath()
+ PATH_GLOBALS.getInstance().BACKUP_PATH;

Expand Down Expand Up @@ -262,7 +267,7 @@ public synchronized String backupTable()

stringBuffer.append(this.convertNewLines(value));

stringBuffer.append("','");
stringBuffer.append(this.sqlStrings.SINGLE_QUOTE_COMMA_SEP);
}
stringBuffer.append(rset.getString(colNum));
stringBuffer.append("')\n");
Expand Down
Expand Up @@ -31,8 +31,12 @@ public static SqlStrings getInstance()
public final String AND = " AND ";
public final String UPDATE = "UPDATE ";
public final String SET = " SET ";
public final String SELECT_ALL_FROM = this.SELECT_ALL + this.FROM;

public final String DELETE = "DELETE";
public final String LIKE_QUOTE = " LIKE \"";
public final String INSERT_INTO = "INSERT INTO ";
public final String VALUES = " VALUES ('";

public final String PRIMARY_KEY = "PRIMARY KEY(";
public final String END = ") )";
Expand All @@ -41,6 +45,12 @@ public static SqlStrings getInstance()
public final String CLOSE_QUOTE = "\"";
public final String EQUAL_QUOTE_NO_SPACE = "=\"";

public final String ESCAPE = "\\";
public final String DOUBLE_ESCAPE = "\\\\";
public final String MORE_THAN_QUOTE = " > \"";
public final String LESS_THAN_QUOTE = " < \"";
public final String SINGLE_QUOTE_COMMA_SEP = "','";

public final String SQL_STATEMENT_LABEL = "SQL Statement: ";
public final String COLUMN_VALUE = "\nColumn Value: ";
public final String CREATE_RETURN = " Created Successfully";
Expand Down

0 comments on commit 76ed91f

Please sign in to comment.