internal static string SqlDecl(TableMapping.Column p, bool storeDateTimeAsTicks, IBlobSerializer serializer, IDictionary <Type, string> extraTypeMappings) { //http://www.sqlite.org/lang_createtable.html return(String.Format("\"{0}\" {1} {2} {3} {4} {5} ", p.Name, SqlType(p, storeDateTimeAsTicks, serializer, extraTypeMappings), p.IsAutoInc ? "primary key autoincrement" : null, //autoincrement can not be set with a multiple primary key !p.IsNullable ? "not null" : null, !string.IsNullOrEmpty(p.Collation) ? "collate " + p.Collation : null, p.DefaultValue != null ? "default('" + p.DefaultValue + "') " : null )); }
private static string SqlType(TableMapping.Column p, bool storeDateTimeAsTicks, IBlobSerializer serializer, IDictionary <Type, string> extraTypeMappings) { var clrType = p.ColumnType; var interfaces = clrType.GetTypeInfo().ImplementedInterfaces.ToList(); string extraMapping; if (extraTypeMappings.TryGetValue(clrType, out extraMapping)) { return(extraMapping); } //http://www.sqlite.org/datatype3.html if (clrType == typeof(bool) || clrType == typeof(byte) || clrType == typeof(ushort) || clrType == typeof(sbyte) || clrType == typeof(short) || clrType == typeof(int) || clrType == typeof(uint) || clrType == typeof(long) || interfaces.Contains(typeof(ISerializable <bool>)) || interfaces.Contains(typeof(ISerializable <byte>)) || interfaces.Contains(typeof(ISerializable <ushort>)) || interfaces.Contains(typeof(ISerializable <sbyte>)) || interfaces.Contains(typeof(ISerializable <short>)) || interfaces.Contains(typeof(ISerializable <int>)) || interfaces.Contains(typeof(ISerializable <uint>)) || interfaces.Contains(typeof(ISerializable <long>)) || interfaces.Contains(typeof(ISerializable <ulong>))) { return("integer"); } if (clrType == typeof(float) || clrType == typeof(double) || clrType == typeof(decimal) || interfaces.Contains(typeof(ISerializable <float>)) || interfaces.Contains(typeof(ISerializable <double>)) || interfaces.Contains(typeof(ISerializable <decimal>))) { return("real"); } if (clrType == typeof(string) || interfaces.Contains(typeof(ISerializable <string>)) || clrType == typeof(XElement) || interfaces.Contains(typeof(ISerializable <XElement>)) ) { return("text"); } if (clrType == typeof(TimeSpan) || interfaces.Contains(typeof(ISerializable <TimeSpan>))) { return("integer"); } if (clrType == typeof(DateTime) || interfaces.Contains(typeof(ISerializable <DateTime>))) { return(storeDateTimeAsTicks ? "integer" : "numeric"); } if (clrType == typeof(DateTimeOffset) || interfaces.Contains(typeof(ISerializable <DateTimeOffset>))) { return("integer"); } if (clrType.GetTypeInfo().IsEnum) { return("integer"); } if (clrType == typeof(byte[]) || interfaces.Contains(typeof(ISerializable <byte[]>))) { return("blob"); } if (clrType == typeof(Guid) || interfaces.Contains(typeof(ISerializable <Guid>))) { return("text"); } if (serializer != null && serializer.CanDeserialize(clrType)) { return("blob"); } throw new NotSupportedException("Don't know about " + clrType); }
public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map) { _conn.TraceListener.WriteLine("Executing Query: {0}", this); var stmt = Prepare(); try { var cols = new TableMapping.Column[sqlite.ColumnCount(stmt)]; var type = typeof(T); var isPrimitiveType = type.GetTypeInfo().IsPrimitive || type == typeof(string); for (var i = 0; i < cols.Length; i++) { if (!isPrimitiveType) { var name = sqlite.ColumnName16(stmt, i); cols[i] = map.FindColumn(name); } else { cols[i] = map.CreateColumn(type); } } while (sqlite.Step(stmt) == Result.Row) { var obj = isPrimitiveType ? null : _conn.Resolver.CreateObject(map.MappedType); for (var i = 0; i < cols.Length; i++) { ColType colType; object val; //Support of primitive types if (isPrimitiveType) { //Assert(cols.Length == 1) colType = sqlite.ColumnType(stmt, i); val = ReadCol(stmt, i, colType, cols[i].ColumnType); yield return((T)Convert.ChangeType(val, type, CultureInfo.CurrentCulture)); break; } if (cols[i] == null) { continue; } colType = sqlite.ColumnType(stmt, i); val = ReadCol(stmt, i, colType, cols[i].ColumnType); cols[i].SetValue(obj, val); } if (!isPrimitiveType) { OnInstanceCreated(obj); yield return((T)obj); } } } finally { sqlite.Finalize(stmt); } }