├── .github └── workflows │ └── release.nuget.pack.yaml ├── .gitignore ├── .vscode ├── launch.json └── tasks.json ├── Dapper.Contrib.BulkInsert.sln ├── LICENSE ├── README.md ├── src └── Dapper.Contrib.BulkInsert │ ├── Dapper.Contrib.BulkInsert.csproj │ ├── SqlMapperExtensions.Async.cs │ ├── SqlMapperExtensions.cs │ └── TypeExtensions.cs └── test └── Dapper.Contrib.BulkInsert.Test ├── ClickHouseADOUnitTest.cs ├── ClickHouseClientADOUnitTest.cs ├── Dapper.Contrib.BulkInsert.Test.csproj ├── MySQLUnitTest.cs └── TestUser.cs /.github/workflows/release.nuget.pack.yaml: -------------------------------------------------------------------------------- 1 | name: Upload dotnet package 2 | 3 | on: 4 | release: 5 | types: [created] 6 | 7 | jobs: 8 | deploy: 9 | runs-on: ubuntu-latest 10 | permissions: 11 | packages: write 12 | contents: read 13 | steps: 14 | - uses: actions/checkout@v2 15 | - uses: actions/setup-dotnet@v1 16 | with: 17 | dotnet-version: '3.1.x' # SDK Version to use. 18 | source-url: https://nuget.pkg.github.com/YahuiWong/index.json 19 | env: 20 | NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} 21 | - run: dotnet build --configuration Release src/Dapper.Contrib.BulkInsert 22 | - name: Create the package 23 | run: dotnet pack --configuration Release src/Dapper.Contrib.BulkInsert 24 | - name: Publish the package to GPR 25 | run: dotnet nuget push src/Dapper.Contrib.BulkInsert/bin/Release/*.nupkg -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | 5 | { 6 | "name": ".NET Core Attach", 7 | "type": "coreclr", 8 | "request": "attach" 9 | }, 10 | { 11 | // Use IntelliSense to find out which attributes exist for C# debugging 12 | // Use hover for the description of the existing attributes 13 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 14 | "name": ".NET Core Launch (console)", 15 | "type": "coreclr", 16 | "request": "launch", 17 | "preLaunchTask": "build", 18 | // If you have changed target frameworks, make sure to update the program path. 19 | "program": "${workspaceFolder}/src/Dapper.Contrib.BulkInsert/bin/Debug/netcoreapp3.1/Dapper.Contrib.BulkInsert.dll", 20 | "args": [], 21 | "cwd": "${workspaceFolder}/src/Dapper.Contrib.BulkInsert", 22 | // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console 23 | "console": "internalConsole", 24 | "stopAtEntry": false 25 | }, 26 | { 27 | "name": ".NET Core Attach", 28 | "type": "coreclr", 29 | "request": "attach" 30 | } 31 | ] 32 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "2.0.0", 5 | "tasks": [ 6 | { 7 | "label": "build", 8 | "command": "dotnet", 9 | "type": "process", 10 | "args": [ 11 | "build", 12 | // Ask dotnet build to generate full paths for file names. 13 | "/property:GenerateFullPaths=true", 14 | // Do not generate summary otherwise it leads to duplicate errors in Problems panel 15 | "/consoleloggerparameters:NoSummary" 16 | ], 17 | "problemMatcher": "$msCompile" 18 | }, 19 | { 20 | "label": "test", 21 | "command": "dotnet", 22 | "type": "process", 23 | "args": [ 24 | "test", 25 | // Ask dotnet build to generate full paths for file names. 26 | "/property:GenerateFullPaths=true", 27 | // Do not generate summary otherwise it leads to duplicate errors in Problems panel 28 | "/consoleloggerparameters:NoSummary" 29 | ], 30 | "problemMatcher": "$msCompile" 31 | } 32 | ] 33 | } -------------------------------------------------------------------------------- /Dapper.Contrib.BulkInsert.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.31129.286 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Contrib.BulkInsert", "src\Dapper.Contrib.BulkInsert\Dapper.Contrib.BulkInsert.csproj", "{0D7E53D9-E560-4D52-B487-935E55182E31}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{BF962B48-DA04-4F3D-9365-221B9E55BB96}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{65D473E0-DA42-4814-B7C8-B27248B61BC8}" 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Contrib.BulkInsert.Test", "test\Dapper.Contrib.BulkInsert.Test\Dapper.Contrib.BulkInsert.Test.csproj", "{964DDBE3-F698-4618-ABDD-9B22ECA75431}" 13 | EndProject 14 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C9D62C2C-7D3D-4BA0-9682-2C8E40BD8D7A}" 15 | ProjectSection(SolutionItems) = preProject 16 | README.md = README.md 17 | EndProjectSection 18 | EndProject 19 | Global 20 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 21 | Debug|Any CPU = Debug|Any CPU 22 | Release|Any CPU = Release|Any CPU 23 | EndGlobalSection 24 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 25 | {0D7E53D9-E560-4D52-B487-935E55182E31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 26 | {0D7E53D9-E560-4D52-B487-935E55182E31}.Debug|Any CPU.Build.0 = Debug|Any CPU 27 | {0D7E53D9-E560-4D52-B487-935E55182E31}.Release|Any CPU.ActiveCfg = Release|Any CPU 28 | {0D7E53D9-E560-4D52-B487-935E55182E31}.Release|Any CPU.Build.0 = Release|Any CPU 29 | {964DDBE3-F698-4618-ABDD-9B22ECA75431}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 30 | {964DDBE3-F698-4618-ABDD-9B22ECA75431}.Debug|Any CPU.Build.0 = Debug|Any CPU 31 | {964DDBE3-F698-4618-ABDD-9B22ECA75431}.Release|Any CPU.ActiveCfg = Release|Any CPU 32 | {964DDBE3-F698-4618-ABDD-9B22ECA75431}.Release|Any CPU.Build.0 = Release|Any CPU 33 | EndGlobalSection 34 | GlobalSection(SolutionProperties) = preSolution 35 | HideSolutionNode = FALSE 36 | EndGlobalSection 37 | GlobalSection(NestedProjects) = preSolution 38 | {0D7E53D9-E560-4D52-B487-935E55182E31} = {BF962B48-DA04-4F3D-9365-221B9E55BB96} 39 | {964DDBE3-F698-4618-ABDD-9B22ECA75431} = {65D473E0-DA42-4814-B7C8-B27248B61BC8} 40 | EndGlobalSection 41 | GlobalSection(ExtensibilityGlobals) = postSolution 42 | SolutionGuid = {D8E1BB30-9BEE-4BFE-AFF4-C63D72836B8B} 43 | EndGlobalSection 44 | EndGlobal 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dapper.Contrib.BulkInsert 2 | 3 | Easy to use Dapper batch insert, support MySQL, SQLServer, ClickHouse and other instances of `DbConnection` 4 | 5 | 6 | ## Demo 7 | 8 | ### Entity 9 | 10 | ```c# 11 | [Table("TestUser")] 12 | public class TestUser 13 | { 14 | [Date] 15 | public DateTime ResisterDate { get; set; } 16 | [ClickHouseColumn(Name= "ResisterTime", IsOnlyIgnoreInsert =true)] 17 | public DateTime ResisterTime { get; set; } 18 | [ColumnName("Name")] 19 | public string Name { get; set; } 20 | public int Age { get; set; } 21 | } 22 | 23 | ``` 24 | ### InsertBulk 25 | ```c# 26 | MySqlConnection conn = new MySqlConnection("server=127.0.0.1;Database=Demo;Uid=root;Pwd=root"); 27 | conn.Open(); 28 | conn.Execute("CREATE TABLE IF NOT EXISTS TestUser (ResisterDate Date, ResisterTime DateTime, Name varchar(200), Age int(11)) ENGINE=InnoDB"); 29 | var user = new TestUser() { ResisterDate = DateTime.Now, ResisterTime = DateTime.Now, Age = 18, Name = "Jack" }; 30 | var user2 = new TestUser() { ResisterDate = DateTime.Now, ResisterTime = DateTime.Now, Age = 18, Name = "Tom" }; 31 | 32 | var users = new List() { user, user2 }; 33 | conn.InsertBulk(users); 34 | ``` 35 | 36 | 37 | 38 | Dapper: https://github.com/StackExchange/Dapper 39 | 40 | ClickHouse: https://github.com/yandex/ClickHouse 41 | 42 | ClickHouse Ado.NET Driver: https://github.com/YahuiWong/ClickHouse.Client 43 | -------------------------------------------------------------------------------- /src/Dapper.Contrib.BulkInsert/Dapper.Contrib.BulkInsert.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp3.1 5 | YahuiWong 6 | Dapper bulk insert with Dapper. 7 | https://github.com/YahuiWong/Dapper.Contrib.BulkInsert 8 | https://github.com/YahuiWong/Dapper.Contrib.BulkInsert.git 9 | git 10 | clickhouse,dapper,bulk,mysql,sqlserver 11 | 0.1.9 12 | true 13 | https://github.com/YahuiWong/Dapper.Contrib.BulkInsert/blob/master/LICENSE 14 | Dapper.Contrib.BulkInsert 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/Dapper.Contrib.BulkInsert/SqlMapperExtensions.Async.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Data; 5 | using System.Linq; 6 | using System.Reflection; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace Dapper.Contrib.BulkInsert 11 | { 12 | public static partial class SqlMapperExtensions 13 | { 14 | 15 | 16 | /// 17 | /// Inserts an entity into table "Ts" and returns identity id or number of inserted rows if inserting a list. 18 | /// 19 | /// Open SqlConnection 20 | /// Entity to insert, can be list of entities 21 | /// Number of seconds before command execution timeout 22 | /// Identity of inserted entity, or number of inserted rows if inserting a list 23 | public static async Task InsertBulkAsync(this IDbConnection connection, IEnumerable entityToInsert, int? commandTimeout = null) 24 | { 25 | var cmd = GenerateBulkSql(connection, entityToInsert); 26 | 27 | var wasClosed = connection.State == ConnectionState.Closed; 28 | if (wasClosed) connection.Open(); 29 | 30 | await connection.ExecuteAsync(cmd.Item1,cmd.Item2, null, commandTimeout, CommandType.Text); 31 | if (wasClosed) connection.Close(); 32 | } 33 | /// 34 | /// Inserts an entity into table "Ts" and returns identity id or number of inserted rows if inserting a list. 35 | /// 36 | /// Open SqlConnection 37 | /// Entity to insert, can be list of entities 38 | /// Identity of inserted entity, or number of inserted rows if inserting a list 39 | public static async Task InsertBulkAsync(this ClickHouse.Client.ADO.ClickHouseConnection connection, IEnumerable entityToInsert, int? commandTimeout = null) 40 | { 41 | 42 | var wasClosed = connection.State == ConnectionState.Closed; 43 | if (wasClosed) connection.Open(); 44 | 45 | 46 | var type = entityToInsert.GetType(); 47 | var typeInfo = type.GetTypeInfo(); 48 | bool implementsGenericIEnumerableOrIsGenericIEnumerable = 49 | typeInfo.ImplementedInterfaces.Any(ti => 50 | ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) || 51 | typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>); 52 | 53 | if (implementsGenericIEnumerableOrIsGenericIEnumerable) 54 | { 55 | type = type.GetGenericArguments()[0]; 56 | } 57 | 58 | var name = GetTableName(type); 59 | var sbColumnList = new StringBuilder(null); 60 | var allProperties = TypePropertiesCache(type); 61 | var keyProperties = KeyPropertiesCache(type); 62 | var computedProperties = ComputedPropertiesCache(type); 63 | var allPropertiesExceptKeyAndComputed = 64 | allProperties.Except(keyProperties.Union(computedProperties)).ToList(); 65 | 66 | for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count; i++) 67 | { 68 | var property = allPropertiesExceptKeyAndComputed[i]; 69 | var clickhouseColumn = GetClickHouseColumn(property); 70 | 71 | if (clickhouseColumn?.IsOnlyIgnoreInsert == true) 72 | continue; 73 | sbColumnList.AppendFormat("`{0}`", GetColumnName(property)); 74 | if (i < allPropertiesExceptKeyAndComputed.Count - 1) 75 | sbColumnList.Append(", "); 76 | } 77 | 78 | 79 | List> rowList = new List>(); 80 | for (int j = 0, length = Enumerable.Count(entityToInsert); j < length; j++) 81 | { 82 | var item = Enumerable.ElementAt(entityToInsert, j); 83 | 84 | { 85 | List row = new List(); 86 | 87 | for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count; i++) 88 | { 89 | var property = allPropertiesExceptKeyAndComputed[i]; 90 | var clickhouseColumn = GetClickHouseColumn(property); 91 | 92 | if (clickhouseColumn?.IsOnlyIgnoreInsert == true) 93 | continue; 94 | var val = property.GetValue(item); 95 | if (property.PropertyType.IsValueType) 96 | { 97 | if (property.PropertyType == typeof(DateTime)) 98 | { 99 | var datetimeValue = (DateTime)val; 100 | 101 | if (property.GetCustomAttribute() != null) 102 | { 103 | row.Add(datetimeValue.Date); 104 | } 105 | else 106 | { 107 | row.Add(datetimeValue); 108 | } 109 | } 110 | else 111 | { 112 | row.Add(val); 113 | } 114 | } 115 | else 116 | { 117 | row.Add(val); 118 | } 119 | } 120 | rowList.Add(row); 121 | } 122 | } 123 | using (var bulkCopy = new ClickHouse.Client.Copy.ClickHouseBulkCopy(connection) 124 | { 125 | DestinationTableName = name, 126 | MaxDegreeOfParallelism = 2, 127 | BatchSize = 10000 128 | }) 129 | { 130 | var rows = rowList.Select(s => s.ToArray()); 131 | await bulkCopy.WriteToServerAsync(rows, new[] { sbColumnList.ToString() }); 132 | } 133 | 134 | 135 | 136 | if (wasClosed) connection.Close(); 137 | } 138 | /// 139 | /// Inserts an entity into table "Ts" and returns identity id or number of inserted rows if inserting a list. 140 | /// 141 | /// Open SqlConnection 142 | /// Entity to insert, can be list of entities 143 | /// Identity of inserted entity, or number of inserted rows if inserting a list 144 | public static async Task InsertBulkAsync(this ClickHouse.Ado.ClickHouseConnection connection, IEnumerable entityToInsert, int? commandTimeout = null) 145 | { 146 | await Task.Run(() => InsertBulk(connection, entityToInsert, commandTimeout) 147 | ); 148 | 149 | } 150 | } 151 | 152 | 153 | } 154 | -------------------------------------------------------------------------------- /src/Dapper.Contrib.BulkInsert/SqlMapperExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Data; 5 | using System.Linq; 6 | using System.Reflection; 7 | using System.Text; 8 | using System.Collections.Concurrent; 9 | 10 | 11 | namespace Dapper.Contrib.BulkInsert 12 | { 13 | /// 14 | /// The Dapper.Contrib extensions for Dapper 15 | /// 16 | public static partial class SqlMapperExtensions 17 | { 18 | /// 19 | /// Defined a proxy object with a possibly dirty state. 20 | /// 21 | public interface IProxy //must be kept public 22 | { 23 | /// 24 | /// Whether the object has been changed. 25 | /// 26 | bool IsDirty { get; set; } 27 | } 28 | 29 | /// 30 | /// Defines a table name mapper for getting table names from types. 31 | /// 32 | public interface ITableNameMapper 33 | { 34 | /// 35 | /// Gets a table name from a given . 36 | /// 37 | /// The to get a name from. 38 | /// The table name for the given . 39 | string GetTableName(Type type); 40 | } 41 | 42 | /// 43 | /// The function to get a database type from the given . 44 | /// 45 | /// The connection to get a database type name from. 46 | public delegate string GetDatabaseTypeDelegate(IDbConnection connection); 47 | 48 | /// 49 | /// The function to get a a table name from a given 50 | /// 51 | /// The to get a table name for. 52 | public delegate string TableNameMapperDelegate(Type type); 53 | 54 | /// 55 | /// The function to get a a column name from a given 56 | /// 57 | /// The to get a column name for. 58 | public delegate string ColumnNameMapperDelegate(PropertyInfo propertyInfo); 59 | /// 60 | /// The function to get a a column name from a given 61 | /// 62 | /// The to get a column name for. 63 | public delegate ClickHouseColumnAttribute ClickHouseColumnMapperDelegate(PropertyInfo propertyInfo); 64 | 65 | private static readonly ConcurrentDictionary> KeyProperties = 66 | new ConcurrentDictionary>(); 67 | 68 | private static readonly ConcurrentDictionary> ExplicitKeyProperties 69 | = new ConcurrentDictionary>(); 70 | 71 | private static readonly ConcurrentDictionary> TypeProperties = 72 | new ConcurrentDictionary>(); 73 | 74 | private static readonly ConcurrentDictionary> ComputedProperties = 75 | new ConcurrentDictionary>(); 76 | 77 | private static readonly ConcurrentDictionary GetQueries = 78 | new ConcurrentDictionary(); 79 | 80 | private static readonly ConcurrentDictionary TypeTableName = 81 | new ConcurrentDictionary(); 82 | private static readonly ConcurrentDictionary TypeColumnName = 83 | new ConcurrentDictionary(); 84 | private static readonly ConcurrentDictionary TypeClickHouseColumn = 85 | new ConcurrentDictionary(); 86 | private static List ComputedPropertiesCache(Type type) 87 | { 88 | if (ComputedProperties.TryGetValue(type.TypeHandle, out IEnumerable pi)) 89 | { 90 | return pi.ToList(); 91 | } 92 | 93 | var computedProperties = TypePropertiesCache(type) 94 | .Where(p => p.GetCustomAttributes(true).Any(a => a is ComputedAttribute)).ToList(); 95 | 96 | ComputedProperties[type.TypeHandle] = computedProperties; 97 | return computedProperties; 98 | } 99 | 100 | private static List ExplicitKeyPropertiesCache(Type type) 101 | { 102 | if (ExplicitKeyProperties.TryGetValue(type.TypeHandle, out IEnumerable pi)) 103 | { 104 | return pi.ToList(); 105 | } 106 | 107 | var explicitKeyProperties = TypePropertiesCache(type) 108 | .Where(p => p.GetCustomAttributes(true).Any(a => a is ExplicitKeyAttribute)).ToList(); 109 | 110 | ExplicitKeyProperties[type.TypeHandle] = explicitKeyProperties; 111 | return explicitKeyProperties; 112 | } 113 | 114 | private static List KeyPropertiesCache(Type type) 115 | { 116 | if (KeyProperties.TryGetValue(type.TypeHandle, out IEnumerable pi)) 117 | { 118 | return pi.ToList(); 119 | } 120 | 121 | var allProperties = TypePropertiesCache(type); 122 | var keyProperties = allProperties.Where(p => p.GetCustomAttributes(true).Any(a => a is KeyAttribute)) 123 | .ToList(); 124 | 125 | if (keyProperties.Count == 0) 126 | { 127 | var idProp = allProperties.Find(p => 128 | string.Equals(p.Name, "id", StringComparison.CurrentCultureIgnoreCase)); 129 | if (idProp != null && !idProp.GetCustomAttributes(true).Any(a => a is ExplicitKeyAttribute)) 130 | { 131 | keyProperties.Add(idProp); 132 | } 133 | } 134 | 135 | KeyProperties[type.TypeHandle] = keyProperties; 136 | return keyProperties; 137 | } 138 | 139 | private static List TypePropertiesCache(Type type) 140 | { 141 | if (TypeProperties.TryGetValue(type.TypeHandle, out IEnumerable pis)) 142 | { 143 | return pis.ToList(); 144 | } 145 | 146 | var properties = type.GetProperties().Where(IsWriteable).ToArray(); 147 | TypeProperties[type.TypeHandle] = properties; 148 | return properties.ToList(); 149 | } 150 | 151 | private static bool IsWriteable(PropertyInfo pi) 152 | { 153 | var attributes = pi.GetCustomAttributes(typeof(WriteAttribute), false).AsList(); 154 | if (attributes.Count != 1) return true; 155 | 156 | var writeAttribute = (WriteAttribute)attributes[0]; 157 | return writeAttribute.Write; 158 | } 159 | 160 | 161 | /// 162 | /// Specify a custom table name mapper based on the POCO type name 163 | /// 164 | public static TableNameMapperDelegate TableNameMapper; 165 | 166 | private static string GetTableName(Type type) 167 | { 168 | if (TypeTableName.TryGetValue(type.TypeHandle, out string name)) return name; 169 | 170 | if (TableNameMapper != null) 171 | { 172 | name = TableNameMapper(type); 173 | } 174 | else 175 | { 176 | var info = type; 177 | //NOTE: This as dynamic trick falls back to handle both our own Table-attribute as well as the one in EntityFramework 178 | var tableAttrName = 179 | info.GetCustomAttribute(false)?.Name 180 | ?? (info.GetCustomAttributes(false) 181 | .FirstOrDefault(attr => attr.GetType().Name == "TableAttribute") as dynamic)?.Name; 182 | 183 | if (tableAttrName != null) 184 | { 185 | name = tableAttrName; 186 | } 187 | else 188 | { 189 | name = type.Name + "s"; 190 | if (type.IsInterface() && name.StartsWith("I")) 191 | name = name.Substring(1); 192 | } 193 | } 194 | 195 | TypeTableName[type.TypeHandle] = name; 196 | return name; 197 | } 198 | /// 199 | /// Specify a custom Column name mapper based on the POCO type name 200 | /// 201 | public static ColumnNameMapperDelegate ColumnNameMapper; 202 | private static string GetColumnName(PropertyInfo propertyInfo) 203 | { 204 | if (TypeColumnName.TryGetValue(propertyInfo.GetHashCode(), out string name)) return name; 205 | if (ColumnNameMapper != null) 206 | { 207 | name = ColumnNameMapper(propertyInfo); 208 | } 209 | else 210 | { 211 | var info = propertyInfo; 212 | //NOTE: This as dynamic trick falls back to handle both our own Column-attribute as well as the one in EntityFramework 213 | var columnAttrName = 214 | info.GetCustomAttribute(false)?.Name 215 | ?? (info.GetCustomAttributes(false) 216 | .FirstOrDefault(attr => attr.GetType().Name == "ColumnNameAttribute") as dynamic)?.Name; 217 | 218 | if (columnAttrName != null) 219 | { 220 | name = columnAttrName; 221 | } 222 | else 223 | { 224 | var clickhouseColumn = GetClickHouseColumn(propertyInfo); 225 | 226 | if (!string.IsNullOrEmpty(clickhouseColumn?.Name)) 227 | name = clickhouseColumn.Name; 228 | else 229 | name = info.Name; 230 | 231 | } 232 | } 233 | 234 | TypeColumnName[propertyInfo.GetHashCode()] = name; 235 | return name; 236 | } 237 | /// 238 | /// Specify a custom Column name mapper based on the POCO type name 239 | /// 240 | public static ClickHouseColumnMapperDelegate ClickHouseColumnMapper; 241 | private static ClickHouseColumnAttribute GetClickHouseColumn(PropertyInfo propertyInfo) 242 | { 243 | ; 244 | if (TypeClickHouseColumn.TryGetValue(propertyInfo.GetHashCode(), out ClickHouseColumnAttribute columnAttribute)) return columnAttribute; 245 | if (ClickHouseColumnMapper != null) 246 | { 247 | columnAttribute = ClickHouseColumnMapper(propertyInfo); 248 | } 249 | else 250 | { 251 | var info = propertyInfo; 252 | //NOTE: This as dynamic trick falls back to handle both our own Column-attribute as well as the one in EntityFramework 253 | var columnAtt = 254 | info.GetCustomAttribute(false); 255 | return columnAtt; 256 | } 257 | 258 | TypeClickHouseColumn[propertyInfo.GetHashCode()] = columnAttribute; 259 | return columnAttribute; 260 | } 261 | 262 | private static (string, DynamicParameters) GenerateBulkSql(IDbConnection connection, IEnumerable entityToInsert) 263 | { 264 | var type = entityToInsert.GetType(); 265 | var typeInfo = type.GetTypeInfo(); 266 | bool implementsGenericIEnumerableOrIsGenericIEnumerable = 267 | typeInfo.ImplementedInterfaces.Any(ti => 268 | ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) || 269 | typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>); 270 | 271 | if (implementsGenericIEnumerableOrIsGenericIEnumerable) 272 | { 273 | type = type.GetGenericArguments()[0]; 274 | } 275 | 276 | var name = GetTableName(type); 277 | var sbColumnList = new StringBuilder(null); 278 | var allProperties = TypePropertiesCache(type); 279 | var keyProperties = KeyPropertiesCache(type); 280 | var computedProperties = ComputedPropertiesCache(type); 281 | var allPropertiesExceptKeyAndComputed = 282 | allProperties.Except(keyProperties.Union(computedProperties)).ToList(); 283 | 284 | for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count; i++) 285 | { 286 | var property = allPropertiesExceptKeyAndComputed[i]; 287 | var clickhouseColumn = GetClickHouseColumn(property); 288 | 289 | if (clickhouseColumn?.IsOnlyIgnoreInsert == true) 290 | continue; 291 | sbColumnList.AppendFormat("{0}", GetColumnName(property)); 292 | if (i < allPropertiesExceptKeyAndComputed.Count - 1) 293 | sbColumnList.Append(", "); 294 | } 295 | 296 | var sbParameterList = new StringBuilder(null); 297 | DynamicParameters parameters = new DynamicParameters(); 298 | 299 | for (int j = 0, length = Enumerable.Count(entityToInsert); j < length; j++) 300 | { 301 | var item = Enumerable.ElementAt(entityToInsert,j); 302 | { 303 | sbParameterList.Append("("); 304 | for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count; i++) 305 | { 306 | var property = allPropertiesExceptKeyAndComputed[i]; 307 | 308 | var clickhouseColumn = GetClickHouseColumn(property); 309 | 310 | if (clickhouseColumn?.IsOnlyIgnoreInsert == true) 311 | continue; 312 | 313 | var val = property.GetValue(item); 314 | var columnName = $"@{GetColumnName(property)}{j}{i}"; 315 | sbParameterList.Append(columnName); 316 | if (property.PropertyType.IsValueType) 317 | { 318 | if (property.PropertyType == typeof(DateTime)) 319 | { 320 | var datetimevalue = Convert.ToDateTime(val); 321 | if (property.GetCustomAttribute() != null) 322 | { 323 | parameters.Add(columnName, datetimevalue.Date, DbType.Date); 324 | //sbParameterList.AppendFormat("'{0:yyyy-MM-dd}'", val); 325 | } 326 | else 327 | { 328 | parameters.Add(columnName, datetimevalue, DbType.DateTime); 329 | //sbParameterList.AppendFormat("'{0:yyyy-MM-dd HH:mm:ss}'", val); 330 | } 331 | } 332 | else if (property.PropertyType == typeof(Boolean)) 333 | { 334 | var boolvalue = Convert.ToBoolean(val); 335 | 336 | { 337 | parameters.Add(columnName, boolvalue?1:0, DbType.Int32); 338 | //sbParameterList.AppendFormat("'{0:yyyy-MM-dd HH:mm:ss}'", val); 339 | } 340 | } 341 | else 342 | { 343 | parameters.Add(columnName, val); 344 | //sbParameterList.AppendFormat("{0}", FixValue(val)); 345 | } 346 | } 347 | else 348 | { 349 | parameters.Add(columnName, val); 350 | ///sbParameterList.AppendFormat("'{0}'", FixValue(val)); 351 | } 352 | 353 | if (i < allPropertiesExceptKeyAndComputed.Count - 1) 354 | sbParameterList.Append(", "); 355 | } 356 | 357 | sbParameterList.Append("),"); 358 | } 359 | } 360 | 361 | 362 | sbParameterList.Remove(sbParameterList.Length - 1, 1); 363 | sbParameterList.Append(";"); 364 | //insert list of entities 365 | var cmd = $"insert into {name} ({sbColumnList}) values {sbParameterList}"; 366 | return (cmd,parameters); 367 | } 368 | 369 | 370 | /// 371 | /// Inserts an entity into table "Ts" and returns identity id or number of inserted rows if inserting a list. 372 | /// 373 | /// Open SqlConnection 374 | /// Entity to insert, can be list of entities 375 | /// Identity of inserted entity, or number of inserted rows if inserting a list 376 | public static void InsertBulk(this ClickHouse.Client.ADO.ClickHouseConnection connection, IEnumerable entityToInsert, int? commandTimeout = null) 377 | { 378 | InsertBulkAsync(connection, entityToInsert, commandTimeout).GetAwaiter().GetResult(); 379 | } 380 | 381 | /// 382 | /// Inserts an entity into table "Ts" and returns identity id or number of inserted rows if inserting a list. 383 | /// 384 | /// Open SqlConnection 385 | /// Entity to insert, can be list of entities 386 | /// Identity of inserted entity, or number of inserted rows if inserting a list 387 | public static void InsertBulk(this IDbConnection connection, IEnumerable entityToInsert, int? commandTimeout = null) 388 | { 389 | var cmd = GenerateBulkSql(connection, entityToInsert); 390 | 391 | var wasClosed = connection.State == ConnectionState.Closed; 392 | if (wasClosed) connection.Open(); 393 | 394 | connection.Execute(cmd.Item1, cmd.Item2, null, commandTimeout); 395 | if (wasClosed) connection.Close(); 396 | } 397 | private static (string, List) GenerateCHBulkSql(IDbConnection connection, IEnumerable entityToInsert) 398 | { 399 | var type = entityToInsert.GetType(); 400 | var typeInfo = type.GetTypeInfo(); 401 | bool implementsGenericIEnumerableOrIsGenericIEnumerable = 402 | typeInfo.ImplementedInterfaces.Any(ti => 403 | ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) || 404 | typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>); 405 | 406 | if (implementsGenericIEnumerableOrIsGenericIEnumerable) 407 | { 408 | type = type.GetGenericArguments()[0]; 409 | } 410 | 411 | var name = GetTableName(type); 412 | var sbColumnList = new StringBuilder(null); 413 | var allProperties = TypePropertiesCache(type); 414 | var keyProperties = KeyPropertiesCache(type); 415 | var computedProperties = ComputedPropertiesCache(type); 416 | var allPropertiesExceptKeyAndComputed = 417 | allProperties.Except(keyProperties.Union(computedProperties)).ToList(); 418 | 419 | for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count; i++) 420 | { 421 | var property = allPropertiesExceptKeyAndComputed[i]; 422 | 423 | var clickhouseColumn = GetClickHouseColumn(property); 424 | 425 | if (clickhouseColumn?.IsOnlyIgnoreInsert == true) 426 | continue; 427 | 428 | sbColumnList.AppendFormat("{0}", GetColumnName(property)); 429 | if (i < allPropertiesExceptKeyAndComputed.Count - 1) 430 | sbColumnList.Append(", "); 431 | } 432 | 433 | //var sbParameterList = new StringBuilder(null); 434 | List dynamics = new List(); 435 | 436 | for (int j = 0, length = Enumerable.Count(entityToInsert); j < length; j++) 437 | { 438 | var item = Enumerable.ElementAt(entityToInsert, j); 439 | { 440 | List dynamicsParams = new List(); 441 | //sbParameterList.Append("("); 442 | for (int i = 0,count= allPropertiesExceptKeyAndComputed.Count; i < count; i++) 443 | { 444 | 445 | var property = allPropertiesExceptKeyAndComputed[i]; 446 | 447 | var clickhouseColumn = GetClickHouseColumn(property); 448 | 449 | if (clickhouseColumn?.IsOnlyIgnoreInsert == true) 450 | continue; 451 | 452 | var val = property.GetValue(item); 453 | //var columnName = $"@{GetColumnName(property)}{j}{i}"; 454 | //sbParameterList.Append(columnName); 455 | if (property.PropertyType.IsValueType) 456 | { 457 | if (property.PropertyType == typeof(DateTime)) 458 | { 459 | var datetimevalue = Convert.ToDateTime(val); 460 | if (property.GetCustomAttribute() != null) 461 | { 462 | //parameters.Add(columnName, datetimevalue.Date, DbType.Date); 463 | dynamicsParams.Add(datetimevalue.Date); 464 | //sbParameterList.AppendFormat("'{0:yyyy-MM-dd}'", val); 465 | } 466 | else 467 | { 468 | dynamicsParams.Add(datetimevalue); 469 | //parameters.Add(columnName, datetimevalue, DbType.DateTime); 470 | //sbParameterList.AppendFormat("'{0:yyyy-MM-dd HH:mm:ss}'", val); 471 | } 472 | } 473 | else if (property.PropertyType == typeof(Boolean)) 474 | { 475 | var boolvalue = Convert.ToBoolean(val); 476 | 477 | { 478 | //parameters.Add(columnName, boolvalue ? 1 : 0, DbType.Int32); 479 | dynamicsParams.Add(boolvalue); 480 | //sbParameterList.AppendFormat("'{0:yyyy-MM-dd HH:mm:ss}'", val); 481 | } 482 | } 483 | else 484 | { 485 | dynamicsParams.Add(val); 486 | //sbParameterList.AppendFormat("{0}", FixValue(val)); 487 | } 488 | } 489 | else 490 | { 491 | dynamicsParams.Add(val); 492 | ///sbParameterList.AppendFormat("'{0}'", FixValue(val)); 493 | } 494 | 495 | //if (i < allPropertiesExceptKeyAndComputed.Count - 1) 496 | // sbParameterList.Append(", "); 497 | 498 | } 499 | dynamics.Add(dynamicsParams.ToArray()); 500 | //sbParameterList.Append("),"); 501 | } 502 | } 503 | 504 | 505 | //sbParameterList.Remove(sbParameterList.Length - 1, 1); 506 | //sbParameterList.Append(";"); 507 | //insert list of entities 508 | var cmd = $"insert into {name} ({sbColumnList}) values @bulk; "; 509 | return (cmd, dynamics); 510 | } 511 | /// 512 | /// Inserts an entity into table "Ts" and returns identity id or number of inserted rows if inserting a list. 513 | /// 514 | /// Open SqlConnection 515 | /// Entity to insert, can be list of entities 516 | /// Identity of inserted entity, or number of inserted rows if inserting a list 517 | public static void InsertBulk(this ClickHouse.Ado.ClickHouseConnection connection, IEnumerable entityToInsert, int? commandTimeout = null) 518 | { 519 | var cmd = GenerateCHBulkSql(connection, entityToInsert); 520 | 521 | var wasClosed = connection.State == ConnectionState.Closed; 522 | if (wasClosed) connection.Open(); 523 | 524 | var command = connection.CreateCommand(); 525 | command.CommandText = cmd.Item1; 526 | command.Parameters.Add(new ClickHouse.Ado.ClickHouseParameter 527 | { 528 | ParameterName = "bulk", 529 | Value = cmd.Item2 530 | }); 531 | command.ExecuteNonQuery(); 532 | if (wasClosed) connection.Close(); 533 | 534 | } 535 | 536 | /// 537 | /// Specifies a custom callback that detects the database type instead of relying on the default strategy (the name of the connection type object). 538 | /// Please note that this callback is global and will be used by all the calls that require a database specific adapter. 539 | /// 540 | public static GetDatabaseTypeDelegate GetDatabaseType; 541 | 542 | } 543 | 544 | /// 545 | /// Defines the name of a table to use in Dapper.Contrib commands. 546 | /// 547 | [AttributeUsage(AttributeTargets.Class)] 548 | public class TableAttribute : Attribute 549 | { 550 | /// 551 | /// Creates a table mapping to a specific name for Dapper.Contrib commands 552 | /// 553 | /// The name of this table in the database. 554 | public TableAttribute(string tableName) 555 | { 556 | Name = tableName; 557 | } 558 | 559 | /// 560 | /// The name of the table in the database 561 | /// 562 | public string Name { get; set; } 563 | } 564 | 565 | /// 566 | /// Specifies that this field is a primary key in the database 567 | /// 568 | [AttributeUsage(AttributeTargets.Property)] 569 | public class KeyAttribute : Attribute 570 | { 571 | } 572 | 573 | /// 574 | /// Specifies that this field is a explicitly set primary key in the database 575 | /// 576 | [AttributeUsage(AttributeTargets.Property)] 577 | public class ExplicitKeyAttribute : Attribute 578 | { 579 | } 580 | 581 | /// 582 | /// Specifies whether a field is writable in the database. 583 | /// 584 | [AttributeUsage(AttributeTargets.Property)] 585 | public class WriteAttribute : Attribute 586 | { 587 | /// 588 | /// Specifies whether a field is writable in the database. 589 | /// 590 | /// Whether a field is writable in the database. 591 | public WriteAttribute(bool write) 592 | { 593 | Write = write; 594 | } 595 | 596 | /// 597 | /// Whether a field is writable in the database. 598 | /// 599 | public bool Write { get; } 600 | } 601 | 602 | /// 603 | /// Specifies that this is a computed column. 604 | /// 605 | [AttributeUsage(AttributeTargets.Property)] 606 | public class ComputedAttribute : Attribute 607 | { 608 | } 609 | 610 | /// 611 | /// Specifies that this is a computed column. 612 | /// 613 | [AttributeUsage(AttributeTargets.Property)] 614 | public class DateAttribute : Attribute 615 | { 616 | } 617 | 618 | [AttributeUsage(AttributeTargets.Property)] 619 | public class ColumnNameAttribute : Attribute 620 | { 621 | /// 622 | /// Creates a ColumnName mapping to a specific name for Dapper.Contrib commands 623 | /// 624 | /// The name of this Column in the database. 625 | public ColumnNameAttribute(string ColumnName) 626 | { 627 | Name = ColumnName; 628 | } 629 | 630 | /// 631 | /// The name of the Column Name in the database 632 | /// 633 | public string Name { get; set; } 634 | } 635 | 636 | [AttributeUsage(AttributeTargets.Property)] 637 | public class ClickHouseColumnAttribute : Attribute 638 | { 639 | /// 640 | /// The name of the Column Name in the database 641 | /// 642 | public string Name { get; set; } 643 | public bool IsOnlyIgnoreInsert { get; set; } 644 | } 645 | } -------------------------------------------------------------------------------- /src/Dapper.Contrib.BulkInsert/TypeExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace Dapper.Contrib.BulkInsert 5 | { 6 | internal static class TypeExtensions 7 | { 8 | public static string Name(this Type type) => type.Name; 9 | 10 | public static bool IsValueType(this Type type) => type.IsValueType; 11 | 12 | public static bool IsEnum(this Type type) => type.IsEnum; 13 | 14 | public static bool IsGenericType(this Type type) => type.IsGenericType; 15 | 16 | public static bool IsInterface(this Type type) => type.IsInterface; 17 | 18 | public static TypeCode GetTypeCode(Type type) => Type.GetTypeCode(type); 19 | 20 | public static MethodInfo GetPublicInstanceMethod(this Type type, string name, Type[] types) 21 | { 22 | return type.GetMethod(name, BindingFlags.Instance | BindingFlags.Public, null, types, null); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /test/Dapper.Contrib.BulkInsert.Test/ClickHouseADOUnitTest.cs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YahuiWong/Dapper.Contrib.BulkInsert/7118f090f4561ca638d705965318084a5902f94a/test/Dapper.Contrib.BulkInsert.Test/ClickHouseADOUnitTest.cs -------------------------------------------------------------------------------- /test/Dapper.Contrib.BulkInsert.Test/ClickHouseClientADOUnitTest.cs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YahuiWong/Dapper.Contrib.BulkInsert/7118f090f4561ca638d705965318084a5902f94a/test/Dapper.Contrib.BulkInsert.Test/ClickHouseClientADOUnitTest.cs -------------------------------------------------------------------------------- /test/Dapper.Contrib.BulkInsert.Test/Dapper.Contrib.BulkInsert.Test.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | runtime; build; native; contentfiles; analyzers; buildtransitive 15 | all 16 | 17 | 18 | runtime; build; native; contentfiles; analyzers; buildtransitive 19 | all 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /test/Dapper.Contrib.BulkInsert.Test/MySQLUnitTest.cs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YahuiWong/Dapper.Contrib.BulkInsert/7118f090f4561ca638d705965318084a5902f94a/test/Dapper.Contrib.BulkInsert.Test/MySQLUnitTest.cs -------------------------------------------------------------------------------- /test/Dapper.Contrib.BulkInsert.Test/TestUser.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Dapper.Contrib.BulkInsert.Test 6 | { 7 | [Table("TestUser")] 8 | public class TestUser 9 | { 10 | [Date] 11 | public DateTime ResisterDate { get; set; } 12 | [ClickHouseColumn(Name= "ResisterTime", IsOnlyIgnoreInsert =true)] 13 | public DateTime ResisterTime { get; set; } 14 | [ColumnName("Name")] 15 | public string Name { get; set; } 16 | public int Age { get; set; } 17 | } 18 | } 19 | --------------------------------------------------------------------------------