1 //<script type="text/javascript">
5 * Let's see if libgda can be used to generate our Readers for roo...
7 * Concept - conect to database..
15 * usage: seed generate.js '{"DB_NAME":"XXX","USERNAME":"YYY","PASSWORD":"ZZZ","INI":"/path/to/mydb.ini"}'
19 GObject = imports.gi.GObject;
21 GLib = imports.gi.GLib;
23 console = imports['../../../console.js'];
24 File = imports['../../../File.js'].File;
27 var prov = Gda.Config.list_providers ();
28 //print(prov.dump_as_string());
29 var args = Array.prototype.slice.call(Seed.argv);
30 args.shift();args.shift();// remove first 2
32 if (args.length < 1) {
37 INI : "/path/to/mydb.ini",
39 print("Usage : seed generate.js '" + JSON.stringify(sample) +"'");
42 var cfg = JSON.parse(args[0]);
44 var cnc = Gda.Connection.open_from_string ("MySQL", "DB_NAME=" + cfg.DB_NAME,
45 "USERNAME=" + cfg.USERNAME + ';PASSWORD=' + cfg.PASSWORD,
46 Gda.ConnectionOptions.NONE, null);
53 Gda.DataSelect.prototype.fetchAll = function()
57 for (var i =0;i < this.get_n_columns(); i++) {
58 cols.push(this.get_column_name(i));
60 //print(JSON.stringify(cols, null,4));
61 var iter = this.create_iter();
63 //print(this.get_n_rows());
65 for (var r = 0; r < this.get_n_rows(); r++) {
69 if (cols.length == 1) {
70 res.push(this.get_value_at(0,r).get_string());
75 cols.forEach(function(n,i) {
76 var val = _this.get_value_at(i,r);
77 var type = GObject.type_name(val.g_type) ;
78 var vs = ['GdaBinary', 'GdaBlob' ].indexOf(type) > -1 ? val.value.to_string(1024) : val.value;
79 //print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
80 //print (n + '=' + iter.get_value_at(i).value);
93 'datetime' : 'string',
100 'varchar' : 'string',
102 'longtext' : 'string',
103 'tinytext' : 'string',
104 'mediumtext' : 'string',
106 'timestamp' : 'number',
115 var key_file = new GLib.KeyFile.c_new();
116 if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
120 var groups = key_file.get_groups();
121 groups.forEach(function(g) {
124 var keys = key_file.get_keys(g);
125 keys.forEach(function(k) {
126 ini[g][k] = key_file.get_value(g,k);
131 if (File.isFile(cfg.INI)) {
132 if (cfg.INI.match(/links\.ini$/)) {
135 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
139 if (File.isDirectory(cfg.INI)) {
142 //--- load ini files..
143 // this is very specific.
145 var dirs = File.list( cfg.INI + '/Pman').filter(
147 if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
155 dirs.forEach(function(d) {
156 // this currently misses the web.*/Pman/XXXX/DataObjects..
157 var path = cfg.INI + '/Pman/' + d + '/DataObjects';
159 if (!File.isDirectory(path)) {
162 var inis = File.list(path).filter(
163 function(e) { return e.match(/\.links\.ini$/); }
169 inis.forEach(function(i) {
170 readIni(path + '/' + i);
175 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
176 var inis = File.list(cfg.INI).filter(
177 function(e) { return e.match(/\.links\.ini$/); }
180 inis.forEach(function(i) {
181 readIni(path + '/' + i);
187 print(JSON.stringify(ini, null,4));
193 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
199 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
201 tables.forEach(function(table) {
203 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
206 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
210 var firstTxtCol = '';
212 print(JSON.stringify(schema, null,4));
214 schema.forEach(function(e) {
215 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
227 if (typeof(map[e.Type]) == 'undefined') {
230 name: "ArgumentError",
231 message: "Unknown mapping for type : " + e.Type
234 row.type = map[e.Type];
236 if (row.type == 'string' && !firstTxtCol.length) {
237 firstTxtCol = row.name;
240 if (row.type == 'date') {
241 row.dateFormat = 'Y-m-d';
245 if (combofields.length == 1 && row.type == 'string') {
246 combofields.push(row);
250 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
251 title = title[0].toUpperCase() + title.substring(1);
254 "xtype": "ColumnModel",
256 "width": row.type == 'string' ? 200 : 75,
257 "dataIndex": row.name,
258 "|renderer": row.type != 'date' ?
259 "function(v) { return String.format('{0}', v); }" :
260 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
262 "*prop": "colModel[]"
264 var xtype = 'TextField';
265 if (row.type == 'number') {
266 xtype = 'NumberField';
268 if (row.type == 'date') {
271 if (e.Type == 'text') {
274 if (e.name == 'id') {
277 // what about booleans.. -> checkboxes..
284 width : row.type == 'string' ? 200 : 75,
288 if (xtype == 'TextArea') {
289 form[row.name].height = 100;
298 allowBlank : 'false',
300 emptyText : 'Select ' + table,
301 forceSelection : true,
303 loadingText: 'Searching...',
306 qtip: 'Select ' + table,
308 triggerAction : 'all',
315 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
316 queryParam : '',// SET WHEN USED
317 fieldLabel : table, // SET WHEN USED
319 displayField : '', // SET WHEN USED eg. project_id_name
320 hiddenName : '', // SET WHEN USED eg. project_id
321 name : '', // SET WHEN USED eg. project_id_name
329 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
331 '|beforeload' : 'function (_self, o)' +
333 " o.params = o.params || {};\n" +
334 " // set more here\n" +
340 'xtype' : 'HttpProxy',
343 '|url' : "baseURL + '/Roo/" + table + ".php'",
348 'xtype' : 'JsonReader',
352 'totalProperty' : 'total',
353 '|fields' : JSON.stringify(combofields)
364 //print(JSON.stringify(reader,null,4));
368 combofields : combofields,
370 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
372 firstTxtCol : firstTxtCol,
376 //console.dump(schema );
383 // merge in the linked tables..
384 readers.forEach(function(reader) {
385 if (typeof(ini[reader.table]) == 'undefined') {
389 print("OVERLAY - " + reader.table);
391 for (var col in ini[reader.table]) {
392 var kv = ini[reader.table][col].split(':');
395 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
399 // merge in data (eg. project_id => project_id_*****
401 add.oreader.forEach(function(or) {
403 name : col + '_' + or.name,
408 // col is mapped to something..
409 var combofields = add.combofields;
410 if (add.combofields.length < 2) {
413 if (typeof(reader.form[col]) == 'undefined') {
414 print("missing linked column " + col);
418 var combofields_name = add.combofields[1].name;
419 var old = reader.form[col];
420 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
421 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
422 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
423 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
424 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
425 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
426 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
434 //readers.forEach(function(reader) {
435 // delete reader.oreader;
442 //print(JSON.stringify(readers, null, 4));
444 readers.forEach(function(reader) {
447 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
448 if (!File.isDirectory(dir)) {
453 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
458 xtype : "JsonReader",
459 totalProperty : "total",
462 id : 'id', // maybe no..
463 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
467 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
468 JSON.stringify(jreader, null, 4)
473 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
474 if (!File.isDirectory(dir)) {
479 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
482 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
488 "title": reader.table,
490 "fitContainer": true,
491 "tableName": reader.table,
494 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
500 "autoExpandColumn": reader.firstTxtCol,
503 "|render": "function() \n" +
505 " _this.grid = this; \n" +
506 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
507 " if (_this.panel.active) {\n" +
508 " this.footer.onClick('first');\n" +
516 "*prop": "dataSource",
524 "xtype": "HttpProxy",
526 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
534 "xtype": "PagingToolbar",
537 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
538 "emptyMsg": "No " + reader.table + " found",
549 "cls": "x-btn-text-icon",
550 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
552 "|click": "function()\n"+
554 " //yourdialog.show( { id : 0 } , function() {\n"+
555 " // _this.grid.footer.onClick('first');\n"+
559 "|xns": "Roo.Toolbar"
564 "cls": "x-btn-text-icon",
565 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
567 "|click": "function()\n"+
569 " var s = _this.grid.getSelectionModel().getSelections();\n"+
570 " if (!s.length || (s.length > 1)) {\n"+
571 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
575 " //_this.dialog.show(s[0].data, function() {\n"+
576 " // _this.grid.footer.onClick('first');\n"+
582 "|xns": "Roo.Toolbar"
586 "cls": "x-btn-text-icon",
587 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
590 "|click": "function()\n"+
592 " //Pman.genericDelete(_this, _this.grid.tableName); \n"+
596 "|xns": "Roo.Toolbar"
600 ].concat( reader.colmodel)
610 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
611 if (!File.isDirectory(dir)) {
614 var formElements = [];
615 for (var k in reader.form) {
616 if (k == 'id') { // should really do primary key testing..
619 formElements.push(reader.form[k]);
621 formElements.push(reader.form['id']);
623 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
626 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
633 "|actioncomplete" : "function(_self,action)\n"+
635 " if (action.type == 'setdata') {\n"+
636 " //_this.dialog.el.mask(\"Loading\");\n"+
637 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
640 " if (action.type == 'load') {\n"+
641 " _this.dialog.el.unmask();\n"+
644 " if (action.type =='submit') {\n"+
646 " _this.dialog.el.unmask();\n"+
647 " _this.dialog.hide();\n"+
649 " if (_this.callback) {\n"+
650 " _this.callback.call(_this, _this.form.getValues());\n"+
652 " _this.form.reset();\n"+
657 "|rendered" : "function (form)\n"+
659 " _this.form= form;\n"+
663 style : "margin:10px;",
664 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
675 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
676 if (!File.isDirectory(dir)) {
680 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
683 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
686 JSON.stringify(reader.combo, null, 4)