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));
61 var iter = this.create_iter();
63 while (iter.move_next()) {
64 if (cols.length == 1) {
65 res.push(iter.get_value_at(0).get_string());
70 cols.forEach(function(n,i) {
71 var val = iter.get_value_at(i);
72 var type = GObject.type_name(val.g_type) ;
73 var vs = type == 'GdaBlob' ? val.value.to_string(1024) : val.value;
74 // print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
75 //print (n + '=' + iter.get_value_at(i).value);
88 'datetime' : 'string',
97 'longtext' : 'string',
98 'mediumtext' : 'string',
108 var key_file = new GLib.KeyFile.c_new();
109 if (!key_file.load_from_file (key_file, fn , GLib.KeyFileFlags.NONE )) {
113 var groups = key_file.get_groups(key_file);
114 groups.forEach(function(g) {
117 var keys = key_file.get_keys(key_file,g);
118 keys.forEach(function(k) {
119 ini[g][k] = key_file.get_value(key_file,g,k);
124 if (File.isFile(cfg.INI)) {
125 if (cfg.INI.match(/links\.ini$/)) {
128 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
132 if (File.isDirectory(cfg.INI)) {
135 //--- load ini files..
136 // this is very specific.
137 var dirs = File.list( GLib.get_home_dir() + '/gitlive').filter(
138 function(e) { return e.match(/^Pman/); }
141 dirs.forEach(function(d) {
142 // this currently misses the web.*/Pman/XXXX/DataObjects..
143 var path = GLib.get_home_dir() + '/gitlive/' + d + '/DataObjects';
144 if (!File.isDirectory(path)) {
145 path = GLib.get_home_dir() + '/gitlive/' + d + '/Pman/DataObjects';
147 if (!File.isDirectory(path)) {
150 var inis = File.list(path).filter(
151 function(e) { return e.match(/\.links\.ini$/); }
157 inis.forEach(function(i) {
158 readIni(path + '/' + i);
166 print(JSON.stringify(ini, null,4));
172 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
178 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
180 tables.forEach(function(table) {
182 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
185 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
189 var firstTxtCol = '';
193 schema.forEach(function(e) {
194 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
206 if (typeof(map[e.Type]) == 'undefined') {
209 name: "ArgumentError",
210 message: "Unknown mapping for type : " + e.Type
213 row.type = map[e.Type];
215 if (row.type == 'string' && !firstTxtCol.length) {
216 firstTxtCol = row.name;
219 if (row.type == 'date') {
220 row.dateFormat = 'Y-m-d';
224 if (combofields.length == 1 && row.type == 'string') {
225 combofields.push(row);
229 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
230 title = title[0].toUpperCase() + title.substring(1);
233 "xtype": "ColumnModel",
235 "width": row.type == 'string' ? 200 : 75,
236 "dataIndex": row.name,
237 "|renderer": row.type != 'date' ?
238 "function(v) { return String.format('{0}', v); }" :
239 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
241 "*prop": "colModel[]"
243 var xtype = 'TextField';
244 if (row.type == 'number') {
245 xtype = 'NumberField';
247 if (row.type == 'date') {
250 if (e.Type == 'text') {
254 // what about booleans.. -> checkboxes..
261 width : row.type == 'string' ? 200 : 75,
265 if (xtype == 'TextArea') {
266 form[row.name].height = 100;
275 allowBlank : 'false',
277 emptyText : 'Select ' + table,
278 forceSelection : true,
280 loadingText: 'Searching...',
283 qtip: 'Select ' + table,
289 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
290 queryParam : '',// SET WHEN USED
291 fieldLabel : table, // SET WHEN USED
292 hiddenName : '', // SET WHEN USED eg. project_id
293 name : '', // SET WHEN USED eg. project_id_name
301 '|beforeload' : 'function (_self, o)' +
303 " o.params = o.params || {};\n" +
304 " // set more here\n" +
310 'xtype' : 'HttpProxy',
313 '|url' : "baseURL + '/Roo/" + table + ".php'",
318 'xtype' : 'JsonReader',
322 'totalProperty' : 'total',
323 '|fields' : JSON.stringify(combofields)
334 //print(JSON.stringify(reader,null,4));
338 combofields : combofields,
340 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
342 firstTxtCol : firstTxtCol,
346 //console.dump(schema );
353 // merge in the linked tables..
354 readers.forEach(function(reader) {
355 if (typeof(ini[reader.table]) == 'undefined') {
359 print("OVERLAY - " + reader.table);
361 for (var col in ini[reader.table]) {
362 var kv = ini[reader.table][col].split(':');
363 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
365 // merge in data (eg. project_id => project_id_*****
367 add.oreader.forEach(function(or) {
372 name : col + '_' + or.name,
377 // col is mapped to something..
378 var combofields = add.combofields;
379 var combofields_name = add.combofields[1].name;
380 var old = reader.form[col];
381 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
382 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
383 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
384 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
385 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
386 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
394 //readers.forEach(function(reader) {
395 // delete reader.oreader;
402 //print(JSON.stringify(readers, null, 4));
404 readers.forEach(function(reader) {
407 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
408 if (!File.isDirectory(dir)) {
413 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
418 xtype : "JsonReader",
419 totalProperty : "total",
422 id : 'id', // maybe no..
423 '|fields' : JSON.stringify(reader.reader, null,4)
427 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
428 JSON.stringify(jreader, null, 4)
433 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
434 if (!File.isDirectory(dir)) {
439 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
442 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
448 "title": reader.table,
450 "fitContainer": true,
451 "tableName": reader.table,
454 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
460 "autoExpandColumn": reader.firstTxtCol,
463 "|render": "function() \n" +
465 " _this.grid = this; \n" +
466 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
467 " if (_this.panel.active) {\n" +
468 " this.footer.onClick('first');\n" +
476 "*prop": "dataSource",
484 "xtype": "HttpProxy",
486 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
494 "xtype": "PagingToolbar",
497 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
498 "emptyMsg": "No " + reader.table + " found",
509 "cls": "x-btn-text-icon",
510 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
512 "|click": "function()\n"+
514 " //yourdialog.show( { id : 0 } , function() {\n"+
515 " // _this.grid.footer.onClick('first');\n"+
524 "cls": "x-btn-text-icon",
525 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
527 "|click": "function()\n"+
529 " var s = _this.grid.getSelectionModel().getSelections();\n"+
530 " if (!s.length || (s.length > 1)) {\n"+
531 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
535 " //_this.dialog.show(s[0].data, function() {\n"+
536 " // _this.grid.footer.onClick('first');\n"+
546 "cls": "x-btn-text-icon",
547 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
550 "|click": "function()\n"+
552 " //Pman.genericDelete(_this, _this.grid.tableName); \n"+
560 ].concat( reader.colmodel)
570 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
571 if (!File.isDirectory(dir)) {
575 for (var k in reader.form) {
576 if (k == 'id') { // should really do primary key testing..
579 formElements.push(reader.form[k]);
582 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
585 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
592 "|actioncomplete" : "function(_self,action)\n"+
594 " if (action.type == 'setdata') {\n"+
595 " //_this.dialog.el.mask(\"Loading\");\n"+
596 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
599 " if (action.type == 'load') {\n"+
600 " _this.dialog.el.unmask();\n"+
603 " if (action.type =='submit') {\n"+
605 " _this.dialog.el.unmask();\n"+
606 " _this.dialog.hide();\n"+
608 " if (_this.callback) {\n"+
609 " _this.callback.call(_this, _this.form.getValues());\n"+
611 " _this.form.reset();\n"+
616 "|rendered" : "function (form)\n"+
618 " _this.form= form;\n"+
622 style : "margin:10px;",
623 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",