This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
vba:access:classes:sqlscript [11.06.2015 12:50:29] yaslaw |
vba:access:classes:sqlscript [18.01.2017 10:24:56] yaslaw |
||
---|---|---|---|
Line 1: | Line 1: | ||
<const> | <const> | ||
- | version=1.1.1 | + | version=1.5.2 |
- | vdate=09.06.2015 | + | vdate=18.01.2017 |
fname=sqlscript.cls | fname=sqlscript.cls | ||
ns=%NAMESPACE% | ns=%NAMESPACE% | ||
Line 59: | Line 59: | ||
* **saUpdate** | * **saUpdate** | ||
* **saInsertOnDuplicateUpdate** Spezialfall INSERT INTO ... ON DUPLICATE KEY UPDATE .... | * **saInsertOnDuplicateUpdate** Spezialfall INSERT INTO ... ON DUPLICATE KEY UPDATE .... | ||
+ | * **saSelectInto** SELECT INTO. Das erstellen einer Tabelle aus einem SELECT | ||
* **saInsert** | * **saInsert** | ||
* **saDelete** | * **saDelete** | ||
Line 87: | 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 ==== | ||
=== execute === | === execute === | ||
>Container und Statement | >Container und Statement | ||
- | <code>result = container.execute([paramters] [,action]</code> | + | <code>result = container.execute([paramters] [,action])</code> |
Ist das Objekt ein Container, werden alle Scripte in dem Container ausgeführt. | Ist das Objekt ein Container, werden alle Scripte in dem Container ausgeführt. | ||
\\ Der Rückgabewert ist je nach [[#action]] unterschiedlich. | \\ Der Rückgabewert ist je nach [[#action]] unterschiedlich. | ||
Line 119: | Line 190: | ||
| saAlter | Boolean | | | | saAlter | Boolean | | | ||
| saInsertOnDuplicateUpdate | Long | Anzahl betroffener Zeilen | | | saInsertOnDuplicateUpdate | Long | Anzahl betroffener Zeilen | | ||
+ | | saSelectInto | Long | Anzahl betroffener Zeilen | | ||
| saInsert | Long | Anzahl betroffener Zeilen | | | saInsert | Long | Anzahl betroffener Zeilen | | ||
| saUpdate | Long | Anzahl betroffener Zeilen | | | saUpdate | Long | Anzahl betroffener Zeilen | | ||
| 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 169: | Line 215: | ||
' */ | ' */ | ||
Public Sub readText(ByVal iScriptText As String)</code> | Public Sub readText(ByVal iScriptText As String)</code> | ||
- | |||
- | === addNewScript() === | ||
- | >Container | ||
- | <code>container.addNewScript(command [,action])</code> | ||
- | Erstellt in einem Container ein neues Script aus einem SQL-Statement. | ||
- | \\ Siehe auch das Beispiel [[#Manuell erstellter Container]] | ||
- | <code vb>'/** | ||
- | ' * Creiert ein neues Script und hängt es an | ||
- | ' * Diese Funktion kann nur in einem ScriptContainer ausgeführt werden | ||
- | ' * @param String SQL-Statement | ||
- | ' * @param sqlActions | ||
- | ' * @return Boolean Gibt an, ob das iScript mit der Action iAction geparst werden konnte | ||
- | ' */ | ||
- | Public Function addNewScript(ByVal iCmd As String, Optional ByVal iAction As sqlActions = saAutomatic) As Boolean</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 Properties ==== | ==== Wichtigste Properties ==== | ||
Line 229: | 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 260: | 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 299: | 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 ==== | ||
Line 304: | Line 372: | ||
===== Code ===== | ===== Code ===== | ||
- | <source '%%fpath%%/%%fname%%' vb> | + | <WRAP round download> |
+ | ><fc #808080>//Das Modul hat versteckte Attribute. Damit diese aktiv übernommen werden reicht es nicht aus, den Code in ein neues Modul zu kopieren. Man muss das Modul aus der Datei nach VBA importieren.//</fc> | ||
+ | >{{popup>:vba:vba_importfile.png|Bild zum Import}} | ||
+ | |||
+ | {{%%ns%%:%%fname%%|Download %%fname%% (V-%%version%%)}} | ||
+ | </WRAP> |