├── .gitignore ├── NewTest ├── Dao │ └── SugarDao.cs ├── DataBase │ ├── AREA.sql │ ├── INSERTTEST.sql │ ├── SCHOOL.sql │ ├── SEQ.sql │ ├── STUDENT.sql │ ├── SUBJECT.sql │ ├── TEST.sql │ └── TESTUPDATECOLUMNS.sql ├── Demos │ ├── Ado.cs │ ├── AttributesMapping.cs │ ├── CreateClass.cs │ ├── Delete.cs │ ├── EnumDemo.cs │ ├── Filter.cs │ ├── Filter2.cs │ ├── IDemos.cs │ ├── IgnoreErrorColumns.cs │ ├── InitConfig.cs │ ├── Insert.cs │ ├── Log.cs │ ├── MappingColumns.cs │ ├── MappingTable.cs │ ├── PubMethod.cs │ ├── Select.cs │ ├── SerialNumber.cs │ ├── SerializerDateFormat.cs │ ├── Test.cs │ ├── Tran.cs │ └── Update.cs ├── Models │ ├── Area.cs │ ├── InsertTest.cs │ ├── LanguageTest.cs │ ├── School.cs │ ├── Student.cs │ ├── Student2.cs │ ├── StudentGroup.cs │ ├── Subject.cs │ ├── Test.cs │ ├── TestUpdateColumns.cs │ ├── V_LanguageTest.cs │ └── V_Student.cs ├── NewTest.csproj ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── app.config └── bin │ └── Debug │ └── NewTest.vshost.exe ├── OracleSugar.sln ├── OracleSugar.suo ├── README.md └── SqlSugar ├── Base ├── OracleConfig.cs └── SqlHelper.cs ├── Cloud ├── CloudModels.cs ├── CloudPubMethod.cs └── TaskExtensions.cs ├── Core ├── IDataReaderEntityBuilder.cs ├── IDataRecordExtensions.cs └── ResolveExpress │ ├── Constant.cs │ ├── Expressions.cs │ ├── Main.cs │ ├── Method.cs │ ├── Models.cs │ ├── Property.cs │ ├── ResolveFieldName.cs │ └── ResolveSelect.cs ├── Generating ├── ClassGenerating.cs ├── ClassTemplate.cs └── SugarAttribute.cs ├── Lib └── Oracle.ManagedDataAccess.dll ├── NuGet.exe ├── OracleSugar.csproj ├── OracleSugar.nuspec ├── Properties └── AssemblyInfo.cs ├── PubModel ├── KeyValue.cs ├── PubEnum.cs └── PubModel.cs ├── Queryable ├── Queryable.cs └── QueryableExtensions.cs ├── SqlSugarClient.cs ├── Sqlable ├── Sqlable.cs └── SqlableExtensions.cs ├── Tool ├── CacheManager.cs ├── Check.cs ├── FileSugar.cs ├── IEnumerableExtensions.cs ├── IStorageObject.cs ├── IsWhatExtensions.cs ├── JsonConverter.cs ├── LanguageHelper.cs ├── PubConvert.cs ├── SqlException.cs ├── SqlSugarTool.cs ├── SqlSugarToolCoreDiff.cs ├── SqlSugarToolExtensions.cs └── SqlSugarToolWithSqlMethod.cs └── nuget.bat /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | # DNX 42 | project.lock.json 43 | artifacts/ 44 | 45 | *_i.c 46 | *_p.c 47 | *_i.h 48 | *.ilk 49 | *.meta 50 | *.obj 51 | *.pch 52 | *.pdb 53 | *.pgc 54 | *.pgd 55 | *.rsp 56 | *.sbr 57 | *.tlb 58 | *.tli 59 | *.tlh 60 | *.tmp 61 | *.tmp_proj 62 | *.log 63 | *.vspscc 64 | *.vssscc 65 | .builds 66 | *.pidb 67 | *.svclog 68 | *.scc 69 | 70 | # Chutzpah Test files 71 | _Chutzpah* 72 | 73 | # Visual C++ cache files 74 | ipch/ 75 | *.aps 76 | *.ncb 77 | *.opensdf 78 | *.sdf 79 | *.cachefile 80 | 81 | # Visual Studio profiler 82 | *.psess 83 | *.vsp 84 | *.vspx 85 | 86 | # TFS 2012 Local Workspace 87 | $tf/ 88 | 89 | # Guidance Automation Toolkit 90 | *.gpState 91 | 92 | # ReSharper is a .NET coding add-in 93 | _ReSharper*/ 94 | *.[Rr]e[Ss]harper 95 | *.DotSettings.user 96 | 97 | # JustCode is a .NET coding add-in 98 | .JustCode 99 | 100 | # TeamCity is a build add-in 101 | _TeamCity* 102 | 103 | # DotCover is a Code Coverage Tool 104 | *.dotCover 105 | 106 | # NCrunch 107 | _NCrunch_* 108 | .*crunch*.local.xml 109 | 110 | # MightyMoose 111 | *.mm.* 112 | AutoTest.Net/ 113 | 114 | # Web workbench (sass) 115 | .sass-cache/ 116 | 117 | # Installshield output folder 118 | [Ee]xpress/ 119 | 120 | # DocProject is a documentation generator add-in 121 | DocProject/buildhelp/ 122 | DocProject/Help/*.HxT 123 | DocProject/Help/*.HxC 124 | DocProject/Help/*.hhc 125 | DocProject/Help/*.hhk 126 | DocProject/Help/*.hhp 127 | DocProject/Help/Html2 128 | DocProject/Help/html 129 | 130 | # Click-Once directory 131 | publish/ 132 | 133 | # Publish Web Output 134 | *.[Pp]ublish.xml 135 | *.azurePubxml 136 | ## TODO: Comment the next line if you want to checkin your 137 | ## web deploy settings but do note that will include unencrypted 138 | ## passwords 139 | #*.pubxml 140 | 141 | *.publishproj 142 | 143 | # NuGet Packages 144 | *.nupkg 145 | # The packages folder can be ignored because of Package Restore 146 | **/packages/* 147 | # except build/, which is used as an MSBuild target. 148 | !**/packages/build/ 149 | # Uncomment if necessary however generally it will be regenerated when needed 150 | #!**/packages/repositories.config 151 | 152 | # Windows Azure Build Output 153 | csx/ 154 | *.build.csdef 155 | 156 | # Windows Store app package directory 157 | AppPackages/ 158 | 159 | # Visual Studio cache files 160 | # files ending in .cache can be ignored 161 | *.[Cc]ache 162 | # but keep track of directories ending in .cache 163 | !*.[Cc]ache/ 164 | 165 | # Others 166 | ClientBin/ 167 | [Ss]tyle[Cc]op.* 168 | ~$* 169 | *~ 170 | *.dbmdl 171 | *.dbproj.schemaview 172 | *.pfx 173 | *.publishsettings 174 | node_modules/ 175 | orleans.codegen.cs 176 | 177 | # RIA/Silverlight projects 178 | Generated_Code/ 179 | 180 | # Backup & report files from converting an old project file 181 | # to a newer Visual Studio version. Backup files are not needed, 182 | # because we have git ;-) 183 | _UpgradeReport_Files/ 184 | Backup*/ 185 | UpgradeLog*.XML 186 | UpgradeLog*.htm 187 | 188 | # SQL Server files 189 | *.mdf 190 | *.ldf 191 | 192 | # Business Intelligence projects 193 | *.rdl.data 194 | *.bim.layout 195 | *.bim_*.settings 196 | 197 | # Microsoft Fakes 198 | FakesAssemblies/ 199 | 200 | # Node.js Tools for Visual Studio 201 | .ntvs_analysis.dat 202 | 203 | # Visual Studio 6 build log 204 | *.plg 205 | 206 | # Visual Studio 6 workspace options file 207 | *.opt 208 | 209 | # LightSwitch generated files 210 | GeneratedArtifacts/ 211 | _Pvt_Extensions/ 212 | ModelManifest.xml 213 | -------------------------------------------------------------------------------- /NewTest/Dao/SugarDao.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using OracleSugar; 5 | namespace NewTest.Dao 6 | { 7 | /// 8 | /// SqlSugar 9 | /// 10 | public class SugarDao 11 | { 12 | //禁止实例化 13 | private SugarDao() 14 | { 15 | 16 | } 17 | public static string ConnectionString 18 | { 19 | get 20 | { 21 | string reval = "Data Source=localhost/orcl;User ID=system;Password=JHL52771jhl;"; //这里可以动态根据cookies或session实现多库切换 22 | return reval; 23 | } 24 | } 25 | public static SqlSugarClient GetInstance() 26 | { 27 | var db = new SqlSugarClient(ConnectionString); 28 | db.IsEnableLogEvent = true;//启用日志事件 29 | db.LogEventStarting = (sql, par) => { Console.WriteLine(sql + " " + par+"\r\n"); }; 30 | return db; 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /NewTest/DataBase/AREA.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Oracle Data Transfer 3 | Oracle Client Version : 11.2.0.4.0 4 | 5 | Source Server : oracle 6 | Source Server Version : 110200 7 | Source Host : localhost:1521 8 | Source Schema : SYSTEM 9 | 10 | Target Server Type : ORACLE 11 | Target Server Version : 110200 12 | File Encoding : 65001 13 | 14 | Date: 2016-10-27 04:22:44 15 | */ 16 | 17 | 18 | -- ---------------------------- 19 | -- Table structure for AREA 20 | -- ---------------------------- 21 | DROP TABLE "SYSTEM"."AREA"; 22 | CREATE TABLE "SYSTEM"."AREA" ( 23 | "ID" NUMBER(9) NOT NULL , 24 | "NAME" VARCHAR2(255 BYTE) NULL 25 | ) 26 | LOGGING 27 | NOCOMPRESS 28 | NOCACHE 29 | 30 | ; 31 | 32 | -- ---------------------------- 33 | -- Records of AREA 34 | -- ---------------------------- 35 | INSERT INTO "SYSTEM"."AREA" VALUES ('1', '上海'); 36 | INSERT INTO "SYSTEM"."AREA" VALUES ('2', '北京'); 37 | INSERT INTO "SYSTEM"."AREA" VALUES ('3', '南通'); 38 | INSERT INTO "SYSTEM"."AREA" VALUES ('4', '日本'); 39 | 40 | -- ---------------------------- 41 | -- Indexes structure for table AREA 42 | -- ---------------------------- 43 | 44 | -- ---------------------------- 45 | -- Checks structure for table AREA 46 | -- ---------------------------- 47 | ALTER TABLE "SYSTEM"."AREA" ADD CHECK ("ID" IS NOT NULL); 48 | 49 | -- ---------------------------- 50 | -- Primary Key structure for table AREA 51 | -- ---------------------------- 52 | ALTER TABLE "SYSTEM"."AREA" ADD PRIMARY KEY ("ID"); 53 | -------------------------------------------------------------------------------- /NewTest/DataBase/INSERTTEST.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Oracle Data Transfer 3 | Oracle Client Version : 11.2.0.4.0 4 | 5 | Source Server : oracle 6 | Source Server Version : 110200 7 | Source Host : localhost:1521 8 | Source Schema : SYSTEM 9 | 10 | Target Server Type : ORACLE 11 | Target Server Version : 110200 12 | File Encoding : 65001 13 | 14 | Date: 2016-10-27 04:21:53 15 | */ 16 | 17 | 18 | -- ---------------------------- 19 | -- Table structure for INSERTTEST 20 | -- ---------------------------- 21 | DROP TABLE "SYSTEM"."INSERTTEST"; 22 | CREATE TABLE "SYSTEM"."INSERTTEST" ( 23 | "ID" NUMBER(9) NOT NULL , 24 | "V1" VARCHAR2(255 BYTE) NULL , 25 | "V2" VARCHAR2(255 BYTE) NULL , 26 | "V3" VARCHAR2(255 BYTE) NULL , 27 | "INT1" NUMBER(9) NULL , 28 | "D1" DATE NULL , 29 | "TXT" VARCHAR2(1255 BYTE) NULL 30 | ) 31 | LOGGING 32 | NOCOMPRESS 33 | NOCACHE 34 | 35 | ; 36 | 37 | -- ---------------------------- 38 | -- Records of INSERTTEST 39 | -- ---------------------------- 40 | INSERT INTO "SYSTEM"."INSERTTEST" VALUES ('1', '131', '13', '13', '312', TO_DATE('2016-10-12 02:33:26', 'YYYY-MM-DD HH24:MI:SS'), '21312'); 41 | 42 | -- ---------------------------- 43 | -- Indexes structure for table INSERTTEST 44 | -- ---------------------------- 45 | 46 | -- ---------------------------- 47 | -- Checks structure for table INSERTTEST 48 | -- ---------------------------- 49 | ALTER TABLE "SYSTEM"."INSERTTEST" ADD CHECK ("ID" IS NOT NULL); 50 | 51 | -- ---------------------------- 52 | -- Primary Key structure for table INSERTTEST 53 | -- ---------------------------- 54 | ALTER TABLE "SYSTEM"."INSERTTEST" ADD PRIMARY KEY ("ID"); 55 | -------------------------------------------------------------------------------- /NewTest/DataBase/SCHOOL.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Oracle Data Transfer 3 | Oracle Client Version : 11.2.0.4.0 4 | 5 | Source Server : oracle 6 | Source Server Version : 110200 7 | Source Host : localhost:1521 8 | Source Schema : SYSTEM 9 | 10 | Target Server Type : ORACLE 11 | Target Server Version : 110200 12 | File Encoding : 65001 13 | 14 | Date: 2016-10-27 04:23:07 15 | */ 16 | 17 | 18 | -- ---------------------------- 19 | -- Table structure for SCHOOL 20 | -- ---------------------------- 21 | DROP TABLE "SYSTEM"."SCHOOL"; 22 | CREATE TABLE "SYSTEM"."SCHOOL" ( 23 | "ID" NUMBER(9) NOT NULL , 24 | "NAME" VARCHAR2(255 BYTE) NULL , 25 | "AREAID" NUMBER(9) NULL 26 | ) 27 | LOGGING 28 | NOCOMPRESS 29 | NOCACHE 30 | 31 | ; 32 | 33 | -- ---------------------------- 34 | -- Records of SCHOOL 35 | -- ---------------------------- 36 | INSERT INTO "SYSTEM"."SCHOOL" VALUES ('1', '蓝翔2', '1'); 37 | INSERT INTO "SYSTEM"."SCHOOL" VALUES ('2', '蓝翔2', '2'); 38 | INSERT INTO "SYSTEM"."SCHOOL" VALUES ('3', '蓝翔2', '3'); 39 | INSERT INTO "SYSTEM"."SCHOOL" VALUES ('4', '黑马', '2'); 40 | INSERT INTO "SYSTEM"."SCHOOL" VALUES ('5', '幼儿园', '3'); 41 | INSERT INTO "SYSTEM"."SCHOOL" VALUES ('6', '蓝翔', '1'); 42 | 43 | -- ---------------------------- 44 | -- Indexes structure for table SCHOOL 45 | -- ---------------------------- 46 | 47 | -- ---------------------------- 48 | -- Checks structure for table SCHOOL 49 | -- ---------------------------- 50 | ALTER TABLE "SYSTEM"."SCHOOL" ADD CHECK ("ID" IS NOT NULL); 51 | 52 | -- ---------------------------- 53 | -- Primary Key structure for table SCHOOL 54 | -- ---------------------------- 55 | ALTER TABLE "SYSTEM"."SCHOOL" ADD PRIMARY KEY ("ID"); 56 | -------------------------------------------------------------------------------- /NewTest/DataBase/SEQ.sql: -------------------------------------------------------------------------------- 1 |  2 | CREATE SEQUENCE SEQ; 3 | Alter Sequence SEQ Increment By 1000; 4 | Select SEQ.NextVal From Dual; 5 | Alter Sequence SEQ Increment By 1; 6 | 7 | 8 | CREATE SEQUENCE SEQ2; 9 | Alter Sequence SEQ2 Increment By 1000; 10 | Select SEQ2.NextVal From Dual; 11 | Alter Sequence SEQ2 Increment By 1; 12 | 13 | 14 | CREATE SEQUENCE SEQ3; 15 | Alter Sequence SEQ3 Increment By 1000; 16 | Select SEQ3.NextVal From Dual; 17 | Alter Sequence SEQ3 Increment By 1; 18 | 19 | 20 | CREATE SEQUENCE SEQ4; 21 | Alter Sequence SEQ4 Increment By 1000; 22 | Select SEQ4.NextVal From Dual; 23 | Alter Sequence SEQ4 Increment By 1; 24 | 25 | -------------------------------------------------------------------------------- /NewTest/DataBase/STUDENT.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Oracle Data Transfer 3 | Oracle Client Version : 11.2.0.4.0 4 | 5 | Source Server : oracle 6 | Source Server Version : 110200 7 | Source Host : localhost:1521 8 | Source Schema : SYSTEM 9 | 10 | Target Server Type : ORACLE 11 | Target Server Version : 110200 12 | File Encoding : 65001 13 | 14 | Date: 2016-10-27 04:21:44 15 | */ 16 | 17 | 18 | -- ---------------------------- 19 | -- Table structure for STUDENT 20 | -- ---------------------------- 21 | DROP TABLE "SYSTEM"."STUDENT"; 22 | CREATE TABLE "SYSTEM"."STUDENT" ( 23 | "ID" NUMBER(9) NOT NULL , 24 | "NAME" VARCHAR2(255 BYTE) NULL , 25 | "SCH_ID" NUMBER(9) NULL , 26 | "ISOK" NUMBER(1) NULL , 27 | "SEX" VARCHAR2(255 BYTE) NULL 28 | ) 29 | LOGGING 30 | NOCOMPRESS 31 | NOCACHE 32 | 33 | ; 34 | 35 | -- ---------------------------- 36 | -- Records of STUDENT 37 | -- ---------------------------- 38 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('1', '小杰', '1', '1', 'boy'); 39 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('2', '小明', '2', '0', 'grid'); 40 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('3', '张三', '3', '1', 'boy'); 41 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('4', '李四', '2', '0', 'grid'); 42 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('5', '王五', '3', '1', 'boy'); 43 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('6', '小姐', '1', '0', 'grid'); 44 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('7', '小捷', '3', '1', 'grid'); 45 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('8', '小J', '1', '1', 'grid'); 46 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('9', '张1259179525', null, null, null); 47 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('10', '张1910219845', '1', '1', null); 48 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('11', '张1910219845', '2', '0', null); 49 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('12', '张181892235', '1', '1', null); 50 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('13', '张181892235', '2', '0', null); 51 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('14', '张1790732237', null, null, null); 52 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('15', '张832932555', '1', '1', null); 53 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('16', '张832932555', '2', '0', null); 54 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('17', '张218439509', null, null, null); 55 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('18', '张1288635866', '1', '1', null); 56 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('19', '张1288635866', '2', '0', null); 57 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('20', '张682208075', '1', '1', null); 58 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('21', '张682208075', '2', '0', null); 59 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('22', '张1520520149', null, null, null); 60 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('23', '张562720467', '1', '1', null); 61 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('24', '张562720467', '2', '0', null); 62 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('25', '张1597095746', null, null, null); 63 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('26', '张1896764175', '1', '1', null); 64 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('27', '张1896764175', '2', '0', null); 65 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('28', '张168436565', '1', '1', null); 66 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('29', '张168436565', '2', '0', null); 67 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('30', '张751692739', null, null, null); 68 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('31', '张1170848776', '1', '1', null); 69 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('32', '张1170848776', '2', '0', null); 70 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('33', '张131992610', null, null, null); 71 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('34', '张1553560858', '1', '1', null); 72 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('35', '张1553560858', '2', '0', null); 73 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('36', '张1972716895', '1', '1', null); 74 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('37', '张1972716895', '2', '0', null); 75 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('38', '张1434073250', null, null, null); 76 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('39', '张827645459', '1', '1', null); 77 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('40', '张827645459', '2', '0', null); 78 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('41', '张1730013006', null, null, null); 79 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('42', '张2029681435', '1', '1', null); 80 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('43', '张2029681435', '2', '0', null); 81 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('44', '张301353825', '1', '1', null); 82 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('45', '张301353825', '2', '0', null); 83 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('46', '张884609999', null, null, null); 84 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('47', '张1303766036', '1', '1', null); 85 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('48', '张1303766036', '2', '0', null); 86 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('49', '张1821927872', null, null, null); 87 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('50', '张325484545', '1', '1', null); 88 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('51', '张325484545', '2', '0', null); 89 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('52', '张1515168510', '1', '1', null); 90 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('53', '张1515168510', '2', '0', null); 91 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('54', '张205996937', null, null, null); 92 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('55', '张1395680902', '1', '1', null); 93 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('56', '张1395680902', '2', '0', null); 94 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('57', '小妹1386613097', '2', '0', 'gril'); 95 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('58', '张1997341155', '1', '1', null); 96 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('59', '张1997341155', '2', '0', null); 97 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('60', '小子1386613097', '3', '1', 'boy'); 98 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('61', '张2065125301', '2', '0', null); 99 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('62', '张1877853547', null, null, null); 100 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('63', '张920053865', '1', '1', null); 101 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('64', '张920053865', '2', '0', null); 102 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('65', '张624853314', null, null, null); 103 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('66', '张259467670', '1', '1', null); 104 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('67', '张259467670', '2', '0', null); 105 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('68', '张1800523526', '1', '1', null); 106 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('69', '张1800523526', '2', '0', null); 107 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('70', '张1261879881', null, null, null); 108 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('71', '张1681035918', '1', '1', null); 109 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('72', '张1681035918', '2', '0', null); 110 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('73', '张847114170', null, null, null); 111 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('74', '张1910387707', '1', '1', null); 112 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('75', '张1910387707', '2', '0', null); 113 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('76', '张952588025', '1', '1', null); 114 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('77', '张952588025', '2', '0', null); 115 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('78', '张413944380', null, null, null); 116 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('79', '张833100417', '1', '1', null); 117 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('80', '张833100417', '2', '0', null); 118 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('81', '张21154107', null, null, null); 119 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('82', '张1442722355', '1', '1', null); 120 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('83', '张1442722355', '2', '0', null); 121 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('84', '张1861878392', '1', '1', null); 122 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('85', '张1861878392', '2', '0', null); 123 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('86', '张1674606638', null, null, null); 124 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('87', '张716806956', '1', '1', null); 125 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('88', '张716806956', '2', '0', null); 126 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('89', '张2143413923', null, null, null); 127 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('90', '张1417498524', '1', '1', null); 128 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('91', '张1417498524', '2', '0', null); 129 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('92', '张1836654561', '1', '1', null); 130 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('93', '张1836654561', '2', '0', null); 131 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('94', '张1649382807', null, null, null); 132 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('95', '张691583125', '1', '1', null); 133 | INSERT INTO "SYSTEM"."STUDENT" VALUES ('96', '张691583125', '2', '0', null); 134 | 135 | -- ---------------------------- 136 | -- Indexes structure for table STUDENT 137 | -- ---------------------------- 138 | 139 | -- ---------------------------- 140 | -- Checks structure for table STUDENT 141 | -- ---------------------------- 142 | ALTER TABLE "SYSTEM"."STUDENT" ADD CHECK ("ID" IS NOT NULL); 143 | 144 | -- ---------------------------- 145 | -- Primary Key structure for table STUDENT 146 | -- ---------------------------- 147 | ALTER TABLE "SYSTEM"."STUDENT" ADD PRIMARY KEY ("ID"); 148 | -------------------------------------------------------------------------------- /NewTest/DataBase/SUBJECT.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Oracle Data Transfer 3 | Oracle Client Version : 11.2.0.4.0 4 | 5 | Source Server : oracle 6 | Source Server Version : 110200 7 | Source Host : localhost:1521 8 | Source Schema : SYSTEM 9 | 10 | Target Server Type : ORACLE 11 | Target Server Version : 110200 12 | File Encoding : 65001 13 | 14 | Date: 2016-10-27 04:21:38 15 | */ 16 | 17 | 18 | -- ---------------------------- 19 | -- Table structure for SUBJECT 20 | -- ---------------------------- 21 | DROP TABLE "SYSTEM"."SUBJECT"; 22 | CREATE TABLE "SYSTEM"."SUBJECT" ( 23 | "ID" NUMBER(9) NOT NULL , 24 | "NAME" VARCHAR2(255 BYTE) NULL , 25 | "STUDENTID" NUMBER(9) NULL 26 | ) 27 | LOGGING 28 | NOCOMPRESS 29 | NOCACHE 30 | 31 | ; 32 | 33 | -- ---------------------------- 34 | -- Records of SUBJECT 35 | -- ---------------------------- 36 | INSERT INTO "SYSTEM"."SUBJECT" VALUES ('1', '语文', '1'); 37 | INSERT INTO "SYSTEM"."SUBJECT" VALUES ('2', '数学', '2'); 38 | INSERT INTO "SYSTEM"."SUBJECT" VALUES ('3', '.NET', '4'); 39 | INSERT INTO "SYSTEM"."SUBJECT" VALUES ('4', 'JAVA', '5'); 40 | INSERT INTO "SYSTEM"."SUBJECT" VALUES ('5', 'IOS', '6'); 41 | INSERT INTO "SYSTEM"."SUBJECT" VALUES ('6', 'PHP', '7'); 42 | 43 | -- ---------------------------- 44 | -- Indexes structure for table SUBJECT 45 | -- ---------------------------- 46 | 47 | -- ---------------------------- 48 | -- Checks structure for table SUBJECT 49 | -- ---------------------------- 50 | ALTER TABLE "SYSTEM"."SUBJECT" ADD CHECK ("ID" IS NOT NULL); 51 | 52 | -- ---------------------------- 53 | -- Primary Key structure for table SUBJECT 54 | -- ---------------------------- 55 | ALTER TABLE "SYSTEM"."SUBJECT" ADD PRIMARY KEY ("ID"); 56 | -------------------------------------------------------------------------------- /NewTest/DataBase/TESTUPDATECOLUMNS.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Oracle Data Transfer 3 | Oracle Client Version : 11.2.0.4.0 4 | 5 | Source Server : oracle 6 | Source Server Version : 110200 7 | Source Host : localhost:1521 8 | Source Schema : SYSTEM 9 | 10 | Target Server Type : ORACLE 11 | Target Server Version : 110200 12 | File Encoding : 65001 13 | 14 | Date: 2016-10-27 04:21:23 15 | */ 16 | 17 | 18 | -- ---------------------------- 19 | -- Table structure for TESTUPDATECOLUMNS 20 | -- ---------------------------- 21 | DROP TABLE "SYSTEM"."TESTUPDATECOLUMNS"; 22 | CREATE TABLE "SYSTEM"."TESTUPDATECOLUMNS" ( 23 | "VGUID" VARCHAR2(50 BYTE) NOT NULL , 24 | "IDENTITYFIELD" VARCHAR2(255 BYTE) NULL , 25 | "NAME" VARCHAR2(255 BYTE) NULL , 26 | "NAME2" VARCHAR2(255 BYTE) NULL , 27 | "CREATETIME" DATE NULL 28 | ) 29 | LOGGING 30 | NOCOMPRESS 31 | NOCACHE 32 | 33 | ; 34 | 35 | -- ---------------------------- 36 | -- Records of TESTUPDATECOLUMNS 37 | -- ---------------------------- 38 | 39 | -- ---------------------------- 40 | -- Indexes structure for table TESTUPDATECOLUMNS 41 | -- ---------------------------- 42 | 43 | -- ---------------------------- 44 | -- Checks structure for table TESTUPDATECOLUMNS 45 | -- ---------------------------- 46 | ALTER TABLE "SYSTEM"."TESTUPDATECOLUMNS" ADD CHECK ("VGUID" IS NOT NULL); 47 | 48 | -- ---------------------------- 49 | -- Primary Key structure for table TESTUPDATECOLUMNS 50 | -- ---------------------------- 51 | ALTER TABLE "SYSTEM"."TESTUPDATECOLUMNS" ADD PRIMARY KEY ("VGUID"); 52 | -------------------------------------------------------------------------------- /NewTest/Demos/Ado.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using Oracle.ManagedDataAccess.Client; 9 | 10 | namespace NewTest.Demos 11 | { 12 | //完美兼容SqlHelper的所有功能 13 | public class Ado:IDemos 14 | { 15 | 16 | public void Init() 17 | { 18 | Console.WriteLine("启动Ado.Init"); 19 | using (var db = SugarDao.GetInstance()) 20 | { 21 | var r1 = db.GetDataTable("select * from student"); 22 | var r2 = db.GetSingle("select * from student where rownum<=1"); 23 | var r3 = db.GetScalar("select count(1) from student"); 24 | var r4 = db.GetReader("select count(1) from student "); 25 | r4.Dispose(); 26 | var r5 = db.GetString("select name from student where rownum<=1"); 27 | var r6 = db.ExecuteCommand("select 1 from dual"); 28 | 29 | 30 | //参数支持 31 | var p1 = db.GetDataTable("select * from student where id=:id", new {id=1 }); 32 | var p2 = db.GetDataTable("select * from student where id=:id", new Dictionary() { { "id", "1" } });//目前只支持 Dictionary和Dictionary 33 | var p3 = db.GetDataTable("select * from student where id=:id", new OracleParameter(":id", 1)); 34 | 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /NewTest/Demos/AttributesMapping.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using OracleSugar; 9 | namespace NewTest.Demos 10 | { 11 | //通过属性的方法设置别名表和别名字段(主键和自添列都无需设置 SQLSUGAR会帮你自动处理) 12 | //注意:【属性映射和 (SetMappingTables、SetMappingColumns)方式映射 2种选其中一,不清楚底层缓存机质不建议同时使用】 13 | public class AttributesMapping : IDemos 14 | { 15 | 16 | public void Init() 17 | { 18 | Console.WriteLine("启动AttributesMapping.Init"); 19 | using (var db = DBManager.GetInstance()) 20 | { 21 | 22 | //查询 23 | var list = db.Queryable() 24 | .Where(it => it.className.Contains("小")).OrderBy(it => it.classSchoolId).Select(it => new V_Student() { id = it.classId, name = it.className }).ToList(); 25 | var list2 = db.Queryable() 26 | .JoinTable((s1, s2) => s1.classSchoolId == s2.classId) 27 | .OrderBy((s1, s2) => s1.classId) 28 | .Select((s1, s2) => new V_Student() { id = s1.classId, name = s1.className, SchoolName = s2.className }).ToList(); 29 | 30 | //添加 31 | TestStudent s = new TestStudent(); 32 | s.className = "属性名"; 33 | s.classSchoolId = 1; 34 | s.classId = new Random().Next(2000, 111111111); 35 | var id = db.Insert(s); 36 | s.classId = id.ObjToInt(); 37 | 38 | s.classId = new Random().Next(2000, 111111111); ; 39 | db.SqlBulkCopy(new List() { s }); 40 | 41 | 42 | //更新 43 | db.Update(s); 44 | db.Update(s, 100); 45 | db.Update(s, it => it.classId == 100); 46 | db.SqlBulkReplace(new List() { s }); 47 | 48 | //删除 49 | db.Delete(it => it.classId == 100); 50 | 51 | //根据实体赋值实体一定要有主键,并且要有值。 52 | db.Delete(new TestStudent() { classId = 200 }); 53 | } 54 | } 55 | 56 | /// 57 | /// 属性只作为初始化映射,SetMappingTables和SetMappingColumns可以覆盖 58 | /// 59 | [SugarMapping(TableName = "Student")] 60 | public class TestStudent 61 | { 62 | 63 | [SugarMapping(ColumnName = "id")] 64 | public int classId { get; set; } 65 | 66 | [SugarMapping(ColumnName = "name")] 67 | public string className { get; set; } 68 | 69 | [SugarMapping(ColumnName = "sch_id")] 70 | public int classSchoolId { get; set; } 71 | 72 | public int isOk { get; set; } 73 | 74 | /// 75 | /// 数据库并没有这一列 76 | /// 77 | public string errorField { get; set; } 78 | } 79 | 80 | 81 | [SugarMapping(TableName = "School")] 82 | public class TestSchool 83 | { 84 | 85 | [SugarMapping(ColumnName = "id")] 86 | public int classId { get; set; } 87 | 88 | [SugarMapping(ColumnName = "name")] 89 | public string className { get; set; } 90 | 91 | public int AreaId = 1; 92 | } 93 | } 94 | 95 | public class DBManager 96 | { 97 | 98 | public static SqlSugarClient GetInstance() 99 | { 100 | var db = new SqlSugarClient(SugarDao.ConnectionString); 101 | db.IsEnableAttributeMapping = true;//启用属性映射 102 | db.IsIgnoreErrorColumns = true;//忽略非数据库列 103 | 104 | 105 | 106 | db.IsEnableLogEvent = true;//启用日志事件 107 | db.LogEventStarting = (sql, par) => { Console.WriteLine(sql + " " + par + "\r\n"); }; 108 | 109 | 110 | return db; 111 | } 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /NewTest/Demos/CreateClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using OracleSugar; 9 | 10 | namespace NewTest.Demos 11 | { 12 | //生成实体函数 13 | public class CreateClass : IDemos 14 | { 15 | 16 | public void Init() 17 | { 18 | Console.WriteLine("启动CreateClass.Init"); 19 | using (var db = SugarDao.GetInstance()) 20 | { 21 | //可以结合别名表,请看别名表的用法 22 | //db.SetMappingTables(mappingTableList); 23 | 24 | db.ClassGenerating.CreateClassFiles(db, ("e:/TestModels"), "Models"); 25 | 26 | 27 | //批量设置别名表,可以方便生成指定格式的实体对象 28 | db.ClassGenerating.ForeachTables(db, tableName => 29 | { 30 | db.AddMappingTable(new KeyValue() { Key = "Rename_"+tableName, Value = tableName }); 31 | }); 32 | 33 | //生成的文件都Rename_开头 34 | db.ClassGenerating.CreateClassFiles(db, ("e:/TestModels"), "Models", 35 | null, 36 | className => { 37 | //生成文件之后的回调 38 | }, tableName => { 39 | //生成文件之前的回调 40 | }); 41 | 42 | 43 | //只生成student和school表的实体 44 | db.ClassGenerating.CreateClassFilesByTableNames(db, "e:/TestModels2", "Models", new string[] { "student", "school" }); 45 | 46 | //根据表名生成class字符串 47 | var str = db.ClassGenerating.TableNameToClass(db, "Student"); 48 | 49 | var dynamicToClassStr = db.ClassGenerating.DynamicToClass(new { id = 1 }, "dyName"); 50 | 51 | //根据SQL语句生成class字符串 52 | var str2 = db.ClassGenerating.SqlToClass(db, "select * from Student where rownum<=1", "student"); 53 | 54 | //改变值(lassTemplate.ItemTemplate=XXXX)可以自定义格式 55 | var tempItem = ClassTemplate.ItemTemplate;//例如可以在生成的实体添加默认构造函数给指定的字段赋默认值或者公司信息等 56 | var temp = ClassTemplate.Template; 57 | var temSummary = ClassTemplate.ClassFieldSummaryTemplate; 58 | 59 | 60 | //设置新格式模板 61 | //主键Guid.New(), 62 | //CreateTime为DateTime.Now 63 | //IsRemove=0 64 | //UpdateTime为DateTime.Now 65 | ClassTemplate.Template = "using System;\r\nusing System.Linq;\r\nusing System.Text;\r\n\r\nnamespace $namespace\r\n{\r\n public class $className\r\n {\r\n public $className() \r\n { \r\n this.CreateTime = DateTime.Now;\r\n this. = 0;\r\n this.UpdateTime=DateTime.Now;\r\n this.$primaryKeyName=Guid.NewGuid().ToString(\"N\").ToUpper();\r\n }\r\n $foreach\r\n }\r\n}\r\n"; 66 | 67 | //新格式的实体字符串 68 | var str3 = db.ClassGenerating.TableNameToClass(db, "Student"); 69 | 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /NewTest/Demos/Delete.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | 9 | namespace NewTest.Demos 10 | { 11 | //删除的例子 12 | public class Delete:IDemos 13 | { 14 | 15 | public void Init() 16 | { 17 | Console.WriteLine("启动Deletes.Init"); 18 | using (var db = SugarDao.GetInstance()) 19 | { 20 | 21 | 22 | //删除根据主键 23 | db.Delete(10); 24 | 25 | //删除根据表达示 26 | db.Delete(it => it.id > 100);//支持it=>array.contains(it.id) 27 | 28 | //主键批量删除 29 | db.Delete(new string[] { "100", "101", "102" }); 30 | 31 | //非主键批量删除 32 | db.Delete(it => it.name, new string[] { "" }); 33 | db.Delete(it => it.id, new int[] { 20, 22 }); 34 | 35 | 36 | //根据实体赋值实体一定要有主键,并且要有值。 37 | db.Delete(new School() { id = 200 }); 38 | 39 | //根据字符串删除 40 | db.Delete("id=:id", new { id = 100 }); 41 | 42 | //假删除 43 | //db.FalseDelete("is_del", 100); 44 | //等同于 update school set is_del=1 where id in(100) 45 | //db.FalseDelete("is_del", it=>it.id==100); 46 | 47 | 48 | } 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /NewTest/Demos/EnumDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using OracleSugar; 9 | 10 | namespace NewTest.Demos 11 | { 12 | //枚举的支持 13 | public class EnumDemo:IDemos 14 | { 15 | 16 | public void Init() 17 | { 18 | Console.WriteLine("启动EnumDemo.Init"); 19 | using (SqlSugarClient db = SugarDao.GetInstance()) 20 | { 21 | var stuList = db.Queryable().ToList(); 22 | db.Insert(new Student() { sch_id = SchoolEnum.北大青鸟 }); 23 | db.Update(new Student() { sch_id = SchoolEnum.it清华, id = 11 }); 24 | var stuList2 = db.Queryable().Where(it => it.sch_id == SchoolEnum.全智).ToList(); 25 | } 26 | } 27 | public class Student 28 | { 29 | 30 | /// 31 | /// 说明:- 32 | /// 默认:- 33 | /// 可空:False 34 | /// 35 | public int id { get; set; } 36 | 37 | /// 38 | /// 说明:- 39 | /// 默认:- 40 | /// 可空:True 41 | /// 42 | public string name { get; set; } 43 | 44 | /// 45 | /// 说明:- 46 | /// 默认:- 47 | /// 可空:False 48 | /// 49 | public SchoolEnum sch_id { get; set; } 50 | 51 | /// 52 | /// 说明:- 53 | /// 默认:- 54 | /// 可空:True 55 | /// 56 | public string sex { get; set; } 57 | 58 | /// 59 | /// 说明:- 60 | /// 默认:- 61 | /// 可空:False 62 | /// 63 | public bool isOk { get; set; } 64 | 65 | } 66 | 67 | public enum SchoolEnum 68 | { 69 | 北大青鸟 = 1, 70 | it清华 = 2, 71 | 全智 = 3 72 | } 73 | } 74 | 75 | 76 | 77 | } 78 | -------------------------------------------------------------------------------- /NewTest/Demos/Filter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using OracleSugar; 9 | 10 | namespace NewTest.Demos 11 | { 12 | //过滤器用法 13 | //使用场合(例如:假删除查询,这个时候就可以设置一个过滤器,不需要每次都 .Where(it=>it.IsDelete==true)) 14 | public class Filter : IDemos 15 | { 16 | 17 | public void Init() 18 | { 19 | Console.WriteLine("启动Filter.Init"); 20 | using (SqlSugarClient db = SugarDaoFilter.GetInstance())//开启数据库连接 21 | { 22 | //设置走哪个过滤器 23 | db.CurrentFilterKey = "role,role2"; //支持多个过滤器以逗号隔开 24 | 25 | //queryable 26 | var list = db.Queryable().ToList(); //通过全局过滤器对需要权限验证的数据进行过滤 27 | //相当于db.Queryable().Where("id=@id",new{id=1}) 28 | 29 | 30 | //sqlable 31 | var list2 = db.Sqlable().From("s").SelectToList("*"); 32 | //同上 33 | 34 | //sqlQuery 35 | var list3 = db.SqlQuery("select * from Student WHERE 1=1"); 36 | //同上 37 | } 38 | } 39 | /// 40 | /// 扩展SqlSugarClient 41 | /// 42 | public class SugarDaoFilter 43 | { 44 | //禁止实例化 45 | private SugarDaoFilter() 46 | { 47 | 48 | } 49 | /// 50 | /// 页面所需要的过滤函数 51 | /// 52 | private static Dictionary> _filterParas = new Dictionary>() 53 | { 54 | { "role",()=>{ 55 | return new KeyValueObj(){ Key=" id=:id" , Value=new{ id=1}}; 56 | } 57 | }, 58 | { "role2",()=>{ 59 | return new KeyValueObj(){ Key=" id>0"}; 60 | } 61 | }, 62 | }; 63 | public static SqlSugarClient GetInstance() 64 | { 65 | string connection = SugarDao.ConnectionString; //这里可以动态根据cookies或session实现多库切换 66 | var db = new SqlSugarClient(connection); 67 | db.SetFilterItems(_filterParas); 68 | 69 | db.IsEnableLogEvent = true;//启用日志事件 70 | db.LogEventStarting = (sql, par) => { Console.WriteLine(sql + " " + par + "\r\n"); }; 71 | return db; 72 | } 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /NewTest/Demos/Filter2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using OracleSugar; 9 | 10 | namespace NewTest.Demos 11 | { 12 | //行过滤加列过滤 13 | //权限管理的最佳设计 14 | public class Filter2 : IDemos 15 | { 16 | 17 | public void Init() 18 | { 19 | Console.WriteLine("启动Filter2.Init"); 20 | using (SqlSugarClient db = SugarDaoFilter.GetInstance())//开启数据库连接 21 | { 22 | //设置走哪个过滤器 23 | db.CurrentFilterKey = "role1"; 24 | //queryable 25 | var list = db.Queryable().ToJson(); //where id=1 , 可以查看id和name 26 | 27 | 28 | //设置走哪个过滤器 29 | db.CurrentFilterKey = "role2"; 30 | //queryable 31 | var list2 = db.Queryable().ToJson(); //where id=2 , 可以查看name 32 | 33 | } 34 | } 35 | /// 36 | /// 扩展SqlSugarClient 37 | /// 38 | public class SugarDaoFilter 39 | { 40 | //禁止实例化 41 | private SugarDaoFilter() 42 | { 43 | 44 | } 45 | /// 46 | /// 页面所需要的过滤行 47 | /// 48 | private static Dictionary> _filterRos = new Dictionary>() 49 | { 50 | { "role1",()=>{ 51 | return new KeyValueObj(){ Key=" id=:id" , Value=new{ id=1}}; 52 | } 53 | }, 54 | { "role2",()=>{ 55 | return new KeyValueObj() { Key = " id=:id", Value = new { id = 2 } }; 56 | } 57 | }, 58 | }; 59 | /// 60 | /// 页面所需要的过滤列 61 | /// 62 | private static Dictionary> _filterColumns = new Dictionary>() 63 | { 64 | { "role1",new List(){"id","name"} 65 | }, 66 | { "role2",new List(){"name"} 67 | }, 68 | }; 69 | public static SqlSugarClient GetInstance() 70 | { 71 | string connection = SugarDao.ConnectionString; //这里可以动态根据cookies或session实现多库切换 72 | var db = new SqlSugarClient(connection); 73 | 74 | //支持sqlable和queryable 75 | db.SetFilterItems(_filterRos); 76 | 77 | //列过滤只支持queryable 78 | db.SetFilterItems(_filterColumns); 79 | 80 | 81 | db.IsEnableLogEvent = true;//启用日志事件 82 | db.LogEventStarting = (sql, par) => { Console.WriteLine(sql + " " + par + "\r\n"); }; 83 | return db; 84 | } 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /NewTest/Demos/IDemos.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | namespace NewTest.Demos 6 | { 7 | public interface IDemos 8 | { 9 | void Init(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /NewTest/Demos/IgnoreErrorColumns.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using OracleSugar; 9 | 10 | namespace NewTest.Demos 11 | { 12 | //排除错误列 13 | public class IgnoreErrorColumns : IDemos 14 | { 15 | 16 | public void Init() 17 | { 18 | Console.WriteLine("启动IgnoreErrorColumns.Init"); 19 | using (var db = SugarDao.GetInstance()) 20 | { 21 | db.IsIgnoreErrorColumns = true; 22 | 23 | //Student表并没有 AreaName 24 | var id= db.Insert(new STUDENT() { name = "张三", AreaName = "北大" }); 25 | 26 | 27 | db.Update(new STUDENT() { id=id.ObjToInt() ,name = "张三2", AreaName = "北大" }); 28 | } 29 | } 30 | public class STUDENT { 31 | /// 32 | /// Desc:- 33 | /// Default:- 34 | /// Nullable:False 35 | /// 36 | public int id { get; set; } 37 | 38 | /// 39 | /// Desc:- 40 | /// Default:- 41 | /// Nullable:True 42 | /// 43 | public string name { get; set; } 44 | 45 | /// 46 | /// Desc:学校ID 47 | /// Default:- 48 | /// Nullable:True 49 | /// 50 | public int? sch_id { get; set; } 51 | 52 | /// 53 | /// Desc:- 54 | /// Default:- 55 | /// Nullable:True 56 | /// 57 | public string sex { get; set; } 58 | 59 | /// 60 | /// Desc:- 61 | /// Default:- 62 | /// Nullable:True 63 | /// 64 | public Boolean? isOk { get; set; } 65 | 66 | public string SchoolName { get; set; } 67 | 68 | public string AreaName { get; set; } 69 | 70 | public string SubjectName { get; set; } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /NewTest/Demos/InitConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using OracleSugar; 9 | 10 | namespace NewTest.Demos 11 | { 12 | /// 13 | ///如何避免初始化SqlSugarClient时,参数赋值引起的性能的浪费 14 | /// 15 | public class InitConfig : IDemos 16 | { 17 | 18 | public void Init() 19 | { 20 | Console.WriteLine("启动InitConfig.Init"); 21 | using (SqlSugarClient db = SugarPocoDao.GetInstance())//开启数据库连接 22 | { 23 | 24 | } 25 | } 26 | 27 | /// 28 | /// SqlSugarClient初始化全配置类 29 | /// 30 | public class DaoInitConfig 31 | { 32 | //别名列 33 | public static List columnMappingList= new List() { 34 | new KeyValue(){ Key="entityId", Value="tableId"}, 35 | new KeyValue(){ Key="entityName", Value="tableName"} 36 | }; 37 | 38 | //别名表 39 | public static List tableMappingList = null; 40 | 41 | 42 | //流水号 43 | public static List serialNumber = new List(){ 44 | new PubModel.SerialNumber(){TableName="Student", FieldName="name", GetNumFunc=()=>{ return "stud-"+DateTime.Now.ToString("yyyy-MM-dd");}}, 45 | new PubModel.SerialNumber(){TableName="School", FieldName="name", GetNumFuncWithDb=db=>{ return "ch-"+DateTime.Now.ToString("syyyy-MM-dd"); }} 46 | }; 47 | 48 | //自动排除非数据库列 49 | public static bool IsIgnoreErrorColumns=true; 50 | 51 | } 52 | 53 | 54 | /// 55 | /// 扩展SqlSugarClient 56 | /// 57 | public class SugarPocoDao 58 | { 59 | //禁止实例化 60 | private SugarPocoDao() 61 | { 62 | 63 | } 64 | 65 | public static SqlSugarClient GetInstance() 66 | { 67 | 68 | string connection = SugarDao.ConnectionString; //这里可以动态根据cookies或session实现多库切换 69 | var db = new SqlSugarClient(connection); 70 | 71 | /**这种写法只给db对象添加了4个指向地址(DaoInitConfig变量都为静态对象),并非指向内容,指向内容初始化后存储在内存当中,所以性能就不用说了 **/ 72 | 73 | db.SetMappingTables(GetMappingTables(db));//设置别名表 74 | 75 | db.SetMappingColumns(DaoInitConfig.columnMappingList);//设置别名列 76 | 77 | db.SetSerialNumber(DaoInitConfig.serialNumber);//设置流水号 78 | 79 | db.IsIgnoreErrorColumns = DaoInitConfig.IsIgnoreErrorColumns; //自动排除非数据库列 80 | 81 | 82 | return db; 83 | } 84 | 85 | /// 86 | /// 批量设置别名表 87 | /// 88 | /// 89 | /// 90 | private static List GetMappingTables(SqlSugarClient db) 91 | { 92 | if (DaoInitConfig.tableMappingList == null) 93 | { 94 | DaoInitConfig.tableMappingList = new List(); 95 | db.ClassGenerating.ForeachTables(db, name =>//内置遍历表名和视图名函数 96 | { 97 | //给所有表名加dbo. 98 | DaoInitConfig.tableMappingList.Add(new KeyValue() { Key = name, Value ="dbo."+name }); 99 | 100 | //动态获取sechma 101 | // DaoInitConfig.tableMappingList.Add(new KeyValue() { Key = name, Value = db.ClassGenerating.GetTableNameWithSchema(db,name) }); 102 | }); 103 | } 104 | return DaoInitConfig.tableMappingList; 105 | } 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /NewTest/Demos/Insert.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using OracleSugar; 9 | namespace NewTest.Demos 10 | { 11 | //插入 12 | public class Insert : IDemos 13 | { 14 | 15 | public void Init() 16 | { 17 | OracleConfig.SequenceMapping = new List() 18 | { 19 | new SequenceModel(){ ColumnName="ID", TableName="STUDENT", Value="SEQ"} 20 | }; 21 | 22 | Console.WriteLine("启动Inset.Init"); 23 | using (var db = SugarDao.GetInstance()) 24 | { 25 | 26 | db.Insert(GetInsertItem()); //插入一条记录 (有主键也好,没主键也好,有自增列也好都可以插进去) 27 | 28 | 29 | db.InsertRange(GetInsertList()); //批量插入 支持(别名表等功能) 30 | 31 | 32 | db.SqlBulkCopy(GetInsertList()); //批量插入 适合海量数据插入 33 | 34 | 35 | 36 | //设置不插入列 37 | db.DisableInsertColumns = new string[] { "sex" };//sex列将不会插入值 38 | Student s = new Student() 39 | { 40 | name = "张" + new Random().Next(1, int.MaxValue), 41 | sex = "gril" 42 | }; 43 | 44 | var id = db.Insert(s); //插入 45 | 46 | //查询刚插入的sex是否有值 47 | var sex = db.Queryable().Single(it => it.id == id.ObjToInt()).sex;//无值 48 | var name = db.Queryable().Single(it => it.id == id.ObjToInt()).name;//有值 49 | 50 | 51 | //SqlBulkCopy同样支持不挺入列设置 52 | db.SqlBulkCopy(GetInsertList()); 53 | 54 | //bob类型测试 55 | db.Insert(new Models.Test { Id = new Random().Next(100, 1111111111), NAME="afa", C_BOB = new byte[] { 1, 2 } }); 56 | } 57 | } 58 | 59 | private static List GetInsertList() 60 | { 61 | List list = new List() 62 | { 63 | new Student() 64 | { 65 | name="张"+new Random().Next(1,int.MaxValue), 66 | isOk=true, 67 | sch_id=1 68 | }, 69 | new Student() 70 | { 71 | name="张"+new Random().Next(1,int.MaxValue), 72 | isOk=false, 73 | sch_id=2 74 | } 75 | }; 76 | return list; 77 | } 78 | 79 | private static Student GetInsertItem() 80 | { 81 | Student s = new Student() 82 | { 83 | name = "张" + new Random().Next(1, int.MaxValue) 84 | }; 85 | return s; 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /NewTest/Demos/Log.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using OracleSugar; 9 | 10 | namespace NewTest.Demos 11 | { 12 | //日志记录功能 13 | public class Log : IDemos 14 | { 15 | 16 | public void Init() 17 | { 18 | Console.WriteLine("启动Log.Init"); 19 | using (var db = SugarDemoDao.GetInstance()) 20 | { 21 | 22 | var a1 = db.Queryable().Where(it => it.id == 1).ToList(); 23 | var a2 = db.Queryable().OrderBy(it => it.id).ToList(); 24 | } 25 | } 26 | 27 | public class SugarConfigs 28 | { 29 | public static Action LogEventStarting = (sql, pars) => 30 | { 31 | Console.WriteLine("starting:" + sql + " " + pars); 32 | 33 | using (var db = SugarDemoDao.GetInstance()) 34 | { 35 | //日志记录件事件里面用到数据库操作 IsEnableLogEvent一定要为false否则将引起死循环,并且要新开一个数据实例 像我这样写就没问题。 36 | db.IsEnableLogEvent = false; 37 | db.ExecuteCommand("select 1"); 38 | } 39 | }; 40 | public static Action LogEventCompleted = (sql, pars) => 41 | { 42 | Console.WriteLine("completed:" + sql + " " + pars); 43 | }; 44 | } 45 | 46 | /// 47 | /// SqlSugar 48 | /// 49 | public class SugarDemoDao 50 | { 51 | 52 | public static SqlSugarClient GetInstance() 53 | { 54 | var db = new SqlSugarClient(SugarDao.ConnectionString); 55 | db.IsEnableLogEvent = true;//启用日志事件 56 | db.LogEventStarting = SugarConfigs.LogEventStarting; 57 | db.LogEventCompleted = SugarConfigs.LogEventCompleted; 58 | return db; 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /NewTest/Demos/MappingColumns.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using System.Data.SqlClient; 7 | using OracleSugar; 8 | namespace NewTest.Demos 9 | { 10 | //别名列的功能 11 | public class MappingColumns : IDemos 12 | { 13 | 14 | public void Init() 15 | { 16 | Console.WriteLine("启动MappingColumns.Init"); 17 | 18 | //全局设置 19 | using (var db = SugarFactory.GetInstance()) 20 | { 21 | var list = db.Queryable().Where(it=>it.classId==1).ToList(); 22 | } 23 | } 24 | 25 | public class Student 26 | { 27 | 28 | //id 29 | public int classId { get; set; } 30 | 31 | //name 32 | public string className { get; set; } 33 | 34 | //sch_id 35 | public int classSchoolId { get; set; } 36 | 37 | public int isOk { get; set; } 38 | } 39 | 40 | /// 41 | /// 全局配置别名列(不区分表) 42 | /// 43 | public class SugarConfigs 44 | { 45 | //key实体字段名 value表字段名 ,KEY唯一否则异常 46 | public static List MpList = new List(){ 47 | new KeyValue(){ Key="classId", Value="id"}, 48 | new KeyValue(){ Key="className", Value="name"}, 49 | new KeyValue(){ Key="classSchoolId", Value="sch_id"} 50 | }; 51 | } 52 | 53 | /// 54 | /// SqlSugar实例工厂 55 | /// 56 | public class SugarFactory 57 | { 58 | 59 | //禁止实例化 60 | private SugarFactory() 61 | { 62 | 63 | } 64 | public static SqlSugarClient GetInstance() 65 | { 66 | string connection = SugarDao.ConnectionString; //这里可以动态根据cookies或session实现多库切换 67 | var db = new SqlSugarClient(connection); 68 | //注意:只有启动属性映射才可以使用SetMappingColumns 69 | db.IsEnableAttributeMapping = true; 70 | db.SetMappingColumns(SugarConfigs.MpList);//设置关联列 (引用地址赋值,每次赋值都只是存储一个内存地址) 71 | return db; 72 | } 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /NewTest/Demos/MappingTable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using OracleSugar; 9 | namespace NewTest.Demos 10 | { 11 | //别名表的功能 12 | public class MappingTable : IDemos 13 | { 14 | 15 | public void Init() 16 | { 17 | Console.WriteLine("启动MappingTable.Init"); 18 | 19 | //单个设置 20 | using (var db = SugarDao.GetInstance()) 21 | { 22 | var list = db.Queryable("Student").ToList();//查询的是 select * from student 而我的实体名称为V_Student 23 | } 24 | 25 | 26 | //全局设置 27 | using (var db = SugarFactory.GetInstance()) 28 | { 29 | var list = db.Queryable().ToList();//查询的是 select * from student 而我的实体名称为V_Student 30 | 31 | 32 | db.SetMappingTables(null);//清空 33 | db.AddMappingTable(new KeyValue() { Key="", Value="" }); 34 | } 35 | 36 | 37 | } 38 | 39 | 40 | /// 41 | /// 全局配置类 42 | /// 43 | public class SugarConfigs 44 | { 45 | //key类名 value表名 46 | public static List MpList = new List(){ 47 | new KeyValue(){ Key="FormAttr", Value="Flow_FormAttr"}, 48 | new KeyValue(){ Key="Student3", Value="Student"}, 49 | new KeyValue(){ Key="V_Student", Value="Student"} 50 | }; 51 | } 52 | 53 | /// 54 | /// SqlSugar实例工厂 55 | /// 56 | public class SugarFactory 57 | { 58 | 59 | //禁止实例化 60 | private SugarFactory() 61 | { 62 | 63 | } 64 | public static SqlSugarClient GetInstance() 65 | { 66 | string connection = SugarDao.ConnectionString; //这里可以动态根据cookies或session实现多库切换 67 | var db = new SqlSugarClient(connection); 68 | 69 | db.SetMappingTables(SugarConfigs.MpList);//设置关联表 (引用地址赋值,每次赋值都只是存储一个内存地址) 70 | 71 | 72 | 73 | //批量设置别名表 74 | //db.ClassGenerating.ForeachTables(db, tableName => 75 | //{ 76 | // db.AddMappingTable(new KeyValue() { Key = tableName.Replace("bbs.",""), Value = tableName }); //key实体名,value表名 77 | //}); 78 | 79 | 80 | return db; 81 | } 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /NewTest/Demos/PubMethod.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using OracleSugar; 9 | namespace NewTest.Demos 10 | { 11 | //公用函数 12 | public class PubMethod:IDemos 13 | { 14 | 15 | public void Init() 16 | { 17 | Console.WriteLine("启动PubMethod.Init"); 18 | using (var db = SugarDao.GetInstance()) 19 | { 20 | //ToJoinSqlInVal 21 | var par = new string[] { "a", "c", "3" }; 22 | var ids = par.ToJoinSqlInVal();//将数组转成 'a','c','3' 有防SQL注入处理 23 | 24 | 25 | //ToSqlValue 26 | try 27 | { 28 | var par2 = "a'"; 29 | var newpar2 = par2.ToSqlValue();//对字符串防注入处理 30 | } 31 | catch (Exception ex) 32 | { 33 | 34 | Console.WriteLine(ex.Message); 35 | } 36 | 37 | //SqlLikeWordEncode 处理LIKE特殊字符 38 | var likestr = SqlSugarTool.SqlLikeWordEncode("[%%%"); 39 | 40 | 41 | //GetParameterArray 获取页面参数所有参数的键和值 42 | //var pars = SqlSugarTool.GetParameterArray(); //web中使用 43 | 44 | 45 | //将匿名对象转成SqlParameter 46 | var par3 = SqlSugarTool.GetParameters(new { id = 1 }); 47 | 48 | 49 | //用于生成多语言的视图 50 | //LanguageHelper.UpdateView() 51 | 52 | } 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /NewTest/Demos/SerialNumber.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using OracleSugar; 9 | 10 | namespace NewTest.Demos 11 | { 12 | //流水号的功能 13 | public class SerialNumber : IDemos 14 | { 15 | 16 | public void Init() 17 | { 18 | Console.WriteLine("启动SerialNumber.Init"); 19 | using (SqlSugarClient db = SugarFactory.GetInstance())//开启数据库连接 20 | { 21 | var dientityValue = db.Insert(new Student() { }); 22 | var name = db.Queryable().Single(it => it.id == dientityValue.ObjToInt()).name; 23 | Console.WriteLine(name); 24 | 25 | var dientityValue2 = db.Insert(new School() { }); 26 | var name2 = db.Queryable().Single(it => it.id == dientityValue2.ObjToInt()).name; 27 | Console.WriteLine(name2); ; 28 | } 29 | } 30 | 31 | /// 32 | /// 全局配置类 33 | /// 34 | public class SugarConfigs 35 | { 36 | public static List NumList = new List(){ 37 | new PubModel.SerialNumber(){TableName="Student", FieldName="name", GetNumFunc=()=>{ //GetNumFunc在没有事中使用 38 | return "stud-"+DateTime.Now.ToString("yyyy-MM-dd"); 39 | }}, 40 | new PubModel.SerialNumber(){TableName="School", FieldName="name", GetNumFuncWithDb=db=>{ //事务中请使用GetNumFuncWithDb保证同一个DB对象,不然会出现死锁 41 | return "ch-"+DateTime.Now.ToString("syyyy-MM-dd"); 42 | }} 43 | }; 44 | } 45 | 46 | /// 47 | /// SqlSugar实例工厂 48 | /// 49 | public class SugarFactory 50 | { 51 | 52 | //禁止实例化 53 | private SugarFactory() 54 | { 55 | 56 | } 57 | public static SqlSugarClient GetInstance() 58 | { 59 | string connection = SugarDao.ConnectionString; //这里可以动态根据cookies或session实现多库切换 60 | var db = new SqlSugarClient(connection); 61 | db.SetSerialNumber(SugarConfigs.NumList);//设置流水号 62 | return db; 63 | } 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /NewTest/Demos/SerializerDateFormat.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using OracleSugar; 9 | 10 | namespace NewTest.Demos 11 | { 12 | //设置ToJson的日期格式 13 | public class SerializerDateFormat : IDemos 14 | { 15 | 16 | public void Init() 17 | { 18 | Console.WriteLine("启动SerializerDateFormat.Init"); 19 | using (SqlSugarClient db = SugarDao.GetInstance()) 20 | { 21 | db.SerializerDateFormat = "yyyy-mm/dd"; 22 | var jsonStr = db.Queryable().OrderBy("id").Take(1).ToJson(); 23 | var jsonStr2 = db.Sqlable().From("t").Where("rownum<=1").SelectToJson("*"); 24 | var jsonStr3 = db.SqlQueryJson("select * from InsertTest where rownum<=1"); 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /NewTest/Demos/Tran.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | using OracleSugar; 9 | 10 | namespace NewTest.Demos 11 | { 12 | //事务 13 | public class Tran : IDemos 14 | { 15 | 16 | public void Init() 17 | { 18 | Console.WriteLine("启动Tran.Init"); 19 | using (SqlSugarClient db = SugarDao.GetInstance())//开启数据库连接 20 | { 21 | db.CommandTimeOut = 30000;//设置超时时间 22 | try 23 | { 24 | db.BeginTran();//开启事务 25 | //db.BeginTran(IsolationLevel.ReadCommitted);+3重载可以设置事世隔离级别 26 | 27 | db.CommitTran();//提交事务 28 | } 29 | catch (Exception) 30 | { 31 | db.RollbackTran();//回滚事务 32 | throw; 33 | } 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /NewTest/Demos/Update.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NewTest.Dao; 6 | using Models; 7 | using System.Data.SqlClient; 8 | 9 | namespace NewTest.Demos 10 | { 11 | //更新 12 | public class Update : IDemos 13 | { 14 | 15 | public void Init() 16 | { 17 | Console.WriteLine("启动Ado.Update"); 18 | using (var db = SugarDao.GetInstance()) 19 | { 20 | 21 | //指定列更新 22 | db.Update(new { name = "蓝翔14" }, it => it.id == 14); //只更新name列 23 | db.Update(new { name = "蓝翔11 23 12", areaId = 2 }, 11, 23, 12); 24 | db.Update(new { name = "蓝翔2" }, new string[] { "11", "21" }); 25 | db.Update(new { name = "蓝翔2" }, it => it.id == 100); 26 | var array=new int[]{1,2,3}; 27 | db.Update(new { name = "蓝翔2" }, it => array.Contains(it.id));// id in 1,2,3 28 | 29 | 30 | //支持字典更新,适合动态权限 31 | var dic = new Dictionary(); 32 | dic.Add("name", "第十三条"); 33 | dic.Add("areaId", "1"); 34 | db.Update(dic, 13); 35 | 36 | 37 | //整个实体更新 38 | db.Update(new School { id = 16, name = "蓝翔16", AreaId = 1 }); 39 | db.Update(new School { id = 12, name = "蓝翔12", AreaId = 2 }, it => it.id == 18); 40 | db.Update(new School() { id = 11, name = "青鸟11" }); 41 | 42 | //设置不更新列 43 | db.DisableUpdateColumns = new string[] { "CreateTime" };//设置CreateTime不更新 44 | 45 | TestUpdateColumns updObj = new TestUpdateColumns() 46 | { 47 | VGUID = Guid.Parse("542b5a27-6984-47c7-a8ee-359e483c8470"), 48 | Name = "xx", 49 | Name2 = "xx2", 50 | IdentityField = 0, 51 | CreateTime = null 52 | }; 53 | 54 | //CreateTime将不会被更新 55 | db.Update(updObj); 56 | //以前实现这种更新需要用指定列的方式实现,现在就简单多了。 57 | 58 | 59 | 60 | //批量更新 数据量小时建议使用 61 | var updateResult = db.UpdateRange(GetUpdateList()); 62 | 63 | //批量更新 数据量大时建议使用 64 | var updateResult2 = db.SqlBulkReplace(GetUpdateList2()); 65 | 66 | 67 | //bob类型测试 68 | db.Update(new Models.Test { Id = 10, NAME = "afa", C_BOB = new byte[] { 1, 2 } }); 69 | } 70 | } 71 | 72 | 73 | 74 | private static List GetUpdateList() 75 | { 76 | List list = new List() 77 | { 78 | new Student() 79 | { 80 | id=1001, 81 | name="1张10011"+new Random().Next(1,int.MaxValue) 82 | }, 83 | new Student() 84 | { 85 | id=1002, 86 | name="1张10021"+new Random().Next(1,int.MaxValue) 87 | } 88 | }; 89 | return list; 90 | } 91 | private static List GetUpdateList2() 92 | { 93 | List list = new List() 94 | { 95 | new Student() 96 | { 97 | id=57, 98 | name="小妹"+new Random().Next(1,int.MaxValue), 99 | isOk=false, 100 | sch_id=2, 101 | sex="gril" 102 | }, 103 | new Student() 104 | { 105 | id=60, 106 | name="小子"+new Random().Next(1,int.MaxValue), 107 | isOk=true, 108 | sch_id=3, 109 | sex="boy" 110 | } 111 | }; 112 | return list; 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /NewTest/Models/Area.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Text; 4 | 5 | namespace Models 6 | { 7 | public class Area 8 | { 9 | 10 | /// 11 | /// Desc:- 12 | /// Default:- 13 | /// Nullable:False 14 | /// 15 | public int id {get;set;} 16 | 17 | /// 18 | /// Desc:- 19 | /// Default:- 20 | /// Nullable:True 21 | /// 22 | public string name {get;set;} 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /NewTest/Models/InsertTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Text; 4 | 5 | namespace Models 6 | { 7 | public class InsertTest 8 | { 9 | 10 | /// 11 | /// Desc:- 12 | /// Default:- 13 | /// Nullable:False 14 | /// 15 | public int id {get;set;} 16 | 17 | /// 18 | /// Desc:- 19 | /// Default:- 20 | /// Nullable:True 21 | /// 22 | public string v1 {get;set;} 23 | 24 | /// 25 | /// Desc:- 26 | /// Default:- 27 | /// Nullable:True 28 | /// 29 | public string v2 {get;set;} 30 | 31 | /// 32 | /// Desc:- 33 | /// Default:- 34 | /// Nullable:True 35 | /// 36 | public string v3 {get;set;} 37 | 38 | /// 39 | /// Desc:- 40 | /// Default:- 41 | /// Nullable:True 42 | /// 43 | public int? int1 {get;set;} 44 | 45 | /// 46 | /// Desc:- 47 | /// Default:- 48 | /// Nullable:True 49 | /// 50 | public DateTime? d1 {get;set;} 51 | 52 | /// 53 | /// Desc:- 54 | /// Default:- 55 | /// Nullable:True 56 | /// 57 | public string txt {get;set;} 58 | 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /NewTest/Models/LanguageTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Text; 4 | 5 | namespace Models 6 | { 7 | public class LanguageTest 8 | { 9 | 10 | /// 11 | /// Desc:- 12 | /// Default:- 13 | /// Nullable:False 14 | /// 15 | public int Id {get;set;} 16 | 17 | /// 18 | /// Desc:- 19 | /// Default:- 20 | /// Nullable:True 21 | /// 22 | public int? LanguageId {get;set;} 23 | 24 | /// 25 | /// Desc:- 26 | /// Default:- 27 | /// Nullable:True 28 | /// 29 | public string Name {get;set;} 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /NewTest/Models/School.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Text; 4 | 5 | namespace Models 6 | { 7 | public class School 8 | { 9 | 10 | /// 11 | /// Desc:地域ID,关联area表 12 | /// Default:- 13 | /// Nullable:False 14 | /// 15 | public int id {get;set;} 16 | 17 | /// 18 | /// Desc:- 19 | /// Default:- 20 | /// Nullable:True 21 | /// 22 | public string name {get;set;} 23 | 24 | /// 25 | /// Desc:- 26 | /// Default:- 27 | /// Nullable:True 28 | /// 29 | public int? AreaId {get;set;} 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /NewTest/Models/Student.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Text; 4 | 5 | namespace Models 6 | { 7 | public class Student 8 | { 9 | 10 | /// 11 | /// Desc:- 12 | /// Default:- 13 | /// Nullable:False 14 | /// 15 | public int id {get;set;} 16 | 17 | /// 18 | /// Desc:- 19 | /// Default:- 20 | /// Nullable:True 21 | /// 22 | public string name {get;set;} 23 | 24 | /// 25 | /// Desc:学校ID 26 | /// Default:- 27 | /// Nullable:True 28 | /// 29 | public int? sch_id {get;set;} 30 | 31 | /// 32 | /// Desc:- 33 | /// Default:- 34 | /// Nullable:True 35 | /// 36 | public string sex {get;set;} 37 | 38 | /// 39 | /// Desc:- 40 | /// Default:- 41 | /// Nullable:True 42 | /// 43 | public Boolean? isOk {get;set;} 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /NewTest/Models/Student2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Text; 4 | 5 | namespace Models 6 | { 7 | public class Student2 8 | { 9 | 10 | /// 11 | /// Desc:- 12 | /// Default:- 13 | /// Nullable:False 14 | /// 15 | public int id { get; set; } 16 | 17 | /// 18 | /// Desc:- 19 | /// Default:- 20 | /// Nullable:True 21 | /// 22 | public string name { get; set; } 23 | 24 | /// 25 | /// Desc:学校ID 26 | /// Default:- 27 | /// Nullable:True 28 | /// 29 | public int sch_id { get; set; } 30 | 31 | /// 32 | /// Desc:- 33 | /// Default:- 34 | /// Nullable:True 35 | /// 36 | public string sex { get; set; } 37 | 38 | /// 39 | /// Desc:- 40 | /// Default:- 41 | /// Nullable:True 42 | /// 43 | public Boolean isOk { get; set; } 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /NewTest/Models/StudentGroup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace Models 7 | { 8 | public class StudentGroup 9 | { 10 | public string Sex { get; set; } 11 | public int Count { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /NewTest/Models/Subject.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Text; 4 | 5 | namespace Models 6 | { 7 | public class Subject 8 | { 9 | 10 | /// 11 | /// Desc:- 12 | /// Default:- 13 | /// Nullable:False 14 | /// 15 | public int id {get;set;} 16 | 17 | /// 18 | /// Desc:学生ID 19 | /// Default:- 20 | /// Nullable:True 21 | /// 22 | public int? studentId {get;set;} 23 | 24 | /// 25 | /// Desc:- 26 | /// Default:- 27 | /// Nullable:True 28 | /// 29 | public string name {get;set;} 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /NewTest/Models/Test.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Text; 4 | 5 | namespace Models 6 | { 7 | public class Test 8 | { 9 | 10 | /// 11 | /// Desc:- 12 | /// Default:- 13 | /// Nullable:False 14 | /// 15 | public int Id {get;set;} 16 | 17 | /// 18 | /// Desc:- 19 | /// Default:- 20 | /// Nullable:False 21 | /// 22 | public string NAME {get;set;} 23 | 24 | /// 25 | /// Desc:- 26 | /// Default:- 27 | /// Nullable:True 28 | /// 29 | public int? SCH_ID {get;set;} 30 | 31 | /// 32 | /// Desc:- 33 | /// Default:- 34 | /// Nullable:True 35 | /// 36 | public string SEX {get;set;} 37 | 38 | /// 39 | /// Desc:- 40 | /// Default:- 41 | /// Nullable:True 42 | /// 43 | public bool ISOK {get;set;} 44 | 45 | /// 46 | /// Desc:- 47 | /// Default:- 48 | /// Nullable:True 49 | /// 50 | public string C_VARCHAR2 {get;set;} 51 | 52 | /// 53 | /// Desc:- 54 | /// Default:- 55 | /// Nullable:True 56 | /// 57 | public string C_CLOB {get;set;} 58 | 59 | /// 60 | /// Desc:- 61 | /// Default:- 62 | /// Nullable:True 63 | /// 64 | public DateTime? C_DATE {get;set;} 65 | 66 | /// 67 | /// Desc:- 68 | /// Default:- 69 | /// Nullable:True 70 | /// 71 | public int? C_INT {get;set;} 72 | 73 | /// 74 | /// Desc:- 75 | /// Default:- 76 | /// Nullable:True 77 | /// 78 | public short C_INT16 {get;set;} 79 | 80 | /// 81 | /// Desc:- 82 | /// Default:- 83 | /// Nullable:True 84 | /// 85 | public Byte? C_BYTE {get;set;} 86 | 87 | /// 88 | /// Desc:- 89 | /// Default:- 90 | /// Nullable:True 91 | /// 92 | public bool C_BOOL {get;set;} 93 | 94 | /// 95 | /// Desc:- 96 | /// Default:- 97 | /// Nullable:True 98 | /// 99 | public long C_INT64 {get;set;} 100 | 101 | /// 102 | /// Desc:- 103 | /// Default:- 104 | /// Nullable:True 105 | /// 106 | public Byte[] C_BFILE {get;set;} 107 | 108 | /// 109 | /// Desc:- 110 | /// Default:- 111 | /// Nullable:True 112 | /// 113 | public Double? C_DOUBLE {get;set;} 114 | 115 | /// 116 | /// Desc:- 117 | /// Default:- 118 | /// Nullable:True 119 | /// 120 | public Double? C_BIN_DOU {get;set;} 121 | 122 | /// 123 | /// Desc:- 124 | /// Default:- 125 | /// Nullable:True 126 | /// 127 | public string C_CNLOB {get;set;} 128 | 129 | /// 130 | /// Desc:- 131 | /// Default:- 132 | /// Nullable:True 133 | /// 134 | public DateTime? C_TIMESTAMP {get;set;} 135 | 136 | /// 137 | /// Desc:- 138 | /// Default:- 139 | /// Nullable:True 140 | /// 141 | public Byte[] C_BOB {get;set;} 142 | 143 | /// 144 | /// Desc:- 145 | /// Default:- 146 | /// Nullable:True 147 | /// 148 | public string C_LOB {get;set;} 149 | 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /NewTest/Models/TestUpdateColumns.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Text; 4 | 5 | namespace Models 6 | { 7 | public class TestUpdateColumns 8 | { 9 | 10 | /// 11 | /// Desc:- 12 | /// Default:- 13 | /// Nullable:False 14 | /// 15 | public Guid VGUID {get;set;} 16 | 17 | /// 18 | /// Desc:- 19 | /// Default:- 20 | /// Nullable:False 21 | /// 22 | public int IdentityField {get;set;} 23 | 24 | /// 25 | /// Desc:- 26 | /// Default:- 27 | /// Nullable:True 28 | /// 29 | public string Name {get;set;} 30 | 31 | /// 32 | /// Desc:- 33 | /// Default:- 34 | /// Nullable:True 35 | /// 36 | public string Name2 {get;set;} 37 | 38 | /// 39 | /// Desc:- 40 | /// Default:- 41 | /// Nullable:True 42 | /// 43 | public DateTime? CreateTime {get;set;} 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /NewTest/Models/V_LanguageTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Text; 4 | 5 | namespace Models 6 | { 7 | public class V_LanguageTest 8 | { 9 | 10 | /// 11 | /// Desc:- 12 | /// Default:- 13 | /// Nullable:False 14 | /// 15 | public int Id {get;set;} 16 | 17 | /// 18 | /// Desc:- 19 | /// Default:- 20 | /// Nullable:True 21 | /// 22 | public int? LanguageId {get;set;} 23 | 24 | /// 25 | /// Desc:- 26 | /// Default:- 27 | /// Nullable:True 28 | /// 29 | public string Name {get;set;} 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /NewTest/Models/V_Student.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace Models 7 | { 8 | public class V_Student 9 | { 10 | 11 | /// 12 | /// Desc:- 13 | /// Default:- 14 | /// Nullable:False 15 | /// 16 | public int id { get; set; } 17 | 18 | /// 19 | /// Desc:- 20 | /// Default:- 21 | /// Nullable:True 22 | /// 23 | public string name { get; set; } 24 | 25 | /// 26 | /// Desc:学校ID 27 | /// Default:- 28 | /// Nullable:True 29 | /// 30 | public int? sch_id { get; set; } 31 | 32 | /// 33 | /// Desc:- 34 | /// Default:- 35 | /// Nullable:True 36 | /// 37 | public string sex { get; set; } 38 | 39 | /// 40 | /// Desc:- 41 | /// Default:- 42 | /// Nullable:True 43 | /// 44 | public Boolean? isOk { get; set; } 45 | 46 | public string SchoolName { get; set; } 47 | 48 | public string AreaName { get; set; } 49 | 50 | public string SubjectName { get; set; } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /NewTest/NewTest.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {038A0E08-792C-45DE-BF85-FD0277109758} 9 | Exe 10 | Properties 11 | NewTest 12 | NewTest 13 | v4.0 14 | 15 | 16 | 512 17 | 18 | 19 | x86 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | 28 | 29 | x86 30 | pdbonly 31 | true 32 | bin\Release\ 33 | TRACE 34 | prompt 35 | 4 36 | 37 | 38 | 39 | ..\SqlSugar\Lib\Oracle.ManagedDataAccess.dll 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | {46A17E67-7E3E-4369-9B1F-43BA34BBDAEF} 93 | OracleSugar 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 114 | -------------------------------------------------------------------------------- /NewTest/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using OracleSugar; 6 | using NewTest.Demos; 7 | namespace NewTest 8 | { 9 | class Program 10 | { 11 | /// 12 | /// SqlSugar的功能介绍 13 | /// 14 | /// 15 | static void Main(string[] args) 16 | { 17 | 18 | //设置执行的DEMO 19 | string switchOn = "select"; 20 | IDemos demo = null; 21 | switch (switchOn) 22 | { 23 | /****************************基本功能**************************************/ 24 | //查询 25 | case "select": demo = new Select(); break; 26 | //删除 27 | case "delete": demo = new Delete(); break; 28 | //插入 29 | case "insert": demo = new Insert(); break; 30 | //更新 31 | case "update": demo = new Update(); break; 32 | //基层函数的用法 33 | case "ado": demo = new Ado(); break; 34 | //事务 35 | case "tran": demo = new Tran(); break; 36 | //创建实体函数 37 | case "createclass": demo = new CreateClass(); break; 38 | //T4生成 http://www.cnblogs.com/sunkaixuan/p/5751503.html 39 | 40 | //日志记录 41 | case "log": demo = new Log(); break; 42 | //枚举支持 43 | case "enum": demo = new EnumDemo(); break; 44 | 45 | 46 | 47 | /****************************实体映射**************************************/ 48 | //自动排除非数据库列 49 | case "ignoreerrorcolumns": demo = new IgnoreErrorColumns(); break; 50 | //别名表 51 | case "mappingtable":demo=new MappingTable(); break; 52 | //别名列 53 | case "mappingcolumns": demo = new MappingColumns(); break; 54 | //通过属性的方法设置别名表和别名字段 55 | case "attributesmapping": demo = new AttributesMapping(); break; 56 | 57 | 58 | 59 | /****************************业务应用**************************************/ 60 | //过滤器 61 | case "filter": demo = new Filter(); break; 62 | //过滤器2 63 | case "filter2": demo = new Filter2(); break; 64 | //流水号功能 65 | case "serialnumber": demo = new SerialNumber(); break; 66 | 67 | //多语言支持 http://www.cnblogs.com/sunkaixuan/p/5709583.html 68 | //多库并行计算 http://www.cnblogs.com/sunkaixuan/p/5046517.html 69 | 70 | //配置与实例的用法 71 | case "initconfig": demo = new InitConfig(); break; 72 | 73 | 74 | 75 | /****************************支持**************************************/ 76 | //公开函数数 77 | case "pubmethod": demo = new PubMethod(); break; 78 | //设置ToJson的日期格式 79 | case "serializerdateformat":demo =new SerializerDateFormat();break; 80 | 81 | 82 | 83 | /****************************测试用例**************************************/ 84 | case "test": demo = new Test(); break; 85 | 86 | default: Console.WriteLine("switchOn的值错误,请输入正确的 case"); break; 87 | 88 | } 89 | //执行DEMO 90 | demo.Init(); 91 | 92 | //更多例子请查看API 93 | //http://www.cnblogs.com/sunkaixuan/p/5654695.html 94 | Console.WriteLine("执行成功请关闭窗口"); 95 | Console.ReadKey(); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /NewTest/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的常规信息通过以下 6 | // 特性集控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("NewTest")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("NewTest")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 使此程序集中的类型 18 | // 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型, 19 | // 则将该类型上的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("ece2f9bf-2032-417f-884c-cf55d75780db")] 24 | 25 | // 程序集的版本信息由下面四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 内部版本号 30 | // 修订号 31 | // 32 | // 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值, 33 | // 方法是按如下所示使用“*”: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /NewTest/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /NewTest/bin/Debug/NewTest.vshost.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DotNetNext/OracleSugar/c737c567d382f72b25c3bc3eacd8d7e3931f803d/NewTest/bin/Debug/NewTest.vshost.exe -------------------------------------------------------------------------------- /OracleSugar.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OracleSugar", "SqlSugar\OracleSugar.csproj", "{46A17E67-7E3E-4369-9B1F-43BA34BBDAEF}" 5 | EndProject 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewTest", "NewTest\NewTest.csproj", "{038A0E08-792C-45DE-BF85-FD0277109758}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Debug|Mixed Platforms = Debug|Mixed Platforms 12 | Debug|x86 = Debug|x86 13 | Release|Any CPU = Release|Any CPU 14 | Release|Mixed Platforms = Release|Mixed Platforms 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {46A17E67-7E3E-4369-9B1F-43BA34BBDAEF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {46A17E67-7E3E-4369-9B1F-43BA34BBDAEF}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {46A17E67-7E3E-4369-9B1F-43BA34BBDAEF}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU 21 | {46A17E67-7E3E-4369-9B1F-43BA34BBDAEF}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU 22 | {46A17E67-7E3E-4369-9B1F-43BA34BBDAEF}.Debug|x86.ActiveCfg = Debug|Any CPU 23 | {46A17E67-7E3E-4369-9B1F-43BA34BBDAEF}.Release|Any CPU.ActiveCfg = Release|Any CPU 24 | {46A17E67-7E3E-4369-9B1F-43BA34BBDAEF}.Release|Any CPU.Build.0 = Release|Any CPU 25 | {46A17E67-7E3E-4369-9B1F-43BA34BBDAEF}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU 26 | {46A17E67-7E3E-4369-9B1F-43BA34BBDAEF}.Release|Mixed Platforms.Build.0 = Release|Any CPU 27 | {46A17E67-7E3E-4369-9B1F-43BA34BBDAEF}.Release|x86.ActiveCfg = Release|Any CPU 28 | {038A0E08-792C-45DE-BF85-FD0277109758}.Debug|Any CPU.ActiveCfg = Debug|x86 29 | {038A0E08-792C-45DE-BF85-FD0277109758}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 30 | {038A0E08-792C-45DE-BF85-FD0277109758}.Debug|Mixed Platforms.Build.0 = Debug|x86 31 | {038A0E08-792C-45DE-BF85-FD0277109758}.Debug|x86.ActiveCfg = Debug|x86 32 | {038A0E08-792C-45DE-BF85-FD0277109758}.Debug|x86.Build.0 = Debug|x86 33 | {038A0E08-792C-45DE-BF85-FD0277109758}.Release|Any CPU.ActiveCfg = Release|x86 34 | {038A0E08-792C-45DE-BF85-FD0277109758}.Release|Mixed Platforms.ActiveCfg = Release|x86 35 | {038A0E08-792C-45DE-BF85-FD0277109758}.Release|Mixed Platforms.Build.0 = Release|x86 36 | {038A0E08-792C-45DE-BF85-FD0277109758}.Release|x86.ActiveCfg = Release|x86 37 | {038A0E08-792C-45DE-BF85-FD0277109758}.Release|x86.Build.0 = Release|x86 38 | EndGlobalSection 39 | GlobalSection(SolutionProperties) = preSolution 40 | HideSolutionNode = FALSE 41 | EndGlobalSection 42 | EndGlobal 43 | -------------------------------------------------------------------------------- /OracleSugar.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DotNetNext/OracleSugar/c737c567d382f72b25c3bc3eacd8d7e3931f803d/OracleSugar.suo -------------------------------------------------------------------------------- /SqlSugar/Base/OracleConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Reflection; 6 | using Oracle.ManagedDataAccess.Client; 7 | using System.Text.RegularExpressions; 8 | 9 | namespace OracleSugar 10 | { 11 | /// 12 | /// 序列 13 | /// 14 | public class SequenceModel 15 | { 16 | /// 17 | /// 表名 18 | /// 19 | public string TableName { get; set; } 20 | /// 21 | /// 列名 22 | /// 23 | public string ColumnName { get; set; } 24 | /// 25 | /// 序列的值 26 | /// 27 | public string Value { get; set; } 28 | } 29 | 30 | /// 31 | /// Oracle参数配置 32 | /// 33 | public static class OracleConfig 34 | { 35 | //public static string ToOracleTableName(this string value) 36 | //{ 37 | // return value; 38 | //} 39 | 40 | //public static object ToOracleParValue(this object value) 41 | //{ 42 | // if (value == null) 43 | // { 44 | // return DBNull.Value; 45 | // } 46 | // var type = value.GetType(); 47 | // if (type == SqlSugarTool.BoolType) 48 | // { 49 | // return Convert.ToBoolean(value) ? 1 : 0; 50 | // } 51 | // else if (type == SqlSugarTool.GuidType) 52 | // { 53 | // return value.ToString(); 54 | // } 55 | // return value; 56 | 57 | //} 58 | internal static void SetParType(string typeName, bool isBinary, string name, OracleParameter par, SqlSugarClient db) 59 | { 60 | var colInfos = db.ClassGenerating.GetTableColumns(db, typeName.ToUpper()).Where(it => it.COLUMN_NAME.ObjToString().ToLower() == name.GetOracleParameterNameNoParSymbol().ToLower()).ToList(); 61 | if (colInfos.IsValuable()) 62 | { 63 | var colInfo = colInfos.Single(); 64 | if (colInfo.DATA_TYPE.ObjToString() == "BFILE") 65 | { 66 | par.OracleDbType = OracleDbType.BFile; 67 | } 68 | else if (colInfo.DATA_TYPE.ObjToString() == "BLOB") 69 | { 70 | par.OracleDbType = OracleDbType.Blob; 71 | } 72 | else if (colInfo.DATA_TYPE.ObjToString() == "RAW") 73 | { 74 | par.OracleDbType = OracleDbType.Raw; 75 | } 76 | else if (colInfo.DATA_TYPE.ObjToString() == "LONGRAW") 77 | { 78 | par.OracleDbType = OracleDbType.LongRaw; 79 | } 80 | } 81 | } 82 | internal static void SetParType(string typeName, PropertyInfo prop, OracleParameter par, SqlSugarClient db) 83 | { 84 | var isBinary = prop.PropertyType == SqlSugarTool.ByteArrayType; 85 | if (isBinary) 86 | { 87 | var colInfos = db.ClassGenerating.GetTableColumns(db, typeName.ToUpper()).Where(it => it.COLUMN_NAME.ObjToString().ToLower() == prop.Name.ToLower()).ToList(); 88 | if (colInfos.IsValuable()) 89 | { 90 | var colInfo = colInfos.Single(); 91 | if (colInfo.DATA_TYPE.ObjToString() == "BFILE") 92 | { 93 | par.OracleDbType = OracleDbType.BFile; 94 | } 95 | else if (colInfo.DATA_TYPE.ObjToString() == "BLOB") 96 | { 97 | par.OracleDbType = OracleDbType.Blob; 98 | } 99 | else if (colInfo.DATA_TYPE.ObjToString() == "RAW") 100 | { 101 | par.OracleDbType = OracleDbType.Raw; 102 | } 103 | else if (colInfo.DATA_TYPE.ObjToString() == "LONGRAW") 104 | { 105 | par.OracleDbType = OracleDbType.LongRaw; 106 | } 107 | } 108 | } 109 | } 110 | 111 | internal static bool ToColumnTypeNullable(this object value) 112 | { 113 | return value.ToString() == "Y" ? true : false; 114 | } 115 | /// 116 | /// 设置Number与实体的类型映射 117 | /// 默认: 118 | /// {1,"bool"}, 119 | /// {3,"byte"}, 120 | /// {4,"short"}, 121 | /// {9,"int"}, 122 | /// {18,"long"} 123 | /// 124 | public static Dictionary OracleNumberTypeMapping = new Dictionary() 125 | { 126 | {1,"bool"}, 127 | {3,"byte"}, 128 | {4,"short"}, 129 | {9,"int"}, 130 | {18,"long"} 131 | }; 132 | /// 133 | /// 设置序列 134 | /// 135 | public static List SequenceMapping = new List(); 136 | 137 | 138 | internal static string GetOracleSql(string sql) 139 | { 140 | if (sql == null) return null; 141 | if (sql.Contains("@")) 142 | { 143 | sql = Regex.Replace(sql, @" \@", " :"); 144 | sql = Regex.Replace(sql, @"\>\@", ">:"); 145 | sql = Regex.Replace(sql, @"\<\@", "<:"); 146 | sql = Regex.Replace(sql, @"\=\@", "=:"); 147 | } 148 | return sql; 149 | } 150 | 151 | internal static void SetParsName(params OracleParameter[] pars) 152 | { 153 | if (pars != null && pars.Length > 0) 154 | { 155 | foreach (var item in pars) 156 | { 157 | if (item.ParameterName.StartsWith("@")) 158 | { 159 | item.ParameterName = ":" + item.ParameterName.TrimStart('@'); 160 | } 161 | } 162 | } 163 | } 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /SqlSugar/Cloud/CloudModels.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Data; 6 | using System.Threading.Tasks; 7 | 8 | namespace OracleSugar 9 | { 10 | 11 | /// 12 | /// 云计算连接配置 13 | /// 14 | public class CloudConnectionConfig 15 | { 16 | /// 17 | /// 处理机率,值越大机率越高 18 | /// 19 | public int Rate { get; set; } 20 | /// 21 | /// 链接字符串名称 22 | /// 23 | public string ConnectionString { get; set; } 24 | } 25 | 26 | /// 27 | /// 云搜索Task反回类 28 | /// 29 | public class CloudSearchResult 30 | { 31 | /// 32 | /// 集合 33 | /// 34 | public List Entities { get; set; } 35 | /// 36 | /// 单个对象 37 | /// 38 | public T Value { get; set; } 39 | /// 40 | /// DataTable 41 | /// 42 | public DataTable DataTable { get; set; } 43 | /// 44 | /// 连接字符串 45 | /// 46 | public string ConnectionString { get; set; } 47 | /// 48 | /// 数量 49 | /// 50 | public int Count { get; set; } 51 | } 52 | 53 | 54 | 55 | /// 56 | /// 云计扩展类 57 | /// 58 | /// 59 | public class Taskable 60 | { 61 | /// 62 | /// 任务 63 | /// 64 | public Task>[] Tasks { get; set; } 65 | /// 66 | /// sql 67 | /// 68 | public string Sql { get; set; } 69 | /// 70 | /// 数据库参数(例如:new{id=1,name="张三"}) 71 | /// 72 | public object WhereObj { get; set; } 73 | 74 | } 75 | 76 | /// 77 | /// 云计扩展类 78 | /// 79 | /// 80 | public class TaskableWithCount 81 | { 82 | /// 83 | /// 任务 84 | /// 85 | public Task>[] Tasks { get; set; } 86 | /// 87 | /// sql 88 | /// 89 | public string Sql { get; set; } 90 | /// 91 | /// 数据库参数(例如:new{id=1,name="张三"}) 92 | /// 93 | public object WhereObj { get; set; } 94 | 95 | } 96 | 97 | internal class PageRowInnerParamsResult 98 | { 99 | public DataRow Row { get; set; } 100 | public int Count { get; set; } 101 | public int PageIndex { get; set; } 102 | public int PageSize { get; set; } 103 | public int Begin { get; set; } 104 | public int End { get; set; } 105 | public string Sql { get; set; } 106 | public bool IsEqs { get; set; } 107 | public string OrderByField { get; set; } 108 | public string UnqueField { get; set; } 109 | public int RowIndex { get; set; } 110 | public bool isGreater { get; set; } 111 | public string Symbol { get; set; } 112 | public object OrderByValue { get; set; } 113 | public object UnqueValue { get; set; } 114 | public OrderByType OrderByType { get; set; } 115 | public string FullOrderByString { get; set; } 116 | public string FullOrderByStringReverse { get; set; } 117 | public object WhereObj { get; set; } 118 | public string SymbolReverse { get; set; } 119 | public int ConfigCount { get; set; } 120 | public OrderByType OrderByTypeReverse { get; set; } 121 | } 122 | 123 | internal class PageRowInnerParamsResultMultipleOrderBy 124 | { 125 | public DataRow Row { get; set; } 126 | public int Count { get; set; } 127 | public int PageIndex { get; set; } 128 | public int PageSize { get; set; } 129 | public int Begin { get; set; } 130 | public int End { get; set; } 131 | public string Sql { get; set; } 132 | public string UnqueField { get; set; } 133 | public int RowIndex { get; set; } 134 | public bool isGreater { get; set; } 135 | public object UnqueValue { get; set; } 136 | public string FullOrderByString { get; set; } 137 | public string FullOrderByStringReverse { get; set; } 138 | public object WhereObj { get; set; } 139 | public int ConfigCount { get; set; } 140 | 141 | public string orderByFieldsString { get; set; } 142 | 143 | public List OrderByTypes { get; set; } 144 | 145 | public int SampleEachIndex { get; set; } 146 | } 147 | 148 | /// 149 | /// 字典排序类 150 | /// 151 | public class OrderByDictionary 152 | { 153 | /// 154 | /// 排序字段 155 | /// 156 | public string OrderByField { get; set; } 157 | /// 158 | /// 排序类型 159 | /// 160 | public OrderByType OrderByType { get; set; } 161 | /// 162 | /// 排序字符串 163 | /// 164 | public string OrderByString 165 | { 166 | get 167 | { 168 | return string.Format(" {0} {1} ", OrderByField, OrderByType.ToString()); 169 | } 170 | } 171 | /// 172 | /// 排序字符返转 173 | /// 174 | public string OrderByStringReverse 175 | { 176 | get 177 | { 178 | return string.Format(" {0} {1} ", OrderByField, OrderByTypeReverse.ToString()); 179 | } 180 | } 181 | /// 182 | /// 排序字符返转 183 | /// 184 | public OrderByType OrderByTypeReverse 185 | { 186 | get 187 | { 188 | return IsAsc ? OrderByType.Desc : OrderByType.Asc; 189 | } 190 | } 191 | /// 192 | /// 是升序 193 | /// 194 | public bool IsAsc 195 | { 196 | get 197 | { 198 | return OrderByType == OrderByType.Asc; 199 | } 200 | } 201 | /// 202 | /// 比较符 203 | /// 204 | public string Symbol 205 | { 206 | get 207 | { 208 | return IsAsc ? "<" : ">"; 209 | } 210 | } 211 | /// 212 | /// 比较符反转 213 | /// 214 | public string SymbolReverse 215 | { 216 | get 217 | { 218 | return IsAsc ? ">" : "<"; 219 | } 220 | } 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /SqlSugar/Cloud/CloudPubMethod.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OracleSugar 8 | { 9 | 10 | internal class CloudPubMethod 11 | { 12 | public static Random random = new Random(); 13 | 14 | /// 15 | /// 获取pageIndex 16 | /// 17 | /// 18 | /// 19 | public static int GetPageIndex(int pageIndex, double configCount) 20 | { 21 | var index = pageIndex / configCount; 22 | if (index <= 1) 23 | { 24 | index = 1; 25 | } 26 | else if (pageIndex % configCount != 0) 27 | { 28 | index = (int)index + 1; 29 | } 30 | return (int)index; 31 | } 32 | 33 | /// 34 | /// 根据rate获取随机Connection 35 | /// 36 | /// 37 | public static string GetConnection(List configList) 38 | { 39 | Check.Exception(configList == null || configList.Count == 0, "CloudPubMethod.GetConnection.configList不能为null并且count>0。"); 40 | List connectionNameList = new List(); 41 | SetConnectionNameList(configList, ref connectionNameList); 42 | var index = random.Next(0, connectionNameList.Count); 43 | return connectionNameList[index]; 44 | } 45 | 46 | /// 47 | /// 并行执行任务并且传入索引 48 | /// 49 | /// 函数参数i 50 | /// task数组 51 | /// 索引 52 | public static void TaskFactory(Func method, Task[] tasks, int i) 53 | { 54 | tasks[i] = Task.Factory.StartNew(() => 55 | { 56 | return method(i); 57 | }); ; 58 | } 59 | 60 | 61 | private static void SetConnectionNameList(List configList, ref List connectionNameList) 62 | { 63 | var cacheKey = "SetConnectionNameList"; 64 | var cm = CacheManager>.GetInstance(); 65 | if (cm.ContainsKey(cacheKey)) 66 | { 67 | connectionNameList = cm[cacheKey]; 68 | } 69 | else 70 | { 71 | foreach (CloudConnectionConfig config in configList) 72 | { 73 | for (int i = 0; i < config.Rate; i++) 74 | { 75 | connectionNameList.Add(config.ConnectionString); 76 | } 77 | } 78 | cm.Add(cacheKey, connectionNameList, cm.Day); 79 | } 80 | } 81 | /// 82 | /// 分页索引是否很小 83 | /// 84 | /// 85 | /// 86 | /// 87 | /// 88 | /// 89 | public static bool GetIsSmallPageIndex(int pageIndex, int pageSize, int configCount, int maxHandleNumber) 90 | { 91 | if (pageIndex <= configCount) 92 | { 93 | return pageIndex * pageSize * configCount <= maxHandleNumber; 94 | 95 | } 96 | return false; 97 | } 98 | /// 99 | /// 倒数分页索引是否很小 100 | /// 101 | /// 102 | /// 103 | /// 104 | /// 105 | /// 106 | /// 107 | public static bool GetIsSmallPageIndexByReverse(int totalPage, int pageIndex, int pageSize, int configCount, int maxHandleNumber) 108 | { 109 | //倒数第几页 110 | var lastPage=(totalPage - pageIndex)+1 ; 111 | if (lastPage <= configCount) 112 | { 113 | return lastPage * pageSize <= maxHandleNumber; 114 | } 115 | return false; 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /SqlSugar/Cloud/TaskExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Data; 7 | namespace OracleSugar 8 | { 9 | /// 10 | /// Taskable扩展类 11 | /// 12 | public static class TaskExtensions 13 | { 14 | /// 15 | /// 获取count 16 | /// 17 | /// 18 | /// 19 | public static int Count(this Taskable thisValue) 20 | { 21 | return thisValue.Tasks.Select(it => it.Result.Value).Sum(); 22 | } 23 | 24 | /// 25 | ///是否存在这条记录 26 | /// 27 | /// 28 | /// 29 | public static bool Any(this Taskable thisValue) 30 | { 31 | return thisValue.Tasks.Select(it => it.Result.Value).Count() > 0; 32 | } 33 | 34 | /// 35 | /// 获取最大值 36 | /// 37 | /// 38 | /// 39 | /// 40 | public static T Max(this Taskable thisValue) 41 | { 42 | var isClass = typeof(T).IsClass; 43 | if (isClass) 44 | { 45 | Check.Exception(isClass, "TaskExtensions.Max.thisValue T只能是为string和值类型。"); 46 | } 47 | return thisValue.Tasks.Select(it => it.Result.Value).Max(); 48 | } 49 | 50 | /// 51 | /// 获取平均值 52 | /// 53 | /// 54 | /// 55 | public static int Avg(this TaskableWithCount thisValue) 56 | { 57 | var count = thisValue.Tasks.Select(it => it.Result.Count).Sum(); 58 | if (count == 0) return 0; 59 | var reval = thisValue.Tasks.Select(it => it.Result.Value * it.Result.Count).Sum() / count; 60 | return reval; 61 | } 62 | /// 63 | /// 获取平均值 64 | /// 65 | /// 66 | /// 67 | public static decimal Avg(this TaskableWithCount thisValue) 68 | { 69 | var count = thisValue.Tasks.Select(it => it.Result.Count).Sum(); 70 | if (count == 0) return 0; 71 | var reval = thisValue.Tasks.Select(it => it.Result.Value * it.Result.Count).Sum() / count; 72 | return reval; 73 | } 74 | /// 75 | /// 获取平均值 76 | /// 77 | /// 78 | /// 79 | public static double Avg(this TaskableWithCount thisValue) 80 | { 81 | var count = thisValue.Tasks.Select(it => it.Result.Count).Sum(); 82 | if (count == 0) return 0; 83 | var reval = thisValue.Tasks.Select(it => it.Result.Value * it.Result.Count).Sum() / count; 84 | return reval; 85 | } 86 | 87 | 88 | /// 89 | /// 获取最小值 90 | /// 91 | /// 92 | /// 93 | /// 94 | public static T Min(this Taskable thisValue) 95 | { 96 | var isClass = typeof(T).IsClass; 97 | if (isClass) 98 | { 99 | Check.Exception(isClass, "TaskExtensions.Min.thisValue T只能是为string和值类型。"); 100 | } 101 | return thisValue.Tasks.Select(it => it.Result.Value).Min(); 102 | } 103 | 104 | 105 | /// 106 | /// 将Task中的结果集合并成List集成 107 | /// 108 | /// 109 | /// 110 | /// 111 | public static List ToList(this Taskable thisValue) where T : class 112 | { 113 | var isClass = typeof(T).IsClass; 114 | if (!isClass) 115 | { 116 | Check.Exception(isClass, "TaskExtensions.ToList.thisValue T只能为class。"); 117 | } 118 | return thisValue.MergeEntities(); 119 | } 120 | 121 | 122 | /// 123 | /// 返回序列中的唯一元素;如果该序列为空,此方法将引发异常;如果该序列包含多个元素,此方法将引发异常。 124 | /// 125 | /// 126 | /// 127 | /// 128 | public static T ToSingle(this Taskable thisValue) where T : class 129 | { 130 | var isClass = typeof(T).IsClass; 131 | if (!isClass) 132 | { 133 | Check.Exception(isClass, "TaskExtensions.ToSingle.thisValue T只能为class。"); 134 | } 135 | return thisValue.MergeEntities().Single(); 136 | } 137 | 138 | /// 139 | /// 返回序列中的唯一元素;如果该序列为空,则返回默认值;如果该序列包含多个元素,此方法将引发异常。 140 | /// 141 | /// 142 | /// 143 | /// 144 | public static T ToSingleOrDefault(this Taskable thisValue) where T : class 145 | { 146 | var isClass = typeof(T).IsClass; 147 | if (!isClass) 148 | { 149 | Check.Exception(isClass, "TaskExtensions.ToSingle.thisValue T只能为class。"); 150 | } 151 | return thisValue.MergeEntities().SingleOrDefault(); 152 | } 153 | 154 | 155 | /// 156 | /// 获取第一行数据,,如果序列中不包含任何元素,则会抛出异常 157 | /// 158 | /// 159 | /// 160 | /// 161 | public static T ToFirst(this Taskable thisValue) where T : class 162 | { 163 | var isClass = typeof(T).IsClass; 164 | if (!isClass) 165 | { 166 | Check.Exception(isClass, "TaskExtensions.ToSingle.thisValue T只能为class。"); 167 | } 168 | return thisValue.MergeEntities().First(); 169 | } 170 | 171 | /// 172 | /// 如果序列中不包含任何元素,则返回默认值。 173 | /// 174 | /// 175 | /// 176 | /// 177 | public static T ToFirstOrDefault(this Taskable thisValue) where T : class 178 | { 179 | var isClass = typeof(T).IsClass; 180 | if (!isClass) 181 | { 182 | Check.Exception(isClass, "TaskExtensions.ToSingle.thisValue T只能为class。"); 183 | } 184 | return thisValue.MergeEntities().FirstOrDefault(); 185 | } 186 | 187 | 188 | 189 | /// 190 | /// 将结果集合并到一个集合 191 | /// 192 | /// 193 | /// 194 | /// 195 | public static IEnumerable MergeTable(this Taskable thisValue) 196 | { 197 | 198 | var isDataTable = typeof(System.Data.DataTable) == typeof(DataTable); 199 | if (!isDataTable) 200 | { 201 | Check.Exception(isDataTable, "TaskExtensions.MergeTable.thisValue T只能为DataTable。"); 202 | } 203 | var reval = thisValue.Tasks.SelectMany(it => it.Result.DataTable.AsEnumerable()).ToList(); 204 | return reval; 205 | } 206 | 207 | /// 208 | /// 将结果集合并到一个集合 209 | /// 210 | /// 211 | /// 212 | /// 213 | public static List MergeEntities(this Taskable thisValue) where T : class 214 | { 215 | 216 | var reval = thisValue.Tasks.SelectMany(it => it.Result.Entities).ToList(); 217 | return reval; 218 | } 219 | 220 | 221 | /// 222 | /// 将结果集合并到一个集合 223 | /// 224 | /// 225 | /// 226 | /// 227 | public static List MergeValue(this Taskable thisValue) 228 | { 229 | 230 | var reval = thisValue.Tasks.Select(it => it.Result.Value).ToList(); 231 | return reval; 232 | } 233 | } 234 | 235 | } 236 | -------------------------------------------------------------------------------- /SqlSugar/Core/IDataRecordExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Data; 6 | 7 | namespace OracleSugar 8 | { 9 | /// 10 | /// ** 描述:IDataRecord扩展类 11 | /// ** 创始时间:2016-8-7 12 | /// ** 修改时间:- 13 | /// ** 作者:孙凯旋 14 | /// ** 使用说明: 15 | /// 16 | public static class IDataRecordExtensions 17 | { 18 | /// 19 | /// 获取bool 20 | /// 21 | /// 22 | /// 23 | /// 24 | public static bool? GetConvertBoolean(this IDataRecord dr, int i) 25 | { 26 | if (dr.IsDBNull(i)) 27 | { 28 | return null; 29 | } 30 | var reval = dr.GetBoolean(i); 31 | return reval; 32 | } 33 | 34 | /// 35 | /// 获取byte 36 | /// 37 | /// 38 | /// 39 | /// 40 | public static byte? GetConvertByte(this IDataRecord dr, int i) 41 | { 42 | if (dr.IsDBNull(i)) 43 | { 44 | return null; 45 | } 46 | var reval = dr.GetByte(i); 47 | return reval; 48 | } 49 | 50 | /// 51 | /// 获取char 52 | /// 53 | /// 54 | /// 55 | /// 56 | public static char? GetConvertChar(this IDataRecord dr, int i) 57 | { 58 | if (dr.IsDBNull(i)) 59 | { 60 | return null; 61 | } 62 | var reval = dr.GetChar(i); 63 | return reval; 64 | } 65 | 66 | /// 67 | /// 获取时间 68 | /// 69 | /// 70 | /// 71 | /// 72 | public static DateTime? GetConvertDateTime(this IDataRecord dr, int i) 73 | { 74 | if (dr.IsDBNull(i)) 75 | { 76 | return null; 77 | } 78 | var reval = dr.GetDateTime(i); 79 | return reval; 80 | } 81 | 82 | /// 83 | /// 获取转换Decimal 84 | /// 85 | /// 86 | /// 87 | /// 88 | public static decimal? GetConvertDecimal(this IDataRecord dr, int i) 89 | { 90 | if (dr.IsDBNull(i)) 91 | { 92 | return null; 93 | } 94 | var reval = dr.GetDecimal(i); 95 | return reval; 96 | } 97 | 98 | /// 99 | /// 获取Double 100 | /// 101 | /// 102 | /// 103 | /// 104 | public static double? GetConvertDouble(this IDataRecord dr, int i) 105 | { 106 | if (dr.IsDBNull(i)) 107 | { 108 | return null; 109 | } 110 | var reval = dr.GetDouble(i); 111 | return reval; 112 | } 113 | 114 | /// 115 | /// 获取GUID 116 | /// 117 | /// 118 | /// 119 | /// 120 | public static Guid? GetConvertGuid(this IDataRecord dr, int i) 121 | { 122 | if (dr.IsDBNull(i)) 123 | { 124 | return null; 125 | } 126 | var reval = dr.GetGuid(i); 127 | return reval; 128 | } 129 | 130 | /// 131 | /// 获取int16 132 | /// 133 | /// 134 | /// 135 | /// 136 | public static short? GetConvertInt16(this IDataRecord dr, int i) 137 | { 138 | if (dr.IsDBNull(i)) 139 | { 140 | return null; 141 | } 142 | var reval = dr.GetInt16(i); 143 | return reval; 144 | } 145 | 146 | /// 147 | /// 获取int32 148 | /// 149 | /// 150 | /// 151 | /// 152 | public static Int32? GetConvertInt32(this IDataRecord dr, int i) 153 | { 154 | if (dr.IsDBNull(i)) 155 | { 156 | return null; 157 | } 158 | var reval = dr.GetInt32(i); 159 | return reval; 160 | } 161 | 162 | /// 163 | /// 获取int64 164 | /// 165 | /// 166 | /// 167 | /// 168 | public static long? GetConvetInt64(this IDataRecord dr, int i) 169 | { 170 | if (dr.IsDBNull(i)) 171 | { 172 | return null; 173 | } 174 | var reval = dr.GetInt64(i); 175 | return reval; 176 | } 177 | 178 | /// 179 | /// 获取float 180 | /// 181 | /// 182 | /// 183 | /// 184 | public static float? GetConvertFloat(this IDataRecord dr, int i) 185 | { 186 | if (dr.IsDBNull(i)) 187 | { 188 | return null; 189 | } 190 | var reval = dr.GetFloat(i); 191 | return reval; 192 | } 193 | 194 | /// 195 | /// 获取其它类型 196 | /// 197 | /// 198 | /// 199 | /// 200 | /// 201 | public static Nullable GetOtherNull(this IDataReader dr, int i) where T : struct 202 | { 203 | if (dr.IsDBNull(i)) 204 | { 205 | return null; 206 | } 207 | return (T)Convert.ChangeType(dr.GetValue(i), typeof(T)); 208 | 209 | } 210 | 211 | /// 212 | /// 获取其它类型 213 | /// 214 | /// 215 | /// 216 | /// 217 | /// 218 | public static T GetOther(this IDataReader dr, int i) 219 | { 220 | return (T)Convert.ChangeType(dr.GetValue(i), typeof(T)); 221 | } 222 | 223 | /// 224 | /// 获取枚举 225 | /// 226 | /// 227 | /// 228 | /// 229 | /// 230 | public static Nullable GetConvertEnum_Nullable(this IDataReader dr, int i) where T : struct 231 | { 232 | if (dr.IsDBNull(i)) 233 | { 234 | return null; 235 | } 236 | object value = dr.GetValue(i); 237 | T t = (T)Enum.ToObject(typeof(T), value); 238 | return t; 239 | } 240 | 241 | } 242 | } 243 | -------------------------------------------------------------------------------- /SqlSugar/Core/ResolveExpress/Constant.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace OracleSugar 7 | { 8 | //局部类:拉姆达解析公用常量 9 | internal partial class ResolveExpress 10 | { 11 | /// 12 | /// 解析bool类型用到的字典 13 | /// 14 | public static List ConstantBoolDictionary = new List() 15 | { 16 | new ExpressBoolModel(){ Key=Guid.NewGuid(), OldValue="True", Type=SqlSugarTool.StringType}, 17 | new ExpressBoolModel(){ Key=Guid.NewGuid(), OldValue="False",Type=SqlSugarTool.StringType}, 18 | new ExpressBoolModel(){ Key=Guid.NewGuid(), OldValue="True",Type=SqlSugarTool.BoolType}, 19 | new ExpressBoolModel(){ Key=Guid.NewGuid(), OldValue="False",Type=SqlSugarTool.BoolType} 20 | 21 | }; 22 | /// 23 | /// 字段名解析错误 24 | /// 25 | public const string FileldErrorMessage = "OrderBy、GroupBy、In、Min和Max等操作不是有效拉姆达格式 ,正确格式 it=>it.name "; 26 | /// 27 | /// 拉姆达解析错误 28 | /// 29 | public const string ExpToSqlError= @"拉姆达解析出错,不是有效的函数,找不到合适函数你可以使用这种字符串写法.Where(""date>dateadd(@date)"",new{date=DateTime.Now}), 30 | 支持的函数有(请复制到本地查看,数量比较多): 31 | db.Queryable().Where(it => it.field == parValue.ObjToString()); 32 | db.Queryable().Where(it => it.field == parValue.ObjToDate()); 33 | db.Queryable().Where(it => it.field == parValue.ObjToInt()) 34 | db.Queryable().Where(it => it.field == parValue.ObjToDecimal()) 35 | db.Queryable().Where(it => it.field == parValue.ObjToMoney()) 36 | db.Queryable().Where(it => it.field == parValue.Trim()) 37 | db.Queryable().Where(it => it.field == parValue.ToString()) 38 | db.Queryable().Where(it => it.field == Convert.ToXXX(parValue)) 39 | db.Queryable().Where(it => it.field.Contains(parValue)) 40 | db.Queryable().Where(it => it.field.StartsWith(parValue)) 41 | db.Queryable().Where(it => it.field.EndsWith(parValue)) 42 | db.Queryable().Where(it => !string.IsNullOrEmpty(it.parValue)) 43 | db.Queryable().Where(it => arrayOrList.Contains(it.parValue)) 44 | db.Queryable().Where(it => it.field.Equals(it.parValue)) 45 | db.Queryable().Where(it => it.field.Length>10) 46 | db.Queryable().Where(c => c.field == parValue.ToLower()).ToList(); 47 | db.Queryable().Where(c => c.field == parValue.ToUpper()).ToList(); 48 | "; 49 | /// 50 | /// 运算符错误 51 | /// 52 | public const string OperatorError = "拉姆达解析出错:不支持{0}此种运算符查找!"; 53 | 54 | /// 55 | /// 拉姆达解析唯一标识 56 | /// 57 | public static object ExpErrorUniqueKey = Guid.NewGuid(); 58 | 59 | /// 60 | /// 拉姆达函数错误 61 | /// 62 | public const string ExpMethodError = "拉姆达表达式中的函数用法不正确,正确写法 it=>it.name.{0}(参数) ,不支持的写法it=> 参数.{0}(it.name)。"; 63 | 64 | 65 | /// 66 | /// 拉姆达函数错误2 67 | /// 68 | public const string ExpMethodError2 = "拉姆达表达式中的函数用法不正确,正确写法 it=>it.name==参数.{0} ,不支持的写法it=>it.name.{0}==参数。"; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /SqlSugar/Core/ResolveExpress/Models.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace OracleSugar 7 | { 8 | //局部类:解析用到的实体 9 | internal partial class ResolveExpress 10 | { 11 | /// 12 | /// 拉姆达成员类型 13 | /// 14 | public enum MemberType 15 | { 16 | None = 0, 17 | Key = 1, 18 | Value = 2 19 | } 20 | /// 21 | /// 用来处理bool类型的实体 22 | /// 23 | public class ExpressBoolModel 24 | { 25 | /// 26 | /// 唯一标识 27 | /// 28 | public Guid Key { get; set; } 29 | /// 30 | /// 数据类型 31 | /// 32 | public Type Type { get; set; } 33 | /// 34 | /// 原始值 35 | /// 36 | public string OldValue { get; set; } 37 | /// 38 | /// 处事后的值 39 | /// 40 | public string NewValue 41 | { 42 | get 43 | { 44 | if (Type == SqlSugarTool.BoolType) 45 | { 46 | return Convert.ToBoolean(OldValue) ? "1" : "0"; 47 | } 48 | else 49 | { 50 | return OldValue.ToString(); 51 | } 52 | } 53 | } 54 | /// 55 | /// 处理后的运算对象 56 | /// 57 | public string ConditionalValue 58 | { 59 | get 60 | { 61 | return Convert.ToBoolean(OldValue) ? "(1=1)" : "(1=2)"; 62 | } 63 | } 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /SqlSugar/Core/ResolveExpress/Property.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace OracleSugar 7 | { 8 | //局部类:解析属性 9 | internal partial class ResolveExpress 10 | { 11 | private string GetProMethod(string methodName, string value, bool isField) 12 | { 13 | switch (methodName) 14 | { 15 | case "Length": 16 | return ProLength(value, isField); 17 | default: throw new SqlSugarException("不支持属性扩展方法" + methodName + "。"); 18 | } 19 | } 20 | private string ProLength(string value, bool isField) 21 | { 22 | if (isField) 23 | { 24 | return string.Format("length({0})", value.GetTranslationSqlName()); 25 | } 26 | else 27 | { 28 | return string.Format("{0}", value.ObjToString().Length); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /SqlSugar/Core/ResolveExpress/ResolveFieldName.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Linq.Expressions; 6 | 7 | namespace OracleSugar 8 | { 9 | //局部类 解析字段名 10 | internal partial class ResolveExpress 11 | { 12 | /// 13 | /// 获取拉姆达表达式的字段值 14 | /// 15 | /// 16 | /// 数据库访问对象 17 | /// 18 | public string GetExpressionRightField(Expression exp, SqlSugarClient db) 19 | { 20 | DB = db; 21 | string reval = ""; 22 | LambdaExpression lambda = exp as LambdaExpression; 23 | var isConvet = lambda.Body.NodeType.IsIn(ExpressionType.Convert); 24 | var isMember = lambda.Body.NodeType.IsIn(ExpressionType.MemberAccess); 25 | if (!isConvet && !isMember) 26 | { 27 | throw new SqlSugarException(FileldErrorMessage); 28 | } 29 | try 30 | { 31 | if (isConvet) 32 | { 33 | var memberExpr =((UnaryExpression)lambda.Body).Operand as MemberExpression; 34 | reval= memberExpr.Member.Name; 35 | } 36 | else//isMember 37 | { 38 | reval= (lambda.Body as MemberExpression).Member.Name; 39 | } 40 | } 41 | catch (Exception) 42 | { 43 | throw new SqlSugarException(FileldErrorMessage); 44 | } 45 | if (DB != null && DB.IsEnableAttributeMapping && DB._mappingColumns.IsValuable()) 46 | { 47 | if (DB._mappingColumns.Any(it => it.Key == reval)) 48 | { 49 | var dbName = DB._mappingColumns.Single(it => it.Key == reval).Value; 50 | return dbName; 51 | } 52 | } 53 | return reval; 54 | } 55 | 56 | /// 57 | /// 获取拉姆达表达式的字段值多个T模式 58 | /// 59 | /// 60 | /// 数据库访问对象 61 | /// 62 | public string GetExpressionRightFieldByNT(Expression exp, SqlSugarClient db) 63 | { 64 | DB = db; 65 | string reval = ""; 66 | LambdaExpression lambda = exp as LambdaExpression; 67 | var isConvet = lambda.Body.NodeType.IsIn(ExpressionType.Convert); 68 | var isMember = lambda.Body.NodeType.IsIn(ExpressionType.MemberAccess); 69 | if (!isConvet && !isMember) 70 | { 71 | throw new SqlSugarException(FileldErrorMessage); 72 | } 73 | try 74 | { 75 | if (isConvet) 76 | { 77 | var memberExpr = ((UnaryExpression)lambda.Body).Operand as MemberExpression; 78 | reval= memberExpr.ToString(); 79 | } 80 | else//isMember 81 | { 82 | reval= lambda.Body.ToString(); 83 | } 84 | } 85 | catch (Exception) 86 | { 87 | throw new SqlSugarException(FileldErrorMessage); 88 | } 89 | if (DB != null && DB.IsEnableAttributeMapping && DB._mappingColumns.IsValuable()) 90 | { 91 | if (DB._mappingColumns.Any(it => reval.EndsWith("."+it.Key))) 92 | { 93 | var preName = reval.Split('.').First(); 94 | var dbName = DB._mappingColumns.Single(it => reval.EndsWith("." + it.Key)).Value; 95 | return preName+"."+dbName; 96 | } 97 | } 98 | return reval; 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /SqlSugar/Generating/ClassTemplate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace OracleSugar 7 | { 8 | 9 | /// 10 | /// 生成实体格式模版 11 | /// 12 | public class ClassTemplate 13 | { 14 | /// 15 | ///替换模版 16 | /// 17 | /// 18 | /// 19 | /// 20 | /// 21 | /// 22 | /// 23 | internal static string Replace(string templateStr, string nameSpaceStr, string foreachStr, string classNameStr, List primaryKeyName = null) 24 | { 25 | if (nameSpaceStr.IsNullOrEmpty()) 26 | { 27 | nameSpaceStr = "System"; 28 | } 29 | templateStr = templateStr.Replace("$foreach", foreachStr) 30 | .Replace("$namespace", nameSpaceStr) 31 | .Replace("$className", classNameStr); 32 | //处理主键 33 | if (primaryKeyName != null && primaryKeyName.Count > 0) 34 | { 35 | templateStr = templateStr.Replace("$primaryKeyName", primaryKeyName[0]); 36 | //处理特殊的主键取值 37 | for (int i = 0; i < primaryKeyName.Count; i++) 38 | { 39 | templateStr = templateStr.Replace("$primaryKeyName_" + i + "", primaryKeyName[i]); 40 | } 41 | } 42 | return templateStr; 43 | } 44 | 45 | /// 46 | /// 字段模版 47 | /// 48 | public static string ItemTemplate = " public {0}{3} {1} {2}"; 49 | 50 | /// 51 | /// 生成的实体类模版 52 | /// 53 | public static string Template = 54 | @"using System; 55 | using System.Linq; 56 | using System.Text; 57 | 58 | namespace $namespace 59 | { 60 | public class $className 61 | { 62 | $foreach 63 | } 64 | } 65 | "; 66 | /// 67 | /// 生成实体类字段摘要模版 68 | /// 69 | public static string ClassFieldSummaryTemplate = @" /// 70 | /// Desc:{0} 71 | /// Default:{1} 72 | /// Nullable:{2} 73 | /// 74 | "; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /SqlSugar/Generating/SugarAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Reflection; 6 | 7 | namespace OracleSugar 8 | { 9 | /// 10 | /// 表名属性 11 | /// 12 | [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, Inherited = true)] 13 | public class SugarMappingAttribute : Attribute 14 | { 15 | private string tableName; 16 | /// 17 | /// 据库对应的表名 18 | /// 19 | public string TableName 20 | { 21 | get { return tableName; } 22 | set { tableName = value; } 23 | } 24 | 25 | private string columnName; 26 | /// 27 | /// 数据库对应的列名 28 | /// 29 | public string ColumnName 30 | { 31 | get { return columnName; } 32 | set { columnName = value; } 33 | } 34 | } 35 | 36 | internal class ReflectionSugarMapping 37 | { 38 | /// 39 | /// 通过反射取自定义属性 40 | /// 41 | /// 42 | public static SugarMappingModel GetMappingInfo() 43 | { 44 | Type objType = typeof(T); 45 | string cacheKey = "ReflectionSugarMapping.DisplaySelfAttribute" + objType.FullName; 46 | var cm = CacheManager.GetInstance(); 47 | if (cm.ContainsKey(cacheKey)) 48 | { 49 | return cm[cacheKey]; 50 | } 51 | else 52 | { 53 | SugarMappingModel reval = new SugarMappingModel(); 54 | string tableName = string.Empty; 55 | List columnInfoList = new List(); 56 | var oldName = objType.Name; 57 | //取属性上的自定义特性 58 | foreach (PropertyInfo propInfo in objType.GetProperties()) 59 | { 60 | object[] objAttrs = propInfo.GetCustomAttributes(typeof(SugarMappingAttribute), true); 61 | if (objAttrs.Length > 0) 62 | { 63 | if (objAttrs[0] is SugarMappingAttribute) 64 | { 65 | SugarMappingAttribute attr = objAttrs[0] as SugarMappingAttribute; 66 | if (attr != null) 67 | { 68 | columnInfoList.Add(new KeyValue() { Key = propInfo.Name, Value = attr.ColumnName }); //列名 69 | } 70 | } 71 | } 72 | } 73 | 74 | //取类上的自定义特性 75 | object[] objs = objType.GetCustomAttributes(typeof(SugarMappingAttribute), true); 76 | foreach (object obj in objs) 77 | { 78 | if (obj is SugarMappingAttribute) 79 | { 80 | SugarMappingAttribute attr = obj as SugarMappingAttribute; 81 | if (attr != null) 82 | { 83 | 84 | tableName = attr.TableName;//表名只有获取一次 85 | break; 86 | } 87 | } 88 | } 89 | if (string.IsNullOrEmpty(tableName)) 90 | { 91 | tableName = objType.Name; 92 | } 93 | reval.TableMaping = new KeyValue() { Key = oldName, Value = tableName }; 94 | reval.ColumnsMapping = columnInfoList; 95 | cm.Add(cacheKey,reval,cm.Day); 96 | return reval; 97 | } 98 | } 99 | } 100 | 101 | internal class SugarMappingModel 102 | { 103 | 104 | public KeyValue TableMaping { get; set; } 105 | public List ColumnsMapping { get; set; } 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /SqlSugar/Lib/Oracle.ManagedDataAccess.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DotNetNext/OracleSugar/c737c567d382f72b25c3bc3eacd8d7e3931f803d/SqlSugar/Lib/Oracle.ManagedDataAccess.dll -------------------------------------------------------------------------------- /SqlSugar/NuGet.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DotNetNext/OracleSugar/c737c567d382f72b25c3bc3eacd8d7e3931f803d/SqlSugar/NuGet.exe -------------------------------------------------------------------------------- /SqlSugar/OracleSugar.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {46A17E67-7E3E-4369-9B1F-43BA34BBDAEF} 9 | Library 10 | Properties 11 | OracleSugar 12 | OracleSugar 13 | v4.0 14 | 512 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | bin\Debug\OracleSugar.xml 25 | 26 | 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | bin\Release\OracleSugar.xml 34 | 35 | 36 | 37 | ..\WebTest\App_Lib\Oracle.ManagedDataAccess.dll 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 108 | -------------------------------------------------------------------------------- /SqlSugar/OracleSugar.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | oracleSugar 5 | 3.5.2.7 6 | OracleSugar ORM 7 | sun kaixuan 8 | landa 9 | http://www.apache.org/licenses/LICENSE-2.0.html 10 | http://www.cnblogs.com/sunkaixuan/p/5654695.html 11 | https://secure.gravatar.com/avatar/a82c03402497b2e58fd65038a3699b30 12 | false 13 | OracleSugar ORM .NET 4.0+ High-performance, lightweight http://www.cnblogs.com/sunkaixuan/p/5654695.html 14 | Copyright 2015 15 | asp.net orm 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /SqlSugar/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的常规信息通过以下 6 | // 特性集控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("OracleSugar")] 9 | [assembly: AssemblyDescription("作者:孙凯旋,蓝灯软件架构师")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("上海蓝灯软件有限公司")] 12 | [assembly: AssemblyProduct("SqlSugar")] 13 | [assembly: AssemblyCopyright("Copyright © 上 海蓝灯软件有限公司 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 使此程序集中的类型 18 | // 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型, 19 | // 则将该类型上的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("59fd4b0d-8a69-476f-ad02-33a9e179a7c2")] 24 | 25 | // 程序集的版本信息由下面四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 内部版本号 30 | // 修订号 31 | // 32 | // 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值, 33 | // 方法是按如下所示使用“*”: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("3.5.2.7")] 36 | [assembly: AssemblyFileVersion("3.5.2.7")] 37 | -------------------------------------------------------------------------------- /SqlSugar/PubModel/KeyValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace OracleSugar 7 | { 8 | /// 9 | /// 自定义键值类 key is string, value is string 10 | /// 11 | public class KeyValue 12 | { 13 | /// 14 | /// 键 15 | /// 16 | public string Key { get; set; } 17 | /// 18 | /// 值 19 | /// 20 | public string Value { get; set; } 21 | } 22 | /// 23 | /// 自定义键值类 key is string, value is object 24 | /// 25 | public class KeyValueObj 26 | { 27 | /// 28 | /// 键 29 | /// 30 | public string Key { get; set; } 31 | /// 32 | /// 值 33 | /// 34 | public object Value { get; set; } 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /SqlSugar/PubModel/PubEnum.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace OracleSugar 7 | { 8 | /// 9 | /// join类型 10 | /// 11 | public enum JoinType 12 | { 13 | /// 14 | /// 等值连接 15 | /// 16 | Inner = 0, 17 | /// 18 | /// 左外连 19 | /// 20 | Left = 1, 21 | /// 22 | /// 右外连 23 | /// 24 | Right = 2 25 | } 26 | /// 27 | /// Apply类型 28 | /// 29 | public enum ApplyType 30 | { 31 | /// 32 | /// 笛卡尔积 33 | /// 34 | Cross = 1, 35 | /// 36 | /// 外连 37 | /// 38 | Outer = 2 39 | } 40 | /// 41 | /// 排序类型 42 | /// 43 | public enum OrderByType 44 | { 45 | /// 46 | /// 升序 47 | /// 48 | Asc = 0, 49 | /// 50 | /// 降序 51 | /// 52 | Desc = 1 53 | } 54 | /// 55 | /// 分页类型 56 | /// 57 | public enum PageModel 58 | { 59 | /// 60 | /// 05分页 61 | /// 62 | RowNumber = 0 63 | } 64 | /// 65 | /// 解析类型 66 | /// 67 | public enum ResolveExpressType 68 | { 69 | /// 70 | /// 单个T 71 | /// 72 | OneT = 0, 73 | /// 74 | /// 多个T 75 | /// 76 | NT = 1, 77 | 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /SqlSugar/PubModel/PubModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace OracleSugar 7 | { 8 | /// 9 | /// ** 描述:公共参数表 10 | /// ** 创始时间:2015-7-20 11 | /// ** 修改时间:- 12 | /// ** 作者:sunkaixuan 13 | /// ** 使用说明: 14 | /// 15 | public class PubModel 16 | { 17 | /// 18 | /// 用于存储数据表与列的映射信息 19 | /// 20 | public class DataTableMap 21 | { 22 | /// 23 | /// 表名 24 | /// 25 | public object TABLE_NAME { get; set; } 26 | /// 27 | /// 表ID 28 | /// 29 | public object TABLE_ID { get; set; } 30 | /// 31 | /// 列名 32 | /// 33 | public object COLUMN_NAME { get; set; } 34 | /// 35 | /// 数据类型 36 | /// 37 | public object DATA_TYPE { get; set; } 38 | /// 39 | /// 字符最大长度 40 | /// 41 | public object CHARACTER_MAXIMUM_LENGTH { get; set; } 42 | /// 43 | /// 备注 44 | /// 45 | public object COLUMN_DESCRIPTION { get; set; } 46 | /// 47 | /// 默认值 48 | /// 49 | public object COLUMN_DEFAULT { get; set; } 50 | /// 51 | /// 是否允许为null 52 | /// 53 | public object IS_NULLABLE { get; set; } 54 | /// 55 | /// 是否是主键 56 | /// 57 | public object IS_PRIMARYKEY { get; set; } 58 | /// 59 | /// 精度 60 | /// 61 | public object DATA_SCALE { get; set; } 62 | /// 63 | /// 长度 64 | /// 65 | public object DATA_PRECISION { get; set; } 66 | } 67 | 68 | /// 69 | /// 流水号设置实体 70 | /// 71 | public class SerialNumber 72 | { 73 | /// 74 | /// 表名 75 | /// 76 | public string TableName { get; set; } 77 | /// 78 | /// 字段名 79 | /// 80 | public string FieldName { get; set; } 81 | /// 82 | /// 获取流水号函数 83 | /// 84 | public Func GetNumFunc { get; set; } 85 | /// 86 | /// 获取流水号函数(解决事务中死锁BUG) 87 | /// 88 | public Func GetNumFuncWithDb { get; set; } 89 | } 90 | 91 | /// 92 | /// 多语言设置的参数表 93 | /// 94 | public class Language 95 | { 96 | /// 97 | /// 数据库里面的语言后缀 98 | /// 99 | public string Suffix { get; set; } 100 | /// 101 | /// 数据库语言的VALUE 102 | /// 103 | public int LanguageValue { get; set; } 104 | /// 105 | /// 需要全局替换的字符串Key(用于替换默认语言) 106 | /// 107 | public string ReplaceViewStringKey = "LanguageId=1"; 108 | /// 109 | /// 需要全局替换的字符串Value(用于替换默认语言) 110 | /// 111 | public string ReplaceViewStringValue = "LanguageId = {0}"; 112 | 113 | } 114 | /// 115 | /// SqlSugarClient通用常量 116 | /// 117 | internal class SqlSugarClientConst { 118 | /// 119 | /// 属性设置错误信息 120 | /// 121 | public const string AttrMappingError = @"[SugarMapping(ColumnName = ""{1}"")] 122 | public string {0} {{ get; set; }}已经在其存在于其它表, Columns映射只能在 {0}->{1}和{0}->{2}二者选其一"; 123 | /// 124 | /// SqlQuery查询的SQL模板 125 | /// 126 | public const string SqlQuerySqlTemplate = @"--{0} 127 | {1}"; 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /SqlSugar/Queryable/Queryable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Oracle.ManagedDataAccess.Client; 6 | using System.Linq.Expressions; 7 | 8 | namespace OracleSugar 9 | { 10 | 11 | /// 12 | /// ** 描述:Queryable拉姆达查询对象 13 | /// ** 创始时间:2015-7-13 14 | /// ** 修改时间:- 15 | /// ** 作者:sunkaixuan 16 | /// ** 使用说明: 17 | /// 18 | public class Queryable 19 | { 20 | #region 临时变量 21 | /// 22 | /// T的名称 23 | /// 24 | internal string TName { get { return typeof(T).Name; } } 25 | /// 26 | /// 实体类型 27 | /// 28 | internal Type Type { get { return typeof(T); } } 29 | /// 30 | /// 数据接口 31 | /// 32 | public SqlSugarClient DB = null; 33 | /// 34 | /// Where临时数据 35 | /// 36 | internal List WhereValue = new List(); 37 | /// 38 | /// Skip临时数据 39 | /// 40 | internal int? Skip { get; set; } 41 | /// 42 | /// Take临时数据 43 | /// 44 | internal int? Take { get; set; } 45 | /// 46 | /// Order临时数据 47 | /// 48 | internal string OrderByValue { get; set; } 49 | /// 50 | /// Select临时数据 51 | /// 52 | internal string SelectValue { get; set; } 53 | /// 54 | /// OracleParameter临时数据 55 | /// 56 | internal List Params = new List(); 57 | /// 58 | /// 表名临时数据 59 | /// 60 | internal string TableName { get; set; } 61 | /// 62 | /// 分组查询临时数据 63 | /// 64 | internal string GroupByValue { get; set; } 65 | /// 66 | /// 条件索引临时数据 67 | /// 68 | internal int WhereIndex = 1; 69 | /// 70 | /// 联表查询临时数据 71 | /// 72 | internal List JoinTableValue = new List(); 73 | 74 | #endregion 75 | 76 | 77 | #region 公开函数 78 | /// 79 | /// 联表查询 80 | /// 81 | /// 联接的表对象 82 | /// 表达式 83 | /// Join的类型 84 | /// 85 | public Queryable JoinTable(Expression> expression, JoinType type = JoinType.Left) 86 | { 87 | return this.JoinTable(expression, type); 88 | } 89 | 90 | /// 91 | /// 联表查询 92 | /// 93 | /// 联接表的对象 94 | /// 联接表的对象 95 | /// 表达式 96 | /// Join的类型 97 | /// 98 | public Queryable JoinTable(Expression> expression, JoinType type = JoinType.Left) 99 | { 100 | return this.JoinTable(expression, type); 101 | } 102 | 103 | /// 104 | /// 条件筛选 105 | /// 106 | /// 表实体类型 107 | /// 表达式条件 108 | /// 109 | public Queryable Where(Expression> expression) 110 | { 111 | return this.Where(expression); 112 | } 113 | /// 114 | /// 条件筛选 115 | /// 116 | /// 表实体类型 117 | /// Where后面的Sql条件语句 (例如: id=@id ) 118 | /// 匿名参数 (例如:new{id=1,name="张三"}) 119 | /// 120 | public Queryable Where(string whereString, object whereObj = null) 121 | { 122 | return this.Where(whereString, whereObj); 123 | } 124 | 125 | /// 126 | /// 条件筛选 127 | /// 128 | /// 表实体类型 129 | /// 表实体类型 130 | /// 表达式条件 131 | /// 132 | public Queryable Where(Expression> expression) 133 | { 134 | return this.Where(expression); 135 | } 136 | 137 | /// 138 | /// 条件筛选 139 | /// 140 | /// 表实体类型 141 | /// 表实体类型 142 | /// 表实体类型 143 | /// 表达式条件 144 | /// 145 | public Queryable Where(Expression> expression) 146 | { 147 | return this.Where(expression); 148 | } 149 | 150 | /// 151 | /// 条件筛选 152 | /// 153 | /// 表实体类型 154 | /// 表实体类型 155 | /// 表实体类型 156 | /// 表实体类型 157 | /// 表达式条件 158 | /// 159 | public Queryable Where(Expression> expression) 160 | { 161 | return this.Where(expression); 162 | } 163 | 164 | /// 165 | /// 排序 166 | /// 167 | /// 表实体类型 168 | /// 例如 (s1,s2)=>s1.id,相当于 order by s1.id 169 | /// 排序类型 170 | /// 171 | public Queryable OrderBy(Expression> expression, OrderByType type = OrderByType.Asc) 172 | { 173 | return this.OrderBy(expression, type); 174 | } 175 | 176 | /// 177 | /// 获取最大值 178 | /// 179 | /// 返回类型 180 | /// 列名 181 | /// 182 | public TResult Max(string maxField) 183 | { 184 | return this.Max(maxField); 185 | } 186 | 187 | /// 188 | /// 获取最小值 189 | /// 190 | /// 返回类型 191 | /// 列名 192 | /// 193 | public TResult Min(string minField) 194 | { 195 | return this.Min(minField); 196 | } 197 | 198 | /// 199 | /// 将源数据对象转换到新对象中 200 | /// 201 | /// 原数据实体类型 202 | /// 返回值的新实体类型 203 | /// 给新实体赋值的表达式 204 | /// 205 | public Queryable Select(Expression> expression) 206 | { 207 | return this.Select(expression); 208 | } 209 | 210 | /// 211 | /// 将源数据对象转换到新对象中 212 | /// 213 | /// 原数据实体类型 214 | /// 原数据实体类型 215 | /// 返回值的新实体类型 216 | /// 给新实体赋值的表达式 217 | /// 218 | public Queryable Select(Expression> expression) 219 | { 220 | return this.Select(expression); 221 | } 222 | 223 | /// 224 | /// 将源数据对象转换到新对象中 225 | /// 226 | /// 原数据实体类型 227 | /// 原数据实体类型 228 | /// 原数据实体类型 229 | /// 返回值的新实体类型 230 | /// 给新实体赋值的表达式 231 | /// 232 | public Queryable Select(Expression> expression) 233 | { 234 | return this.Select(expression); 235 | } 236 | 237 | /// 238 | /// 将源数据对象转换到新对象中 239 | /// 240 | /// 原数据实体类型 241 | /// 原数据实体类型 242 | /// 原数据实体类型 243 | /// 原数据实体类型 244 | /// 返回值的新实体类型 245 | /// 给新实体赋值的表达式 246 | /// 247 | public Queryable Select(Expression> expression) 248 | { 249 | return this.Select(expression); 250 | } 251 | 252 | /// 253 | /// 将源数据对象转换到新对象中 254 | /// 255 | /// 返回值的新实体类型 256 | /// 给新实体赋值的表达式 257 | /// 258 | public Queryable Select(Expression> expression) 259 | { 260 | return this.Select(expression); 261 | } 262 | 263 | /// 264 | /// 将源数据对象转换到新对象中 265 | /// 266 | /// 返回值的新实体类型 267 | /// 查询字符串(例如 id,name) 268 | /// 269 | public Queryable Select(string select) 270 | { 271 | return this.Select(select); 272 | } 273 | #endregion 274 | 275 | 276 | } 277 | } 278 | -------------------------------------------------------------------------------- /SqlSugar/Sqlable/Sqlable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Oracle.ManagedDataAccess.Client; 6 | 7 | namespace OracleSugar 8 | { 9 | /// 10 | /// ** 描述:Queryable是多表查询基类,基于拥有大量查询扩展函数 11 | /// ** 创始时间:2015-7-13 12 | /// ** 修改时间:- 13 | /// ** 作者:sunkaixuan 14 | /// ** 使用说明: 15 | /// 16 | public class Sqlable 17 | { 18 | /// 19 | /// 数据接口 20 | /// 21 | public SqlSugarClient DB = null; 22 | /// 23 | /// sql临时数据 24 | /// 25 | public StringBuilder Sql { get; set; } 26 | /// 27 | /// Where临时数据 28 | /// 29 | public List Where = new List(); 30 | /// 31 | /// OrderBy临时数据 32 | /// 33 | public string OrderBy { get; set; } 34 | /// 35 | /// GroupBy临时数据 36 | /// 37 | public string GroupBy { get; set; } 38 | /// 39 | /// 参数 40 | /// 41 | public List Params = new List(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /SqlSugar/Tool/CacheManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Collections; 6 | using System.Linq.Expressions; 7 | 8 | namespace OracleSugar 9 | { 10 | /// 11 | /// ** 描述:缓存操作类 12 | /// ** 创始时间:2015-6-9 13 | /// ** 修改时间:- 14 | /// ** 作者:sunkaixuan 15 | /// ** 使用说明:http://www.cnblogs.com/sunkaixuan/p/4563462.html 16 | /// 17 | /// 值类型 18 | internal class CacheManager : IStorageObject 19 | { 20 | 21 | readonly System.Collections.Concurrent.ConcurrentDictionary InstanceCache = new System.Collections.Concurrent.ConcurrentDictionary(); 22 | 23 | #region 全局变量 24 | private static CacheManager _instance = null; 25 | private static readonly object _instanceLock = new object(); 26 | #endregion 27 | 28 | #region 构造函数 29 | 30 | private CacheManager() { } 31 | #endregion 32 | 33 | #region 属性 34 | /// 35 | ///根据key获取value 36 | /// 37 | /// 38 | public override V this[string key] 39 | { 40 | get 41 | { 42 | return this.Get(key); 43 | } 44 | } 45 | #endregion 46 | 47 | #region 公共函数 48 | 49 | /// 50 | /// 验证key是否存在 51 | /// 52 | /// key 53 | /// /// 存在true 不存在false. /// /// 54 | public override bool ContainsKey(string key) 55 | { 56 | return this.InstanceCache.ContainsKey(key); 57 | 58 | //throw new NotImplementedException(); 59 | //return HttpRuntime.Cache[CreateKey(key)] != null; 60 | } 61 | 62 | /// 63 | /// 根据key获取value 64 | /// 65 | /// key 66 | /// 67 | public override V Get(string key) 68 | { 69 | if (this.ContainsKey(key)) 70 | return this.InstanceCache[key]; 71 | else 72 | return default(V); 73 | } 74 | 75 | /// 76 | /// 获取实例 (单例模式) 77 | /// 78 | /// 79 | public static CacheManager GetInstance() 80 | { 81 | if (_instance == null) 82 | lock (_instanceLock) 83 | if (_instance == null) 84 | _instance = new CacheManager(); 85 | return _instance; 86 | } 87 | 88 | /// 89 | /// 插入缓存(默认20分钟) 90 | /// 91 | /// key 92 | /// value 93 | public override void Add(string key, V value) 94 | { 95 | this.InstanceCache.GetOrAdd(key, value); 96 | } 97 | 98 | /// 99 | /// 插入缓存 100 | /// 101 | /// key 102 | /// value 103 | /// 过期时间单位秒 104 | public void Add(string key, V value, int cacheDurationInSeconds) 105 | { 106 | Add(key, value); 107 | } 108 | 109 | ///// 110 | ///// 插入缓存. 111 | ///// 112 | ///// key 113 | ///// value 114 | ///// 过期时间单位秒 115 | ///// 缓存项属性 116 | //public void Add(string key, V value, int cacheDurationInSeconds, CacheItemPriority priority) 117 | //{ 118 | // string keyString = CreateKey(key); 119 | // HttpRuntime.Cache.Insert(keyString, value, null, DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null); 120 | //} 121 | 122 | ///// 123 | ///// 插入缓存. 124 | ///// 125 | ///// key 126 | ///// value 127 | ///// 过期时间单位秒 128 | ///// 缓存项属性 129 | //public void Add(string key, V value, int cacheDurationInSeconds, CacheDependency dependency, CacheItemPriority priority) 130 | //{ 131 | // //string keyString = CreateKey(key); 132 | // //HttpRuntime.Cache.Insert(keyString, value, dependency, DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null); 133 | //} 134 | 135 | /// 136 | /// 删除缓存 137 | /// 138 | /// key 139 | public override void Remove(string key) 140 | { 141 | V val; 142 | this.InstanceCache.TryRemove(key, out val); 143 | 144 | //throw new NotImplementedException(); 145 | //HttpRuntime.Cache.Remove(CreateKey(key)); 146 | } 147 | 148 | /// 149 | /// 清除所有缓存 150 | /// 151 | public override void RemoveAll() 152 | { 153 | this.InstanceCache.Clear(); 154 | 155 | } 156 | 157 | /// 158 | /// 清除所有缓存 159 | /// 160 | /// 表达式条件 161 | public override void RemoveAll(Func removeExpression) 162 | { 163 | //throw new NotImplementedException(); 164 | //System.Web.Caching.Cache _cache = HttpRuntime.Cache; 165 | var allKeyList = GetAllKey(); 166 | var delKeyList = allKeyList.Where(removeExpression).ToList(); 167 | foreach (var key in delKeyList) 168 | { 169 | Remove(key); 170 | } 171 | } 172 | 173 | /// 174 | /// 获取所有缓存key 175 | /// 176 | /// 177 | public override IEnumerable GetAllKey() 178 | { 179 | return this.InstanceCache.Keys; 180 | 181 | //throw new NotImplementedException(); 182 | //IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator(); 183 | //while (CacheEnum.MoveNext()) 184 | //{ 185 | // yield return CacheEnum.Key.ToString(); 186 | //} 187 | } 188 | #endregion 189 | 190 | 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /SqlSugar/Tool/Check.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace OracleSugar 7 | { 8 | /// 9 | /// ** 描述:验证失败,则抛出异常 10 | /// ** 创始时间:2015-7-19 11 | /// ** 修改时间:- 12 | /// ** 作者:sunkaixuan 13 | /// ** 修改人:sunkaixuan 14 | /// ** 使用说明: 15 | /// 16 | public class Check 17 | { 18 | /// 19 | /// 使用导致此异常的参数的名称初始化 System.ArgumentNullException 类的新实例。 20 | /// 21 | /// 22 | /// 23 | public static void ArgumentNullException(object checkObj, string message) 24 | { 25 | if (checkObj == null) 26 | throw new ArgumentNullException(message); 27 | } 28 | /// 29 | /// 使用指定的错误消息初始化 System.Exception 类的新实例。 30 | /// 31 | /// true则引发异常 32 | /// 错误信息 33 | /// 参数 34 | public static void Exception(bool isException, string message, params string[] args) 35 | { 36 | if (isException) 37 | throw new SqlSugarException(string.Format(message, args)); 38 | } 39 | } 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /SqlSugar/Tool/IStorageObject.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace OracleSugar 3 | { 4 | /// 5 | /// ** 描述:存储对象接口 6 | /// ** 创始时间:2015-5-29 7 | /// ** 修改时间:- 8 | /// ** 作者:sunkaixuan 9 | /// 10 | internal abstract class IStorageObject 11 | { 12 | 13 | public int Minutes = 60; 14 | public int Hour = 60 * 60; 15 | public int Day = 60 * 60 * 24; 16 | public abstract void Add(string key, V value); 17 | public abstract bool ContainsKey(string key); 18 | public abstract V Get(string key); 19 | public abstract global::System.Collections.Generic.IEnumerable GetAllKey(); 20 | public abstract void Remove(string key); 21 | public abstract void RemoveAll(); 22 | public abstract void RemoveAll(Func removeExpression); 23 | public abstract V this[string key] { get; } 24 | } 25 | } -------------------------------------------------------------------------------- /SqlSugar/Tool/IsWhatExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Text.RegularExpressions; 6 | 7 | namespace OracleSugar 8 | { 9 | /// 10 | /// ** 描述:逻辑判段是什么? 11 | /// ** 创始时间:2015-5-29 12 | /// ** 修改时间:- 13 | /// ** 作者:sunkaixuan 14 | /// ** 使用说明:http://www.cnblogs.com/sunkaixuan/p/4539654.html 15 | /// 16 | internal static class IsWhatExtensions 17 | { 18 | /// 19 | /// 值在的范围? 20 | /// 21 | /// 22 | /// 大于等于begin 23 | /// 小于等于end 24 | /// 25 | public static bool IsInRange(this int thisValue, int begin, int end) 26 | { 27 | return thisValue >= begin && thisValue <= end; 28 | } 29 | /// 30 | /// 值在的范围? 31 | /// 32 | /// 33 | /// 大于等于begin 34 | /// 小于等于end 35 | /// 36 | public static bool IsInRange(this DateTime thisValue, DateTime begin, DateTime end) 37 | { 38 | return thisValue >= begin && thisValue <= end; 39 | } 40 | 41 | /// 42 | /// 在里面吗? 43 | /// 44 | /// 45 | /// 46 | /// 47 | /// 48 | public static bool IsIn(this T thisValue, params T[] values) 49 | { 50 | return values.Contains(thisValue); 51 | } 52 | 53 | /// 54 | /// 在里面吗? 55 | /// 56 | /// 57 | /// 58 | /// 59 | public static bool IsContainsIn(this string thisValue, params string[] inValues) 60 | { 61 | return inValues.Any(it => thisValue.Contains(it)); 62 | } 63 | 64 | /// 65 | /// 是null或""? 66 | /// 67 | /// 68 | public static bool IsNullOrEmpty(this object thisValue) 69 | { 70 | if (thisValue == null || thisValue == DBNull.Value) return true; 71 | return thisValue.ToString() == ""; 72 | } 73 | /// 74 | /// 是null或""? 75 | /// 76 | /// 77 | public static bool IsNullOrEmpty(this Guid? thisValue) 78 | { 79 | if (thisValue == null) return true; 80 | return thisValue == Guid.Empty; 81 | } 82 | /// 83 | /// 是null或""? 84 | /// 85 | /// 86 | public static bool IsNullOrEmpty(this Guid thisValue) 87 | { 88 | if (thisValue == null) return true; 89 | return thisValue == Guid.Empty; 90 | } 91 | 92 | /// 93 | /// 有值?(与IsNullOrEmpty相反) 94 | /// 95 | /// 96 | public static bool IsValuable(this object thisValue) 97 | { 98 | if (thisValue == null || thisValue == DBNull.Value) return false; 99 | return thisValue.ToString() != ""; 100 | } 101 | /// 102 | /// 有值?(与IsNullOrEmpty相反) 103 | /// 104 | /// 105 | public static bool IsValuable(this IEnumerable thisValue) 106 | { 107 | if (thisValue == null || thisValue.Count() == 0) return false; 108 | return true; 109 | } 110 | 111 | /// 112 | /// 是零? 113 | /// 114 | /// 115 | /// 116 | public static bool IsZero(this object thisValue) 117 | { 118 | return (thisValue == null || thisValue.ToString() == "0"); 119 | } 120 | 121 | /// 122 | /// 是INT? 123 | /// 124 | /// 125 | /// 126 | public static bool IsInt(this object thisValue) 127 | { 128 | if (thisValue == null) return false; 129 | return Regex.IsMatch(thisValue.ToString(), @"^\d+$"); 130 | } 131 | /// 132 | /// 不是INT? 133 | /// 134 | /// 135 | /// 136 | public static bool IsNoInt(this object thisValue) 137 | { 138 | if (thisValue == null) return true; 139 | return !Regex.IsMatch(thisValue.ToString(), @"^\d+$"); 140 | } 141 | 142 | /// 143 | /// 是金钱? 144 | /// 145 | /// 146 | /// 147 | public static bool IsMoney(this object thisValue) 148 | { 149 | if (thisValue == null) return false; 150 | double outValue = 0; 151 | return double.TryParse(thisValue.ToString(), out outValue); 152 | } 153 | 154 | /// 155 | /// 是GUID? 156 | /// 157 | /// 158 | /// 159 | public static bool IsGuid(this object thisValue) 160 | { 161 | if (thisValue == null) return false; 162 | Guid outValue = Guid.Empty; 163 | return Guid.TryParse(thisValue.ToString(), out outValue); 164 | } 165 | 166 | /// 167 | /// 是时间? 168 | /// 169 | /// 170 | /// 171 | public static bool IsDate(this object thisValue) 172 | { 173 | if (thisValue == null) return false; 174 | DateTime outValue = DateTime.MinValue; 175 | return DateTime.TryParse(thisValue.ToString(), out outValue); 176 | } 177 | 178 | /// 179 | /// 是邮箱? 180 | /// 181 | /// 182 | /// 183 | public static bool IsEamil(this object thisValue) 184 | { 185 | if (thisValue == null) return false; 186 | return Regex.IsMatch(thisValue.ToString(), @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"); 187 | } 188 | 189 | /// 190 | /// 是手机? 191 | /// 192 | /// 193 | /// 194 | public static bool IsMobile(this object thisValue) 195 | { 196 | if (thisValue == null) return false; 197 | return Regex.IsMatch(thisValue.ToString(), @"^\d{11}$"); 198 | } 199 | 200 | /// 201 | /// 是座机? 202 | /// 203 | public static bool IsTelephone(this object thisValue) 204 | { 205 | if (thisValue == null) return false; 206 | return System.Text.RegularExpressions.Regex.IsMatch(thisValue.ToString(), @"^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}$"); 207 | 208 | } 209 | 210 | /// 211 | /// 是身份证? 212 | /// 213 | /// 214 | /// 215 | public static bool IsIDcard(this object thisValue) 216 | { 217 | if (thisValue == null) return false; 218 | return System.Text.RegularExpressions.Regex.IsMatch(thisValue.ToString(), @"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$"); 219 | } 220 | 221 | /// 222 | /// 是传真? 223 | /// 224 | /// 225 | /// 226 | public static bool IsFax(this object thisValue) 227 | { 228 | if (thisValue == null) return false; 229 | return System.Text.RegularExpressions.Regex.IsMatch(thisValue.ToString(), @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$"); 230 | } 231 | 232 | /// 233 | /// 是适合正则匹配? 234 | /// 235 | /// 236 | /// 237 | /// 238 | public static bool IsMatch(this object thisValue, string pattern) 239 | { 240 | if (thisValue == null) return false; 241 | Regex reg = new Regex(pattern); 242 | return reg.IsMatch(thisValue.ToString()); 243 | } 244 | 245 | /// 246 | /// 是否是动态类型 247 | /// 248 | /// 249 | /// 250 | public static bool IsAnonymousType(this Type type) 251 | { 252 | string typeName = type.Name; 253 | return typeName.Contains("<>") && typeName.Contains("__") && typeName.Contains("AnonymousType"); 254 | } 255 | /// 256 | /// 是List类型 257 | /// 258 | /// 259 | /// 260 | public static bool IsCollectionsList(this string thisValue) 261 | { 262 | return (thisValue + "").StartsWith("System.Collections.Generic.List"); 263 | } 264 | /// 265 | /// 是string[]类型 266 | /// 267 | /// 268 | /// 269 | public static bool IsStringArray(this string thisValue) 270 | { 271 | return (thisValue + "").IsMatch(@"System\.[a-z,A-Z,0-9]+?\[\]"); 272 | } 273 | /// 274 | /// 是Enumerable 275 | /// 276 | /// 277 | /// 278 | public static bool IsEnumerable(this string thisValue) 279 | { 280 | return (thisValue + "").StartsWith("System.Linq.Enumerable"); 281 | } 282 | } 283 | } 284 | -------------------------------------------------------------------------------- /SqlSugar/Tool/LanguageHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace OracleSugar 7 | { 8 | /// 9 | /// ** 描述:多语言视图帮助类 10 | /// ** 创始时间:2016-8-7 11 | /// ** 修改时间:- 12 | /// ** 作者:孙凯旋 13 | /// ** 使用说明: 14 | /// 15 | public class LanguageHelper 16 | { 17 | /// 18 | /// 多语言视图的前缀 19 | /// 20 | public static string PreSuffix = "_$_"; 21 | /// 22 | /// 获取所有需要生成多语言的视图名称 23 | /// 24 | /// 25 | /// 26 | internal static List GetLanguageViewNameList(SqlSugarClient db) 27 | { 28 | string key = "LanguageHelper.GetViewNameList"; 29 | var cm = CacheManager>.GetInstance(); 30 | if (cm.ContainsKey(key)) 31 | { 32 | return cm[key]; 33 | } 34 | else 35 | { 36 | var list = db.SqlQuery(@" 37 | select a.name from sys.objects a 38 | JOIN sys.sql_modules b on a.[object_id]=b.[object_id] 39 | where [type]='v' 40 | and b.[definition] like '%" + db.Language.ReplaceViewStringKey + @"%' 41 | and a.name not like '%"+PreSuffix+@"%' 42 | ").ToList(); 43 | cm.Add(key, list, cm.Day); 44 | return list; 45 | } 46 | 47 | } 48 | 49 | /// 50 | /// 创建多语言视图,带有LanguageId=1的所有有视图1替换成languageValue 并且新创视图 名称为 原有视图名+_$_+suffix 51 | /// 52 | /// 53 | public static void UpdateView(PubModel.Language lan, SqlSugarClient db) 54 | { 55 | if (lan == null) return; 56 | if (lan.Suffix.IsNullOrEmpty()) 57 | { 58 | Check.Exception(true, "LanguageHelper.lan.Suffix is Null Or Empty"); 59 | } 60 | if (PreSuffix.IsNullOrEmpty()) 61 | { 62 | Check.Exception(true, "LanguageHelper.PreSuffix is Null Or Empty"); 63 | } 64 | 65 | 66 | if (!lan.Suffix.StartsWith(PreSuffix)) 67 | { 68 | lan.Suffix = PreSuffix + lan.Suffix; 69 | } 70 | 71 | string sql = @" 72 | 73 | --验证参数传递规则 74 | if LEFT(ltrim(@Suffix),3)<>'" + PreSuffix + @"' 75 | begin 76 | raiserror('参数传递格式不规范',16,1) 77 | return; 78 | end 79 | else 80 | if(ISNULL("+lan.LanguageValue+@",'')='') 81 | begin 82 | raiserror('参数传递格式不规范',16,1) 83 | return; 84 | end 85 | 86 | declare 87 | @name varchar(100), --视图名称 88 | @definition varchar(max) --视图脚本 89 | --删除数据库里面所有带传递参数几号的视图 90 | declare my_cursor cursor for 91 | select a.name,b.[definition] from sys.objects a 92 | JOIN sys.sql_modules b on a.[object_id]=b.[object_id] 93 | where [type]='v' 94 | and b.[definition] like '%" + lan.ReplaceViewStringKey + @"%' 95 | and a.name not like '%"+PreSuffix+@"%' 96 | --打开处理器 97 | open my_cursor 98 | fetch next from my_cursor into @name,@definition 99 | while @@FETCH_STATUS=0 100 | begin 101 | --脚本查询语言ID更改,并且更改新脚本语言的对象名称 102 | set @definition=REPLACE( 103 | REPLACE( 104 | @definition, 105 | '" + lan.ReplaceViewStringKey + @"', 106 | '" + string.Format(lan.ReplaceViewStringValue, lan.LanguageValue) + @"' 107 | ), 108 | @name, 109 | @name+@Suffix 110 | ) 111 | --判断新脚本语言的对象名称是否存在,存在删除 112 | exec( 113 | ' 114 | if object_id('''+@name+@Suffix+''',''v'') is not null 115 | begin 116 | drop view '+@name+@Suffix+' 117 | end 118 | 119 | ' 120 | ) 121 | exec(@definition) 122 | fetch next from my_cursor into @name,@definition 123 | end 124 | close my_cursor 125 | deallocate my_cursor 126 | "; 127 | 128 | db.ExecuteCommand(sql, new { Suffix = lan.Suffix }); 129 | 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /SqlSugar/Tool/PubConvert.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace OracleSugar 7 | { 8 | /// 9 | /// ** 描述:公用转换函数 10 | /// ** 创始时间:2015-6-9 11 | /// ** 修改时间:- 12 | /// ** 作者:sunkaixuan 13 | /// ** 使用说明: 14 | /// 15 | public static class PubConvert 16 | { 17 | #region 强转成int 如果失败返回 0 18 | /// 19 | /// 强转成int 如果失败返回 0 20 | /// 21 | /// 22 | /// 23 | public static int ObjToInt(this object thisValue) 24 | { 25 | int reval = 0; 26 | if (thisValue == null) return 0; 27 | if (thisValue.GetType().IsEnum) return Convert.ToInt32(thisValue); 28 | if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval)) 29 | { 30 | return reval; 31 | } 32 | return reval; 33 | } 34 | #endregion 35 | #region 强转成int 如果失败返回 errorValue 36 | /// 37 | /// 强转成int 如果失败返回 errorValue 38 | /// 39 | /// 40 | /// 41 | /// 42 | public static int ObjToInt(this object thisValue, int errorValue) 43 | { 44 | int reval = 0; 45 | if (thisValue != null &&thisValue != DBNull.Value&& int.TryParse(thisValue.ToString(), out reval)) 46 | { 47 | return reval; 48 | } 49 | return errorValue; 50 | } 51 | #endregion 52 | #region 强转成double 如果失败返回 0 53 | /// 54 | /// 强转成money 如果失败返回 0 55 | /// 56 | /// 57 | /// 58 | public static double ObjToMoney(this object thisValue) 59 | { 60 | double reval = 0; 61 | if (thisValue != null &&thisValue != DBNull.Value&& double.TryParse(thisValue.ToString(), out reval)) 62 | { 63 | return reval; 64 | } 65 | return 0; 66 | } 67 | #endregion 68 | #region 强转成double 如果失败返回 errorValue 69 | /// 70 | /// 强转成double 如果失败返回 errorValue 71 | /// 72 | /// 73 | /// 74 | /// 75 | public static double ObjToMoney(this object thisValue, double errorValue) 76 | { 77 | double reval = 0; 78 | if (thisValue != null &&thisValue != DBNull.Value&& double.TryParse(thisValue.ToString(), out reval)) 79 | { 80 | return reval; 81 | } 82 | return errorValue; 83 | } 84 | #endregion 85 | #region 强转成string 如果失败返回 "" 86 | /// 87 | /// 强转成string 如果失败返回 "" 88 | /// 89 | /// 90 | /// 91 | public static string ObjToString(this object thisValue) 92 | { 93 | if (thisValue != null) return thisValue.ToString().Trim(); 94 | return ""; 95 | } 96 | #endregion 97 | #region 强转成string 如果失败返回 errorValue 98 | /// 99 | /// 强转成string 如果失败返回 str 100 | /// 101 | /// 102 | /// 103 | /// 104 | public static string ObjToString(this object thisValue, string errorValue) 105 | { 106 | if (thisValue != null) return thisValue.ToString().Trim(); 107 | return errorValue; 108 | } 109 | #endregion 110 | #region 强转成Decimal 如果失败返回 0 111 | /// 112 | /// 强转成Decimal 如果失败返回 0 113 | /// 114 | /// 115 | /// 116 | public static Decimal ObjToDecimal(this object thisValue) 117 | { 118 | Decimal reval = 0; 119 | if (thisValue != null &&thisValue != DBNull.Value&& decimal.TryParse(thisValue.ToString(), out reval)) 120 | { 121 | return reval; 122 | } 123 | return 0; 124 | } 125 | #endregion 126 | #region 强转成Decimal 如果失败返回 errorValue 127 | /// 128 | /// 强转成Decimal 如果失败返回 errorValue 129 | /// 130 | /// 131 | /// 132 | /// 133 | public static Decimal ObjToDecimal(this object thisValue, decimal errorValue) 134 | { 135 | Decimal reval = 0; 136 | if (thisValue != null &&thisValue != DBNull.Value&& decimal.TryParse(thisValue.ToString(), out reval)) 137 | { 138 | return reval; 139 | } 140 | return errorValue; 141 | } 142 | #endregion 143 | #region 强转成DateTime 如果失败返回 DateTime.MinValue 144 | /// 145 | /// 强转成DateTime 如果失败返回 DateTime.MinValue 146 | /// 147 | /// 148 | /// 149 | public static DateTime ObjToDate(this object thisValue) 150 | { 151 | DateTime reval = DateTime.MinValue; 152 | if (thisValue != null &&thisValue != DBNull.Value&& DateTime.TryParse(thisValue.ToString(), out reval)) 153 | { 154 | return reval; 155 | } 156 | return reval; 157 | } 158 | #endregion 159 | #region 强转成DateTime 如果失败返回 errorValue 160 | /// 161 | /// 强转成DateTime 如果失败返回 errorValue 162 | /// 163 | /// 164 | /// 165 | /// 166 | public static DateTime ObjToDate(this object thisValue, DateTime errorValue) 167 | { 168 | DateTime reval = DateTime.MinValue; 169 | if (thisValue != null &&thisValue != DBNull.Value&& DateTime.TryParse(thisValue.ToString(), out reval)) 170 | { 171 | return reval; 172 | } 173 | return errorValue; 174 | } 175 | #endregion 176 | #region 强转成Bool 如果失败返回 false 177 | /// 178 | /// 强转成Bool 如果失败返回 false 179 | /// 180 | /// 181 | /// 182 | public static bool ObjToBool(this object thisValue) 183 | { 184 | bool reval = false; 185 | if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval)) 186 | { 187 | return reval; 188 | } 189 | return reval; 190 | } 191 | #endregion 192 | } 193 | } 194 | -------------------------------------------------------------------------------- /SqlSugar/Tool/SqlException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace OracleSugar 7 | { 8 | /// 9 | /// ** 描述:SqlSugar自定义异常 10 | /// ** 创始时间:2015-7-13 11 | /// ** 修改时间:- 12 | /// ** 作者:sunkaixuan 13 | /// ** 使用说明: 14 | /// 15 | public class SqlSugarException : Exception 16 | { 17 | /// 18 | /// SqlSugar异常 19 | /// 20 | /// 错误信息 21 | public SqlSugarException(string message) 22 | : base(message) 23 | { 24 | 25 | } 26 | /// 27 | /// SqlSugar异常 28 | /// 29 | /// 错误信息 30 | /// ORM生成的SQL 31 | public SqlSugarException(string message, string sql) 32 | : base(GetMessage(message, sql)) 33 | { 34 | 35 | } 36 | /// 37 | /// SqlSugar异常 38 | /// 39 | /// 错误信息 40 | /// ORM生成的SQL 41 | /// 错误函数的参数 42 | public SqlSugarException(string message, string sql, object pars) 43 | : base(GetMessage(message, sql, pars)) 44 | { 45 | 46 | } 47 | /// 48 | /// SqlSugar异常 49 | /// 50 | /// 错误信息 51 | /// 错误函数的参数 52 | public SqlSugarException(string message,object pars) 53 | : base(GetMessage(message,pars)) 54 | { 55 | 56 | } 57 | 58 | private static string GetMessage(string message, object pars) 59 | { 60 | var parsStr = string.Empty; ; 61 | if (pars != null) 62 | { 63 | parsStr = JsonConverter.Serialize(pars); 64 | } 65 | var reval = GetLineMessage("错误信息", message) + GetLineMessage("函数参数", parsStr); 66 | return reval; 67 | 68 | } 69 | 70 | 71 | private static string GetMessage(string message, string sql, object pars) 72 | { 73 | if (pars == null) 74 | { 75 | return GetMessage(message, sql); 76 | } 77 | else 78 | { 79 | var reval = GetLineMessage("错误信息 ", message) + GetLineMessage("ORM生成的Sql", sql) + GetLineMessage("函数参数 ", JsonConverter.Serialize(pars)); 80 | return reval; 81 | } 82 | } 83 | 84 | 85 | private static string GetMessage(string message, string sql) 86 | { 87 | var reval = GetLineMessage("错误信息 ", message) + GetLineMessage("ORM生成的Sql", sql); 88 | return reval; 89 | } 90 | 91 | private static string GetLineMessage(string key, string value) 92 | { 93 | return string.Format("{0} : 【{1}】\r\n", key, value); 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /SqlSugar/Tool/SqlSugarToolCoreDiff.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Data; 6 | using System.Web; 7 | using Oracle.ManagedDataAccess.Client; 8 | 9 | namespace OracleSugar 10 | { 11 | /// 12 | /// SqlSugarTool局部类与Core有差异的部分(方便工具移植到.NetCore版本) 13 | /// 14 | public partial class SqlSugarTool 15 | { 16 | private static void FillValueTypeToDictionary(Type type, IDataReader dr, List strReval) 17 | { 18 | using (IDataReader re = dr) 19 | { 20 | Dictionary reval = new Dictionary(); 21 | while (re.Read()) 22 | { 23 | if (SqlSugarTool.DicOO == type) 24 | { 25 | var kv = new KeyValuePair((object)Convert.ChangeType(re.GetValue(0), typeof(object)), (int)Convert.ChangeType(re.GetValue(1), typeof(object))); 26 | strReval.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair))); 27 | } 28 | else if (SqlSugarTool.Dicii == type) 29 | { 30 | var kv = new KeyValuePair((int)Convert.ChangeType(re.GetValue(0), typeof(int)), (int)Convert.ChangeType(re.GetValue(1), typeof(int))); 31 | strReval.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair))); 32 | } 33 | else if (SqlSugarTool.DicSi == type) 34 | { 35 | var kv = new KeyValuePair((string)Convert.ChangeType(re.GetValue(0), typeof(string)), (int)Convert.ChangeType(re.GetValue(1), typeof(int))); 36 | strReval.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair))); 37 | } 38 | else if (SqlSugarTool.DicSo == type) 39 | { 40 | var kv = new KeyValuePair((string)Convert.ChangeType(re.GetValue(0), typeof(string)), (object)Convert.ChangeType(re.GetValue(1), typeof(object))); 41 | strReval.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair))); 42 | } 43 | else if (SqlSugarTool.DicSS == type) 44 | { 45 | var kv = new KeyValuePair((string)Convert.ChangeType(re.GetValue(0), typeof(string)), (string)Convert.ChangeType(re.GetValue(1), typeof(string))); 46 | strReval.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair))); 47 | } 48 | else 49 | { 50 | Check.Exception(true, "暂时不支持该类型的Dictionary 你可以试试 Dictionary或者联系作者!!"); 51 | } 52 | } 53 | } 54 | } 55 | 56 | private static void FillValueTypeToArray(Type type, IDataReader dr, List strReval) 57 | { 58 | using (IDataReader re = dr) 59 | { 60 | int count = dr.FieldCount; 61 | var childType = type.GetElementType(); 62 | while (re.Read()) 63 | { 64 | object[] array = new object[count]; 65 | for (int i = 0; i < count; i++) 66 | { 67 | array[i] = Convert.ChangeType(re.GetValue(i), childType); 68 | } 69 | if (childType == SqlSugarTool.StringType) 70 | strReval.Add((T)Convert.ChangeType(array.Select(it => (string)it).ToArray(), type)); 71 | else if (childType == SqlSugarTool.ObjType) 72 | strReval.Add((T)Convert.ChangeType(array.Select(it => (object)it).ToArray(), type)); 73 | else if (childType == SqlSugarTool.BoolType) 74 | strReval.Add((T)Convert.ChangeType(array.Select(it => (bool)it).ToArray(), type)); 75 | else if (childType == SqlSugarTool.ByteType) 76 | strReval.Add((T)Convert.ChangeType(array.Select(it => (byte)it).ToArray(), type)); 77 | else if (childType == SqlSugarTool.DecType) 78 | strReval.Add((T)Convert.ChangeType(array.Select(it => (decimal)it).ToArray(), type)); 79 | else if (childType == SqlSugarTool.GuidType) 80 | strReval.Add((T)Convert.ChangeType(array.Select(it => (Guid)it).ToArray(), type)); 81 | else if (childType == SqlSugarTool.DateType) 82 | strReval.Add((T)Convert.ChangeType(array.Select(it => (DateTime)it).ToArray(), type)); 83 | else if (childType == SqlSugarTool.IntType) 84 | strReval.Add((T)Convert.ChangeType(array.Select(it => (int)it).ToArray(), type)); 85 | else 86 | Check.Exception(true, "暂时不支持该类型的Array 你可以试试 object[] 或者联系作者!!"); 87 | } 88 | } 89 | } 90 | 91 | /// 92 | /// 获取参数到键值集合根据页面Request参数 93 | /// 94 | /// 95 | public static Dictionary GetParameterDictionary(bool isNotNullAndEmpty = false) 96 | { 97 | if (SpecialRequestForm == null) 98 | { 99 | Dictionary paraDictionaryByGet = HttpContext.Current.Request.QueryString.Keys.Cast() 100 | .ToDictionary(k => k, v => HttpContext.Current.Request.QueryString[v]); 101 | 102 | Dictionary paraDictionaryByPost = HttpContext.Current.Request.Form.Keys.Cast() 103 | .ToDictionary(k => k, v => HttpContext.Current.Request.Form[v]); 104 | 105 | var paraDictionarAll = paraDictionaryByGet.Union(paraDictionaryByPost); 106 | if (isNotNullAndEmpty) 107 | { 108 | paraDictionarAll = paraDictionarAll.Where(it => !string.IsNullOrEmpty(it.Value)); 109 | } 110 | return paraDictionarAll.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value); 111 | } 112 | else 113 | { 114 | 115 | var pars = HttpContext.Current.Request.Form.Keys.Cast() 116 | .ToDictionary(k => k, v => SpecialRequestForm(v)).Where(it => true); 117 | if (isNotNullAndEmpty) 118 | { 119 | pars = pars.Where(it => !string.IsNullOrEmpty(it.Value)); 120 | } 121 | return pars.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value); 122 | } 123 | } 124 | 125 | /// 126 | /// 获取参数到键值集合根据页面Request参数 127 | /// 128 | /// 129 | public static OracleParameter[] GetParameterArray(bool isNotNullAndEmpty = false) 130 | { 131 | Dictionary paraDictionaryByGet = HttpContext.Current.Request.QueryString.Keys.Cast() 132 | .ToDictionary(k => k, v => HttpContext.Current.Request.QueryString[v]); 133 | 134 | Dictionary paraDictionaryByPost = HttpContext.Current.Request.Form.Keys.Cast() 135 | .ToDictionary(k => k, v => HttpContext.Current.Request.Form[v]); 136 | 137 | var paraDictionarAll = paraDictionaryByGet.Union(paraDictionaryByPost); 138 | if (isNotNullAndEmpty) 139 | { 140 | paraDictionarAll = paraDictionarAll.Where(it => !string.IsNullOrEmpty(it.Value)); 141 | } 142 | return paraDictionarAll.Select(it => new OracleParameter(SqlSugarTool.ParSymbol + it.Key, it.Value)).ToArray(); 143 | } 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /SqlSugar/Tool/SqlSugarToolExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Text.RegularExpressions; 6 | using Oracle.ManagedDataAccess.Client; 7 | 8 | namespace OracleSugar 9 | { 10 | /// 11 | /// ** 描述:SqlSugar扩展工具类 12 | /// ** 创始时间:2015-7-19 13 | /// ** 修改时间:- 14 | /// ** 作者:sunkaixuan 15 | /// ** 使用说明: 16 | /// 17 | public static class SqlSugarToolExtensions 18 | { 19 | 20 | /// 21 | /// 将数组转换成Where In 需要的格式(例如:参数 new int{1,2,3} 反回 "'1','2','3'") 22 | /// 23 | /// 24 | /// 25 | public static string ToJoinSqlInVal(this T[] array) 26 | { 27 | if (array == null || array.Length == 0) 28 | { 29 | return ToSqlValue(string.Empty); 30 | } 31 | else 32 | { 33 | return string.Join(",", array.Where(c => c != null).Select(it => (it + "").ToSuperSqlFilter().ToSqlValue())); 34 | } 35 | } 36 | /// 37 | /// 将字符串转换成SQL参数所需要的格式(例如: 参数value返回'value') 38 | /// 39 | /// 40 | /// 41 | public static string ToSqlValue(this string value) 42 | { 43 | return string.Format("'{0}'", value.ToSqlFilter()); 44 | } 45 | /// 46 | ///SQL注入过滤 47 | /// 48 | /// 49 | /// 50 | public static string ToSqlFilter(this string value) 51 | { 52 | if (!value.IsNullOrEmpty()) 53 | { 54 | if (Regex.IsMatch(value, @"%\d|0x\d|0x[0-9,a-z]{6,300}|(\@.*\=)", RegexOptions.IgnoreCase)) 55 | { 56 | throw new SqlSugarException("查询参数不允许存在特殊字符。"); 57 | } 58 | value = value.Replace("'", "''"); 59 | } 60 | return value; 61 | } 62 | /// 63 | /// 指定数据类型,如果不在指定类当中则引发异常(只允许输入指定字母、数字、下划线、时间、GUID) 64 | /// 65 | /// 66 | /// 67 | public static string ToSuperSqlFilter(this string value) 68 | { 69 | if (value.IsNullOrEmpty()) return value; 70 | if (Regex.IsMatch(value, @"^(\w|\.|\:|\-| |\,)+$")) 71 | { 72 | return value; 73 | } 74 | throw new SqlSugarException("指定类型(只允许输入指定字母、数字、下划线、时间、guid)。"); 75 | } 76 | 77 | /// 78 | /// 获取锁字符串 79 | /// 80 | /// 81 | /// 82 | internal static string GetLockString(this bool isNoLock) 83 | { 84 | return isNoLock ? "WITH(NOLOCK)" : null; ; 85 | } 86 | /// 87 | /// 获取Select需要的字段 88 | /// 89 | /// 90 | /// 91 | internal static string GetSelectFiles(this string selectFileds) 92 | { 93 | return selectFileds.IsNullOrEmpty() ? "*" : selectFileds; 94 | } 95 | /// 96 | /// 获取Select需要的字段 97 | /// 98 | /// 99 | /// 100 | /// 101 | public static string GetSelectFiles(this string selectFileds, string typeName) 102 | { 103 | var reval = selectFileds.IsNullOrEmpty() ? "*" : selectFileds; 104 | return reval == "*" ? string.Format("{0}.*", typeName) : selectFileds; 105 | } 106 | /// 107 | /// 获取分组 108 | /// 109 | /// 110 | /// 111 | internal static string GetGroupBy(this string groupByFileds) 112 | { 113 | return groupByFileds.IsNullOrEmpty() ? "" : " GROUP BY " + groupByFileds; 114 | } 115 | /// 116 | /// 将Request里的参数转成OracleParameter[] 117 | /// 118 | /// 119 | internal static void RequestParasToOracleParameters(OracleParameterCollection oldParas) 120 | { 121 | var oldParaList = oldParas.Cast().ToList(); 122 | var paraDictionarAll = SqlSugarTool.GetParameterDictionary(); 123 | if (paraDictionarAll != null && paraDictionarAll.Count() > 0) 124 | { 125 | 126 | foreach (KeyValuePair it in paraDictionarAll) 127 | { 128 | 129 | var par = new OracleParameter("@" + it.Key, it.Value); 130 | if (!oldParaList.Any(oldPara => oldPara.ParameterName == ("@" + it.Key))) 131 | { 132 | oldParas.Add(par); 133 | } 134 | } 135 | } 136 | } 137 | /// 138 | /// 获取转释后的表名和列名 139 | /// 140 | /// 141 | /// 142 | public static string GetTranslationSqlName(this string tableName) 143 | { 144 | return SqlSugarTool.GetTranslationSqlName(tableName); 145 | } 146 | /// 147 | /// 获取参数名 148 | /// 149 | /// 150 | /// 151 | internal static string GetOracleParameterName(this string name) 152 | { 153 | return SqlSugarTool.GetOracleParameterName(name); 154 | } 155 | 156 | /// 157 | ///获取没有符号的参数名称 158 | /// 159 | /// 160 | /// 161 | internal static string GetOracleParameterNameNoParSymbol(this string name) 162 | { 163 | return SqlSugarTool.GetOracleParameterNameNoParSymbol(name); 164 | } 165 | 166 | /// 167 | /// 数组条件筛选 168 | /// 169 | /// 170 | /// 171 | /// 172 | internal static string[] ArrayWhere(this string[] thisValue, Func expression) 173 | { 174 | if (thisValue == null) return null; 175 | thisValue = thisValue.Where(expression).ToArray(); 176 | return thisValue; 177 | } 178 | 179 | /// 180 | /// 数组添加元素 181 | /// 182 | /// 183 | /// 184 | /// 185 | internal static string[] ArrayAdd(this string[] thisValue, params string[] items) 186 | { 187 | if (thisValue == null) thisValue = new string[] { }; 188 | var reval = thisValue.ToList(); 189 | reval.AddRange(items); 190 | thisValue = reval.ToArray(); 191 | return thisValue; 192 | } 193 | 194 | /// 195 | /// 数组移除 196 | /// 197 | /// 198 | /// 199 | /// 200 | internal static string[] ArrayRemove(this string[] thisValue, string item) 201 | { 202 | if (thisValue == null) thisValue = new string[] { }; 203 | var reval = thisValue.ToList(); 204 | reval.Remove(item); 205 | thisValue = reval.ToArray(); 206 | return thisValue; 207 | } 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /SqlSugar/nuget.bat: -------------------------------------------------------------------------------- 1 | %~dp0nuget.exe pack %~dp0OracleSugar.csproj -OutputDirectory %~dp0 --------------------------------------------------------------------------------