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 = type == 'GdaBinary' ? 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.
142 var dirs = File.list( GLib.get_home_dir() + '/gitlive').filter(
143 function(e) { return e.match(/^Pman/); }
146 dirs.forEach(function(d) {
147 // this currently misses the web.*/Pman/XXXX/DataObjects..
148 var path = GLib.get_home_dir() + '/gitlive/' + d + '/DataObjects';
149 if (!File.isDirectory(path)) {
150 path = GLib.get_home_dir() + '/gitlive/' + d + '/Pman/DataObjects';
153 if (!File.isDirectory(path)) {
156 var inis = File.list(path).filter(
157 function(e) { return e.match(/\.links\.ini$/); }
163 inis.forEach(function(i) {
164 readIni(path + '/' + i);
169 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
170 var inis = File.list(cfg.INI).filter(
171 function(e) { return e.match(/\.links\.ini$/); }
174 inis.forEach(function(i) {
175 readIni(path + '/' + i);
181 print(JSON.stringify(ini, null,4));
187 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
193 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
195 tables.forEach(function(table) {
197 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
200 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
204 var firstTxtCol = '';
206 //print(JSON.stringify(schema, null,4));
208 schema.forEach(function(e) {
209 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
221 if (typeof(map[e.Type]) == 'undefined') {
224 name: "ArgumentError",
225 message: "Unknown mapping for type : " + e.Type
228 row.type = map[e.Type];
230 if (row.type == 'string' && !firstTxtCol.length) {
231 firstTxtCol = row.name;
234 if (row.type == 'date') {
235 row.dateFormat = 'Y-m-d';
239 if (combofields.length == 1 && row.type == 'string') {
240 combofields.push(row);
244 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
245 title = title[0].toUpperCase() + title.substring(1);
248 "xtype": "ColumnModel",
250 "width": row.type == 'string' ? 200 : 75,
251 "dataIndex": row.name,
252 "|renderer": row.type != 'date' ?
253 "function(v) { return String.format('{0}', v); }" :
254 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
256 "*prop": "colModel[]"
258 var xtype = 'TextField';
259 if (row.type == 'number') {
260 xtype = 'NumberField';
262 if (row.type == 'date') {
265 if (e.Type == 'text') {
268 if (e.name == 'id') {
271 // what about booleans.. -> checkboxes..
278 width : row.type == 'string' ? 200 : 75,
282 if (xtype == 'TextArea') {
283 form[row.name].height = 100;
292 allowBlank : 'false',
294 emptyText : 'Select ' + table,
295 forceSelection : true,
297 loadingText: 'Searching...',
300 qtip: 'Select ' + table,
302 triggerAction : 'all',
309 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
310 queryParam : '',// SET WHEN USED
311 fieldLabel : table, // SET WHEN USED
313 displayField : '', // SET WHEN USED eg. project_id_name
314 hiddenName : '', // SET WHEN USED eg. project_id
315 name : '', // SET WHEN USED eg. project_id_name
323 '|beforeload' : 'function (_self, o)' +
325 " o.params = o.params || {};\n" +
326 " // set more here\n" +
332 'xtype' : 'HttpProxy',
335 '|url' : "baseURL + '/Roo/" + table + ".php'",
340 'xtype' : 'JsonReader',
344 'totalProperty' : 'total',
345 '|fields' : JSON.stringify(combofields)
356 //print(JSON.stringify(reader,null,4));
360 combofields : combofields,
362 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
364 firstTxtCol : firstTxtCol,
368 //console.dump(schema );
375 // merge in the linked tables..
376 readers.forEach(function(reader) {
377 if (typeof(ini[reader.table]) == 'undefined') {
381 print("OVERLAY - " + reader.table);
383 for (var col in ini[reader.table]) {
384 var kv = ini[reader.table][col].split(':');
385 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
387 // merge in data (eg. project_id => project_id_*****
389 add.oreader.forEach(function(or) {
391 name : col + '_' + or.name,
396 // col is mapped to something..
397 var combofields = add.combofields;
398 if (add.combofields.length < 2) {
403 var combofields_name = add.combofields[1].name;
404 var old = reader.form[col];
405 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
406 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
407 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
408 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
409 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
410 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
411 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
419 //readers.forEach(function(reader) {
420 // delete reader.oreader;
427 //print(JSON.stringify(readers, null, 4));
429 readers.forEach(function(reader) {
432 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
433 if (!File.isDirectory(dir)) {
438 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
443 xtype : "JsonReader",
444 totalProperty : "total",
447 id : 'id', // maybe no..
448 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
452 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
453 JSON.stringify(jreader, null, 4)
458 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
459 if (!File.isDirectory(dir)) {
464 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
467 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
473 "title": reader.table,
475 "fitContainer": true,
476 "tableName": reader.table,
479 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
485 "autoExpandColumn": reader.firstTxtCol,
488 "|render": "function() \n" +
490 " _this.grid = this; \n" +
491 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
492 " if (_this.panel.active) {\n" +
493 " this.footer.onClick('first');\n" +
501 "*prop": "dataSource",
509 "xtype": "HttpProxy",
511 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
519 "xtype": "PagingToolbar",
522 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
523 "emptyMsg": "No " + reader.table + " found",
534 "cls": "x-btn-text-icon",
535 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
537 "|click": "function()\n"+
539 " //yourdialog.show( { id : 0 } , function() {\n"+
540 " // _this.grid.footer.onClick('first');\n"+
549 "cls": "x-btn-text-icon",
550 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
552 "|click": "function()\n"+
554 " var s = _this.grid.getSelectionModel().getSelections();\n"+
555 " if (!s.length || (s.length > 1)) {\n"+
556 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
560 " //_this.dialog.show(s[0].data, function() {\n"+
561 " // _this.grid.footer.onClick('first');\n"+
571 "cls": "x-btn-text-icon",
572 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
575 "|click": "function()\n"+
577 " //Pman.genericDelete(_this, _this.grid.tableName); \n"+
585 ].concat( reader.colmodel)
595 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
596 if (!File.isDirectory(dir)) {
599 var formElements = [];
600 for (var k in reader.form) {
601 if (k == 'id') { // should really do primary key testing..
604 formElements.push(reader.form[k]);
606 formElements.push(reader.form['id']);
608 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
611 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
618 "|actioncomplete" : "function(_self,action)\n"+
620 " if (action.type == 'setdata') {\n"+
621 " //_this.dialog.el.mask(\"Loading\");\n"+
622 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
625 " if (action.type == 'load') {\n"+
626 " _this.dialog.el.unmask();\n"+
629 " if (action.type =='submit') {\n"+
631 " _this.dialog.el.unmask();\n"+
632 " _this.dialog.hide();\n"+
634 " if (_this.callback) {\n"+
635 " _this.callback.call(_this, _this.form.getValues());\n"+
637 " _this.form.reset();\n"+
642 "|rendered" : "function (form)\n"+
644 " _this.form= form;\n"+
648 style : "margin:10px;",
649 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
660 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
661 if (!File.isDirectory(dir)) {
665 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
668 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
671 JSON.stringify(reader.combo, null, 4)