Browse Source

added 'exists' (fixed issue #1038)

Nicolas Cannasse 13 years ago
parent
commit
ed40309952
1 changed files with 13 additions and 2 deletions
  1. 13 2
      std/sys/db/TableCreate.hx

+ 13 - 2
std/sys/db/TableCreate.hx

@@ -74,7 +74,7 @@ class TableCreate {
 			throw "SQL Connection not initialized on Manager";
 		var dbName = cnx.dbName();
 		var infos = manager.dbInfos();
-		var sql = "CREATE TABLE "+quote(infos.name)+ " (";
+		var sql = "CREATE TABLE " + quote(infos.name) + " (";
 		var decls = [];
 		var hasID = false;
 		for( f in infos.fields ) {
@@ -84,7 +84,7 @@ class TableCreate {
 			case DUId, DBigId:
 				hasID = true;
 				if( dbName == "SQLite" )
-					throw "S"+Std.string(f.t).substr(1)+" is not supported by "+dbName+" : use SId instead";
+					throw "S" + Std.string(f.t).substr(1)+" is not supported by " + dbName + " : use SId instead";
 			default:
 			}
 			decls.push(quote(f.name)+" "+getTypeSQL(f.t,dbName)+(f.isNull ? "" : " NOT NULL"));
@@ -98,4 +98,15 @@ class TableCreate {
 		cnx.request(sql);
 	}
 
+	public static function exists( manager : sys.db.Manager<Dynamic> ) : Bool {
+		var cnx : Connection = untyped manager.getCnx();
+		if( cnx == null )
+			throw "SQL Connection not initialized on Manager";
+		try {
+			cnx.request("SELECT * FROM `"+manager.dbInfos().name+"` LIMIT 1");
+			return true;
+		} catch( e : Dynamic ) {
+			return false;
+		}
+	}
 }