summaryrefslogtreecommitdiffstats
path: root/javascripts/libs/browser_engines/sqlite_driver.js
blob: 1aa65860e51070899ccaae30eefc072382d682a7 (plain)
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
define(function () {
	var SQLite_driver = function () {
		return this;
	}
	
	SQLite_driver.prototype.getSchemaStructure = function (args) {
		/*
			We are going to get the schema structure by running a special query to get back the table 
			definitions.  We'll use that query to find the column names and types by parsing out the columns
			from the create table statements.
		*/
		// this query gets back a result set which contains each table, along with the create table statement used
		var schema_sql = "select * from sqlite_master where type IN ('table', 'view') and name != '__WebKitDatabaseInfoTable__' order by type,name";
		
		var localCallback = function (resultSets) {
			var colMap = {};
			var schemaStruct = [];
			
			for (var i in resultSets[0]["RESULTS"]["COLUMNS"])
			{
				colMap[resultSets[0]["RESULTS"]["COLUMNS"][i]] = i;
			}
			
			for (var j in resultSets[0]["RESULTS"]["DATA"])
			{
				var tableDef = resultSets[0]["RESULTS"]["DATA"][j][colMap["sql"]];
				var tableName = resultSets[0]["RESULTS"]["DATA"][j][colMap["name"]];
				var tableType = resultSets[0]["RESULTS"]["DATA"][j][colMap["type"]];

				var tableStruct = {
					"table_name": tableName,
					"table_type": tableType,
					"columns": []
				};

				if (tableType == "table") { // sadly, there is no easy way to get the columns back from views in WebSQL
					var colsDef = /^[\s\S]*?\(([\s\S]*)\)$/.exec(tableDef)[1].split(/,\s*/);
					
					
					for (var k in colsDef) {
						var col_components = colsDef[k].replace(/(^\s*)|(\s*$)/, '').split(/\s+/);
						tableStruct["columns"].push({
							"name": col_components.shift(), // first part of the col def
							"type": col_components.join(' ') // rest of the col def
						})
					}
				}
				schemaStruct.push(tableStruct);
			}
			
			args["callback"](schemaStruct);
			
		}
		
		
		// use the method available in child classes to get the results from our special query
		this.executeQuery({sql: schema_sql, "success": localCallback});
		
	}

	SQLite_driver.prototype.splitStatement = function (statements, separator)
	{
		if (! separator) separator = ";";
		var escaped_separator = separator.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
		
		var sArray = (statements ? statements.split(new RegExp(escaped_separator + "\s*\r?(\n|$)")) : []);
		return sArray; 
	}
	
	return SQLite_driver;
});