aboutsummaryrefslogtreecommitdiff
path: root/src/monkey/seaspider/org/gnunet/seaspider/ExpressionDatabaseHandler.java
blob: 5886fc90b10807ea7995b6a68a7a7a2d09816933 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package org.gnunet.seaspider;

import java.io.File;

import org.tmatesoft.sqljet.core.SqlJetException;
import org.tmatesoft.sqljet.core.SqlJetTransactionMode;
import org.tmatesoft.sqljet.core.table.ISqlJetTable;
import org.tmatesoft.sqljet.core.table.SqlJetDb;

public class ExpressionDatabaseHandler {
	
	private static SqlJetDb db = null;
	
	public static void createExpressionDatabase(String databasePath) {
		String createTableQuery = "CREATE TABLE Expression ( expr_ID INT NOT NULL PRIMARY KEY , " +
		"file_name TEXT NOT NULL , expr_syntax TEXT NOT NULL ," +
		" start_lineno INT, end_lineno INT)";
		
		File dbFile = new File(databasePath + "/GNUnetExpressions.db");
		dbFile.delete();/* Delete it if already existent */        
		
		/* Create Expressions database */
		try {
			db = SqlJetDb.open(dbFile, true);
			db.getOptions().setAutovacuum(true);
			db.beginTransaction(SqlJetTransactionMode.WRITE);
			try {
				db.getOptions().setUserVersion(1);/* Sets the user's cookie */
			} finally {
				db.commit();
			}
			/* Create table Expression */
			db.createTable(createTableQuery);
		}
		catch (SqlJetException e) {
			e.printStackTrace();
		}
	}
	
	
	public static void closeDatabase()
	{
		try {
			db.close();
		} catch (SqlJetException e) {
			e.printStackTrace();
		}
	}
	
	
	public static void insertIntoExpressionTable(String fileName, String expressionSyntax, 
												int startLineNo, int endLineNo)
	{
		if (expressionSyntax.matches("[0-9]*"))
			return;
		if (expressionSyntax.startsWith("\""))
			return;
		if (false)
			System.out.println (fileName  + ":[" + startLineNo + "-" + endLineNo + "]: " + expressionSyntax);
		if (true)
			return;
		if (db == null) {
			System.out.println("Error:Database handle is not initialized. Program will exit now!");
			System.exit(1);
		}
		
		ISqlJetTable table;
		try {
			table = db.getTable("Expression");
			db.beginTransaction(SqlJetTransactionMode.WRITE);
			try {
				table.insert(fileName, expressionSyntax, startLineNo, endLineNo);
			} finally {
				db.commit();
			}
		}
		catch (SqlJetException e) {
			e.printStackTrace();
		}
	}
}