Handle to deal with Expression Database
[oweals/gnunet.git] / src / monkey / seaspider / org / gnunet / seaspider / ExpressionDatabaseHandler.java
1 package org.gnunet.seaspider.parser;
2
3 import java.io.File;
4
5 import org.tmatesoft.sqljet.core.SqlJetException;
6 import org.tmatesoft.sqljet.core.SqlJetTransactionMode;
7 import org.tmatesoft.sqljet.core.table.ISqlJetTable;
8 import org.tmatesoft.sqljet.core.table.SqlJetDb;
9
10 public class ExpressionDatabaseHandler {
11         
12         private static SqlJetDb db = null;
13         
14         public static void createExpressionDatabase(String databasePath) {
15                 String createTableQuery = "CREATE TABLE Expression ( expr_ID INT NOT NULL PRIMARY KEY , " +
16                 "file_name TEXT NOT NULL , expr_syntax TEXT NOT NULL ," +
17                 " start_lineno INT NOT NULL , end_lineno INT NOT NULL , " +
18                 "scope_start_lineno INT NOT NULL , scope_end_lineno INT NOT NULL)";
19                 
20                 File dbFile = new File(databasePath + "/GNUnetExpressions.db");
21                 dbFile.delete();/* Delete it if already existent */        
22                 
23                 /* Create Expressions database */
24                 try {
25                         db = SqlJetDb.open(dbFile, true);
26                         db.getOptions().setAutovacuum(true);
27                         db.beginTransaction(SqlJetTransactionMode.WRITE);
28                         try {
29                                 db.getOptions().setUserVersion(1);/* Sets the user's cookie */
30                         } finally {
31                                 db.commit();
32                         }
33                         /* Create table Expression */
34                         db.createTable(createTableQuery);
35                 }
36                 catch (SqlJetException e) {
37                         e.printStackTrace();
38                 }
39         }
40         
41         
42         public static void closeDatabase()
43         {
44                 try {
45                         db.close();
46                 } catch (SqlJetException e) {
47                         e.printStackTrace();
48                 }
49         }
50         
51         
52         public static void insertIntoExpressionTable(String fileName, String expressionSyntax, 
53                                                                                                 int startLineNo, int endLineNo, int scopeStartLineNo,
54                                                                                                 int scopeEndLineNo)
55         {
56                 if (db == null) {
57                         System.out.println("Error:Database handle is not initialized. Program will exit now!");
58                         System.exit(1);
59                 }
60                 
61                 ISqlJetTable table;
62                 try {
63                         table = db.getTable("Expression");
64                         db.beginTransaction(SqlJetTransactionMode.WRITE);
65                         try {
66                                 table.insert(fileName, expressionSyntax, startLineNo, endLineNo, scopeStartLineNo, scopeEndLineNo);
67                         } finally {
68                                 db.commit();
69                         }
70                 }
71                 catch (SqlJetException e) {
72                         e.printStackTrace();
73                 }
74         }
75 }