1 //<script type="text/javascript">
4 * This is a hacky generator to generate element definitions for the Roo version from the database
6 * Let's see if libgda can be used to generate our Readers for roo...
8 * Concept - conect to database..
16 * usage: seed generate.js
21 *Hack needed to latest GLib-2.0.gir
23 * <record name="KeyFile" c:type="GKeyFile" disguised="1">
24 <constructor name="new" c:identifier="g_key_file_new">
25 <return-value transfer-ownership="full">
26 <type name="KeyFile" c:type="GKeyFile*"/>
31 * remove introspectable =0 from g_key_file_get_groups
32 * and add transfer-owneership = none to return value
33 * remove introspectable =0 from g_key_file_get_keys
34 * and add transfer-owneership = none to return value*
38 GObject = imports.gi.GObject;
40 GLib = imports.gi.GLib;
42 console = imports.console;
43 File = imports.File.File;
44 Options = imports.Options.Options;
48 var prov = Gda.Config.list_providers ();
49 //print(prov.dump_as_string());
52 help_description : 'Element builder for App Builder based on database schema',
55 { arg_long : 'DBTYPE' , arg_short : 't', description : 'Database Type (eg. MySQL or PostgreSQL ' },
56 { arg_long : 'DBNAME' , arg_short : 'd', description : 'Database Name' },
57 { arg_long : 'USERNAME' , arg_short : 'u', description : 'Username'},
58 { arg_long : 'PASSWORD' , arg_short : 'p', description : '' , arg_default :'' },
59 { arg_long : 'INI' , arg_short : 'I', description :
60 'Either base directory which has Pman/***/DataObjects/***.links.ini or location of ini file.' },
67 var cfg = o.parse(Seed.argv);
68 print(JSON.stringify(cfg, null,4));
70 var cnc = Gda.Connection.open_from_string (cfg.DBTYPE,
71 "DB_NAME=" + cfg.DBNAME,
72 "USERNAME=" + cfg.USERNAME + ';PASSWORD=' + cfg.PASSWORD,
73 Gda.ConnectionOptions.NONE, null);
80 Gda.DataSelect.prototype.fetchAll = function()
84 for (var i =0;i < this.get_n_columns(); i++) {
85 cols.push(this.get_column_name(i));
87 //print(JSON.stringify(cols, null,4));
88 var iter = this.create_iter();
90 //print(this.get_n_rows());
92 for (var r = 0; r < this.get_n_rows(); r++) {
96 if (cols.length == 1) {
97 res.push(this.get_value_at(0,r).get_string());
102 cols.forEach(function(n,i) {
103 var val = _this.get_value_at(i,r);
104 var type = GObject.type_name(val.g_type) ;
105 var vs = ['GdaBinary', 'GdaBlob' ].indexOf(type) > -1 ? val.value.to_string(1024) : val.value;
106 //print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
107 //print (n + '=' + iter.get_value_at(i).value);
121 'time' : 'string', //bogus
130 'varchar' : 'string',
132 'longtext' : 'string',
133 'tinytext' : 'string',
134 'mediumtext' : 'string',
136 'timestamp' : 'number',
145 print('Read INI : ' + fn);
146 var key_file = new GLib.KeyFile.c_new();
147 if (!key_file.load_from_file (fn , GLib.KeyFileFlags.NONE )) {
151 var groups = key_file.get_groups();
152 groups.forEach(function(g) {
155 var keys = key_file.get_keys(g);
156 if (!keys) { return; }
157 keys.forEach(function(k) {
158 ini[g][k] = key_file.get_value(g,k);
163 if (File.isFile(cfg.INI)) {
164 if (cfg.INI.match(/links\.ini$/)) {
167 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
172 if (File.isDirectory(cfg.INI)) {
175 //--- load ini files..
176 // this is very specific.
178 var dirs = File.list( cfg.INI + '/Pman').filter(
180 if (!File.isDirectory(cfg.INI + '/Pman/' + e + '/DataObjects')) {
188 dirs.forEach(function(d) {
189 // this currently misses the web.*/Pman/XXXX/DataObjects..
190 var path = cfg.INI + '/Pman/' + d + '/DataObjects';
192 if (!File.isDirectory(path)) {
195 var inis = File.list(path).filter(
196 function(e) { return e.match(/\.links\.ini$/); }
202 inis.forEach(function(i) {
203 readIni(path + '/' + i);
208 // look at web.XXXX/Pman/XXX/DataObjects/*.ini
209 var inis = File.list(cfg.INI).filter(
210 function(e) { return e.match(/\.links\.ini$/); }
213 inis.forEach(function(i) {
214 readIni(path + '/' + i);
220 //print(JSON.stringify(ini, null,4));
226 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
229 switch(opts.DBTYPE) {
231 query_tables = "SHOW TABLES";
232 query_describe_table = "DESCRIBE '%s'";
236 query_tables = "select c.relname FROM pg_catalog.pg_class c " +
237 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace " +
238 "WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')" +
239 "AND pg_catalog.pg_table_is_visible(c.oid) ";
250 var tables = Gda.execute_select_command(cnc, query_tables).fetchAll();
251 print(JSON.stringify(tables));
254 tables.forEach(function(table) {
256 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
259 var combofields= [ { name : 'id', type: 'int' } ]; // technically the primary key..
263 var firstTxtCol = '';
265 //print(JSON.stringify(schema, null,4));
267 schema.forEach(function(e) {
268 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
280 if (typeof(map[e.Type]) == 'undefined') {
283 name: "ArgumentError",
284 message: "Unknown mapping for type : " + e.Type
287 row.type = map[e.Type];
289 if (row.type == 'string' && !firstTxtCol.length) {
290 firstTxtCol = row.name;
293 if (row.type == 'date') {
294 row.dateFormat = 'Y-m-d';
298 if (combofields.length == 1 && row.type == 'string') {
299 combofields.push(row);
303 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
304 title = title[0].toUpperCase() + title.substring(1);
307 "xtype": "ColumnModel",
309 "width": row.type == 'string' ? 200 : 75,
310 "dataIndex": row.name,
311 "|renderer": row.type != 'date' ?
312 "function(v) { return String.format('{0}', v); }" :
313 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
315 "*prop": "colModel[]"
317 var xtype = 'TextField';
318 if (row.type == 'number') {
319 xtype = 'NumberField';
321 if (row.type == 'date') {
324 if (e.Type == 'text') {
327 if (row.name == 'id') {
330 // what about booleans.. -> checkboxes..
337 width : row.type == 'string' ? 200 : 75,
341 if (xtype == 'TextArea') {
342 form[row.name].height = 100;
344 if (xtype == 'Hidden') {
345 delete form[row.name].fieldLabel;
346 delete form[row.name].width;
354 allowBlank : 'false',
356 emptyText : 'Select ' + table,
357 forceSelection : true,
359 loadingText: 'Searching...',
362 qtip: 'Select ' + table,
364 triggerAction : 'all',
371 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', // SET WHEN USED
372 queryParam : '',// SET WHEN USED
373 fieldLabel : table, // SET WHEN USED
375 displayField : '', // SET WHEN USED eg. project_id_name
376 hiddenName : '', // SET WHEN USED eg. project_id
377 name : '', // SET WHEN USED eg. project_id_name
385 '|sortInfo' : '{ direction : \'ASC\', field: \'id\' }',
387 '|beforeload' : 'function (_self, o)' +
389 " o.params = o.params || {};\n" +
390 " // set more here\n" +
396 'xtype' : 'HttpProxy',
399 '|url' : "baseURL + '/Roo/" + table + ".php'",
404 'xtype' : 'JsonReader',
408 'totalProperty' : 'total',
409 '|fields' : JSON.stringify(combofields)
420 //print(JSON.stringify(reader,null,4));
424 combofields : combofields,
426 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
428 firstTxtCol : firstTxtCol,
432 //console.dump(schema );
439 // merge in the linked tables..
440 readers.forEach(function(reader) {
441 if (typeof(ini[reader.table]) == 'undefined') {
445 print("OVERLAY - " + reader.table);
447 for (var col in ini[reader.table]) {
448 var kv = ini[reader.table][col].split(':');
451 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
455 // merge in data (eg. project_id => project_id_*****
457 add.oreader.forEach(function(or) {
459 name : col + '_' + or.name,
464 // col is mapped to something..
465 var combofields = add.combofields;
466 if (add.combofields.length < 2) {
469 if (typeof(reader.form[col]) == 'undefined') {
470 print (JSON.stringify(reader.form, null,4));
471 print("missing linked column " + col);
475 var combofields_name = add.combofields[1].name;
476 var old = reader.form[col];
477 reader.form[col] = JSON.parse(JSON.stringify(add.combo)); // clone
478 reader.form[col].queryParam = 'query[' + combofields_name + ']';// SET WHEN USED
479 reader.form[col].fieldLabel = old.fieldLabel; // SET WHEN USED
480 reader.form[col].hiddenName = old.name; // SET WHEN USED eg. project_id
481 reader.form[col].displayField = combofields_name; // SET WHEN USED eg. project_id
482 reader.form[col].name = old.name + '_' + combofields_name; // SET WHEN USED eg. project_id_name
483 reader.form[col].tpl = '<div class="x-grid-cell-text x-btn button"><b>{' + combofields_name +'}</b> </div>'; // SET WHEN USED
491 //readers.forEach(function(reader) {
492 // delete reader.oreader;
499 //print(JSON.stringify(readers, null, 4));
501 readers.forEach(function(reader) {
504 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
505 if (!File.isDirectory(dir)) {
506 print("mkdir " + dir);
511 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
516 xtype : "JsonReader",
517 totalProperty : "total",
520 id : 'id', // maybe no..
522 '|fields' : JSON.stringify(reader.reader, null,4).replace(/"/g,"'")
526 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
527 JSON.stringify(jreader, null, 4)
532 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
533 if (!File.isDirectory(dir)) {
534 print("mkdir " + dir);
539 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
542 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
548 "title": reader.table,
550 "fitContainer": true,
551 "tableName": reader.table,
555 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
561 "autoExpandColumn": reader.firstTxtCol,
564 "|render": "function() \n" +
566 " _this.grid = this; \n" +
567 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
568 " if (_this.panel.active) {\n" +
569 " this.footer.onClick('first');\n" +
572 "|rowdblclick": "function (_self, rowIndex, e)\n" +
574 " if (!_this.dialog) return;\n" +
575 " _this.dialog.show( this.getDataSource().getAt(rowIndex), function() {\n" +
576 " _this.grid.footer.onClick('first');\n" +
584 "*prop": "dataSource",
587 '|sortInfo' : "{ field : '" + reader.firstTxtCol + "', direction: 'ASC' }",
593 "xtype": "HttpProxy",
595 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
603 "xtype": "PagingToolbar",
606 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
607 "emptyMsg": "No " + reader.table + " found",
618 "cls": "x-btn-text-icon",
619 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
621 "|click": "function()\n"+
623 " if (!_this.dialog) return;\n" +
624 " _this.dialog.show( { id : 0 } , function() {\n"+
625 " _this.grid.footer.onClick('first');\n"+
629 "|xns": "Roo.Toolbar"
634 "cls": "x-btn-text-icon",
635 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
637 "|click": "function()\n"+
639 " var s = _this.grid.getSelectionModel().getSelections();\n"+
640 " if (!s.length || (s.length > 1)) {\n"+
641 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
644 " if (!_this.dialog) return;\n" +
645 " _this.dialog.show(s[0].data, function() {\n"+
646 " _this.grid.footer.onClick('first');\n"+
652 "|xns": "Roo.Toolbar"
656 "cls": "x-btn-text-icon",
657 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
660 "|click": "function()\n"+
662 " Pman.genericDelete(_this, '" + reader.table + "'); \n"+
666 "|xns": "Roo.Toolbar"
670 ].concat( reader.colmodel)
680 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
681 if (!File.isDirectory(dir)) {
682 print("mkdir " + dir);
685 var formElements = [];
687 for (var k in reader.form) {
688 if (k == 'id') { // should really do primary key testing..
691 formHeight += reader.form[k].xtype == 'TextArea' ? 100 : 30;
693 formElements.push(reader.form[k]);
695 if (reader.form['id']) {
696 formElements.push(reader.form['id']);
700 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
706 "|actioncomplete" : "function(_self,action)\n"+
708 " if (action.type == 'setdata') {\n"+
709 " //_this.dialog.el.mask(\"Loading\");\n"+
710 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
713 " if (action.type == 'load') {\n"+
714 " _this.dialog.el.unmask();\n"+
717 " if (action.type =='submit') {\n"+
719 " _this.dialog.el.unmask();\n"+
720 " _this.dialog.hide();\n"+
722 " if (_this.callback) {\n"+
723 " _this.callback.call(_this, _this.form.getValues());\n"+
725 " _this.form.reset();\n"+
730 "|rendered" : "function (form)\n"+
732 " _this.form= form;\n"+
736 style : "margin:10px;",
737 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",
743 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
746 JSON.stringify( frmCfg, null, 4)
755 dir = GLib.get_home_dir() + '/.Builder/Roo.form.ComboBox';
756 if (!File.isDirectory(dir)) {
757 print("mkdir " + dir);
761 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
764 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
767 JSON.stringify(reader.combo, null, 4)
778 dir = GLib.get_home_dir() + '/.Builder/Roo.LayoutDialog';
779 if (!File.isDirectory(dir)) {
780 print("mkdir " + dir);
783 var formElements = [];
784 for (var k in reader.form) {
785 if (k == 'id') { // should really do primary key testing..
788 formElements.push(reader.form[k]);
790 formElements.push(reader.form['id']);
792 print("WRITE: " + dir + '/' + cfg.DBNAME + '_' + reader.table + '.json');
795 dir + '/' + cfg.DBNAME + '_' + reader.table + '.json',
801 "collapsible": false,
802 "height": formHeight,
804 "title": "Edit / Create " + reader.table,
806 "xtype": "LayoutDialog",
811 "xtype": "LayoutRegion",
816 "xtype": "ContentPanel",
825 "click": "function (_self, e)\n{\n _this.dialog.hide();\n}"
827 "*prop": "buttons[]",
834 "click": "function (_self, e)\n{\n // do some checks?\n \n \n _this.dialog.el.mask(\"Saving\");\n _this.form.doAction(\"submit\");\n\n}"
836 "*prop": "buttons[]",