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',
113 var key_file = new GLib.KeyFile.c_new();
114 if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
118 var groups = key_file.get_groups();
119 groups.forEach(function(g) {
122 var keys = key_file.get_keys(g);
123 keys.forEach(function(k) {
124 ini[g][k] = key_file.get_value(g,k);
129 if (File.isFile(cfg.INI)) {
130 if (cfg.INI.match(/links\.ini$/)) {
133 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
137 if (File.isDirectory(cfg.INI)) {
140 //--- load ini files..
141 // this is very specific.
143 var dirs = File.list( cfg.INI + '/Pman').filter(
145 if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
153 dirs.forEach(function(d) {
154 // this currently misses the web.*/Pman/XXXX/DataObjects..
155 var path = cfg.INI + '/Pman/' + d + '/DataObjects';
157 if (!File.isDirectory(path)) {
160 var inis = File.list(path).filter(
161 function(e) { return e.match(/\.links\.ini$/); }
167 inis.forEach(function(i) {
168 readIni(path + '/' + i);
173 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
174 var inis = File.list(cfg.INI).filter(
175 function(e) { return e.match(/\.links\.ini$/); }
178 inis.forEach(function(i) {
179 readIni(path + '/' + i);
185 print(JSON.stringify(ini, null,4));
191 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
197 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
199 tables.forEach(function(table) {
201 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
204 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
208 var firstTxtCol = '';
210 print(JSON.stringify(schema, null,4));
212 schema.forEach(function(e) {
213 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
225 if (typeof(map[e.Type]) == 'undefined') {
228 name: "ArgumentError",
229 message: "Unknown mapping for type : " + e.Type
232 row.type = map[e.Type];
234 if (row.type == 'string' && !firstTxtCol.length) {
235 firstTxtCol = row.name;
238 if (row.type == 'date') {
239 row.dateFormat = 'Y-m-d';
243 if (combofields.length == 1 && row.type == 'string') {
244 combofields.push(row);
248 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
249 title = title[0].toUpperCase() + title.substring(1);
252 "xtype": "ColumnModel",
254 "width": row.type == 'string' ? 200 : 75,
255 "dataIndex": row.name,
256 "|renderer": row.type != 'date' ?
257 "function(v) { return String.format('{0}', v); }" :
258 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
260 "*prop": "colModel[]"
262 var xtype = 'TextField';
263 if (row.type == 'number') {
264 xtype = 'NumberField';
266 if (row.type == 'date') {
269 if (e.Type == 'text') {
272 if (e.name == 'id') {
275 // what about booleans.. -> checkboxes..
282 width : row.type == 'string' ? 200 : 75,
286 if (xtype == 'TextArea') {
287 form[row.name].height = 100;
296 allowBlank : 'false',
298 emptyText : 'Select ' + table,
299 forceSelection : true,
301 loadingText: 'Searching...',
304 qtip: 'Select ' + table,
306 triggerAction : 'all',
313 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
314 queryParam : '',// SET WHEN USED
315 fieldLabel : table, // SET WHEN USED
317 displayField : '', // SET WHEN USED eg. project_id_name
318 hiddenName : '', // SET WHEN USED eg. project_id
319 name : '', // SET WHEN USED eg. project_id_name
327 '|beforeload' : 'function (_self, o)' +
329 " o.params = o.params || {};\n" +
330 " // set more here\n" +
336 'xtype' : 'HttpProxy',
339 '|url' : "baseURL + '/Roo/" + table + ".php'",
344 'xtype' : 'JsonReader',
348 'totalProperty' : 'total',
349 '|fields' : JSON.stringify(combofields)
360 //print(JSON.stringify(reader,null,4));
364 combofields : combofields,
366 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
368 firstTxtCol : firstTxtCol,
372 //console.dump(schema );
379 // merge in the linked tables..
380 readers.forEach(function(reader) {
381 if (typeof(ini[reader.table]) == 'undefined') {
385 print("OVERLAY - " + reader.table);
387 for (var col in ini[reader.table]) {
388 var kv = ini[reader.table][col].split(':');
389 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
391 // merge in data (eg. project_id => project_id_*****
393 add.oreader.forEach(function(or) {
395 name : col + '_' + or.name,
400 // col is mapped to something..
401 var combofields = add.combofields;
402 if (add.combofields.length < 2) {
405 if (typeof(reader.form[col]) == 'undefined') {
406 print("missing linked column " + col);
410 var combofields_name = add.combofields[1].name;
411 var old = reader.form[col];
412 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
413 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
414 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
415 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
416 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
417 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
418 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
426 //readers.forEach(function(reader) {
427 // delete reader.oreader;
434 //print(JSON.stringify(readers, null, 4));
436 readers.forEach(function(reader) {
439 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
440 if (!File.isDirectory(dir)) {
445 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
450 xtype : "JsonReader",
451 totalProperty : "total",
454 id : 'id', // maybe no..
455 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
459 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
460 JSON.stringify(jreader, null, 4)
465 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
466 if (!File.isDirectory(dir)) {
471 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
474 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
480 "title": reader.table,
482 "fitContainer": true,
483 "tableName": reader.table,
486 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
492 "autoExpandColumn": reader.firstTxtCol,
495 "|render": "function() \n" +
497 " _this.grid = this; \n" +
498 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
499 " if (_this.panel.active) {\n" +
500 " this.footer.onClick('first');\n" +
508 "*prop": "dataSource",
516 "xtype": "HttpProxy",
518 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
526 "xtype": "PagingToolbar",
529 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
530 "emptyMsg": "No " + reader.table + " found",
541 "cls": "x-btn-text-icon",
542 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
544 "|click": "function()\n"+
546 " //yourdialog.show( { id : 0 } , function() {\n"+
547 " // _this.grid.footer.onClick('first');\n"+
556 "cls": "x-btn-text-icon",
557 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
559 "|click": "function()\n"+
561 " var s = _this.grid.getSelectionModel().getSelections();\n"+
562 " if (!s.length || (s.length > 1)) {\n"+
563 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
567 " //_this.dialog.show(s[0].data, function() {\n"+
568 " // _this.grid.footer.onClick('first');\n"+
578 "cls": "x-btn-text-icon",
579 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
582 "|click": "function()\n"+
584 " //Pman.genericDelete(_this, _this.grid.tableName); \n"+
592 ].concat( reader.colmodel)
602 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
603 if (!File.isDirectory(dir)) {
606 var formElements = [];
607 for (var k in reader.form) {
608 if (k == 'id') { // should really do primary key testing..
611 formElements.push(reader.form[k]);
613 formElements.push(reader.form['id']);
615 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
618 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
625 "|actioncomplete" : "function(_self,action)\n"+
627 " if (action.type == 'setdata') {\n"+
628 " //_this.dialog.el.mask(\"Loading\");\n"+
629 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
632 " if (action.type == 'load') {\n"+
633 " _this.dialog.el.unmask();\n"+
636 " if (action.type =='submit') {\n"+
638 " _this.dialog.el.unmask();\n"+
639 " _this.dialog.hide();\n"+
641 " if (_this.callback) {\n"+
642 " _this.callback.call(_this, _this.form.getValues());\n"+
644 " _this.form.reset();\n"+
649 "|rendered" : "function (form)\n"+
651 " _this.form= form;\n"+
655 style : "margin:10px;",
656 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
667 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
668 if (!File.isDirectory(dir)) {
672 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
675 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
678 JSON.stringify(reader.combo, null, 4)