|
| 1 | +export function singular( name ) { |
| 2 | + // ddl.options["Duality View"].value != 'yes' |
| 3 | + // identity columns in JSON document are singular |
| 4 | + if( name == null ) |
| 5 | + return name; |
| 6 | + if( name.toUpperCase().endsWith('IES') ) |
| 7 | + return name.substring(0,name.length-3)+'y'; |
| 8 | + if( name.toUpperCase().endsWith('ES') ) |
| 9 | + return name.substring(0,name.length-1); |
| 10 | + if( name.toUpperCase().endsWith('S') ) |
| 11 | + return name.substring(0,name.length-1); |
| 12 | + return name; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Ported from dbtools-common Service.quoteIdentifier() and AMENDED for QSQL usage |
| 17 | + * Diffs: 1. Space separator does not warrant double quote |
| 18 | + * 2. Lower case letters do not warrant double quote |
| 19 | + * |
| 20 | + * Quotes an identifier based on the following: |
| 21 | + * 1) Any lower/mixed case identifier is quoted |
| 22 | + * 2) Any identifier starting with the quote string is NOT quoted. If it is currently quoted (first/last characters |
| 23 | + * are the quote character) then no quoting is needed, and if it not quoted but contains a quote character, the name is |
| 24 | + * invalid. |
| 25 | + * 3) The string is checked for invalid characters; any invalid characters require the string be quoted. A valid identifier |
| 26 | + * starts with a letter and contains only letters, digits, or one of _$#. Note that the database allows any valid character |
| 27 | + * in the database character set. We by policy use the basic ASCII character set to determine letters. |
| 28 | + * |
| 29 | + * Conflicting requirements: |
| 30 | + * - SYS.DBMS_SQLTUNE.ACCEPT_ALL_SQL_PROFILES composite object names shouldn't be quoted |
| 31 | + * - Bug 20667620: weird column name "NO." should |
| 32 | + * @param s |
| 33 | + * @param quoteChar |
| 34 | + * @return |
| 35 | + */ |
| 36 | +function quoteIdentifier(/*String*/ s, /*char*/ quoteChar ) { |
| 37 | + let quoteString = '"'; //String.valueOf(quoteChar); |
| 38 | + if( s == null ) // to be able to safely wrap any string value |
| 39 | + return null; |
| 40 | + |
| 41 | + // We want to check 3 things here: |
| 42 | + // 1) The string is a valid identifier (starts with a letter, contains only valid characters,--!!!-- don't care if a reserved word |
| 43 | + // 2) The string is all upper case. By policy, we will quote any lower or mixed cased strings |
| 44 | + // 3) the string is not currently quoted. We will only check the first character for a quote. A quote appearing anywhere |
| 45 | + // else or a starting quote w/o an ending quote is going to make the string invalid no matter what (quotes cannot |
| 46 | + // be escaped in Oracle identifiers). |
| 47 | + let quote = false; |
| 48 | + // Bug 9215024 column name can have "_" so we can't exclude them. |
| 49 | + // ^^^^^^^^^^ |
| 50 | + // Edit Wars: the loop below iterates through list of characters, and as soon |
| 51 | + // it encounters illegitimate symbol, it sets the "need quotation" flag |
| 52 | + const legitimateChars = "$#_ "; // !NO ".": see this function JavaDoc above |
| 53 | + if ( !s.startsWith(quoteString) && !quote ) { |
| 54 | + const chars = s; //new char[s.length()]; |
| 55 | + //s.getChars(0, chars.length, chars, 0); |
| 56 | + if( chars.length > 0 && '0' <= chars[0] && chars[0] <= '9' ) |
| 57 | + quote = true; |
| 58 | + else for ( let i in chars ) { |
| 59 | + const c = chars[i]; |
| 60 | + if( legitimateChars.indexOf(c) < 0 && ( |
| 61 | + c < '0' || '9' < c && c < 'A' || 'Z' < c && c < 'a' || 'z' < c |
| 62 | + )) { |
| 63 | + quote = true; |
| 64 | + break; |
| 65 | + } |
| 66 | + // wierd case with double quote inside |
| 67 | + // ... |
| 68 | + } |
| 69 | + } |
| 70 | + if( s.startsWith("_") || s.startsWith("$") || s.startsWith("#") ) |
| 71 | + quote = true; |
| 72 | + if( !quote ) |
| 73 | + quoteString = ''; |
| 74 | + return quoteString+s+quoteString; |
| 75 | +} |
| 76 | + |
| 77 | +export function canonicalObjectName( nAme ) { |
| 78 | + if( nAme == null ) |
| 79 | + return null; |
| 80 | + if( nAme.indexOf('"') == 0 ) |
| 81 | + return nAme; |
| 82 | + let possiblyQuoted = quoteIdentifier(nAme); |
| 83 | + if( possiblyQuoted.indexOf('"') == 0 ) |
| 84 | + return possiblyQuoted; |
| 85 | + possiblyQuoted = possiblyQuoted.replace(/ /g,"_"); |
| 86 | + return possiblyQuoted; |
| 87 | +}; |
| 88 | + |
| 89 | + |
| 90 | +export function concatNames(chunk1, chunk2, chunk3) { |
| 91 | + let quote = false; |
| 92 | + if( chunk3 == null ) |
| 93 | + chunk3 = ''; |
| 94 | + if( 0 == chunk1.indexOf('"') ) { |
| 95 | + quote = true; |
| 96 | + chunk1 = chunk1.substring(1,chunk1.length-1); |
| 97 | + } |
| 98 | + if( 0 == chunk2.indexOf('"') ) { |
| 99 | + quote = true; |
| 100 | + chunk2 = chunk2.substring(1,chunk2.length-1); |
| 101 | + } |
| 102 | + if( 0 == chunk3.indexOf('"') ) { |
| 103 | + quote = true; |
| 104 | + chunk3 = chunk3.substring(1,chunk3.length-1); |
| 105 | + } |
| 106 | + let ret = chunk1+chunk2+chunk3; |
| 107 | + if( quote ) |
| 108 | + ret = '"'+ret+'"'; |
| 109 | + else |
| 110 | + ret = ret.toLowerCase(); |
| 111 | + |
| 112 | + return ret; |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +export default {singular, canonicalObjectName, concatNames}; |
0 commit comments