1 34 package smallsql.database; 35 36 import java.io.*; 37 import java.sql.*; 38 39 40 43 abstract class TableView { 44 45 static final int MAGIC_TABLE = 'S' << 24 | 'Q' << 16 | 'L' << 8 | 'T'; 46 static final int MAGIC_VIEW = 'S' << 24 | 'Q' << 16 | 'L' << 8 | 'V'; 47 static final int TABLE_VIEW_VERSION = 2; 48 static final int TABLE_VIEW_OLD_VERSION = 1; 49 50 final String name; 51 final Columns columns; 52 53 57 private long timestamp = System.currentTimeMillis(); 58 59 static final int LOCK_NONE = 0; static final int LOCK_INSERT = 1; static final int LOCK_READ = 2; static final int LOCK_WRITE = 3; static final int LOCK_TAB = 4; 65 66 TableView(String name, Columns columns){ 67 this.name = name; 68 this.columns = columns; 69 } 70 71 74 static TableView load(SSConnection con, Database database, String name) throws SQLException{ 75 RandomAccessFile raFile = null; 76 try{ 77 String fileName = Utils.createTableViewFileName( database, name ); 78 File file = new File( fileName ); 79 if(!file.exists()) 80 throw Utils.createSQLException("Table or View '" + name + "' does not exist"); 81 raFile = new RandomAccessFile( file, "rw" ); 82 int magic = raFile.readInt(); 83 int version = raFile.readInt(); 84 switch(magic){ 85 case MAGIC_TABLE: 86 case MAGIC_VIEW: 87 break; 88 default: 89 throw Utils.createSQLException("File '" + fileName + "' is not a valid Table or View store."); 90 } 91 if(version > TABLE_VIEW_VERSION) 92 throw Utils.createSQLException("File version (" + version + ") of file '" + fileName + "' is to new for this runtume."); 93 if(version < TABLE_VIEW_OLD_VERSION) 94 throw Utils.createSQLException("File version (" + version + ") of file '" + fileName + "' is to old for this runtume."); 95 if(magic == MAGIC_TABLE) 96 return new Table( con, name, raFile, raFile.getFilePointer(), version); 97 return new View ( con, name, raFile, raFile.getFilePointer()); 98 }catch(Throwable e){ 99 if(raFile != null) 100 try{ 101 raFile.close(); 102 }catch(Exception e2){ 103 DriverManager.println(e2.toString()); 104 } 105 throw Utils.createSQLException(e); 106 } 107 } 108 109 110 File getFile(Database database, String name) throws Exception { 111 return new File( Utils.createTableViewFileName( database, name ) ); 112 } 113 114 115 RandomAccessFile createFile(Database database) throws Exception { 116 File file = getFile( database, name ); 117 boolean ok = file.createNewFile(); 118 if(!ok) throw Utils.createSQLException("Table or View '" + name + "' already exists."); 119 RandomAccessFile raFile = new RandomAccessFile( file, "rw" ); 120 writeMagic(raFile); 121 return raFile; 122 } 123 124 abstract void writeMagic(RandomAccessFile raFile) throws Exception ; 125 126 127 String getName(){ 128 return name; 129 } 130 131 132 long getTimestamp(){ 133 return timestamp; 134 } 135 136 137 140 final int findColumnIdx(String name){ 141 for(int i=0; i<columns.size(); i++){ 143 if( columns.get(i).getName().equalsIgnoreCase(name) ) return i; 144 } 145 return -1; 146 } 147 148 149 152 final Column findColumn(String name){ 153 for(int i=0; i<columns.size(); i++){ 154 Column column = columns.get(i); 155 if( column.getName().equalsIgnoreCase(name) ) return column; 156 } 157 return null; 158 } 159 160 161 162 165 void close() throws Exception {} 166 167 } 168 169 170 | Popular Tags |