Example #1
0
        public int Insert(object obj, string extra, Type objType)
        {
            if (obj == null || objType == null)
            {
                return(0);
            }
            TableMapping mapping = GetMapping(objType);

            if (mapping.PK != null && mapping.PK.IsAutoGuid)
            {
                PropertyInfo property = objType.GetProperty(mapping.PK.PropertyName);
                if (property != null && property.GetGetMethod().Invoke(obj, null).Equals(Guid.Empty))
                {
                    property.SetValue(obj, Guid.NewGuid(), null);
                }
            }
            TableMapping.Column[] array = (string.Compare(extra, "OR REPLACE", StringComparison.OrdinalIgnoreCase) != 0) ? mapping.InsertColumns : mapping.InsertOrReplaceColumns;
            object[] array2             = new object[array.Length];
            for (int i = 0; i < array2.Length; i++)
            {
                array2[i] = array[i].GetValue(obj);
            }
            PreparedSqlLiteInsertCommand insertCommand = mapping.GetInsertCommand(this, extra);
            int result;

            try
            {
                result = insertCommand.ExecuteNonQuery(array2);
            }
            catch (SQLiteException ex)
            {
                if (SQLite3.ExtendedErrCode(Handle) == SQLite3.ExtendedResult.ConstraintNotNull)
                {
                    throw NotNullConstraintViolationException.New(ex.Result, ex.Message, mapping, obj);
                }
                throw;
                IL_0120 :;
            }
            if (mapping.HasAutoIncPK)
            {
                long id = SQLite3.LastInsertRowid(Handle);
                mapping.SetAutoIncPK(obj, id);
            }
            return(result);
        }