From 20a278b6140f4ab9701311c95e9067249a309478 Mon Sep 17 00:00:00 2001 From: Bubar Date: Wed, 10 Jun 2015 10:54:48 +0200 Subject: [PATCH] PHP compatibility --- sys/db/Admin.hx | 100 ++++++++++++++++++++++++++++++++----------- sys/db/AdminStyle.hx | 12 +++++- 2 files changed, 85 insertions(+), 27 deletions(-) diff --git a/sys/db/Admin.hx b/sys/db/Admin.hx index 8f6841d..7141f60 100644 --- a/sys/db/Admin.hx +++ b/sys/db/Admin.hx @@ -30,6 +30,13 @@ import sys.db.Types; import sys.db.Custom; import sys.db.TableInfos.TableType; import sys.db.TableInfos.ManagerAccess; +#if neko +import neko.Lib; +import neko.Web; +#elseif php +import php.Lib; +import php.Web; +#end class Admin { @@ -75,10 +82,10 @@ class Admin { } } - function getTables() { + function getTables():Array { var tables = new Array(); - var classes = neko.Lib.getClasses(); - crawl(tables,classes); + var classes = Lib.getClasses(); + crawl(tables, classes); tables.sort(function(t1,t2) { return if( t1.name > t2.name ) 1 else if( t1.name < t2.name ) -1 else 0; }); return tables; } @@ -90,20 +97,26 @@ class Admin { return false; } + /** + * Filter classes who sublass sys.db.Object and get TableInfos + */ function crawl(tables : Array,classes : Dynamic) { for( cname in Reflect.fields(classes) ) { var v : Dynamic = Reflect.field(classes,cname); var c = cname.charAt(0); if( c >= "a" && c <= "z" ) { + //explore sub packages crawl(tables,v); continue; } + #if !php if( haxe.rtti.Meta.getType(v).rtti == null ) continue; + #end var s = Type.getSuperClass(v); while( s != null ) { - if( s == Object ) { + if( s == sys.db.Object ) { tables.push(new TableInfos(Type.getClassName(v))); break; } @@ -119,8 +132,10 @@ class Admin { var sync = false; var allTables = new List(); var rq = execute(TableInfos.allTablesRequest()); - for( r in rq ) + for( r in rq ){ + if (rq.getResult(0) == null) continue; allTables.add(rq.getResult(0)); + } var windows = Sys.systemName() == "Windows"; for( t in getTables() ) { var rights = getRights(createInstance(t)); @@ -155,10 +170,8 @@ class Admin { if( !allTables.isEmpty() ) { style.beginList(); for( t in allTables ) { - if( t == "ForumSearch" ) - continue; style.beginItem(); - style.text("Table "+t+" does not have any matching object"); + style.text('Table "$t" does not have any matching object'); style.endItem(); } style.endList(); @@ -215,9 +228,14 @@ class Admin { defval = Id.decode(defval); case DSerialized: defval = new Serialized(defval).escape(); + case DNekoSerialized: - var v = try haxe.Serializer.run(neko.Lib.localUnserialize(defval)) catch( e : Dynamic ) ("ERROR : " + Std.string(e)); + #if neko + var v = try haxe.Serializer.run(Lib.localUnserialize(defval)) catch( e : Dynamic ) ("ERROR : " + Std.string(e)); defval = new Serialized(v).escape(); + #else + throw "DNekoSerialized is only available on neko target"; + #end case DData: #if haxe3 var str = try haxe.Serializer.run((untyped table.manager).doUnserialize(f.name, defval)) catch( e : Dynamic ) ("ERROR : " + Std.string(e)); @@ -238,6 +256,9 @@ class Admin { style.inputField(f.name,f.type,table.nulls.exists(f.name),defval); } + /** + * Prints an insert form + */ function insert(table : TableInfos, ?params : Map, ?error : String, ?errorMsg : String ) { var binary = false; for( f in table.fields ) @@ -369,9 +390,14 @@ class Admin { case DSerialized: return new Serialized(v).encode(); case DNekoSerialized: + #if neko var str = new Serialized(v).encode(); var val = neko.Lib.serialize(haxe.Unserializer.run(str)); return val; + #else + throw "DNekoSerialized is only available on neko target"; + return null; + #end case DData: var s = new Serialized(v).encode(); if( s.length > 0xFFFFFF ) @@ -393,10 +419,12 @@ class Admin { } } - function createInstance( table : TableInfos ) : Object { - var c = Type.createEmptyInstance(table.cl); + function createInstance( table : TableInfos ) : Object { #if php - untyped c.__init_object(); + var c = Type.createInstance(table.cl,[]); + //untyped c.__init_object(); + #else + var c = Type.createEmptyInstance(table.cl); #end return c; } @@ -420,6 +448,9 @@ class Admin { return untyped t.dbSearch(); } + /** + * insert a new object in a table + */ function doInsert( table : TableInfos, params : Map ) { var inst = createInstance(table); updateParams(table,params); @@ -450,12 +481,16 @@ class Admin { try { if( !getRights(inst).can.insert ) throw "Can't insert"; + if(inst == null) + throw "instance is null"; inst.insert(); log("Inserted "+table.name+" "+table.identifier(inst)); } catch( e : Dynamic ) { insert(table,params,null,Std.string(e)); return; } + + //display a blank form to insert a new object if( params.exists("__new") ) { insert(table,params); return; @@ -585,8 +620,11 @@ class Admin { style.goto(table.className+"/edit/"+table.identifier(inst)); } + /** + * Sync some of the object fields from the web request + */ function updateParams( table : TableInfos, params : Map ) { - var tmp = neko.Web.getMultipart(maxUploadSize); + var tmp = Web.getMultipart(maxUploadSize); for( k in tmp.keys() ) params.set(k,tmp.get(k)); for( r in table.relations ) { @@ -624,6 +662,9 @@ class Admin { } } + /** + * Deletes a record + */ function doDelete( table : TableInfos, id : String ) { var inst = table.fromIdentifier(id); if( inst == null ) { @@ -652,9 +693,9 @@ class Admin { edit(table,id,null,null,"Can't Download data"); return; } - neko.Web.setHeader("Content-Type","text/binary"); - neko.Web.setHeader("Content-Length",Std.string(data.length)); - neko.Lib.print(data); + Web.setHeader("Content-Type","text/binary"); + Web.setHeader("Content-Length",Std.string(data.length)); + Sys.print(data); } function search( table : TableInfos, params : Map ) { @@ -780,7 +821,7 @@ class Admin { if( has(rights.invisible,f.name) ) continue; var data = Reflect.field(r,f.name); - var str = try Std.string(data) catch( e : Dynamic ) { if(!Std.is(data,Date)) neko.Lib.rethrow(e); "#INVALID"; }; + var str = try Std.string(data) catch( e : Dynamic ) { if(!Std.is(data,Date)) Lib.rethrow(e); "#INVALID"; }; if( str.length >= 20 ) str = str.substr(0,17) + "..."; style.nextRow(false); @@ -899,11 +940,15 @@ class Admin { return i.unique+"@"+i.keys.join("@"); } + /** + * check for differences between classes and tables in DB + */ function needSync( t : TableInfos ) { var desc = execute(t.descriptionRequest()).getResult(1); var inf = TableInfos.fromDescription(desc); var renames = new Map(); hasSyncAction = false; + // ADD/CHANGE FIELDS for( f in t.fields ) { var t2 = inf.fields.get(f.name); @@ -913,7 +958,7 @@ class Admin { if( !t.hfields.exists(n) && Type.enumEq(inf.fields.get(n),f.type) && inf.nulls.get(n) == t.nulls.get(f.name) ) { rename = true; renames.set(n,true); - syncAction(t,["rename",n,f.name],"Rename field "+n+" to "+f.name); + syncAction(t,["rename",n,f.name],'Rename field "$n" to "${f.name}"'); break; } syncAction(t,["add",f.name],"Add field "+f.name,!rename); @@ -1018,19 +1063,21 @@ class Admin { public function process( ?url : Array ) { if( url == null ) { - url = neko.Web.getURI().split("/"); + url = Web.getURI().split("/"); url.shift(); // empty : url starts with / url.shift(); // "admin" if( url[0] == "index.n" ) url.shift(); } if( url.length == 0 ) url.push(""); - var params = neko.Web.getParams(); + var params = Web.getParams(); switch( url[0] ) { case "": + style = new AdminStyle(null); index(); return; + case "doSync": style = new AdminStyle(null); doSync(params); @@ -1066,7 +1113,9 @@ class Admin { } static function log(msg:String) { - neko.Web.logMessage("[DBADM] "+neko.Web.getHostName()+" "+Date.now().toString()+" "+neko.Web.getClientIP()+" - "+msg); + #if neko + Web.logMessage("[DBADM] " + neko.Web.getHostName() + " " + Date.now().toString() + " " + neko.Web.getClientIP() + " - " + msg); + #end } public static function handler() { @@ -1077,10 +1126,11 @@ class Admin { // rollback in case of multiple delete/update - no effect on DB struct changes // since they are done outside of transaction Manager.cnx.rollback(); - neko.Lib.print("
");
-			neko.Lib.print(Std.string(e));
-			neko.Lib.print(haxe.CallStack.toString(haxe.CallStack.exceptionStack()));
-			neko.Lib.print("
"); + + Sys.print("

Error :

"+Std.string(e)); + Sys.print("

Stack :

");
+			Sys.print(haxe.CallStack.toString(haxe.CallStack.exceptionStack()));
+			Sys.print("
"); } } diff --git a/sys/db/AdminStyle.hx b/sys/db/AdminStyle.hx index dc17b96..6f37f25 100644 --- a/sys/db/AdminStyle.hx +++ b/sys/db/AdminStyle.hx @@ -27,6 +27,14 @@ import haxe.macro.Context; import sys.db.TableInfos.TableType; #end +#if neko +import neko.Lib; +import neko.Web; +#elseif php +import php.Lib; +import php.Web; +#end + class MacroHelper { public macro static function importFile( file : String ) { @@ -64,7 +72,7 @@ class AdminStyle { for( x in Reflect.fields(params) ) str = str.split("@"+x).join(Reflect.field(params,x)); } - neko.Lib.println(str); + Sys.println(str); } public function text(str,?title) { @@ -122,7 +130,7 @@ class AdminStyle { } public function goto(url) { - neko.Web.redirect(BASE_URL+url); + Web.redirect(BASE_URL+url); } public function link( url, name ) {