Esempio n. 1
0
        public static string SqlType(TableMapping.Column p)
        {
            var clrType = p.ColumnType;

            if (clrType == typeof(Boolean) || clrType == typeof(Byte) || clrType == typeof(UInt16) || clrType == typeof(SByte) || clrType == typeof(Int16) || clrType == typeof(Int32))
            {
                return("integer");
            }
            else if (clrType == typeof(UInt32) || clrType == typeof(Int64))
            {
                return("bigint");
            }
            else if (clrType == typeof(Single) || clrType == typeof(Double) || clrType == typeof(Decimal))
            {
                return("float");
            }
            else if (clrType == typeof(String))
            {
                int len = p.MaxStringLength;
                return("varchar(" + len + ")");
            }
            else if (clrType == typeof(DateTime))
            {
                return("datetime");
            }
            else if (clrType.IsEnum)
            {
                return("integer");
            }
            else
            {
                throw new NotSupportedException("Don't know about " + clrType);
            }
        }
Esempio n. 2
0
        public IEnumerable <object> ExecuteQuery(TableMapping map)
        {
            if (_conn.Trace)
            {
                Console.WriteLine("Executing Query: " + this);
            }

            var stmt = Prepare();

            var cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)];

            for (int i = 0; i < cols.Length; i++)
            {
                var name = Marshal.PtrToStringUni(SQLite3.ColumnName16(stmt, i));
                cols[i] = map.FindColumn(name);
            }

            while (SQLite3.Step(stmt) == SQLite3.Result.Row)
            {
                var obj = Activator.CreateInstance(map.MappedType);
                map.SetConnection(obj, _conn);
                for (int i = 0; i < cols.Length; i++)
                {
                    if (cols[i] == null)
                    {
                        continue;
                    }
                    var val = ReadCol(stmt, i, cols[i].ColumnType);
                    cols[i].SetValue(obj, val);
                }
                yield return(obj);
            }

            SQLite3.Finalize(stmt);
        }
Esempio n. 3
0
        public int Delete <T>(object primaryKey)
        {
            TableMapping mapping = GetMapping(typeof(T));

            TableMapping.Column pK = mapping.PK;
            if (pK == null)
            {
                throw new NotSupportedException("Cannot delete " + mapping.TableName + ": it has no PK");
            }
            string query = $"delete from \"{mapping.TableName}\" where \"{pK.Name}\" = ?";

            return(Execute(query, primaryKey));
        }
Esempio n. 4
0
        public int Delete(object objectToDelete)
        {
            TableMapping mapping = GetMapping(objectToDelete.GetType());

            TableMapping.Column pK = mapping.PK;
            if (pK == null)
            {
                throw new NotSupportedException("Cannot delete " + mapping.TableName + ": it has no PK");
            }
            string query = $"delete from \"{mapping.TableName}\" where \"{pK.Name}\" = ?";

            return(Execute(query, pK.GetValue(objectToDelete)));
        }
Esempio n. 5
0
        public static string SqlType(TableMapping.Column p, bool storeDateTimeAsTicks)
        {
            Type columnType = p.ColumnType;

            if (columnType == typeof(bool) || columnType == typeof(byte) || columnType == typeof(ushort) || columnType == typeof(sbyte) || columnType == typeof(short) || columnType == typeof(int))
            {
                return("integer");
            }
            if (columnType == typeof(uint) || columnType == typeof(long))
            {
                return("bigint");
            }
            if (columnType == typeof(float) || columnType == typeof(double) || columnType == typeof(decimal))
            {
                return("float");
            }
            if (columnType == typeof(string))
            {
                int?maxStringLength = p.MaxStringLength;
                if (maxStringLength.HasValue)
                {
                    return("varchar(" + maxStringLength.Value + ")");
                }
                return("varchar");
            }
            if (columnType == typeof(TimeSpan))
            {
                return("bigint");
            }
            if (columnType == typeof(DateTime))
            {
                return((!storeDateTimeAsTicks) ? "datetime" : "bigint");
            }
            if (columnType == typeof(DateTimeOffset))
            {
                return("bigint");
            }
            if (columnType.IsEnum)
            {
                return("integer");
            }
            if (columnType == typeof(byte[]))
            {
                return("blob");
            }
            if (columnType == typeof(Guid))
            {
                return("varchar(36)");
            }
            throw new NotSupportedException("Don't know about " + columnType);
        }
Esempio n. 6
0
        public static bool TryGetSql(Type t, TableMapping.Column c, out string result)
        {
            if (SqlTypes == null)
            {
                Initialize();
                RegisterDefaultTypes();
            }

            if (SqlTypes.ContainsKey(t))
            {
                result = SqlTypes[t](c);
                return(true);
            }

            result = null;
            return(false);
        }
Esempio n. 7
0
        public static string SqlDecl(TableMapping.Column p)
        {
            string decl = "\"" + p.Name + "\" " + SqlType(p) + " ";

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

            return(decl);
        }
Esempio n. 8
0
        public int Update(object obj, Type objType)
        {
            int num = 0;

            if (obj == null || objType == null)
            {
                return(0);
            }
            TableMapping mapping = GetMapping(objType);

            TableMapping.Column pk = mapping.PK;
            if (pk != null)
            {
                IEnumerable <TableMapping.Column> source = from p in mapping.Columns
                                                           where p != pk
                                                           select p;
                IEnumerable <object> collection = from c in source
                                                  select c.GetValue(obj);

                List <object> list = new List <object>(collection);
                list.Add(pk.GetValue(obj));
                string query = string.Format("update \"{0}\" set {1} where {2} = ? ", mapping.TableName, string.Join(",", (from c in source
                                                                                                                           select "\"" + c.Name + "\" = ? ").ToArray()), pk.Name);
                try
                {
                    return(Execute(query, list.ToArray()));
                }
                catch (SQLiteException ex)
                {
                    if (ex.Result == SQLite3.Result.Constraint && SQLite3.ExtendedErrCode(Handle) == SQLite3.ExtendedResult.ConstraintNotNull)
                    {
                        throw NotNullConstraintViolationException.New(ex, mapping, obj);
                    }
                    throw ex;
IL_014e:
                    return(num);
                }
            }
            throw new NotSupportedException("Cannot update " + mapping.TableName + ": it has no PK");
        }
Esempio n. 9
0
        public static string SqlDecl(TableMapping.Column p, bool storeDateTimeAsTicks)
        {
            string text = "\"" + p.Name + "\" " + SqlType(p, storeDateTimeAsTicks) + " ";

            if (p.IsPK)
            {
                text += "primary key ";
            }
            if (p.IsAutoInc)
            {
                text += "autoincrement ";
            }
            if (!p.IsNullable)
            {
                text += "not null ";
            }
            if (!string.IsNullOrEmpty(p.Collation))
            {
                text = text + "collate " + p.Collation + " ";
            }
            return(text);
        }
Esempio n. 10
0
 public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map)
 {
     if (_conn.Trace)
     {
         _conn.InvokeTrace("Executing Query: " + this);
     }
     lock (_conn.SyncObject)
     {
         IntPtr stmt = Prepare();
         try
         {
             TableMapping.Column[] cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)];
             for (int j = 0; j < cols.Length; j++)
             {
                 string name = SQLite3.ColumnName16(stmt, j);
                 cols[j] = map.FindColumn(name);
             }
             while (SQLite3.Step(stmt) == SQLite3.Result.Row)
             {
                 object obj = Activator.CreateInstance(map.MappedType);
                 for (int i = 0; i < cols.Length; i++)
                 {
                     if (cols[i] != null)
                     {
                         SQLite3.ColType colType = SQLite3.ColumnType(stmt, i);
                         object          val     = ReadCol(stmt, i, colType, cols[i].ColumnType);
                         cols[i].SetValue(obj, val);
                     }
                 }
                 OnInstanceCreated(obj);
                 yield return((T)obj);
             }
         }
         finally
         {
             ((_003CExecuteDeferredQuery_003Ec__Iterator3 <T>) /*Error near IL_0234: stateMachine*/)._003C_003E__Finally0();
         }
     }
 }
Esempio n. 11
0
        public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map)
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing Query: " + this);
            }

            var stmt = Prepare();

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

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

                while (SQLite3.Step(stmt) == SQLite3.Result.Row)
                {
                    var obj = Activator.CreateInstance(map.MappedType);
                    for (int i = 0; i < cols.Length; i++)
                    {
                        if (cols[i] == null)
                        {
                            continue;
                        }
                        var colType = SQLite3.ColumnType(stmt, i);
                        var val     = ReadCol(stmt, i, colType, cols[i].ColumnType);
                        cols[i].SetValue(obj, val);
                    }
                    OnInstanceCreated(obj);
                    yield return((T)obj);
                }
            } finally {
                SQLite3.Finalize(stmt);
            }
        }
Esempio n. 12
0
        public static string SqlType(TableMapping.Column p, bool storeDateTimeAsTicks)
        {
            var clrType = p.ColumnType;

            if (clrType == typeof(Boolean) || clrType == typeof(Byte) || clrType == typeof(UInt16) || clrType == typeof(SByte) || clrType == typeof(Int16) || clrType == typeof(Int32) || clrType == typeof(UInt32) || clrType == typeof(Int64))
            {
                return("integer");
            }
            else if (clrType == typeof(Single) || clrType == typeof(Double) || clrType == typeof(Decimal))
            {
                return("float");
            }
            else if (clrType == typeof(String))
            {
                int?len = p.MaxStringLength;

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

                return("varchar");
            }
            else if (clrType == typeof(TimeSpan))
            {
                return("bigint");
            }
            else if (clrType == typeof(DateTime))
            {
                return(storeDateTimeAsTicks ? "bigint" : "datetime");
            }
            else if (clrType == typeof(DateTimeOffset))
            {
                return("bigint");

#if !USE_NEW_REFLECTION_API
            }
            else if (clrType.IsEnum)
            {
#else
            }
            else if (clrType.GetTypeInfo().IsEnum)
            {
#endif
                if (p.StoreAsText)
                {
                    return("varchar");
                }
                else
                {
                    return("integer");
                }
            }
            else if (clrType == typeof(byte[]))
            {
                return("blob");
            }
            else if (clrType == typeof(Guid))
            {
                return("varchar(36)");
            }
            else
            {
                throw new NotSupportedException("Don't know about " + clrType);
            }
        }