Sqlite.hx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package sys.db;
  2. class Sqlite
  3. {
  4. static var type:Class<cs.system.data.IDbConnection>;
  5. /**
  6. Opens a new SQLite connection on the specified path.
  7. Note that you will need a SQLite ADO.NET Provider (see http://www.mono-project.com/docs/database-access/providers/sqlite/).
  8. Also note that this will try to open an assembly named `Mono.Data.Sqlite` if it wasn't loaded yet.
  9. **/
  10. public static function open(file:String):sys.db.Connection
  11. {
  12. var cnxString = 'Data Source=$file';
  13. if (type == null)
  14. {
  15. var t = null;
  16. var assemblies = cs.system.AppDomain.CurrentDomain.GetAssemblies();
  17. for (i in 0...assemblies.Length)
  18. {
  19. var a = assemblies[i];
  20. t = a.GetType('Mono.Data.Sqlite.SqliteConnection');
  21. if (t == null)
  22. t = a.GetType('System.Data.SQLite.SQLiteConnection');
  23. if (t != null)
  24. {
  25. break;
  26. }
  27. }
  28. if (t == null)
  29. {
  30. var asm = cs.system.reflection.Assembly.Load('Mono.Data.Sqlite');
  31. t = asm.GetType('Mono.Data.Sqlite.SqliteConnection');
  32. }
  33. if (t != null)
  34. type = cast cs.Lib.fromNativeType(t);
  35. }
  36. if (type == null)
  37. {
  38. throw "No ADO.NET SQLite provider was found!";
  39. }
  40. var ret = Type.createInstance(type,[cnxString]);
  41. ret.Open();
  42. return cs.db.AdoNet.create(ret,'SQLite');
  43. }
  44. }