Example #1
0
        internal static string SqlDecl(TableMapping.Column p, bool storeDateTimeAsTicks, IBlobSerializer serializer,
                                       IDictionary <Type, string> extraTypeMappings)
        {
            var decl = "\"" + p.Name + "\" " + SqlType(p, storeDateTimeAsTicks, serializer, extraTypeMappings) + " ";

            if (p.IsPK)
            {
                decl += "primary key ";
            }
            if (p.IsAutoInc)
            {
                decl += "autoincrement ";
            }
            if (!p.IsNullable)
            {
                decl += "not null ";
            }
            if (!string.IsNullOrEmpty(p.Collation))
            {
                decl += "collate " + p.Collation + " ";
            }
            if (p.DefaultValue != null)
            {
                decl += "default('" + p.DefaultValue + "') ";
            }

            return(decl);
        }
Example #2
0
        public static string SqlType(TableMapping.Column p, bool storeDateTimeAsTicks, IBlobSerializer serializer)
        {
            Type clrType    = p.ColumnType;
            var  interfaces = clrType.GetTypeInfo().ImplementedInterfaces.ToList();

            if (clrType == typeof(Boolean) || clrType == typeof(Byte) || clrType == typeof(UInt16) ||
                clrType == typeof(SByte) || clrType == typeof(Int16) || clrType == typeof(Int32) ||
                interfaces.Contains(typeof(ISerializable <Boolean>)) ||
                interfaces.Contains(typeof(ISerializable <Byte>)) ||
                interfaces.Contains(typeof(ISerializable <UInt16>)) ||
                interfaces.Contains(typeof(ISerializable <SByte>)) ||
                interfaces.Contains(typeof(ISerializable <Int16>)) ||
                interfaces.Contains(typeof(ISerializable <Int32>)))
            {
                return("integer");
            }
            if (clrType == typeof(UInt32) || clrType == typeof(Int64) ||
                interfaces.Contains(typeof(ISerializable <UInt32>)) ||
                interfaces.Contains(typeof(ISerializable <Int64>)))
            {
                return("bigint");
            }
            if (clrType == typeof(Single) || clrType == typeof(Double) || clrType == typeof(Decimal) ||
                interfaces.Contains(typeof(ISerializable <Single>)) ||
                interfaces.Contains(typeof(ISerializable <Double>)) ||
                interfaces.Contains(typeof(ISerializable <Decimal>)))
            {
                return("float");
            }
            if (clrType == typeof(String) || interfaces.Contains(typeof(ISerializable <String>)))
            {
                int len = p.MaxStringLength;
                return("varchar(" + len + ")");
            }
            if (clrType == typeof(TimeSpan) || interfaces.Contains(typeof(ISerializable <TimeSpan>)))
            {
                return("bigint");
            }
            if (clrType == typeof(DateTime) || interfaces.Contains(typeof(ISerializable <DateTime>)))
            {
                return(storeDateTimeAsTicks ? "bigint" : "datetime");
            }
            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("varchar(36)");
            }
            if (serializer != null && serializer.CanDeserialize(clrType))
            {
                return("blob");
            }
            throw new NotSupportedException("Don't know about " + clrType);
        }
Example #3
0
        public static string SqlType(TableMapping.Column p, bool storeDateTimeAsTicks, IBlobSerializer serializer)
        {
            Type clrType = p.ColumnType;

            if (clrType == typeof(Boolean) || clrType == typeof(Byte) || clrType == typeof(UInt16) ||
                clrType == typeof(SByte) || clrType == typeof(Int16) || clrType == typeof(Int32))
            {
                return("integer");
            }
            if (clrType == typeof(UInt32) || clrType == typeof(Int64))
            {
                return("bigint");
            }
            if (clrType == typeof(Single) || clrType == typeof(Double) || clrType == typeof(Decimal))
            {
                return("float");
            }
            if (clrType == typeof(String))
            {
                int len = p.MaxStringLength;
                return("varchar(" + len + ")");
            }
            if (clrType == typeof(TimeSpan))
            {
                return("bigint");
            }
            if (clrType == typeof(DateTime))
            {
                return(storeDateTimeAsTicks ? "bigint" : "datetime");
            }
            if (clrType.IsEnum)
            {
                return("integer");
            }
            if (clrType == typeof(byte[]))
            {
                return("blob");
            }
            if (clrType == typeof(Guid))
            {
                return("varchar(36)");
            }
            if (serializer != null && serializer.CanDeserialize(clrType))
            {
                return("blob");
            }
            throw new NotSupportedException("Don't know about " + clrType);
        }
Example #4
0
        public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map)
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing Query: " + this);
            }

            IDbStatement stmt = Prepare();

            try
            {
                var cols = new TableMapping.Column[_sqlitePlatform.SQLiteApi.ColumnCount(stmt)];

                for (int i = 0; i < cols.Length; i++)
                {
                    string name = _sqlitePlatform.SQLiteApi.ColumnName16(stmt, i);
                    cols[i] = map.FindColumn(name);
                }

                while (_sqlitePlatform.SQLiteApi.Step(stmt) == Result.Row)
                {
                    object obj = Activator.CreateInstance(map.MappedType);
                    for (int i = 0; i < cols.Length; i++)
                    {
                        if (cols[i] == null)
                        {
                            continue;
                        }
                        ColType colType = _sqlitePlatform.SQLiteApi.ColumnType(stmt, i);
                        object  val     = ReadCol(stmt, i, colType, cols[i].ColumnType);
                        cols[i].SetValue(obj, val);
                    }
                    OnInstanceCreated(obj);
                    yield return((T)obj);
                }
            }
            finally
            {
                _sqlitePlatform.SQLiteApi.Finalize(stmt);
            }
        }
Example #5
0
        public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map)
        {
            _conn.TraceListener.WriteLine("Executing Query: {0}", this);

            var stmt = Prepare();

            try
            {
                var cols = new TableMapping.Column[_sqlitePlatform.SQLiteApi.ColumnCount(stmt)];

                for (var i = 0; i < cols.Length; i++)
                {
                    var name = _sqlitePlatform.SQLiteApi.ColumnName16(stmt, i);
                    cols[i] = map.FindColumn(name);
                }

                while (_sqlitePlatform.SQLiteApi.Step(stmt) == Result.Row)
                {
                    var obj = _conn.Resolver.CreateObject(map.MappedType);
                    for (var i = 0; i < cols.Length; i++)
                    {
                        if (cols[i] == null)
                        {
                            continue;
                        }
                        var colType = _sqlitePlatform.SQLiteApi.ColumnType(stmt, i);
                        var val     = ReadCol(stmt, i, colType, cols[i].ColumnType);
                        cols[i].SetValue(obj, val);
                    }
                    OnInstanceCreated(obj);
                    yield return((T)obj);
                }
            }
            finally
            {
                _sqlitePlatform.SQLiteApi.Finalize(stmt);
            }
        }
Example #6
0
        public static string SqlDecl(TableMapping.Column p, bool storeDateTimeAsTicks, IBlobSerializer serializer)
        {
            string decl = "\"" + p.Name + "\" " + SqlType(p, storeDateTimeAsTicks, serializer) + " ";

            if (p.IsPK)
            {
                decl += "primary key ";
            }
            if (p.IsAutoInc)
            {
                decl += "autoincrement ";
            }
            if (!p.IsNullable)
            {
                decl += "not null ";
            }
            if (!string.IsNullOrEmpty(p.Collation))
            {
                decl += "collate " + p.Collation + " ";
            }

            return(decl);
        }
Example #7
0
        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);
            }

            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("float");
            }
            if (clrType == typeof(string) || interfaces.Contains(typeof(ISerializable <string>)))
            {
                var len = p.MaxStringLength;

                if (len.HasValue)
                {
                    return("varchar(" + len.Value + ")");
                }

                return("varchar");
            }
            if (clrType == typeof(TimeSpan) || interfaces.Contains(typeof(ISerializable <TimeSpan>)))
            {
                return("bigint");
            }
            if (clrType == typeof(DateTime) || interfaces.Contains(typeof(ISerializable <DateTime>)))
            {
                return(storeDateTimeAsTicks ? "bigint" : "datetime");
            }
            if (clrType == typeof(DateTimeOffset))
            {
                return("bigint");
            }
            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("varchar(36)");
            }
            if (serializer != null && serializer.CanDeserialize(clrType))
            {
                return("blob");
            }
            throw new NotSupportedException("Don't know about " + clrType);
        }
Example #8
0
        public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map)
        {
            _conn.TraceListener.WriteLine("Executing Query: {0}", this);

            var stmt = Prepare();

            try
            {
                var cols = new TableMapping.Column[_sqlitePlatform.SQLiteApi.ColumnCount(stmt)];

                var seenColumns = new HashSet <TableMapping.Column>();
                for (var i = 0; i < cols.Length; i++)
                {
                    var name = _sqlitePlatform.SQLiteApi.ColumnName16(stmt, i);

                    var namedColumns = map.FindColumns(name);
                    if (namedColumns.Count == 1)
                    {
                        cols[i] = namedColumns[0];
                    }
                    else if (namedColumns.Count > 1)
                    {
                        cols[i] = namedColumns.FirstOrDefault(c => !seenColumns.Contains(c));
                    }
                    if (cols[i] != null)
                    {
                        seenColumns.Add(cols[i]);
                    }
                }

                while (_sqlitePlatform.SQLiteApi.Step(stmt) == Result.Row)
                {
                    var obj = map.IsJoinTable
                        ? _conn.Resolver.CreateObject(map.MappedType, new[] { _conn.Resolver })
                        : _conn.Resolver.CreateObject(map.MappedType);
                    for (var i = 0; i < cols.Length; i++)
                    {
                        if (cols[i] == null)
                        {
                            continue;
                        }
                        var colType = _sqlitePlatform.SQLiteApi.ColumnType(stmt, i);
                        var val     = ReadCol(stmt, i, colType, cols[i].ColumnType);
                        if (!map.IsJoinTable)
                        {
                            cols[i].SetValue(obj, val);
                        }
                        else
                        {
                            cols[i].SetJoinValue(obj, val);
                        }
                    }
                    OnInstanceCreated(obj);
                    yield return((T)obj);
                }
            }
            finally
            {
                _sqlitePlatform.SQLiteApi.Finalize(stmt);
            }
        }