File: Sqlite.hx

package info (click to toggle)
haxe 1%3A3.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 23,464 kB
  • ctags: 9,612
  • sloc: ml: 83,200; ansic: 1,724; makefile: 473; java: 349; cs: 314; python: 250; sh: 43; cpp: 39; xml: 25
file content (48 lines) | stat: -rw-r--r-- 1,257 bytes parent folder | download
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
package sys.db;

class Sqlite
{
	static var type:Class<cs.system.data.IDbConnection>;
	/**
		Opens a new SQLite connection on the specified path.
		Note that you will need a SQLite ADO.NET Provider (see http://www.mono-project.com/docs/database-access/providers/sqlite/).
		Also note that this will try to open an assembly named `Mono.Data.Sqlite` if it wasn't loaded yet.
	**/
	public static function open(file:String):sys.db.Connection
	{
		var cnxString = 'Data Source=$file';
		if (type == null)
		{
			var t = null;
			var assemblies = cs.system.AppDomain.CurrentDomain.GetAssemblies();
			for (i in 0...assemblies.Length)
			{
				var a = assemblies[i];
				t = a.GetType('Mono.Data.Sqlite.SqliteConnection');
				if (t == null)
					t = a.GetType('System.Data.SQLite.SQLiteConnection');
				if (t != null)
				{
					break;
				}
			}

			if (t == null)
			{
				var asm = cs.system.reflection.Assembly.Load('Mono.Data.Sqlite');
				t = asm.GetType('Mono.Data.Sqlite.SqliteConnection');
			}

			if (t != null)
				type = cast cs.Lib.fromNativeType(t);
		}

		if (type == null)
		{
			throw "No ADO.NET SQLite provider was found!";
		}
		var ret = Type.createInstance(type,[cnxString]);
		ret.Open();
		return cs.db.AdoNet.create(ret,'SQLite');
	}
}