| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Data.SQLite; |
| 4 | using System.Diagnostics; |
| 5 | using System.IO; |
| 6 | using System.Linq; |
| 7 | using System.Reflection; |
| 8 | using System.Text; |
| 9 | using System.Threading.Tasks; |
| 10 | |
| 11 | namespace Core.Librarys.SQLite |
| 12 | { |
| 13 | ///<summary> |
| 14 | /// |
| 15 | ///</summary> |
| 16 | public class IModelInfo |
| 17 | { |
| 18 | ///<summary> |
| 19 | /// |
| 20 | ///</summary> |
| 21 | public string Name { get; set; } |
| 22 | ///<summary> |
| 23 | /// |
| 24 | ///</summary> |
| 25 | public string TableName { get { return Name + "s"; } } |
| 26 | ///<summary> |
| 27 | /// |
| 28 | ///</summary> |
| 29 | public List<IModelProperties> Properties { get; set; } |
| 30 | } |
| 31 | ///<summary> |
| 32 | /// |
| 33 | ///</summary> |
| 34 | public class IModelProperties |
| 35 | { |
| 36 | ///<summary> |
| 37 | /// |
| 38 | ///</summary> |
| 39 | public string Name { get; set; } |
| 40 | public DbType Type { get; set; } |
| 41 | public bool PK { get; set; } |
| 42 | |
| 43 | } |
| 44 | |
| 45 | public enum DbType |
| 46 | { |
| 47 | INTEGER, |
| 48 | datetime, |
| 49 | nvarchar, |
| 50 | @int, |
| 51 | bit |
| 52 | } |
| 53 | |
| 54 | ///<summary> |
| 55 | /// |
| 56 | ///</summary> |
| 57 | public class SQLiteBuilder |
| 58 | { |
| 59 | public static bool IsSelfChecking = true; |
| 60 | |
| 61 | private System.Data.Entity.Infrastructure.DbModel model; |
| 62 | ///<summary> |
| 63 | /// |
| 64 | ///</summary> |
| 65 | private string connstr; |
| 66 | ///<summary> |
| 67 | /// |
| 68 | ///</summary> |
| 69 | private string versionFile; |
| 70 | |
| 71 | ///<summary> |
| 72 | /// |
| 73 | ///</summary> |
| 74 | private string dbFile; |
| 75 | ///<summary> |
| 76 | /// |
| 77 | ///</summary> |
| 78 | private System.Data.Entity.Core.Metadata.Edm.EdmModel storeModel; |
| 79 | private System.Data.Entity.Core.Common.DbProviderManifest providerManifest; |
| 80 | public SQLiteBuilder(System.Data.Entity.Infrastructure.DbModel model) |
| 81 | { |
| 82 | this.model = model; |
| 83 | |
| 84 | if (model != null) |
| 85 | { |
| 86 | connstr = model.ProviderInfo.ProviderManifestToken; |
| 87 | |
| 88 | storeModel = model.StoreModel; |
| 89 | |
| 90 | providerManifest = model.ProviderManifest; |
| 91 | |
| 92 | dbFile = connstr.Split('=')[1]; |
| 93 | |
| 94 | versionFile = $"{dbFile}.version"; |
| 95 | |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | ///<summary> |
| 100 | /// |
| 101 | ///</summary> |
| 102 | public bool SelfCheck() |
| 103 | { |
| 104 | if (IsHandleDb()) |
| 105 | { |
| 106 | bool res = HandleTable(); |
| 107 | |
| 108 | IsSelfChecking = false; |
| 109 | |
| 110 | return res; |
| 111 | } |
| 112 | |
| 113 | IsSelfChecking = false; |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | ///<summary> |
| 119 | /// |
| 120 | ///</summary> |
| 121 | private bool HandleTable() |
| 122 | { |
| 123 | try |
| 124 | { |
| 125 | bool isNew = true; |
| 126 | |
| 127 | |
| 128 | string dir = Path.Combine(Path.GetDirectoryName(dbFile), "backup"); |
| 129 | string backupName = Path.Combine(dir, $"data.handle.backup"); |
| 130 | if (!Directory.Exists(dir)) |
| 131 | { |
| 132 | Directory.CreateDirectory(dir); |
| 133 | } |
| 134 | if (File.Exists(backupName)) |
| 135 | { |
| 136 | File.Delete(backupName); |
| 137 | } |
| 138 | |
| 139 | if (File.Exists(dbFile)) |
| 140 | { |
| 141 | isNew = false; |
| 142 | |
| 143 | File.Copy(dbFile, backupName); |
| 144 | } |
| 145 | var modelInfos = GetModelInfos(); |
| 146 | |
| 147 | var modelInfosDb = GetModelInfosForDb(); |
| 148 | |
| 149 | foreach (var model in modelInfos) |
| 150 | { |
| 151 | |
| 152 | string tableName = model.TableName; |
| 153 | |
| 154 | var modelInfoDb = modelInfosDb.Where(m => m.TableName == tableName); |
| 155 | |
| 156 | if (modelInfoDb.Count() <= 0) |
| 157 | { |
| 158 | |
| 159 | string csql = GetCreateTableSQL(model); |
| 160 | ExecuteNonQuery(csql); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | |
| 165 | |
| 166 | |
| 167 | var modelDb = modelInfoDb.Single(); |
| 168 | |
| 169 | foreach (var item in model.Properties) |
| 170 | { |
| 171 | if (modelDb.Properties.Where(m => m.Name == item.Name).Count() <= 0) |
| 172 | { |
| 173 | string csql = GetCreateColumnSQL(tableName, item); |
| 174 | ExecuteNonQuery(csql); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | foreach (var dbModel in modelInfosDb) |
| 185 | { |
| 186 | |
| 187 | string tableName = dbModel.TableName; |
| 188 | |
| 189 | |
| 190 | var model = modelInfos.Where(m => m.TableName == tableName).FirstOrDefault(); |
| 191 | |
| 192 | if (model == null) |
| 193 | { |
| 194 | |
| 195 | |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | |
| 200 | |
| 201 | bool isDel = false; |
| 202 | |
| 203 | |
| 204 | |
| 205 | foreach (var dbItem in dbModel.Properties) |
| 206 | { |
| 207 | |
| 208 | if (!model.Properties.Where(m => m.Name == dbItem.Name).Any()) |
| 209 | { |
| 210 | isDel = true; |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | } |
| 216 | if (isDel) |
| 217 | { |
| 218 | |
| 219 | string field = string.Join(",", model.Properties.Select(m => m.Name).ToArray()); |
| 220 | |
| 221 | |
| 222 | string tempName = $"{tableName}_temp_{DateTime.Now.ToString("yyyyMMddHHmmss")}"; |
| 223 | |
| 224 | |
| 225 | ExecuteNonQuery(GetCreateTableSQL(model, tempName)); |
| 226 | |
| 227 | /// |
| 228 | ExecuteNonQuery($"INSERT INTO {tempName}({field}) SELECT {field} FROM {tableName}"); |
| 229 | |
| 230 | |
| 231 | |
| 232 | ExecuteNonQuery($"drop table {tableName}"); |
| 233 | |
| 234 | |
| 235 | |
| 236 | ExecuteNonQuery($"alter table {tempName} rename to {tableName}"); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | return true; |
| 242 | } |
| 243 | catch (Exception ex) |
| 244 | { |
| 245 | Logger.Error($"SQLiteBuilder handle fail!Exception Message:{ex.Message}"); |
| 246 | return false; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | |
| 251 | |
| 252 | private string SQLiteEscape(string str) |
| 253 | { |
| 254 | str = str.Replace("/", "//"); |
| 255 | str = str.Replace("'", "''"); |
| 256 | str = str.Replace("[", "/["); |
| 257 | str = str.Replace("]", "/]"); |
| 258 | str = str.Replace("%", "/%"); |
| 259 | str = str.Replace("&", "/&"); |
| 260 | str = str.Replace("_", "/_"); |
| 261 | str = str.Replace("(", "/("); |
| 262 | str = str.Replace(")", "/)"); |
| 263 | return str; |
| 264 | } |
| 265 | |
| 266 | #region 是否需要处理 |
| 267 | ///<summary> |
| 268 | /// |
| 269 | ///</summary> |
| 270 | ///<returns></returns> |
| 271 | private bool IsHandleDb() |
| 272 | { |
| 273 | string dir = Path.GetDirectoryName(versionFile); |
| 274 | if (!Directory.Exists(dir)) |
| 275 | { |
| 276 | Directory.CreateDirectory(dir); |
| 277 | } |
| 278 | |
| 279 | bool f = File.Exists(versionFile); |
| 280 | |
| 281 | |
| 282 | string coreVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); |
| 283 | if (f) |
| 284 | { |
| 285 | |
| 286 | string dbVersion = File.ReadAllText(versionFile); |
| 287 | if (dbVersion != coreVersion) |
| 288 | { |
| 289 | |
| 290 | File.WriteAllText(versionFile, coreVersion); |
| 291 | return true; |
| 292 | } |
| 293 | |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | File.WriteAllText(versionFile, coreVersion); |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | #endregion |
| 302 | |
| 303 | #region 生成创建列SQL语句 |
| 304 | ///<summary> |
| 305 | /// |
| 306 | ///</summary> |
| 307 | ///<param name="tableName"></param> |
| 308 | ///<param name="properties"></param> |
| 309 | ///<returns></returns> |
| 310 | private string GetCreateColumnSQL(string tableName, IModelProperties properties) |
| 311 | { |
| 312 | string typeDefault = !properties.PK ? properties.Type == DbType.@int ? "NULL DEFAULT 0" : "NULL DEFAULT ''" : ""; |
| 313 | |
| 314 | if(properties.Type == DbType.bit) |
| 315 | { |
| 316 | typeDefault = " DEFAULT 0"; |
| 317 | properties.Type = DbType.INTEGER; |
| 318 | } |
| 319 | |
| 320 | if (properties.PK) |
| 321 | { |
| 322 | properties.Type = DbType.INTEGER; |
| 323 | } |
| 324 | |
| 325 | return $"ALTER table {tableName} ADD COLUMN [{properties.Name}] {properties.Type} {typeDefault}"; |
| 326 | } |
| 327 | #endregion |
| 328 | |
| 329 | #region 生成创建表SQL语句 |
| 330 | ///<summary> |
| 331 | /// |
| 332 | ///</summary> |
| 333 | ///<param name="model"></param> |
| 334 | ///<returns></returns> |
| 335 | private string GetCreateTableSQL(IModelInfo model, string tableName = null) |
| 336 | { |
| 337 | tableName = string.IsNullOrEmpty(tableName) ? model.TableName : tableName; |
| 338 | |
| 339 | string sql = $"CREATE TABLE \"{tableName}\" ("; |
| 340 | foreach (var p in model.Properties) |
| 341 | { |
| 342 | string typeDefault = p.Type == DbType.@int ? " NULL DEFAULT 0" : " NULL DEFAULT ''"; |
| 343 | |
| 344 | if (p.Type == DbType.bit) |
| 345 | { |
| 346 | typeDefault = " DEFAULT 0"; |
| 347 | p.Type = DbType.INTEGER; |
| 348 | } |
| 349 | |
| 350 | if (p.PK) |
| 351 | { |
| 352 | typeDefault = ""; |
| 353 | p.Type = DbType.INTEGER; |
| 354 | } |
| 355 | |
| 356 | string add = p.PK ? $"{p.Type} PRIMARY KEY" : p.Type.ToString(); |
| 357 | string addChar = p == model.Properties[0] ? "" : ","; |
| 358 | sql += $"{addChar}[{p.Name}] {add}{typeDefault}"; |
| 359 | } |
| 360 | sql += ")"; |
| 361 | return sql; |
| 362 | } |
| 363 | #endregion |
| 364 | |
| 365 | #region 判断表是否存在数据库中 |
| 366 | ///<summary> |
| 367 | /// |
| 368 | ///</summary> |
| 369 | ///<param name="tableName"></param> |
| 370 | ///<returns></returns> |
| 371 | private bool IsTableExistDb(string tableName) |
| 372 | { |
| 373 | using (var con = new SQLiteConnection(@"Data Source=.\data.db")) |
| 374 | { |
| 375 | con.Open(); |
| 376 | using (var cmd = new SQLiteCommand(con)) |
| 377 | { |
| 378 | cmd.CommandText = "select count(*) from sqlite_master where type='table' and name = 'StatisticModels3'"; |
| 379 | var rd = cmd.ExecuteReader(); |
| 380 | if (rd.Read()) |
| 381 | { |
| 382 | var count = rd.GetInt32(0); |
| 383 | return count > 0; |
| 384 | } |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | #endregion |
| 392 | |
| 393 | #region 获取所有数据模型的信息(从代码) |
| 394 | ///<summary> |
| 395 | /// |
| 396 | ///</summary> |
| 397 | ///<returns></returns> |
| 398 | private List<IModelInfo> GetModelInfos() |
| 399 | { |
| 400 | var result = new List<IModelInfo>(); |
| 401 | |
| 402 | foreach (var model in storeModel.EntityTypes) |
| 403 | { |
| 404 | var i = new IModelInfo(); |
| 405 | var ps = new List<IModelProperties>(); |
| 406 | |
| 407 | i.Name = model.Name; |
| 408 | i.Properties = ps; |
| 409 | |
| 410 | foreach (var p in model.Properties) |
| 411 | { |
| 412 | Enum.TryParse(p.TypeName, out DbType dbType); |
| 413 | |
| 414 | ps.Add(new IModelProperties() |
| 415 | { |
| 416 | Name = p.Name, |
| 417 | PK = p.IsStoreGeneratedIdentity, |
| 418 | Type = dbType |
| 419 | }); |
| 420 | } |
| 421 | result.Add(i); |
| 422 | } |
| 423 | return result; |
| 424 | } |
| 425 | #endregion |
| 426 | |
| 427 | #region 获取所有数据模型的信息(从数据库) |
| 428 | ///<summary> |
| 429 | /// |
| 430 | ///</summary> |
| 431 | ///<returns></returns> |
| 432 | private List<IModelInfo> GetModelInfosForDb() |
| 433 | { |
| 434 | var result = new List<IModelInfo>(); |
| 435 | |
| 436 | using (var con = new SQLiteConnection(connstr)) |
| 437 | { |
| 438 | con.Open(); |
| 439 | using (var cmd = new SQLiteCommand(con)) |
| 440 | { |
| 441 | cmd.CommandText = "select name from sqlite_master where type='table' order by name"; |
| 442 | using (var rd = cmd.ExecuteReader()) |
| 443 | { |
| 444 | |
| 445 | |
| 446 | while (rd.Read()) |
| 447 | { |
| 448 | var modelInfo = new IModelInfo(); |
| 449 | modelInfo.Name = rd["name"].ToString().Substring(0, rd["name"].ToString().Length - 1); |
| 450 | modelInfo.Properties = GetModelProperties(modelInfo.TableName); |
| 451 | result.Add(modelInfo); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | } |
| 457 | |
| 458 | return result; |
| 459 | } |
| 460 | #endregion |
| 461 | |
| 462 | #region 获取表的所有属性(从数据库) |
| 463 | private List<IModelProperties> GetModelProperties(string tableName) |
| 464 | { |
| 465 | var result = new List<IModelProperties>(); |
| 466 | |
| 467 | using (var con = new SQLiteConnection(connstr)) |
| 468 | { |
| 469 | con.Open(); |
| 470 | using (var cmd = new SQLiteCommand(con)) |
| 471 | { |
| 472 | cmd.CommandText = $"PRAGMA table_info([{tableName}])"; |
| 473 | using (var rd = cmd.ExecuteReader()) |
| 474 | { |
| 475 | while (rd.Read()) |
| 476 | { |
| 477 | var mp = new IModelProperties(); |
| 478 | mp.Name = rd["name"].ToString(); |
| 479 | Enum.TryParse(rd["type"].ToString(), out DbType dbType); |
| 480 | mp.Type = dbType; |
| 481 | mp.PK = rd["pk"].ToString() == "1"; |
| 482 | result.Add(mp); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | return result; |
| 488 | } |
| 489 | #endregion |
| 490 | |
| 491 | |
| 492 | |
| 493 | ///<summary> |
| 494 | /// |
| 495 | ///</summary> |
| 496 | ///<param name="sql"></param> |
| 497 | ///<returns></returns> |
| 498 | private int ExecuteNonQuery(string sql) |
| 499 | { |
| 500 | using (var con = new SQLiteConnection(connstr)) |
| 501 | { |
| 502 | con.Open(); |
| 503 | using (var cmd = new SQLiteCommand(con)) |
| 504 | { |
| 505 | cmd.CommandText = sql; |
| 506 | return cmd.ExecuteNonQuery(); |
| 507 | } |
| 508 | |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | |
| 513 | } |
| 514 | |
| 515 | } |