Builder/Provider/Database/generate.js
[app.Builder.js] / Builder / Provider / Database / generate.js
index eb187f6..c95ecc1 100644 (file)
@@ -12,6 +12,7 @@
  * 
  * write readers..
  * 
+ * usage: seed generate.js  '{"DB_NAME":"XXX","USERNAME":"YYY","PASSWORD":"ZZZ","INI":"/path/to/mydb.ini"}'
  * 
  */
 Gda  = imports.gi.Gda;
@@ -27,6 +28,7 @@ var prov = Gda.Config.list_providers ();
 //print(prov.dump_as_string());
 var args = Array.prototype.slice.call(Seed.argv);
 args.shift();args.shift();// remove first 2
+print(args.length );
 if (args.length < 1) {
     var sample = {
         DB_NAME : "XXX",
@@ -55,21 +57,26 @@ Gda.DataSelect.prototype.fetchAll = function()
     for (var i =0;i < this.get_n_columns(); i++) {
         cols.push(this.get_column_name(i));
     }
-    //console.dump(cols);
+    //print(JSON.stringify(cols, null,4));
     var iter = this.create_iter();
     var res = [];
-    while (iter.move_next()) {
+    //print(this.get_n_rows());
+    var _this = this;
+    for (var r = 0; r < this.get_n_rows(); r++) {
+        
+        // single clo..
+        //print("GOT ROW");
         if (cols.length == 1) {
-            res.push(iter.get_value_at(0).get_string());
+            res.push(this.get_value_at(0,r).get_string());
             continue;
         }
         var add = { };
         
         cols.forEach(function(n,i) {
-           var val = iter.get_value_at(i);
-           var type = GObject.type_name(val.g_type) ;
-           var vs = type == 'GdaBlob' ? val.value.to_string(1024) : val.value;
-         //  print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
+            var val = _this.get_value_at(i,r);
+            var type = GObject.type_name(val.g_type) ;
+            var vs = ['GdaBinary', 'GdaBlob' ].indexOf(type) > -1 ? val.value.to_string(1024) : val.value;
+            //print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
             //print (n + '=' + iter.get_value_at(i).value);
             add[n] = vs;
         });
@@ -95,7 +102,8 @@ var map = {
     'longtext' : 'string',
     'mediumtext' : 'string',
     'enum' : 'string',
-    
+    'timestamp' : 'number',
+    'blob' => 'text'
     
 }
 
@@ -103,18 +111,18 @@ var ini = { }
 
 function readIni(fn)
 {
-    var key_file = GLib.key_file_new();
-    if (!GLib.key_file_load_from_file (key_file, fn , GLib.KeyFileFlags.NONE )) {
+    var key_file = new GLib.KeyFile.c_new();
+    if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
         return;
     }
    
-    var groups = GLib.key_file_get_groups(key_file);
+    var groups = key_file.get_groups();
     groups.forEach(function(g) {
         ini[g] = {}
            
-        var keys = GLib.key_file_get_keys(key_file,g);
+        var keys = key_file.get_keys(g);
         keys.forEach(function(k) {
-            ini[g][k] = GLib.key_file_get_value(key_file,g,k);
+            ini[g][k] = key_file.get_value(g,k);
         })
     })
     
@@ -132,16 +140,21 @@ if (File.isDirectory(cfg.INI)) {
 
     //--- load ini files..
     // this is very specific.
-    var dirs = File.list( GLib.get_home_dir() + '/gitlive').filter( 
-        function(e) { return e.match(/^Pman/); }
+    
+    var dirs = File.list( cfg.INI + '/Pman').filter( 
+        function(e) { 
+            if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
+                return false;
+            }
+            return true;
+        }
     );
     
+     
     dirs.forEach(function(d) {
         // this currently misses the web.*/Pman/XXXX/DataObjects..
-        var path = GLib.get_home_dir() + '/gitlive/' + d + '/DataObjects';
-        if (!File.isDirectory(path)) {
-            path = GLib.get_home_dir() + '/gitlive/' + d + '/Pman/DataObjects';
-        }
+        var path = cfg.INI + '/Pman/' + d + '/DataObjects';
+         
         if (!File.isDirectory(path)) {
             return; //skip
         }
@@ -156,10 +169,19 @@ if (File.isDirectory(cfg.INI)) {
             readIni(path + '/' + i); 
             
         })
-
-        
-        
     });
+    // look at web.XXXX/Pman/XXX/DataObjects/*.ini
+    var inis = File.list(cfg.INI).filter(
+        function(e) { return e.match(/\.links\.ini$/); }
+    )
+    
+     inis.forEach(function(i) {
+        readIni(path + '/' + i); 
+        
+    })
+    
+    
 }
 print(JSON.stringify(ini, null,4));
  //console.dump(ini);
@@ -180,12 +202,13 @@ tables.forEach(function(table) {
     var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
     var reader = []; 
     var colmodel = []; 
-    var combofields= []; 
+    var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
+         
     var form = {}
        
     var firstTxtCol = '';
     
-    
+    print(JSON.stringify(schema, null,4));
     
     schema.forEach(function(e)  {
         var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
@@ -218,6 +241,10 @@ tables.forEach(function(table) {
         }
         reader.push(row);
         
+        if (combofields.length == 1 && row.type == 'string') {
+            combofields.push(row);
+        }
+        
         
         var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
         title  = title[0].toUpperCase() + title.substring(1);
@@ -243,7 +270,9 @@ tables.forEach(function(table) {
         if (e.Type == 'text') {
             xtype = 'TextArea';
         }
-        
+        if (e.name == 'id') {
+            xtype = 'Hidden';
+        }
         // what about booleans.. -> checkboxes..
         
         
@@ -275,13 +304,18 @@ tables.forEach(function(table) {
         pageSize : 20,
         qtip: 'Select ' + table,
         selectOnFocus: true,
-        tirggerAction : all,
+        triggerAction : 'all',
         typeAhead: true,
-        valueField : id,
+        
         width: 300,
-        tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // 
+        
+        
+        
+        tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
         queryParam : '',// SET WHEN USED
         fieldLabel : table,  // SET WHEN USED
+        valueField : 'id',
+        displayField : '', // SET WHEN USED eg. project_id_name
         hiddenName : '', // SET WHEN USED eg. project_id
         name : '', // SET WHEN USED eg. project_id_name
         items : [
@@ -327,6 +361,8 @@ tables.forEach(function(table) {
     //print(JSON.stringify(reader,null,4));
     readers.push({
         table : table ,
+        combo : combo,
+        combofields : combofields,
         reader :  reader,
         oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
         colmodel : colmodel,
@@ -354,13 +390,8 @@ readers.forEach(function(reader) {
         var add = readers.filter(function(r) { return r.table == kv[0] })[0];
         
         // merge in data (eg. project_id => project_id_*****
-        var fc = false;
+     
         add.oreader.forEach(function(or) {
-            if (or.type == 'string' && !fc) {
-                fc = or.name
-            }
-            
-            
             reader.reader.push({
                 name : col + '_' + or.name,
                 type : or.type
@@ -368,18 +399,24 @@ readers.forEach(function(reader) {
         });
         
         // col is mapped to something..
-        if (fc) {
-            
-            
-            reader.form[col] = {
-                    // a combo!!!
-            }
-            
-            
+        var combofields = add.combofields;
+        if (add.combofields.length < 2) {
+            continue;
+        }
+        if (typeof(reader.form[col]) == 'undefined') {
+            print("missing linked column " + col);
+            continue;
         }
         
-        
-        
+        var combofields_name = add.combofields[1].name;
+        var old =   reader.form[col];
+        reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
+        reader.form[col].queryParam  = 'query[' + combofields_name + ']';// SET WHEN USED
+        reader.form[col].fieldLabel = old.fieldLabel;  // SET WHEN USED
+        reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
+        reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
+        reader.form[col].name  = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
+        reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
         
              
     };
@@ -416,7 +453,7 @@ readers.forEach(function(reader) {
         root : "data",
         '*prop' : "reader",
         id : 'id', // maybe no..
-        '|fields' :  JSON.stringify(reader.reader, null,4)
+        '|fields' :  JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
     };
     
     File.write(
@@ -567,13 +604,14 @@ readers.forEach(function(reader) {
     if (!File.isDirectory(dir)) {
         File.mkdir(dir);
     }
-    formElements = [];
+    var formElements = [];
     for (var k in reader.form) {
         if (k == 'id') { // should really do primary key testing..
             continue;
-           }
+        }
         formElements.push(reader.form[k]);
     }
+    formElements.push(reader.form['id']);
 
     print("WRITE: " +  dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
     
@@ -591,7 +629,7 @@ readers.forEach(function(reader) {
                     "       //_this.dialog.el.mask(\"Loading\");\n"+
                     "       //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
                     "       return;\n"+
-                    "    }
+                    "    }\n"+
                     "    if (action.type == 'load') {\n"+
                     "        _this.dialog.el.unmask();\n"+
                     "        return;\n"+
@@ -605,24 +643,51 @@ readers.forEach(function(reader) {
                     "            _this.callback.call(_this, _this.form.getValues());\n"+
                     "         }\n"+
                     "         _this.form.reset();\n"+
-                    "         return;"\n"+
+                    "         return;\n"+
                     "    }\n"+
                     "}\n",
                 
                 "|rendered" : "function (form)\n"+
                     "{\n"+
-                    "    _this.form= form;
+                    "    _this.form= form;\n"+
                     "}\n"
-            }
+            },
             method : "POST",
             style : "margin:10px;",
             "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
             items : formElements
-        })
+        }, null, 4)
     );
             
             
-                   
+   
+   
+   
+     /// COMBO..
+    
+    dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox'; 
+    if (!File.isDirectory(dir)) {
+        File.mkdir(dir);
+    }
+   
+    print("WRITE: " +  dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
+    
+    File.write(
+        dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
+            
+       
+        JSON.stringify(reader.combo, null, 4)
+    );
+            
+   
+   
+   
+   
+   
+   
+   
+   
+   
 });