DbFirstProvider.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. namespace SqlSugar
  8. {
  9. public abstract partial class DbFirstProvider : IDbFirst
  10. {
  11. public virtual ISqlSugarClient Context { get; set; }
  12. private string ClassTemplate { get; set; }
  13. private string ClassDescriptionTemplate { get; set; }
  14. private string PropertyTemplate { get; set; }
  15. private string PropertyDescriptionTemplate { get; set; }
  16. private string ConstructorTemplate { get; set; }
  17. private string UsingTemplate { get; set; }
  18. private string Namespace { get; set; }
  19. private bool IsAttribute { get; set; }
  20. private bool IsDefaultValue { get; set; }
  21. private Func<string, bool> WhereColumnsfunc;
  22. private Func<string, string> FormatFileNameFunc { get; set; }
  23. private Func<string, string> FormatClassNameFunc { get; set; }
  24. private Func<string, string> FormatPropertyNameFunc { get; set; }
  25. private bool IsStringNullable {get;set; }
  26. private Func<DbColumnInfo,string,string,string> PropertyTextTemplateFunc { get; set; }
  27. private Func<string, string> ReplaceClassStringFunc { get; set; }
  28. private ISqlBuilder SqlBuilder
  29. {
  30. get
  31. {
  32. return InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
  33. }
  34. }
  35. private List<DbTableInfo> TableInfoList { get; set; }
  36. public DbFirstProvider()
  37. {
  38. this.ClassTemplate = DbFirstTemplate.ClassTemplate;
  39. this.ClassDescriptionTemplate = DbFirstTemplate.ClassDescriptionTemplate;
  40. this.PropertyTemplate = DbFirstTemplate.PropertyTemplate;
  41. this.PropertyDescriptionTemplate = DbFirstTemplate.PropertyDescriptionTemplate;
  42. this.ConstructorTemplate = DbFirstTemplate.ConstructorTemplate;
  43. this.UsingTemplate = DbFirstTemplate.UsingTemplate;
  44. }
  45. public void Init()
  46. {
  47. this.Context.Utilities.RemoveCacheAll();
  48. if (!this.Context.DbMaintenance.IsAnySystemTablePermissions())
  49. {
  50. Check.Exception(true, "Dbfirst and Codefirst requires system table permissions");
  51. }
  52. this.TableInfoList = this.Context.Utilities.TranslateCopy(this.Context.DbMaintenance.GetTableInfoList());
  53. var viewList = this.Context.Utilities.TranslateCopy(this.Context.DbMaintenance.GetViewInfoList());
  54. if (viewList.HasValue())
  55. {
  56. this.TableInfoList.AddRange(viewList);
  57. }
  58. }
  59. #region Setting Template
  60. public IDbFirst StringNullable()
  61. {
  62. IsStringNullable = true;
  63. return this;
  64. }
  65. public IDbFirst SettingClassDescriptionTemplate(Func<string, string> func)
  66. {
  67. this.ClassDescriptionTemplate = func(this.ClassDescriptionTemplate);
  68. return this;
  69. }
  70. public IDbFirst SettingClassTemplate(Func<string, string> func)
  71. {
  72. this.ClassTemplate = func(this.ClassTemplate);
  73. return this;
  74. }
  75. public IDbFirst SettingConstructorTemplate(Func<string, string> func)
  76. {
  77. this.ConstructorTemplate = func(this.ConstructorTemplate);
  78. return this;
  79. }
  80. public IDbFirst SettingPropertyDescriptionTemplate(Func<string, string> func)
  81. {
  82. this.PropertyDescriptionTemplate = func(this.PropertyDescriptionTemplate);
  83. return this;
  84. }
  85. public IDbFirst SettingNamespaceTemplate(Func<string, string> func)
  86. {
  87. this.UsingTemplate = func(this.UsingTemplate);
  88. return this;
  89. }
  90. public IDbFirst SettingPropertyTemplate(Func<string, string> func)
  91. {
  92. this.PropertyTemplate = func(this.PropertyTemplate);
  93. return this;
  94. }
  95. public IDbFirst SettingPropertyTemplate(Func<DbColumnInfo, string,string,string> func)
  96. {
  97. this.PropertyTextTemplateFunc = func;
  98. return this;
  99. }
  100. public RazorFirst UseRazorAnalysis(string razorClassTemplate, string classNamespace = "Models")
  101. {
  102. if (razorClassTemplate == null)
  103. {
  104. razorClassTemplate = "";
  105. }
  106. razorClassTemplate = razorClassTemplate.Replace("@Model.Namespace", classNamespace);
  107. var result = new RazorFirst();
  108. if (this.Context.CurrentConnectionConfig.ConfigureExternalServices?.RazorService != null)
  109. {
  110. List<RazorTableInfo> razorList = new List<RazorTableInfo>();
  111. var tables = this.TableInfoList;
  112. if (tables.HasValue())
  113. {
  114. foreach (var item in tables)
  115. {
  116. var columns = this.Context.DbMaintenance.GetColumnInfosByTableName(item.Name, false);
  117. RazorTableInfo table = new RazorTableInfo()
  118. {
  119. Columns = columns.Where(it => WhereColumnsfunc == null || WhereColumnsfunc(it.DbColumnName)).Select(it => new RazorColumnInfo()
  120. {
  121. ColumnDescription = it.ColumnDescription,
  122. DataType = it.DataType,
  123. DbColumnName = it.DbColumnName,
  124. DefaultValue = it.DefaultValue,
  125. IsIdentity = it.IsIdentity,
  126. IsNullable = it.IsNullable,
  127. IsPrimarykey = it.IsPrimarykey,
  128. Length = it.Length
  129. }).ToList(),
  130. Description = item.Description,
  131. DbTableName = item.Name
  132. };
  133. foreach (var col in table.Columns)
  134. {
  135. col.DataType = GetPropertyTypeName(columns.First(it => it.DbColumnName == col.DbColumnName));
  136. }
  137. razorList.Add(table);
  138. }
  139. }
  140. result.ClassStringList = this.Context.CurrentConnectionConfig.ConfigureExternalServices.RazorService.GetClassStringList(razorClassTemplate, razorList);
  141. }
  142. else
  143. {
  144. Check.Exception(true, ErrorMessage.GetThrowMessage("Need to achieve ConnectionConfig.ConfigureExternal Services.RazorService", "需要实现 ConnectionConfig.ConfigureExternal Services.RazorService接口"));
  145. }
  146. this.Context.Utilities.RemoveCacheAll();
  147. result.FormatFileNameFunc = this.FormatFileNameFunc;
  148. return result;
  149. }
  150. #endregion
  151. #region Setting Content
  152. public IDbFirst IsCreateAttribute(bool isCreateAttribute = true)
  153. {
  154. this.IsAttribute = isCreateAttribute;
  155. return this;
  156. }
  157. public IDbFirst FormatFileName(Func<string, string> formatFileNameFunc)
  158. {
  159. this.FormatFileNameFunc = formatFileNameFunc;
  160. return this;
  161. }
  162. public IDbFirst FormatClassName(Func<string, string> formatClassNameFunc)
  163. {
  164. this.FormatClassNameFunc = formatClassNameFunc;
  165. return this;
  166. }
  167. public IDbFirst FormatPropertyName(Func<string, string> formatPropertyNameFunc)
  168. {
  169. this.FormatPropertyNameFunc = formatPropertyNameFunc;
  170. return this;
  171. }
  172. public IDbFirst CreatedReplaceClassString(Func<string, string> replaceClassStringFunc)
  173. {
  174. this.ReplaceClassStringFunc = replaceClassStringFunc;
  175. return this;
  176. }
  177. public IDbFirst IsCreateDefaultValue(bool isCreateDefaultValue = true)
  178. {
  179. this.IsDefaultValue = isCreateDefaultValue;
  180. return this;
  181. }
  182. #endregion
  183. #region Where
  184. public IDbFirst Where(DbObjectType dbObjectType)
  185. {
  186. if (dbObjectType != DbObjectType.All)
  187. this.TableInfoList = this.TableInfoList.Where(it => it.DbObjectType == dbObjectType).ToList();
  188. return this;
  189. }
  190. public IDbFirst Where(Func<string, bool> func)
  191. {
  192. this.TableInfoList = this.TableInfoList.Where(it => func(it.Name)).ToList();
  193. return this;
  194. }
  195. public IDbFirst WhereColumns(Func<string, bool> func)
  196. {
  197. WhereColumnsfunc = func;
  198. return this;
  199. }
  200. public IDbFirst Where(params string[] objectNames)
  201. {
  202. if (objectNames.HasValue())
  203. {
  204. this.TableInfoList = this.TableInfoList.Where(it => objectNames.Select(x => x.ToLower()).Contains(it.Name.ToLower())).ToList();
  205. }
  206. return this;
  207. }
  208. #endregion
  209. #region Core
  210. public Dictionary<string, string> ToClassStringList(string nameSpace = "Models")
  211. {
  212. this.Namespace = nameSpace;
  213. Dictionary<string, string> result = new Dictionary<string, string>();
  214. if (this.TableInfoList.HasValue())
  215. {
  216. foreach (var tableInfo in this.TableInfoList)
  217. {
  218. try
  219. {
  220. string classText = null;
  221. string className = tableInfo.Name;
  222. var oldClasName = className;
  223. classText = GetClassString(tableInfo, ref className);
  224. result.Remove(className);
  225. if (this.ReplaceClassStringFunc != null)
  226. {
  227. classText=this.ReplaceClassStringFunc(classText);
  228. }
  229. if (FormatClassNameFunc != null&&FormatFileNameFunc != null)
  230. {
  231. className = oldClasName;
  232. }
  233. result.Add(className, classText);
  234. }
  235. catch (Exception ex)
  236. {
  237. Check.Exception(true, "Table '{0}' error,You can filter it with Db.DbFirst.Where(name=>name!=\"{0}\" ) \r\n Error message:{1}", tableInfo.Name, ex.Message);
  238. }
  239. }
  240. }
  241. return result;
  242. }
  243. internal string GetClassString(DbTableInfo tableInfo, ref string className)
  244. {
  245. string classText;
  246. var columns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableInfo.Name,false);
  247. if (this.Context.IgnoreColumns.HasValue())
  248. {
  249. var entityName = this.Context.EntityMaintenance.GetEntityName(tableInfo.Name);
  250. columns = columns.Where(c =>
  251. !this.Context.IgnoreColumns.Any(ig => ig.EntityName.Equals(entityName, StringComparison.CurrentCultureIgnoreCase) && c.DbColumnName == ig.PropertyName)
  252. ).ToList();
  253. }
  254. classText = this.ClassTemplate;
  255. string ConstructorText = IsDefaultValue ? this.ConstructorTemplate : null;
  256. if (this.Context.MappingTables.HasValue())
  257. {
  258. var mappingInfo = this.Context.MappingTables.FirstOrDefault(it => it.DbTableName.Equals(tableInfo.Name, StringComparison.CurrentCultureIgnoreCase));
  259. if (mappingInfo.HasValue())
  260. {
  261. className = mappingInfo.EntityName;
  262. }
  263. if (mappingInfo != null)
  264. {
  265. classText = classText.Replace(DbFirstTemplate.KeyClassName, mappingInfo.EntityName);
  266. }
  267. }
  268. if (FormatClassNameFunc != null)
  269. {
  270. className=FormatClassNameFunc(className);
  271. }
  272. classText = classText.Replace(DbFirstTemplate.KeyClassName, className);
  273. classText = classText.Replace(DbFirstTemplate.KeyNamespace, this.Namespace);
  274. classText = classText.Replace(DbFirstTemplate.KeyUsing, IsAttribute ? (this.UsingTemplate + "using " + UtilConstants.AssemblyName + ";\r\n") : this.UsingTemplate);
  275. classText = classText.Replace(DbFirstTemplate.KeyClassDescription, this.ClassDescriptionTemplate.Replace(DbFirstTemplate.KeyClassDescription, tableInfo.Description + "\r\n"));
  276. classText = classText.Replace(DbFirstTemplate.KeySugarTable, IsAttribute ? string.Format(DbFirstTemplate.ValueSugarTable, tableInfo.Name) : null);
  277. if (columns.HasValue())
  278. {
  279. foreach (var item in columns.Where(it => WhereColumnsfunc == null || WhereColumnsfunc(it.DbColumnName)))
  280. {
  281. var isLast = columns.Last() == item;
  282. var index = columns.IndexOf(item);
  283. string PropertyText = this.PropertyTemplate;
  284. string PropertyDescriptionText = this.PropertyDescriptionTemplate;
  285. string propertyName = GetPropertyName(item);
  286. var oldPropertyName = propertyName;
  287. if (FormatPropertyNameFunc != null)
  288. {
  289. item.DbColumnName=propertyName = FormatPropertyNameFunc(propertyName);
  290. }
  291. string propertyTypeName = GetPropertyTypeName(item);
  292. PropertyText =this.PropertyTextTemplateFunc == null? GetPropertyText(item, PropertyText):this.PropertyTextTemplateFunc(item,this.PropertyTemplate, propertyTypeName);
  293. PropertyDescriptionText = GetPropertyDescriptionText(item, PropertyDescriptionText);
  294. if (this.IsAttribute && item.DataType?.StartsWith("_") == true && PropertyText.Contains("[]"))
  295. {
  296. PropertyDescriptionText += "\r\n [SugarColumn(IsArray=true)]";
  297. }
  298. else if (item?.DataType?.StartsWith("json") == true)
  299. {
  300. PropertyDescriptionText += "\r\n [SugarColumn(IsJson=true)]";
  301. }
  302. else if (FormatPropertyNameFunc != null)
  303. {
  304. if (PropertyText.Contains("SugarColumn"))
  305. {
  306. PropertyText = PropertyText.Replace(")]", ",ColumnName=\"" + oldPropertyName + "\")]");
  307. }
  308. else
  309. {
  310. PropertyDescriptionText += "\r\n [SugarColumn(ColumnName=\"" + oldPropertyName + "\")]";
  311. }
  312. }
  313. PropertyText = PropertyDescriptionText + PropertyText;
  314. classText = classText.Replace(DbFirstTemplate.KeyPropertyName, PropertyText + (isLast ? "" : ("\r\n" + DbFirstTemplate.KeyPropertyName)));
  315. if (ConstructorText.HasValue() && item.DefaultValue != null)
  316. {
  317. var hasDefaultValue = columns.Skip(index + 1).Any(it => it.DefaultValue.HasValue());
  318. if (item.DefaultValue.EqualCase("CURRENT_TIMESTAMP"))
  319. {
  320. item.DefaultValue = "DateTime.Now";
  321. }
  322. else if (item.DefaultValue == "b'1'")
  323. {
  324. item.DefaultValue = "1";
  325. }
  326. ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyPropertyName, propertyName);
  327. ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyDefaultValue, GetPropertyTypeConvert(item)) + (!hasDefaultValue ? "" : this.ConstructorTemplate);
  328. }
  329. }
  330. }
  331. if (!columns.Any(it => it.DefaultValue != null))
  332. {
  333. ConstructorText = null;
  334. }
  335. classText = classText.Replace(DbFirstTemplate.KeyConstructor, ConstructorText);
  336. classText = classText.Replace(DbFirstTemplate.KeyPropertyName, null);
  337. return classText;
  338. }
  339. internal string GetClassString(List<DbColumnInfo> columns, ref string className)
  340. {
  341. string classText = this.ClassTemplate;
  342. string ConstructorText = IsDefaultValue ? this.ConstructorTemplate : null;
  343. classText = classText.Replace(DbFirstTemplate.KeyClassName, className);
  344. classText = classText.Replace(DbFirstTemplate.KeyNamespace, this.Namespace);
  345. classText = classText.Replace(DbFirstTemplate.KeyUsing, IsAttribute ? (this.UsingTemplate + "using " + UtilConstants.AssemblyName + ";\r\n") : this.UsingTemplate);
  346. classText = classText.Replace(DbFirstTemplate.KeyClassDescription, this.ClassDescriptionTemplate.Replace(DbFirstTemplate.KeyClassDescription, "\r\n"));
  347. classText = classText.Replace(DbFirstTemplate.KeySugarTable, IsAttribute ? string.Format(DbFirstTemplate.ValueSugarTable, className) : null);
  348. if (columns.HasValue())
  349. {
  350. foreach (var item in columns)
  351. {
  352. var isLast = columns.Last() == item;
  353. var index = columns.IndexOf(item);
  354. string PropertyText = this.PropertyTemplate;
  355. string PropertyDescriptionText = this.PropertyDescriptionTemplate;
  356. string propertyName = GetPropertyName(item);
  357. string propertyTypeName = item.PropertyName;
  358. PropertyText = GetPropertyText(item, PropertyText);
  359. PropertyDescriptionText = GetPropertyDescriptionText(item, PropertyDescriptionText);
  360. PropertyText = PropertyDescriptionText + PropertyText;
  361. classText = classText.Replace(DbFirstTemplate.KeyPropertyName, PropertyText + (isLast ? "" : ("\r\n" + DbFirstTemplate.KeyPropertyName)));
  362. if (ConstructorText.HasValue() && item.DefaultValue != null)
  363. {
  364. var hasDefaultValue = columns.Skip(index + 1).Any(it => it.DefaultValue.HasValue());
  365. ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyPropertyName, propertyName);
  366. ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyDefaultValue, GetPropertyTypeConvert(item)) + (!hasDefaultValue ? "" : this.ConstructorTemplate);
  367. }
  368. }
  369. }
  370. if (!columns.Any(it => it.DefaultValue != null))
  371. {
  372. ConstructorText = null;
  373. }
  374. classText = classText.Replace(DbFirstTemplate.KeyConstructor, ConstructorText);
  375. classText = classText.Replace(DbFirstTemplate.KeyPropertyName, null);
  376. return classText;
  377. }
  378. public void CreateClassFile(string directoryPath, string nameSpace = "Models")
  379. {
  380. var seChar = Path.DirectorySeparatorChar.ToString();
  381. Check.ArgumentNullException(directoryPath, "directoryPath can't null");
  382. var classStringList = ToClassStringList(nameSpace);
  383. if (classStringList.IsValuable())
  384. {
  385. foreach (var item in classStringList)
  386. {
  387. var fileName = item.Key;
  388. if (FormatFileNameFunc!= null)
  389. {
  390. fileName = FormatFileNameFunc(fileName);
  391. }
  392. var filePath = directoryPath.TrimEnd('\\').TrimEnd('/') + string.Format(seChar + "{0}.cs", fileName);
  393. FileHelper.CreateFile(filePath, item.Value, Encoding.UTF8);
  394. }
  395. }
  396. }
  397. #endregion
  398. #region Private methods
  399. private string GetProertypeDefaultValue(DbColumnInfo item)
  400. {
  401. var result = item.DefaultValue;
  402. if (result == null) return null;
  403. if (Regex.IsMatch(result, @"^\(\'(.+)\'\)$"))
  404. {
  405. result = Regex.Match(result, @"^\(\'(.+)\'\)$").Groups[1].Value;
  406. }
  407. if (Regex.IsMatch(result, @"^\(\((.+)\)\)$"))
  408. {
  409. result = Regex.Match(result, @"^\(\((.+)\)\)$").Groups[1].Value;
  410. }
  411. if (Regex.IsMatch(result, @"^\((.+)\)$"))
  412. {
  413. result = Regex.Match(result, @"^\((.+)\)$").Groups[1].Value;
  414. }
  415. if (result.Equals(this.SqlBuilder.SqlDateNow, StringComparison.CurrentCultureIgnoreCase))
  416. {
  417. result = "DateTime.Now";
  418. }
  419. result = result.Replace("\r", "\t").Replace("\n", "\t");
  420. result = result.IsIn("''", "\"\"") ? string.Empty : result;
  421. return result;
  422. }
  423. private string GetPropertyText(DbColumnInfo item, string PropertyText)
  424. {
  425. string SugarColumnText = DbFirstTemplate.ValueSugarCoulmn;
  426. var propertyName = GetPropertyName(item);
  427. var isMappingColumn = propertyName != item.DbColumnName;
  428. var hasSugarColumn = item.IsPrimarykey == true || item.IsIdentity == true || isMappingColumn;
  429. if (hasSugarColumn && this.IsAttribute)
  430. {
  431. List<string> joinList = new List<string>();
  432. if (item.IsPrimarykey)
  433. {
  434. joinList.Add("IsPrimaryKey=true");
  435. }
  436. if (item.IsIdentity)
  437. {
  438. joinList.Add("IsIdentity=true");
  439. }
  440. if (isMappingColumn)
  441. {
  442. joinList.Add("ColumnName=\"" + item.DbColumnName + "\"");
  443. }
  444. SugarColumnText = string.Format(SugarColumnText, string.Join(",", joinList));
  445. }
  446. else
  447. {
  448. SugarColumnText = null;
  449. }
  450. string typeString = GetPropertyTypeName(item);
  451. PropertyText = PropertyText.Replace(DbFirstTemplate.KeySugarColumn, SugarColumnText);
  452. PropertyText = PropertyText.Replace(DbFirstTemplate.KeyPropertyType, typeString);
  453. PropertyText = PropertyText.Replace(DbFirstTemplate.KeyPropertyName, propertyName);
  454. return PropertyText;
  455. }
  456. private string GetEnityName(DbColumnInfo item)
  457. {
  458. var mappingInfo = this.Context.MappingTables.FirstOrDefault(it => it.DbTableName.Equals(item.TableName, StringComparison.CurrentCultureIgnoreCase));
  459. return mappingInfo == null ? item.TableName : mappingInfo.EntityName;
  460. }
  461. private string GetPropertyName(DbColumnInfo item)
  462. {
  463. if (this.Context.MappingColumns.HasValue())
  464. {
  465. var mappingInfo = this.Context.MappingColumns.SingleOrDefault(it => it.DbColumnName == item.DbColumnName && it.EntityName == GetEnityName(item));
  466. return mappingInfo == null ? item.DbColumnName : mappingInfo.PropertyName;
  467. }
  468. else
  469. {
  470. return item.DbColumnName;
  471. }
  472. }
  473. protected virtual string GetPropertyTypeName(DbColumnInfo item)
  474. {
  475. string result = item.PropertyType != null ? item.PropertyType.Name : this.Context.Ado.DbBind.GetPropertyTypeName(item.DataType);
  476. if (result != "string" && result != "byte[]" && result != "object" && item.IsNullable)
  477. {
  478. result += "?";
  479. }
  480. if (result == "Int32")
  481. {
  482. result = item.IsNullable?"int?":"int";
  483. }
  484. if (result == "String")
  485. {
  486. result = "string";
  487. }
  488. if (result == "string" && item.IsNullable && IsStringNullable)
  489. {
  490. result = result + "?";
  491. }
  492. if (item.OracleDataType.EqualCase("raw") && item.Length == 16)
  493. {
  494. return "Guid";
  495. }
  496. if (item.OracleDataType.EqualCase("number") && item.Length == 1&&item.Scale==0)
  497. {
  498. return "bool";
  499. }
  500. if (result.EqualCase("char")|| result.EqualCase("char?"))
  501. {
  502. return "string";
  503. }
  504. if (item.DataType == "tinyint unsigned")
  505. {
  506. return "short";
  507. }
  508. if (item.DataType == "smallint unsigned")
  509. {
  510. return "ushort";
  511. }
  512. if (item.DataType == "bigint unsigned")
  513. {
  514. return "ulong";
  515. }
  516. if (item.DataType == "int unsigned")
  517. {
  518. return "uint";
  519. }
  520. if (item.DataType == "MediumInt")
  521. {
  522. return "int";
  523. }
  524. if (item.DataType == "MediumInt unsigned")
  525. {
  526. return "uint";
  527. }
  528. return result;
  529. }
  530. private string GetPropertyTypeConvert(DbColumnInfo item)
  531. {
  532. var convertString = GetProertypeDefaultValue(item);
  533. if (convertString == "DateTime.Now" || convertString == null)
  534. return convertString;
  535. if (convertString.ObjToString() == "newid()")
  536. {
  537. return "Guid.NewGuid()";
  538. }
  539. if (item.DataType?.ToString()?.EndsWith("unsigned")==true)
  540. {
  541. return convertString;
  542. }
  543. if (item.DataType == "bit")
  544. return (convertString == "1" || convertString.Equals("true", StringComparison.CurrentCultureIgnoreCase)).ToString().ToLower();
  545. if (convertString.EqualCase("NULL"))
  546. {
  547. return "null";
  548. }
  549. string result = this.Context.Ado.DbBind.GetConvertString(item.DataType) + "(\"" + convertString + "\")";
  550. return result;
  551. }
  552. private string GetPropertyDescriptionText(DbColumnInfo item, string propertyDescriptionText)
  553. {
  554. propertyDescriptionText = propertyDescriptionText.Replace(DbFirstTemplate.KeyPropertyDescription, GetColumnDescription(item.ColumnDescription));
  555. propertyDescriptionText = propertyDescriptionText.Replace(DbFirstTemplate.KeyDefaultValue, GetProertypeDefaultValue(item));
  556. propertyDescriptionText = propertyDescriptionText.Replace(DbFirstTemplate.KeyIsNullable, item.IsNullable.ObjToString());
  557. return propertyDescriptionText;
  558. }
  559. private string GetColumnDescription(string columnDescription)
  560. {
  561. if (columnDescription == null) return columnDescription;
  562. columnDescription = columnDescription.Replace("\r", "\t");
  563. columnDescription = columnDescription.Replace("\n", "\t");
  564. columnDescription = Regex.Replace(columnDescription, "\t{2,}", "\t");
  565. return columnDescription;
  566. }
  567. #endregion
  568. }
  569. }