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 'mediumtext' : 'string',
105 'timestamp' : 'number',
114 var key_file = new GLib.KeyFile.c_new();
115 if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
119 var groups = key_file.get_groups();
120 groups.forEach(function(g) {
123 var keys = key_file.get_keys(g);
124 keys.forEach(function(k) {
125 ini[g][k] = key_file.get_value(g,k);
130 if (File.isFile(cfg.INI)) {
131 if (cfg.INI.match(/links\.ini$/)) {
134 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
138 if (File.isDirectory(cfg.INI)) {
141 //--- load ini files..
142 // this is very specific.
144 var dirs = File.list( cfg.INI + '/Pman').filter(
146 if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
154 dirs.forEach(function(d) {
155 // this currently misses the web.*/Pman/XXXX/DataObjects..
156 var path = cfg.INI + '/Pman/' + d + '/DataObjects';
158 if (!File.isDirectory(path)) {
161 var inis = File.list(path).filter(
162 function(e) { return e.match(/\.links\.ini$/); }
168 inis.forEach(function(i) {
169 readIni(path + '/' + i);
174 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
175 var inis = File.list(cfg.INI).filter(
176 function(e) { return e.match(/\.links\.ini$/); }
179 inis.forEach(function(i) {
180 readIni(path + '/' + i);
186 print(JSON.stringify(ini, null,4));
192 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
198 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
200 tables.forEach(function(table) {
202 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
205 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
209 var firstTxtCol = '';
211 print(JSON.stringify(schema, null,4));
213 schema.forEach(function(e) {
214 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
226 if (typeof(map[e.Type]) == 'undefined') {
229 name: "ArgumentError",
230 message: "Unknown mapping for type : " + e.Type
233 row.type = map[e.Type];
235 if (row.type == 'string' && !firstTxtCol.length) {
236 firstTxtCol = row.name;
239 if (row.type == 'date') {
240 row.dateFormat = 'Y-m-d';
244 if (combofields.length == 1 && row.type == 'string') {
245 combofields.push(row);
249 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
250 title = title[0].toUpperCase() + title.substring(1);
253 "xtype": "ColumnModel",
255 "width": row.type == 'string' ? 200 : 75,
256 "dataIndex": row.name,
257 "|renderer": row.type != 'date' ?
258 "function(v) { return String.format('{0}', v); }" :
259 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
261 "*prop": "colModel[]"
263 var xtype = 'TextField';
264 if (row.type == 'number') {
265 xtype = 'NumberField';
267 if (row.type == 'date') {
270 if (e.Type == 'text') {
273 if (e.name == 'id') {
276 // what about booleans.. -> checkboxes..
283 width : row.type == 'string' ? 200 : 75,
287 if (xtype == 'TextArea') {
288 form[row.name].height = 100;
297 allowBlank : 'false',
299 emptyText : 'Select ' + table,
300 forceSelection : true,
302 loadingText: 'Searching...',
305 qtip: 'Select ' + table,
307 triggerAction : 'all',
314 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
315 queryParam : '',// SET WHEN USED
316 fieldLabel : table, // SET WHEN USED
318 displayField : '', // SET WHEN USED eg. project_id_name
319 hiddenName : '', // SET WHEN USED eg. project_id
320 name : '', // SET WHEN USED eg. project_id_name
328 '|beforeload' : 'function (_self, o)' +
330 " o.params = o.params || {};\n" +
331 " // set more here\n" +
337 'xtype' : 'HttpProxy',
340 '|url' : "baseURL + '/Roo/" + table + ".php'",
345 'xtype' : 'JsonReader',
349 'totalProperty' : 'total',
350 '|fields' : JSON.stringify(combofields)
361 //print(JSON.stringify(reader,null,4));
365 combofields : combofields,
367 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
369 firstTxtCol : firstTxtCol,
373 //console.dump(schema );
380 // merge in the linked tables..
381 readers.forEach(function(reader) {
382 if (typeof(ini[reader.table]) == 'undefined') {
386 print("OVERLAY - " + reader.table);
388 for (var col in ini[reader.table]) {
389 var kv = ini[reader.table][col].split(':');
390 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
392 // merge in data (eg. project_id => project_id_*****
394 add.oreader.forEach(function(or) {
396 name : col + '_' + or.name,
401 // col is mapped to something..
402 var combofields = add.combofields;
403 if (add.combofields.length < 2) {
406 if (typeof(reader.form[col]) == 'undefined') {
407 print("missing linked column " + col);
411 var combofields_name = add.combofields[1].name;
412 var old = reader.form[col];
413 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
414 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
415 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
416 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
417 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
418 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
419 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
427 //readers.forEach(function(reader) {
428 // delete reader.oreader;
435 //print(JSON.stringify(readers, null, 4));
437 readers.forEach(function(reader) {
440 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
441 if (!File.isDirectory(dir)) {
446 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
451 xtype : "JsonReader",
452 totalProperty : "total",
455 id : 'id', // maybe no..
456 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
460 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
461 JSON.stringify(jreader, null, 4)
466 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
467 if (!File.isDirectory(dir)) {
472 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
475 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
481 "title": reader.table,
483 "fitContainer": true,
484 "tableName": reader.table,
487 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
493 "autoExpandColumn": reader.firstTxtCol,
496 "|render": "function() \n" +
498 " _this.grid = this; \n" +
499 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
500 " if (_this.panel.active) {\n" +
501 " this.footer.onClick('first');\n" +
509 "*prop": "dataSource",
517 "xtype": "HttpProxy",
519 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
527 "xtype": "PagingToolbar",
530 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
531 "emptyMsg": "No " + reader.table + " found",
542 "cls": "x-btn-text-icon",
543 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
545 "|click": "function()\n"+
547 " //yourdialog.show( { id : 0 } , function() {\n"+
548 " // _this.grid.footer.onClick('first');\n"+
557 "cls": "x-btn-text-icon",
558 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
560 "|click": "function()\n"+
562 " var s = _this.grid.getSelectionModel().getSelections();\n"+
563 " if (!s.length || (s.length > 1)) {\n"+
564 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
568 " //_this.dialog.show(s[0].data, function() {\n"+
569 " // _this.grid.footer.onClick('first');\n"+
579 "cls": "x-btn-text-icon",
580 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
583 "|click": "function()\n"+
585 " //Pman.genericDelete(_this, _this.grid.tableName); \n"+
593 ].concat( reader.colmodel)
603 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
604 if (!File.isDirectory(dir)) {
607 var formElements = [];
608 for (var k in reader.form) {
609 if (k == 'id') { // should really do primary key testing..
612 formElements.push(reader.form[k]);
614 formElements.push(reader.form['id']);
616 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
619 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
626 "|actioncomplete" : "function(_self,action)\n"+
628 " if (action.type == 'setdata') {\n"+
629 " //_this.dialog.el.mask(\"Loading\");\n"+
630 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
633 " if (action.type == 'load') {\n"+
634 " _this.dialog.el.unmask();\n"+
637 " if (action.type =='submit') {\n"+
639 " _this.dialog.el.unmask();\n"+
640 " _this.dialog.hide();\n"+
642 " if (_this.callback) {\n"+
643 " _this.callback.call(_this, _this.form.getValues());\n"+
645 " _this.form.reset();\n"+
650 "|rendered" : "function (form)\n"+
652 " _this.form= form;\n"+
656 style : "margin:10px;",
657 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
668 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
669 if (!File.isDirectory(dir)) {
673 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
676 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
679 JSON.stringify(reader.combo, null, 4)