@@ -15,20 +15,32 @@ const colDatetimePrecisionOrdinal = 8;
1515
1616async function getSqlScriptAsInsertAsync ( connectionProfile , tableCatalog , tableSchema , tableName , allowIdentityOn = false )
1717{
18- let queryText = sqlUtils . getColumnInfoQuerySql ( tableCatalog , tableSchema , tableName ) ;
19-
20- let results = await sqlUtils . getResultsFromQuerySql ( connectionProfile , "MSSQL" , queryText , tableCatalog ) ;
18+ let provider = connectionProfile . providerName ;
19+ let queryText = "[FAILED TO RESOLVE QUERY TEXT]" ;
20+ if ( provider === "MSSQL" ) {
21+ queryText = sqlUtils . getColumnInfoQuerySql ( tableCatalog , tableSchema , tableName ) ;
22+ }
23+ else if ( provider === "MySQL" ) {
24+ queryText = sqlUtils . getColumnInfoQueryMySql ( tableCatalog , tableSchema , tableName ) ;
25+ }
2126
27+ let results = await sqlUtils . getResultsFromQuerySql ( connectionProfile , provider , queryText , tableCatalog ) ;
2228 if ( ! results || results . rowCount === 0 ) {
23- throw "No se han obtenido resultados de la consulta " ;
29+ throw "No results " ;
2430 }
2531
26- let insertSqlScript = buildFinalScript ( results , tableCatalog , tableSchema , tableName , allowIdentityOn ) ;
32+ let insertSqlScript = "..." ;
33+ if ( provider === "MSSQL" ) {
34+ insertSqlScript = buildFinalScriptMSSQL ( results , tableCatalog , tableSchema , tableName , allowIdentityOn ) ;
35+ }
36+ else if ( provider === "MySQL" ) {
37+ insertSqlScript = buildFinalScriptMySQL ( results , tableCatalog , tableSchema , tableName , allowIdentityOn ) ;
38+ }
2739
2840 return insertSqlScript ;
2941}
3042
31- function buildFinalScript ( results , tableCatalog , tableSchema , tableName , allowIdentityOn )
43+ function buildFinalScriptMSSQL ( results , tableCatalog , tableSchema , tableName , allowIdentityOn )
3244{
3345 let fullScript = [ ] ;
3446 let columsScriptPart = [ ] ;
@@ -101,4 +113,79 @@ function buildFinalScript(results, tableCatalog, tableSchema, tableName, allowId
101113 return fullScript . concat ( columsScriptPart ) . concat ( [ "VALUES" ] ) . concat ( valuesScriptPart ) . join ( '\n' ) ;
102114}
103115
116+ function buildFinalScriptMySQL ( results , tableCatalog , tableSchema , tableName , allowIdentityOn )
117+ {
118+ let fullScript = [ ] ;
119+ let columsScriptPart = [ ] ;
120+ let valuesScriptPart = [ ] ;
121+
122+ columsScriptPart . push ( "(" ) ;
123+ valuesScriptPart . push ( "(" ) ;
124+
125+ let columnIndex = 0 ;
126+ let anyIdentityColumn = false ;
127+
128+ for ( let i = 0 ; i !== results . rowCount ; i ++ )
129+ {
130+ let rowData = results . rows [ i ] ;
131+
132+ let isComputedRaw = rowData [ colComputedOrdinal ] . displayValue ;
133+ let isIdentityRaw = rowData [ colIsIdentityOrdinal ] . displayValue ;
134+ let dataTypeRaw = rowData [ colDataTypeOrdinal ] . displayValue ;
135+
136+ let isComputedColumn = isComputedRaw === "1" ;
137+ let isIdentityColumn = isIdentityRaw === "1" ;
138+ let isTimeStampColumn = dataTypeRaw == "timestamp" ;
139+
140+ if ( isComputedColumn || isTimeStampColumn ) {
141+ continue ;
142+ }
143+
144+ if ( isIdentityColumn )
145+ {
146+ if ( ! allowIdentityOn ) {
147+ continue ;
148+ }
149+
150+ if ( ! anyIdentityColumn ) {
151+ anyIdentityColumn = true ;
152+ }
153+ }
154+
155+ const separator = ( columnIndex === 0 ) ? " " : "," ;
156+
157+ columsScriptPart . push ( "\t\t" + separator + "`" + rowData [ colNameOrdinal ] . displayValue + "`" ) ;
158+
159+ valuesScriptPart . push ( "\t\t" + separator + sqlUtils . getColTypeString (
160+ rowData [ colDataTypeOrdinal ] . displayValue ,
161+ rowData [ colCharsMaxLenOrdinal ] . displayValue ,
162+ rowData [ colNumericPrecisionOrdinal ] . displayValue ,
163+ rowData [ colNumerocScaleOrdinal ] . displayValue ,
164+ rowData [ colIsNullableOrdinal ] . displayValue ,
165+ rowData [ colDatetimePrecisionOrdinal ] . displayValue
166+ ) ) ;
167+
168+ columnIndex += 1 ;
169+ }
170+
171+ const printSetIdentity = allowIdentityOn && anyIdentityColumn ;
172+
173+ if ( printSetIdentity ) {
174+ // no need in MySQL
175+ //fullScript.push(`SET IDENTITY_INSERT \`${tableSchema}\`.\`${tableName}\` ON;\n`);
176+ }
177+
178+ fullScript . push ( `INSERT INTO \`${ tableSchema } \`.\`${ tableName } \`` ) ;
179+
180+ columsScriptPart . push ( ")" ) ;
181+ valuesScriptPart . push ( ");" ) ;
182+
183+ if ( printSetIdentity ) {
184+ // no need in MySQL
185+ //valuesScriptPart.push(`\nSET IDENTITY_INSERT \`${tableSchema}\`.\`${tableName}\` OFF;\n`);
186+ }
187+
188+ return fullScript . concat ( columsScriptPart ) . concat ( [ "VALUES" ] ) . concat ( valuesScriptPart ) . join ( '\n' ) ;
189+ }
190+
104191module . exports . getSqlScriptAsInsertAsync = getSqlScriptAsInsertAsync ;
0 commit comments