generated from processing/processing-library-template-ant
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added QueryBuilder class for query builder functionality and implemen…
…ted some basic queries methods
- Loading branch information
Showing
4 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package samuelal.squelized; | ||
|
||
import java.sql.Types; | ||
|
||
import com.healthmarketscience.sqlbuilder.CreateTableQuery; | ||
import com.healthmarketscience.sqlbuilder.CustomSql; | ||
import com.healthmarketscience.sqlbuilder.InsertQuery; | ||
import com.healthmarketscience.sqlbuilder.OrderObject.Dir; | ||
import com.healthmarketscience.sqlbuilder.SelectQuery; | ||
import com.healthmarketscience.sqlbuilder.dbspec.basic.DbSchema; | ||
import com.healthmarketscience.sqlbuilder.dbspec.basic.DbSpec; | ||
import com.healthmarketscience.sqlbuilder.dbspec.basic.DbTable; | ||
|
||
/* Helper class to support CRUD database operations | ||
* using SqlQueryBuilder library | ||
*/ | ||
public class QueryBuilder { | ||
|
||
static DbSchema dbSchema; | ||
static DbSpec dbSpecs; | ||
|
||
|
||
private static void loadSQLBuilderSchema() { | ||
dbSpecs = new DbSpec(); | ||
dbSchema = dbSpecs.addDefaultSchema(); | ||
} | ||
|
||
public static String createTable(String tableName, String[] columnNames, int[] columnTypes, Integer[] columnLength) { | ||
|
||
// Specify table name | ||
DbTable newTable = dbSchema.addTable(tableName); | ||
|
||
// Add columns | ||
for (int i = 0; i < columnNames.length; i++) { | ||
newTable.addColumn(columnNames[i], columnTypes[i], columnLength[i]); | ||
} | ||
|
||
String query = new CreateTableQuery(newTable, true).validate().toString(); | ||
return query; | ||
} | ||
|
||
public static String insertData(String tableName, String[] columns, Object[] data) { | ||
|
||
InsertQuery insertQuery = new InsertQuery(tableName).addCustomColumns(columns, data); | ||
|
||
String query = insertQuery.validate().toString(); | ||
|
||
return query; | ||
|
||
} | ||
|
||
public static String displayAllTableRecords(String tableName) { | ||
|
||
SelectQuery selectQuery = new SelectQuery().addCustomFromTable(tableName).addAllColumns(); | ||
|
||
String query = selectQuery.validate().toString(); | ||
|
||
return query; | ||
} | ||
|
||
public static String displayRecords(String tableName, String[] columns) { | ||
|
||
SelectQuery selectQuery = new SelectQuery().addCustomFromTable(tableName); | ||
|
||
for (String column : columns) { | ||
selectQuery.addCustomColumns(column); | ||
} | ||
|
||
String query = selectQuery.validate().toString(); | ||
|
||
return query; | ||
} | ||
|
||
public static String displayRecords(String tableName, String[] columns, String orderColumn, boolean ascending) { | ||
|
||
SelectQuery selectQuery = new SelectQuery().addCustomFromTable(tableName); | ||
|
||
for (String column : columns) { | ||
selectQuery.addCustomColumns(column); | ||
} | ||
|
||
selectQuery.addCustomOrdering(orderColumn, ascending ? Dir.ASCENDING : Dir.DESCENDING); | ||
|
||
String query = selectQuery.validate().toString(); | ||
|
||
return query; | ||
} | ||
|
||
|
||
} |
This file contains 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