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..
18 GObject = imports.gi.GObject;
20 GLib = imports.gi.GLib;
22 console = imports['../../../console.js'];
23 File = imports['../../../File.js'].File;
26 var prov = Gda.Config.list_providers ();
27 //print(prov.dump_as_string());
28 var args = Array.prototype.slice.call(Seed.argv);
29 args.shift();args.shift();// remove first 2
30 if (args.length < 1) {
35 INI : "/path/to/mydb.ini",
37 print("Usage : seed generate.js '" + JSON.stringify(sample) +"'");
40 var cfg = JSON.parse(args[0]);
42 var cnc = Gda.Connection.open_from_string ("MySQL", "DB_NAME=" + cfg.DB_NAME,
43 "USERNAME=" + cfg.USERNAME + ';PASSWORD=' + cfg.PASSWORD,
44 Gda.ConnectionOptions.NONE, null);
51 Gda.DataSelect.prototype.fetchAll = function()
55 for (var i =0;i < this.get_n_columns(); i++) {
56 cols.push(this.get_column_name(i));
59 var iter = this.create_iter();
61 while (iter.move_next()) {
62 if (cols.length == 1) {
63 res.push(iter.get_value_at(0).get_string());
68 cols.forEach(function(n,i) {
69 var val = iter.get_value_at(i);
70 var type = GObject.type_name(val.g_type) ;
71 var vs = type == 'GdaBlob' ? val.value.to_string(1024) : val.value;
72 // print(n + " : TYPE: " + GObject.type_name(val.g_type) + " : " + vs);
73 //print (n + '=' + iter.get_value_at(i).value);
86 'datetime' : 'string',
95 'longtext' : 'string',
96 'mediumtext' : 'string',
106 var key_file = GLib.key_file_new();
107 if (!GLib.key_file_load_from_file (key_file, fn , GLib.KeyFileFlags.NONE )) {
111 var groups = GLib.key_file_get_groups(key_file);
112 groups.forEach(function(g) {
115 var keys = GLib.key_file_get_keys(key_file,g);
116 keys.forEach(function(k) {
117 ini[g][k] = GLib.key_file_get_value(key_file,g,k);
122 if (File.isFile(cfg.INI)) {
123 if (cfg.INI.match(/links\.ini$/)) {
126 readIni(cfg.INI.replace(/\.ini$/, ".links.ini"));
130 if (File.isDirectory(cfg.INI)) {
133 //--- load ini files..
134 // this is very specific.
135 var dirs = File.list( GLib.get_home_dir() + '/gitlive').filter(
136 function(e) { return e.match(/^Pman/); }
139 dirs.forEach(function(d) {
140 // this currently misses the web.*/Pman/XXXX/DataObjects..
141 var path = GLib.get_home_dir() + '/gitlive/' + d + '/DataObjects';
142 if (!File.isDirectory(path)) {
143 path = GLib.get_home_dir() + '/gitlive/' + d + '/Pman/DataObjects';
145 if (!File.isDirectory(path)) {
148 var inis = File.list(path).filter(
149 function(e) { return e.match(/\.links\.ini$/); }
155 inis.forEach(function(i) {
156 readIni(path + '/' + i);
164 print(JSON.stringify(ini, null,4));
170 //GLib.key_file_load_from_file (key_file, String file, KeyFileFlags flags) : Boolean
176 var tables = Gda.execute_select_command(cnc, "SHOW TABLES").fetchAll();
178 tables.forEach(function(table) {
180 var schema = Gda.execute_select_command(cnc, "DESCRIBE `" + table+'`').fetchAll();
185 var firstTxtCol = '';
189 schema.forEach(function(e) {
190 var type = e.Type.match(/([^(]+)\(([^\)]+)\)/);
202 if (typeof(map[e.Type]) == 'undefined') {
205 name: "ArgumentError",
206 message: "Unknown mapping for type : " + e.Type
209 row.type = map[e.Type];
211 if (row.type == 'string' && !firstTxtCol.length) {
212 firstTxtCol = row.name;
215 if (row.type == 'date') {
216 row.dateFormat = 'Y-m-d';
221 var title = row.name.replace(/_id/, '').replace(/_/g, ' ');
222 title = title[0].toUpperCase() + title.substring(1);
225 "xtype": "ColumnModel",
227 "width": row.type == 'string' ? 200 : 75,
228 "dataIndex": row.name,
229 "|renderer": row.type != 'date' ?
230 "function(v) { return String.format('{0}', v); }" :
231 "function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }" , // special for date
233 "*prop": "colModel[]"
235 var xtype = 'TextField';
236 if (row.type == 'number') {
237 xtype = 'NumberField';
239 if (row.type == 'date') {
242 if (e.Type == 'text') {
246 // what about booleans.. -> checkboxes..
253 width : row.type == 'string' ? 200 : 75,
257 if (xtype == 'TextArea') {
258 form[row.name].height = 100;
267 allowBlank : 'false',
269 emptyText : 'Select ' + table,
270 forceSelection : true,
272 loadingText: 'Searching...',
275 qtip: 'Select ' + table,
281 tpl : '<div class="x-grid-cell-text x-btn button"><b>{name}</b> </div>', //
282 queryParam : '',// SET WHEN USED
283 fieldLabel : table, // SET WHEN USED
284 hiddenName : '', // SET WHEN USED eg. project_id
285 name : '', // SET WHEN USED eg. project_id_name
294 //print(JSON.stringify(reader,null,4));
298 oreader : JSON.parse(JSON.stringify(reader)), // dupe it..
300 firstTxtCol : firstTxtCol,
304 //console.dump(schema );
311 // merge in the linked tables..
312 readers.forEach(function(reader) {
313 if (typeof(ini[reader.table]) == 'undefined') {
317 print("OVERLAY - " + reader.table);
319 for (var col in ini[reader.table]) {
320 var kv = ini[reader.table][col].split(':');
321 var add = readers.filter(function(r) { return r.table == kv[0] })[0];
323 // merge in data (eg. project_id => project_id_*****
325 add.oreader.forEach(function(or) {
326 if (or.type == 'string' && !fc) {
332 name : col + '_' + or.name,
337 // col is mapped to something..
357 //readers.forEach(function(reader) {
358 // delete reader.oreader;
365 //print(JSON.stringify(readers, null, 4));
367 readers.forEach(function(reader) {
370 var dir = GLib.get_home_dir() + '/.Builder/Roo.data.JsonReader';
371 if (!File.isDirectory(dir)) {
376 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
381 xtype : "JsonReader",
382 totalProperty : "total",
385 id : 'id', // maybe no..
386 '|fields' : JSON.stringify(reader.reader, null,4)
390 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
391 JSON.stringify(jreader, null, 4)
396 dir = GLib.get_home_dir() + '/.Builder/Roo.GridPanel';
397 if (!File.isDirectory(dir)) {
402 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
405 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
411 "title": reader.table,
413 "fitContainer": true,
414 "tableName": reader.table,
417 "|activate": "function() {\n _this.panel = this;\n if (_this.grid) {\n _this.grid.footer.onClick('first');\n }\n}"
423 "autoExpandColumn": reader.firstTxtCol,
426 "|render": "function() \n" +
428 " _this.grid = this; \n" +
429 " //_this.dialog = Pman.Dialog.FILL_IN\n" +
430 " if (_this.panel.active) {\n" +
431 " this.footer.onClick('first');\n" +
439 "*prop": "dataSource",
447 "xtype": "HttpProxy",
449 "|url": "baseURL + '/Roo/" + reader.table + ".php'",
457 "xtype": "PagingToolbar",
460 "displayMsg": "Displaying " + reader.table + "{0} - {1} of {2}",
461 "emptyMsg": "No " + reader.table + " found",
472 "cls": "x-btn-text-icon",
473 "|icon": "Roo.rootURL + 'images/default/dd/drop-add.gif'",
475 "|click": "function()\n"+
477 " //yourdialog.show( { id : 0 } , function() {\n"+
478 " // _this.grid.footer.onClick('first');\n"+
487 "cls": "x-btn-text-icon",
488 "|icon": "Roo.rootURL + 'images/default/tree/leaf.gif'",
490 "|click": "function()\n"+
492 " var s = _this.grid.getSelectionModel().getSelections();\n"+
493 " if (!s.length || (s.length > 1)) {\n"+
494 " Roo.MessageBox.alert(\"Error\", s.length ? \"Select only one Row\" : \"Select a Row\");\n"+
498 " //_this.dialog.show(s[0].data, function() {\n"+
499 " // _this.grid.footer.onClick('first');\n"+
509 "cls": "x-btn-text-icon",
510 "|icon": "rootURL + '/Pman/templates/images/trash.gif'",
513 "|click": "function()\n"+
515 " //Pman.genericDelete(_this, _this.grid.tableName); \n"+
523 ].concat( reader.colmodel)
533 dir = GLib.get_home_dir() + '/.Builder/Roo.form.Form';
534 if (!File.isDirectory(dir)) {
538 for (var k in reader.form) {
539 if (k == 'id') { // should really do primary key testing..
542 formElements.push(reader.form[k]);
545 print("WRITE: " + dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json');
548 dir + '/' + cfg.DB_NAME + '_' + reader.table + '.json',
555 "|actioncomplete" : "function(_self,action)\n"+
557 " if (action.type == 'setdata') {\n"+
558 " //_this.dialog.el.mask(\"Loading\");\n"+
559 " //this.load({ method: 'GET', params: { '_id' : _this.data.id }});\n"+
562 " if (action.type == 'load') {\n"+
563 " _this.dialog.el.unmask();\n"+
566 " if (action.type =='submit') {\n"+
568 " _this.dialog.el.unmask();\n"+
569 " _this.dialog.hide();\n"+
571 " if (_this.callback) {\n"+
572 " _this.callback.call(_this, _this.form.getValues());\n"+
574 " _this.form.reset();\n"+
579 "|rendered" : "function (form)\n"+
585 style : "margin:10px;",
586 "|url" : "baseURL + '/Roo/" + reader.table + ".php'",