This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
vba:access:classes:sqlscript [24.10.2016 10:41:07] yaslaw |
vba:access:classes:sqlscript [04.09.2019 11:45:59] (current) yaslaw |
||
---|---|---|---|
Line 1: | Line 1: | ||
<const> | <const> | ||
- | version=1.5.0 | + | version=1.5.4 |
- | vdate=21.06.2016 | + | vdate=04.09.2019 |
fname=sqlscript.cls | fname=sqlscript.cls | ||
ns=%NAMESPACE% | ns=%NAMESPACE% | ||
Line 88: | Line 88: | ||
* **spIgnore** Fehler ignorieren und weiterfahren | * **spIgnore** Fehler ignorieren und weiterfahren | ||
* **spLogText** Gibt anstelle des [[#returnValue]] gleich den genierten [[#logText]] zurück | * **spLogText** Gibt anstelle des [[#returnValue]] gleich den genierten [[#logText]] zurück | ||
+ | |||
+ | === text2ddlParams === | ||
+ | Parameter um die Text-zu-TableCreate zu steuern | ||
+ | * **tdAutomatic** | ||
+ | * **tdNoHeader ** Die Erste Zeile ist keine Kopfzeile | ||
+ | * **tdSuppressDashLines** Linien die nur aus -_=| und Leerzeichen bestehen werden unterdrückt. Das werden Trennlinien sein | ||
+ | * **tdDropTable ** Erstelle zum CREATE TABEL auch ein DROP TABLE | ||
+ | |||
+ | ==== Creatoren ==== | ||
+ | |||
+ | === instanceByFileDialog() === | ||
+ | >Container | ||
+ | <code>Set container = SQLScript.instanceByFileDialog([filePath])</code> | ||
+ | Erstellt ein Container aus einer Datei, die mittels dem FileDialog ausgewählt wird | ||
+ | <code vb>'/** | ||
+ | ' * Erstellt ein DDLScriptcontainer aus einem File, das über den Filedialog ausgewählt wurde | ||
+ | ' * @example Öffnen des FileDialoges mit direktem ausführen des Codes | ||
+ | ' * Call SQLScript.instanceByFileDialog().execute | ||
+ | ' * @param String Der Pfad/Dateiname, wo der Dialog öffnet | ||
+ | ' * @retrun SQLScript oder bei Abrruch Nothing | ||
+ | ' */ | ||
+ | Public Static Function instanceByFileDialog(Optional ByVal iFilePath As String = Empty) As SQLScript</code> | ||
+ | |||
+ | === instanceByFilePath() === | ||
+ | >Container | ||
+ | <code>Set container = SQLScript.instanceByFilePath(filePath)</code> | ||
+ | Erstellt ein Container aus einer Datei. Der Datepafad muss bekannt sein. | ||
+ | <code vb>'/** | ||
+ | ' * Erstellt ein DDLScriptcontainer aus einem File, das über einen direkten Pfad geöffnet wird | ||
+ | ' * @example Öffnen und ausführen des ersten SQLs aus einer Date ohne Nachfrage | ||
+ | ' * SQLScript.instanceByFilePath("C:\temp\vba_sql_test.sql")(0).execute(spDirect) | ||
+ | ' * @param String Der Pfad/Datreiname | ||
+ | ' * @retrun SQLScript | ||
+ | ' */ | ||
+ | Public Static Function instanceByFilePath(ByVal iFilePath As String) As SQLScript</code> | ||
+ | |||
+ | === instanceByTableText() === | ||
+ | >Container | ||
+ | <code>script = SQLScript.instanceByTableText(text, tabellenName [,Delemiter]</code> | ||
+ | Diese Methode unterscheidet sich von den Anderen. Die Eingabe ist hier kein SQL, sondern eine Tabelle als Text in einer lesbarer form. Zum Beispiel als CSV | ||
+ | <code vb>'/** | ||
+ | ' * Erstellt ein DDLScriptcontainer aus einem Text, der eine Tabelle darstellt | ||
+ | ' * @param String Tabelle in Textform. zB. CSV | ||
+ | ' * @param String Tabellenname | ||
+ | ' * @param String Mögliche Spaltendelemiter als RegExp-Pattern | ||
+ | ' * @param text2ddlParams Steuerparameter | ||
+ | ' * @param String Eine seperate Kopfzeile. Wenn die Kopfzeile bereits im iText ist, | ||
+ | ' * muss sie nicht seperat mitgegeben werden. NoHeader wird ignoriert | ||
+ | ' * @retrun SQLScript | ||
+ | ' */ | ||
+ | Public Function instanceByTableText( _ | ||
+ | ByVal iText As String, _ | ||
+ | ByVal iTableName As String, _ | ||
+ | Optional ByVal iDelemiterRxPattern As String = "[,|;\t]", _ | ||
+ | Optional ByVal iParams As text2ddlParams = tdSuppressDashLines, _ | ||
+ | Optional ByVal iHeader As String = Empty _ | ||
+ | ) As SQLScript</code> | ||
+ | |||
+ | === instanceSubScript()=== | ||
+ | >Statement | ||
+ | <code>script = SQLScript.instanceSubScript(command [,action])</code> | ||
+ | Erstellt eine Instance für ein einzelnes Script aus einem SQL-Statement heraus. | ||
+ | \\ Siehe Beispiel [[#Ein einsames Script erstellen und ausführen]] | ||
+ | <code vb>'/** | ||
+ | ' * Erstellt eine Instanze eines neuen Subscriptes | ||
+ | ' * @param String SQL-String | ||
+ | ' * @param sqlActions Art des Scriptes. Bei -1 wird die Action ermittelt | ||
+ | ' * @retrun SQLScript | ||
+ | ' */ | ||
+ | Public Static Function instanceSubScript(ByVal iCmd As String, Optional ByVal iAction As sqlActions = saAutomatic) As SQLScript</code> | ||
==== Wichtigste Methoden ==== | ==== Wichtigste Methoden ==== | ||
Line 125: | Line 195: | ||
| saDelete | Long | Anzahl betroffener Zeilen | | | saDelete | Long | Anzahl betroffener Zeilen | | ||
| Else | Long | Boolean | | | Else | Long | Boolean | | ||
- | |||
- | === instanceByFileDialog() === | ||
- | >Container | ||
- | <code>Set container = SQLScript.instanceByFileDialog([filePath])</code> | ||
- | Erstellt ein Container aus einer Datei, die mittels dem FileDialog ausgewählt wird | ||
- | <code vb>'/** | ||
- | ' * Erstellt ein DDLScriptcontainer aus einem File, das über den Filedialog ausgewählt wurde | ||
- | ' * @example Öffnen des FileDialoges mit direktem ausführen des Codes | ||
- | ' * Call SQLScript.instanceByFileDialog().execute | ||
- | ' * @param String Der Pfad/Dateiname, wo der Dialog öffnet | ||
- | ' * @retrun SQLScript oder bei Abrruch Nothing | ||
- | ' */ | ||
- | Public Static Function instanceByFileDialog(Optional ByVal iFilePath As String = Empty) As SQLScript</code> | ||
- | |||
- | === instanceByFilePath() === | ||
- | >Container | ||
- | <code>Set container = SQLScript.instanceByFilePath(filePath)</code> | ||
- | Erstellt ein Container aus einer Datei. Der Datepafad muss bekannt sein. | ||
- | <code vb>'/** | ||
- | ' * Erstellt ein DDLScriptcontainer aus einem File, das über einen direkten Pfad geöffnet wird | ||
- | ' * @example Öffnen und ausführen des ersten SQLs aus einer Date ohne Nachfrage | ||
- | ' * SQLScript.instanceByFilePath("C:\temp\vba_sql_test.sql")(0).execute(spDirect) | ||
- | ' * @param String Der Pfad/Datreiname | ||
- | ' * @retrun SQLScript | ||
- | ' */ | ||
- | Public Static Function instanceByFilePath(ByVal iFilePath As String) As SQLScript</code> | ||
=== readFile() === | === readFile() === | ||
Line 171: | Line 215: | ||
' */ | ' */ | ||
Public Sub readText(ByVal iScriptText As String)</code> | Public Sub readText(ByVal iScriptText As String)</code> | ||
- | |||
- | === instanceSubScript()=== | ||
- | >Statement | ||
- | <code>script = SQLScript.instanceSubScript(command [,action])</code> | ||
- | Erstellt eine Instance für ein einzelnes Script aus einem SQL-Statement heraus. | ||
- | \\ Siehe Beispiel [[#Ein einsames Script erstellen und ausführen]] | ||
- | <code vb>'/** | ||
- | ' * Erstellt eine Instanze eines neuen Subscriptes | ||
- | ' * @param String SQL-String | ||
- | ' * @param sqlActions Art des Scriptes. Bei -1 wird die Action ermittelt | ||
- | ' * @retrun SQLScript | ||
- | ' */ | ||
- | Public Static Function instanceSubScript(ByVal iCmd As String, Optional ByVal iAction As sqlActions = saAutomatic) As SQLScript</code> | ||
- | |||
- | === instanceByTableText() === | ||
- | >Container | ||
- | <code>script = SQLScript.instanceByTableText(text, tabellenName [,Delemiter]</code> | ||
- | Diese Methode unterscheidet sich von den Anderen. Die Eingabe ist hier kein SQL, sondern eine Tabelle als Text in einer lesbarer form. Zum Beispiel als CSV | ||
- | <code vb>'/** | ||
- | ' * Erstellt ein DDLScriptcontainer aus einem Text, der eine Tabelle darstellt | ||
- | ' * @param String Tabelle in Textform. zB. CSV | ||
- | ' * @param String Tabellenname | ||
- | ' * @param String Delemiter der Spalten der Tabelle | ||
- | ' * @retrun SQLScript | ||
- | ' */ | ||
- | Public Function instanceByTableText( _ | ||
- | ByVal iText As String, _ | ||
- | ByVal iTableName As String, _ | ||
- | Optional ByVal iDelemiter As String = "," _ | ||
- | ) As SQLScript</code> | ||
==== Wichtigste Properties ==== | ==== Wichtigste Properties ==== | ||
Line 234: | Line 248: | ||
Der pFad des Scriptfiles. Macht ntürlich auch nur bei einem Containerobjekt Sinn | Der pFad des Scriptfiles. Macht ntürlich auch nur bei einem Containerobjekt Sinn | ||
<code vb>Public Property Get filePath() As String</code> | <code vb>Public Property Get filePath() As String</code> | ||
+ | |||
+ | === script === | ||
+ | Gibt das SQL-SQL-Script zurück. Mit ; am Ende. Im Falle eines Containers wird das ganze Script ausgegeben | ||
+ | <code vb>Public Property Get script() As String</code> | ||
==== verwendete Libraries ==== | ==== verwendete Libraries ==== | ||
Line 265: | Line 283: | ||
=== Ein einsames Script erstellen und ausführen === | === Ein einsames Script erstellen und ausführen === | ||
- | Dank der Instance-Funktionen kann man einfache Scripte ohne grossen Codeaufwand ausführen. Beide folgenden Script haben dasselbe Resultat | + | Dank der Instance-Funktionen kann man einfache Scripte ohne grossen Codeaufwand ausführen. Beide folgenden Script haben dasselbe Resultat. (Im Beispiel wird [[vba:access:functions:printrs]] verwendet) |
<code vb>printRs SQLScript.instanceSubScript("SHOW TABLES").execute() | <code vb>printRs SQLScript.instanceSubScript("SHOW TABLES").execute() | ||
- | print SQLScript.instanceSubScript("SHOW TABLES").execute(spLogText)</code> | + | debug.print SQLScript.instanceSubScript("SHOW TABLES").execute(spLogText)</code> |
<code>| table_name | row_count | last_update | indexes | | <code>| table_name | row_count | last_update | indexes | | ||
|-----------------------------|-----------|---------------------|--------------------------------------------| | |-----------------------------|-----------|---------------------|--------------------------------------------| | ||
Line 304: | Line 322: | ||
| A | BYTE | 123 |</code> | | A | BYTE | 123 |</code> | ||
+ | === Text in Tabellenscript wanden === | ||
+ | Mit der Methode instanceByTableText() kann ein Tabelle in Textform in ein Create Table - & Insertscript konvertiert werden. | ||
+ | |||
+ | Häufig habe ich für Test Tabellen in der Textform oder einen ähnlichen. | ||
+ | <code>ID, ITEM_NAME, ITEM_VALUE, CREATE_DATE | ||
+ | ====================================== | ||
+ | 1, ABC , 123 , 1.1.1967 | ||
+ | 2, DEF , 346.3 , 11.12.2010 | ||
+ | 3, GHI , 10098 , 17.11.2016 | ||
+ | 4, JKL , , </code> | ||
+ | |||
+ | Und heir die Umsetzung für ein kleinen Test: | ||
+ | <code vb>Sub testSqlScript() | ||
+ | Dim script As SQLScript | ||
+ | | ||
+ | Dim txt(5) As String | ||
+ | txt(0) = "ID, ITEM_NAME, ITEM_VALUE, CREATE_DATE" | ||
+ | txt(1) = "======================================" | ||
+ | txt(2) = " 1, ABC , 123 , 1.1.1967 " | ||
+ | txt(3) = " 2, DEF , 346.3 , 11.12.2010 " | ||
+ | txt(4) = " 3, GHI , 10098 , 17.11.2016 " | ||
+ | txt(5) = " 4, JKL , , " | ||
+ | | ||
+ | Set script = SQLScript.instanceByTableText(Join(txt, vbCrLf), "TBL_TEST", , tdDropTable + tdSuppressDashLines) | ||
+ | Debug.Print script.script | ||
+ | script.execute spIgnore | ||
+ | Debug.Print script.logText | ||
+ | End Sub</code> | ||
+ | <code sql>DROP TABLE [TBL_TEST]; | ||
+ | CREATE TABLE [TBL_TEST] ([ID] BYTE,[ITEM_NAME] TEXT(3),[ITEM_VALUE] DOUBLE,[CREATE_DATE] DATE); | ||
+ | INSERT INTO [TBL_TEST] ([ID],[ITEM_NAME],[ITEM_VALUE],[CREATE_DATE]) VALUES (1,'ABC',123,#01-01-1967 00:00:00#); | ||
+ | INSERT INTO [TBL_TEST] ([ID],[ITEM_NAME],[ITEM_VALUE],[CREATE_DATE]) VALUES (2,'DEF',346.3,#12-11-2010 00:00:00#); | ||
+ | INSERT INTO [TBL_TEST] ([ID],[ITEM_NAME],[ITEM_VALUE],[CREATE_DATE]) VALUES (3,'GHI',10098,#11-17-2016 00:00:00#); | ||
+ | INSERT INTO [TBL_TEST] ([ID],[ITEM_NAME],[ITEM_VALUE],[CREATE_DATE]) VALUES (4,'JKL',NULL,NULL); | ||
+ | Table droped | ||
+ | |||
+ | Table created | ||
+ | |||
+ | Insert rows | ||
+ | |||
+ | Insert rows | ||
+ | |||
+ | Insert rows | ||
+ | |||
+ | Insert rows</code> | ||
==== Script Beispiele ==== | ==== Script Beispiele ==== |