Skip to content

Commit 8f0fa6c

Browse files
committed
- Added new setSchemaName() method to the Model class
- Minor update to the dir() method in the Directory class git-svn-id: svn://192.168.0.80/JavaXT/javaxt-core@1277 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent af475bb commit 8f0fa6c

File tree

2 files changed

+96
-17
lines changed

2 files changed

+96
-17
lines changed

src/javaxt/io/Directory.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,11 +1526,15 @@ private String[] dir(){
15261526

15271527
java.util.ArrayList<String> files = new java.util.ArrayList<String>();
15281528

1529+
String dir = getPath();
1530+
//dir = this.toFile().getCanonicalPath() + PathSeparator;
1531+
1532+
15291533
//Try listing files using the JNI
15301534
boolean doDir = false;
15311535
if (File.loadDLL()){
15321536
try{
1533-
String list = File.GetFiles(getPath() + "*");
1537+
String list = File.GetFiles(dir + "*");
15341538
if (list!=null){
15351539
for (String name : list.split("\n")){
15361540
name = name.trim();
@@ -1551,11 +1555,9 @@ private String[] dir(){
15511555
//If we're still here, list files using a command prompt
15521556
if (doDir)
15531557
try{
1554-
String path = this.getPath();
1555-
if (path.contains(" ")) path = "\"" + path + "\"";
15561558

15571559
//Execute a windows shell command to get a directory listing
1558-
javaxt.io.Shell cmd = new javaxt.io.Shell("cmd.exe /c dir /OG " + path);
1560+
javaxt.io.Shell cmd = new javaxt.io.Shell("cmd.exe /c dir /OG " + (dir.contains(" ") ? "\"" + dir + "\"" : dir));
15591561
java.util.List<String> output = cmd.getOutput();
15601562
cmd.run();
15611563

@@ -1647,7 +1649,7 @@ else if (isSymLink || isJunction){
16471649
file = new java.io.File(link);
16481650
}
16491651
else {
1650-
file = new java.io.File(path, name);
1652+
file = new java.io.File(dir, name);
16511653
}
16521654
if (file.isDirectory()) name += this.PathSeparator;
16531655
files.add(name);

src/javaxt/sql/Model.java

Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public abstract class Model {
1717

1818
protected Long id;
19-
protected final String tableName;
19+
private String escapedTableName;
2020
private final String modelName;
2121
private final HashMap<String, String> fieldMap;
2222
private String[] keywords;
@@ -36,6 +36,9 @@ public abstract class Model {
3636
private static ConcurrentHashMap<String, Field[]>
3737
fields = new ConcurrentHashMap<String, Field[]>();
3838

39+
private static ConcurrentHashMap<String, String[]>
40+
tables = new ConcurrentHashMap<String, String[]>();
41+
3942

4043
//**************************************************************************
4144
//** Constructor
@@ -60,11 +63,31 @@ protected Model(String tableName){
6063
* You do not need to include the "id" field.
6164
*/
6265
protected Model(String tableName, HashMap<String, String> fieldMap){
66+
67+
//Set modelName
68+
Class c = this.getClass();
69+
String className = c.getName();
70+
modelName = c.getSimpleName();
71+
72+
73+
//Set keywords
6374
synchronized(reservedKeywords){
64-
keywords = reservedKeywords.get(this.getClass().getName());
75+
keywords = reservedKeywords.get(className);
76+
}
77+
78+
79+
//Set tableInfo
80+
synchronized(tables){
81+
String[] tableInfo = tables.get(className);
82+
if (tableInfo==null){
83+
tableInfo = getTableInfo(tableName);
84+
tables.put(className, tableInfo);
85+
}
86+
escapedTableName = tableInfo[0];
6587
}
66-
this.tableName = escape(tableName);
67-
this.modelName = this.getClass().getSimpleName();
88+
89+
90+
//Set fieldMap
6891
this.fieldMap = fieldMap;
6992
}
7093

@@ -145,7 +168,7 @@ protected final void init(long id, String...fieldNames) throws SQLException {
145168
}
146169
if (addID) sql.append(", id");
147170
sql.append(" from ");
148-
sql.append(tableName);
171+
sql.append(escapedTableName);
149172
sql.append(" where id=");
150173

151174

@@ -378,7 +401,7 @@ else if (packageName.startsWith("javaxt.json") ||
378401
Connection conn = getConnection(this.getClass());
379402

380403
StringBuilder sql = new StringBuilder();
381-
sql.append("INSERT INTO " + tableName + " (");
404+
sql.append("INSERT INTO " + escapedTableName + " (");
382405
it = fields.keySet().iterator();
383406
while (it.hasNext()){
384407
java.lang.reflect.Field f = it.next();
@@ -460,7 +483,7 @@ else if (packageName.startsWith("javaxt.geospatial.geometry") ||
460483
if (driver==null) driver = new Driver("","","");
461484

462485
Recordset rs = new Recordset();
463-
rs.open("select * from " + tableName + " where id=" + id, conn, false);
486+
rs.open("select * from " + escapedTableName + " where id=" + id, conn, false);
464487
if (rs.EOF){
465488
rs.addNew();
466489
rs.setValue("id", id);
@@ -508,7 +531,7 @@ public void delete() throws SQLException {
508531
Connection conn = null;
509532
try{
510533
conn = getConnection(this.getClass());
511-
conn.execute("delete from " + tableName + " where id=" + id);
534+
conn.execute("delete from " + escapedTableName + " where id=" + id);
512535
conn.close();
513536
}
514537
catch(SQLException e){
@@ -723,7 +746,7 @@ private static String getSQL(Class c, Object...args){
723746

724747
//Get tableName
725748
String tableName = null;
726-
try{ tableName = ((Model) c.newInstance()).tableName; }
749+
try{ tableName = ((Model) c.newInstance()).escapedTableName; }
727750
catch(Exception e){}
728751

729752
StringBuilder str = new StringBuilder("select ");
@@ -838,9 +861,9 @@ public static void init(Class c, ConnectionPool connectionPool) throws SQLExcept
838861

839862

840863
//Generate list of fields
841-
String tableName = ((Model) c.newInstance()).tableName;
864+
Model model = ((Model) c.newInstance());
842865
Recordset rs = new Recordset();
843-
rs.open("select * from " + tableName + " where id is null", conn);
866+
rs.open("select * from " + model.escapedTableName + " where id is null", conn);
844867
synchronized(fields){
845868
fields.put(className, rs.getFields());
846869
fields.notifyAll();
@@ -879,7 +902,61 @@ public static void init(javaxt.io.Jar jar, ConnectionPool connectionPool) throws
879902
/** Returns the name of the table backing a given Model
880903
*/
881904
public static String getTableName(Model model){
882-
return model.tableName;
905+
return model.escapedTableName;
906+
}
907+
908+
909+
//**************************************************************************
910+
//** setSchemaName
911+
//**************************************************************************
912+
/** Provides an option to override the default schema used by a model
913+
*/
914+
public static void setSchemaName(String schemaName, Class c){
915+
if (!javaxt.sql.Model.class.isAssignableFrom(c)) return;
916+
String className = c.getName();
917+
try{
918+
919+
//Instantiate model to initialize table info
920+
Model model = ((Model) c.newInstance());
921+
922+
923+
//Update table info
924+
synchronized(tables){
925+
String[] tableInfo = tables.get(className);
926+
String tableName = tableInfo[1];
927+
if (schemaName!=null) tableName = schemaName + "." + tableName;
928+
tableInfo = model.getTableInfo(tableName);
929+
tables.put(className, tableInfo);
930+
}
931+
}
932+
catch(Exception e){
933+
Exception ex = new Exception("Failed to update schema for Model: " + className);
934+
ex.setStackTrace(e.getStackTrace());
935+
throw new RuntimeException(ex);
936+
}
937+
}
938+
939+
940+
//**************************************************************************
941+
//** getTableInfo
942+
//**************************************************************************
943+
/** Used to parse a given string and extract table info (e.g. schema name,
944+
* table name, etc).
945+
*/
946+
private String[] getTableInfo(String tableName){
947+
String schemaName;
948+
String escapedTableName;
949+
int idx = tableName.indexOf(".");
950+
if (idx>-1){
951+
schemaName = tableName.substring(0, idx);
952+
tableName = tableName.substring(idx+1);
953+
escapedTableName = escape(schemaName) + "." + tableName;
954+
}
955+
else{
956+
schemaName = null;
957+
escapedTableName = escape(tableName);
958+
}
959+
return new String[]{escapedTableName, tableName, schemaName};
883960
}
884961

885962

0 commit comments

Comments
 (0)