create full sql from selection

This commit is contained in:
Paul Trowbridge 2018-05-25 11:27:02 -04:00
parent 4755ebba5b
commit b03218cd4d
2 changed files with 108 additions and 16 deletions

82
FL.bas
View File

@ -1,5 +1,6 @@
Option Explicit Option Explicit
Public x As New TheBigOne Public x As New TheBigOne
Sub Determine_Active_Range() Sub Determine_Active_Range()
@ -78,7 +79,7 @@ Sub ExtractPNC_CSV()
Dim bal() As String Dim bal() As String
Dim bali As Long Dim bali As Long
Dim sched_loan As String Dim sched_loan As String
Dim p As FileDialog Dim P As FileDialog
Dim i As Long Dim i As Long
Dim j As Long Dim j As Long
Dim m As Long Dim m As Long
@ -94,10 +95,10 @@ Sub ExtractPNC_CSV()
'--------Open file------------- '--------Open file-------------
Set p = Application.FileDialog(msoFileDialogOpen) Set P = Application.FileDialog(msoFileDialogOpen)
p.Show P.Show
'--------Extract text---------- '--------Extract text----------
f = x.FILEp_GetTXT(p.SelectedItems(1), 2000) f = x.FILEp_GetTXT(P.SelectedItems(1), 2000)
'--------resize arrays--------- '--------resize arrays---------
ReDim col(11, UBound(f, 2)) ReDim col(11, UBound(f, 2))
@ -175,11 +176,11 @@ Sub ExtractPNC_CSV()
' sh1.Name = "Collateral" ' sh1.Name = "Collateral"
' sh2.Name = "Balance" ' sh2.Name = "Balance"
If Not x.FILEp_CreateCSV(Mid(p.SelectedItems(1), 1, Len(p.SelectedItems(1)) - 4) & "col.csv", col) Then If Not x.FILEp_CreateCSV(Mid(P.SelectedItems(1), 1, Len(P.SelectedItems(1)) - 4) & "col.csv", col) Then
MsgBox ("error") MsgBox ("error")
End If End If
If Not x.FILEp_CreateCSV(Mid(p.SelectedItems(1), 1, Len(p.SelectedItems(1)) - 4) & "bal.csv", bal) Then If Not x.FILEp_CreateCSV(Mid(P.SelectedItems(1), 1, Len(P.SelectedItems(1)) - 4) & "bal.csv", bal) Then
MsgBox ("error") MsgBox ("error")
End If End If
@ -578,6 +579,8 @@ Sub markdown_whole_sheet()
Call wapi.ClipBoard_SetData(x.markdown_whole_sheet(ActiveSheet)) Call wapi.ClipBoard_SetData(x.markdown_whole_sheet(ActiveSheet))
End Sub End Sub
@ -587,7 +590,7 @@ Sub sql_from_range()
Dim wapi As New Windows_API Dim wapi As New Windows_API
Dim r() As String Dim r() As String
Call wapi.ClipBoard_SetData(x.SQLp_build_sql_values(x.ARRAYp_get_range_string(Selection), True)) Call wapi.ClipBoard_SetData(x.SQLp_build_sql_values(x.ARRAYp_get_range_string(Selection), True, True, Db2))
End Sub End Sub
@ -599,13 +602,74 @@ End Sub
Sub pivot_field_format() Sub pivot_field_format()
ActiveSheet.PivotTables("PivotTable1").PivotFields(ActiveCell.value).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""_);_(@_)" ActiveSheet.PivotTables(1).PivotFields(ActiveCell.value).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""_);_(@_)"
End Sub End Sub
Sub Write_selection() Sub Write_selection()
Dim P As FileDialog
'--------Open file-------------
Set P = Application.FileDialog(msoFileDialogSaveAs)
P.Show
Call x.FILEp_CreateTXT(P.SelectedItems(1), x.SHTp_Get(ActiveSheet.Name, Selection.row, Selection.column, False))
End Sub
Sub dump_markdown()
Dim path As String
Dim s As Worksheet
Dim x As New TheBigOne
Dim wapi As New Windows_API
path = ActiveWorkbook.path & "\" & Mid(ActiveWorkbook.Name, 1, InStr(1, ActiveWorkbook.Name, ".xl")) & "md"
For Each s In ActiveWorkbook.Worksheets
Call wapi.ClipBoard_SetData(x.markdown_whole_sheet(s))
Next s
End Sub
Sub test()
Dim c As New WindCrypt
c.Password = "hi"
c.InBuffer = "test"
Call c.Validate
End Sub
Sub split_forecast_data()
Application.EnableCancelKey = xlDisabled
Dim wb As Workbook
Dim ws As Worksheet
Dim d() As String
Dim u() As String
Dim f() As String
Dim i As Long
d = x.SHTp_Get("Data", 1, 1, True)
u = d
Call x.TBLp_Aggregate(u, False, True, True, Array(1), Array("S"), Array(5, 6, 7, 8))
For i = 1 To UBound(u, 2)
Call Sheets("TEMPLATE").Copy(Sheets(i))
Set ws = Sheets(i)
ws.Name = Left(RTrim(u(0, i)), 20)
f = d
Call x.TBLp_FilterSingle(f, 1, u(0, i), True)
Call x.SHTp_Dump(f, ws.Name, 3, 12, False, True, 16, 17, 18, 19)
Next i
Call x.FILEp_CreateTXT("C:\Users\ptrowbridge\Documents\hc_ubm\SQL\DB2\DB2 for i\Mass_Trigger\g_trig.sql", x.SHTp_Get(ActiveSheet.Name, Selection.row, Selection.column, False))
End Sub End Sub

View File

@ -7,7 +7,7 @@ Public ADOo_errstring As String
Public Enum ADOinterface Public Enum ADOinterface
MicrosoftJetOLEDB4 = 0 MicrosoftJetOLEDB4 = 0
MicrosoftACEOLEDB12 = 1 MicrosoftACEOLEDB12 = 1
SqlServer = 2 SQLServer = 2
SQLServerNativeClient = 3 SQLServerNativeClient = 3
SQLServerNativeClient10 = 4 SQLServerNativeClient10 = 4
OracleODBC = 5 OracleODBC = 5
@ -17,6 +17,12 @@ Public Enum ADOinterface
PostgreSQLODBC = 9 PostgreSQLODBC = 9
End Enum End Enum
Public Enum SQLsyntax
Db2 = 0
SQLServer = 1
PostgreSQL = 2
End Enum
Public Function TBLp_Aggregate(ByRef tbl() As String, ByRef needsort As Boolean, ByRef headers As Boolean, ByRef del_unused As Boolean, ParamArray groupnum_type_sumnum()) As Boolean Public Function TBLp_Aggregate(ByRef tbl() As String, ByRef needsort As Boolean, ByRef headers As Boolean, ByRef del_unused As Boolean, ParamArray groupnum_type_sumnum()) As Boolean
@ -2196,7 +2202,7 @@ Function markdown_whole_sheet(ByRef sh As Worksheet) As String
Dim x As New TheBigOne Dim x As New TheBigOne
Dim tbl() As Variant Dim tbl() As Variant
tbl = sh.range("A1:CZ1000") tbl = sh.range("A1:CZ1000").FormulaR1C1
For ic = 1 To UBound(tbl, 2) For ic = 1 To UBound(tbl, 2)
For ir = 1 To UBound(tbl, 1) For ir = 1 To UBound(tbl, 1)
@ -2207,7 +2213,7 @@ Function markdown_whole_sheet(ByRef sh As Worksheet) As String
Next ir Next ir
Next ic Next ic
tbl = sh.range(sh.Cells(1, 1).Address & ":" & sh.Cells(mr, mc).Address) tbl = sh.range(sh.Cells(1, 1).Address & ":" & sh.Cells(mr, mc).Address).FormulaR1C1
markdown_whole_sheet = Me.markdown_from_table(tbl) markdown_whole_sheet = Me.markdown_from_table(tbl)
@ -2223,7 +2229,7 @@ Function MISCe_colnum_to_letter(ByRef x As Long) As String
End Function End Function
Public Function SQLp_build_sql_values(ByRef tbl() As String, trim As Boolean) As String Public Function SQLp_build_sql_values(ByRef tbl() As String, trim As Boolean, headers As Boolean, syntax As SQLsyntax) As String
Dim i As Long Dim i As Long
@ -2231,6 +2237,8 @@ Public Function SQLp_build_sql_values(ByRef tbl() As String, trim As Boolean) As
Dim sql As String Dim sql As String
Dim rec As String Dim rec As String
Dim type_flag() As String Dim type_flag() As String
Dim col_name As String
Dim start_row As Long
ReDim type_flag(UBound(tbl, 1)) ReDim type_flag(UBound(tbl, 1))
For j = 0 To UBound(tbl, 1) For j = 0 To UBound(tbl, 1)
@ -2253,10 +2261,19 @@ Public Function SQLp_build_sql_values(ByRef tbl() As String, trim As Boolean) As
End If End If
Next j Next j
If headers Then
start_row = 1
For i = 0 To UBound(tbl, 1)
If i > 0 Then col_name = col_name & ","
col_name = col_name & """" & tbl(i, 0) & """"
Next i
Else
start_row = 0
End If
For i = 0 To UBound(tbl, 2) For i = start_row To UBound(tbl, 2)
rec = "" rec = ""
If i <> 0 Then sql = sql & "," & vbCrLf If i <> start_row Then sql = sql & "," & vbCrLf
rec = rec & "(" rec = rec & "("
For j = 0 To UBound(tbl, 1) For j = 0 To UBound(tbl, 1)
If j <> 0 Then rec = rec & "," If j <> 0 Then rec = rec & ","
@ -2298,7 +2315,18 @@ Public Function SQLp_build_sql_values(ByRef tbl() As String, trim As Boolean) As
rec = rec & ")" rec = rec & ")"
sql = sql & rec sql = sql & rec
Next i Next i
'---------build select--------------------------
Select Case syntax
Case SQLsyntax.Db2
sql = "SELECT * FROM TABLE( VALUES" & vbCrLf & sql & vbCrLf & ") x"
Case SQLsyntax.SQLServer
sql = "SELECT * FROM (VALUES" & vbCrLf & sql & vbCrLf & ") x"
Case SQLsyntax.PostgreSQL
sql = "SELECT * FROM (VALUES" & vbCrLf & sql & vbCrLf & ") x"
End Select
If headers Then sql = sql & "(" & col_name & ")"
'---------final assignment----------------------
SQLp_build_sql_values = sql SQLp_build_sql_values = sql
End Function End Function