/* * DDL Builder * Copyright Jake Feasel, 2012 * Released under MIT license * For questions email admin at sqlfiddle dot com * http://github.com/jakefeasel/DDLBuilder */ define( [ "jQuery", "Handlebars", "DateFormat", 'text!./templates/generic.sql', 'text!./templates/oracle.sql', 'text!./templates/sqlite.sql', 'HandlebarsHelpers/each_with_index' ], function ($, Handlebars, dateFormat, generic_template, oracle_template, sqlite_template) { ddl_builder = function (args) { if (!args) args = {}; // output settings this.fieldPrefix = ''; this.fieldSuffix = ''; this.tablePrefix = ''; this.tableSuffix = ''; this.dateFormatMask = "yyyy-mm-dd HH:MM:ss"; this.charType = 'varchar'; this.intType = 'int'; this.floatType = 'numeric'; this.dateType = 'datetime'; // input settings this.valueSeparator = ''; this.column_count = 0; this.definition = { tableName: "Table1", columns: [/* sample column structure { name: 'id', type: 'int', length: '', db_type: 'int4' }, { name: 'name', type: 'char', length: 20, db_type: 'varchar(20)' } */], data: [/* sample data structure // r for "row", v for "value" {r:[{v:1},{v:'Jake'}]}, {r:[{v:2},{v:'Rachel'}]}, {r:[{v:3},{v:'Andrew'}]}, {r:[{v:4},{v:'Ada'}]}, {r:[{v:5},{v:'Lucy O\'Malley'}]} */] }; this.ddlTemplate = generic_template; this.compiledTemplate = Handlebars.compile(this.ddlTemplate); this.setup(args); return this; }; ddl_builder.prototype.setup = function (settings) { for (var opt in settings) { this[opt] = settings[opt]; } if (settings["ddlTemplate"]) this.compiledTemplate = Handlebars.compile(this.ddlTemplate); if (settings["tableName"]) this.definition.tableName = settings.tableName; return this; }; ddl_builder.prototype.setupForDBType = function (type,separator) { switch (type) { case 'SQL Server': this.setup({ statement_separator: separator, fieldPrefix: '[', fieldSuffix: ']', tablePrefix: '[', tableSuffix: ']' }); break; case 'MySQL': this.setup({ statement_separator: separator, fieldPrefix: '`', fieldSuffix: '`', tablePrefix: '`', tableSuffix: '`' }); break; case 'PostgreSQL': this.setup({ statement_separator: separator, dateType: 'timestamp', fieldPrefix: '"', fieldSuffix: '"' }); break; case 'Oracle': var template = oracle_template; this.setup({ dateFormatMask: 'dd-mmm-yyyy hh:MM:ss TT', statement_separator: separator, ddlTemplate: template, dateType: 'timestamp', charType: 'varchar2', fieldPrefix: '"', fieldSuffix: '"' }); break; case 'SQLite': var template = sqlite_template; this.setup({ fieldPrefix: '"', fieldSuffix: '"', tablePrefix: '"', tableSuffix: '"', statement_separator: separator, ddlTemplate: template, dateType: 'DATE', charType: 'TEXT', intType: 'INTEGER', floatType: 'REAL' }); break; } return this; }; ddl_builder.prototype.populateDBTypes = function () { for (var i=0;i this.definition.columns[j].length) { this.definition.columns[j].length = value.length; } tmpRow.push({v:value}); } } this.definition.data.push({r: tmpRow}); } } } this.populateDBTypes(); this.populateWrappers(); return this.render(); }; /* HandlebarsJS-using code below */ Handlebars.registerHelper("formatted_field", function(root) { var colType = ''; var index = -1; for (var j = 0; j < root.columns.length; j++) { if (root.columns[j]) index++; if (index == this.index) { colType = root.columns[j].type; break; } } if (!this.v.length || this.v.toUpperCase() == 'NULL') return 'NULL'; if (colType == 'charType') return new Handlebars.SafeString("'" + this.v + "'"); if (colType == 'dateType') { return new Handlebars.SafeString("'" + dateFormat("UTC:" + this.v, root.dateFormatMask) + "'"); } return this.v; }); Handlebars.registerHelper("column_name_for_index", function(root) { return root.columns[this.index].name; }); ddl_builder.prototype.render = function () { return this.compiledTemplate($.extend(this.definition, {"separator": this.statement_separator})); }; return ddl_builder; });