public static SQLiteTableMeta GetMeta <T>(SQLiteConnection connection) { string metaString = connection.prefix + typeof(T).Name + TableMetaExtension; var item = SQLiteStorageModes.GetItem(metaString, connection.StorageMode); if (item == null) { var newMeta = createMeta <T>(metaString, connection); memoryTableMeta.Add(metaString, newMeta); return(newMeta); } else { SQLiteTableMeta oldMeta = null; // if there is an exception... if (memoryTableMeta.ContainsKey(metaString)) { oldMeta = memoryTableMeta[metaString]; } else { oldMeta = JsonConvert.DeserializeObject <SQLiteTableMeta>(item); memoryTableMeta.Add(metaString, oldMeta); } oldMeta._innerConnection = connection; oldMeta._metaString = metaString; return(oldMeta); } }
public int Update <T>(T row) { var item = getTable <T>(); if (item != null) { List <T> rows = JsonConvert.DeserializeObject <List <T> >(item.ToString()); var meta = SQLiteTableMeta.GetMeta <T>(this); if (meta.HasPrimaryKeyName()) { int length = rows.Count; string primaryKey = meta.PrimaryKeyName; for (int i = 0; i < length; i++) { if (rows[i][primaryKey] == row[primaryKey]) { rows[i] = row; setTable(rows); return(1); } } } } return(0); }
public int InsertAll <T>(IEnumerable <T> inserts) { int count = 0; var item = getTable <T>(); if (item != null) { List <T> rows = JsonConvert.DeserializeObject <List <T> >(item.ToString()); var meta = SQLiteTableMeta.GetMeta <T>(this); bool increment = meta.HasAutoIncrement(); foreach (var row in inserts) { if (increment) { row[meta.AutoIncrementName] = meta.NextAutoIncrement(false); } rows.Add(row); count++; } meta.Save(); setTable(rows); } return(count); }
private static SQLiteTableMeta createMeta <T>(string metaString, SQLiteConnection connection) { var newMetaTagle = new SQLiteTableMeta(); newMetaTagle._innerConnection = connection; newMetaTagle._wasNew = true; newMetaTagle._metaString = metaString; var properties = typeof(T).GetProperties().Where(t => t.GetCustomAttributes().Count() > 0).ToArray(); int length = properties.Length; bool foundAuto = false; bool foundPrimary = false; for (int i = 0; i < length; i++) { var property = properties[i]; var attrs = property.GetCustomAttributes(); if (attrs == null || attrs.Length == 0) { continue; } var name = property.Name; bool foundboth = false; foreach (var attribute in attrs) { if (!foundAuto && attribute.ToString() == "SQLite.AutoIncrement") { newMetaTagle.AutoIncrementName = name; foundAuto = true; } if (!foundPrimary && attribute.ToString() == "SQLite.PrimaryKey") { newMetaTagle.PrimaryKeyName = name; foundPrimary = true; } if (foundPrimary && foundAuto) { foundboth = true; break; } } if (foundboth) { break; } } // store in local storage. newMetaTagle.Save(); return(newMetaTagle); }
public void CreateTable <T>() where T : new() { var item = getTable <T>(); var meta = SQLiteTableMeta.GetMeta <T>(this); if (item == null) { setTable <T>(new List <T>()); } }
public int Insert <T>(T row) { int count = 0; var item = getTable <T>(); if (item != null) { List <T> rows = JsonConvert.DeserializeObject <List <T> >(item.ToString()); var meta = SQLiteTableMeta.GetMeta <T>(this); if (meta.HasAutoIncrement()) { row[meta.AutoIncrementName] = meta.NextAutoIncrement(); } rows.Add(row); count++; setTable(rows); } return(count); }
public int UpdateAll <T>(IEnumerable <T> updates) { int count = 0; var item = getTable <T>(); if (item != null) { List <T> rows = JsonConvert.DeserializeObject <List <T> >(item); var meta = SQLiteTableMeta.GetMeta <T>(this); if (meta.HasPrimaryKeyName()) { string primaryKey = meta.PrimaryKeyName; var order = updates.OrderByDescending(b => b[meta.PrimaryKeyName]).ToList(); for (int i = rows.Count - 1; i > 0 && order.Count > 0; i--) { for (int x = order.Count - 1; x > 0; x--) { var orderItem = order[x]; if (rows[i][primaryKey] == orderItem[primaryKey]) { rows[i] = orderItem; order.RemoveAt(x); count++; break; } } } if (count > 0) { setTable(rows); } } } return(count); }