Пример #1
0
        public T Insert <T> (T obj)
        {
            var type = obj.GetType();
            var cols = Orm.GetColumns(type).Where(c => !Orm.IsAutoInc(c));
            var q    = string.Format("insert into '{0}'({1}) values ({2})", type.Name, string.Join(",", (from c in cols
                                                                                                         select "'" + c.Name + "'").ToArray()), string.Join(",", (from c in cols
                                                                                                                                                                  select "?").ToArray()));
            var vals = from c in cols
                       select c.GetValue(obj, null);

            Execute(q, vals.ToArray());

            var id = SQLite3.LastInsertRowid(_db);

            Orm.SetAutoIncPK(obj, id);
            return(obj);
        }
Пример #2
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);
        }
Пример #3
0
        /// <summary>
        /// Inserts the given object and retrieves its
        /// auto incremented primary key if it has one.
        /// </summary>
        /// <param name="obj">
        /// The object to insert.
        /// </param>
        /// <returns>
        /// The number of rows added to the table.
        /// </returns>
        public int Insert(object obj)
        {
            if (obj == null)
            {
                return(0);
            }

            var map  = GetMapping(obj.GetType());
            var vals = from c in map.InsertColumns
                       select c.GetValue(obj);

            var count = Execute(map.InsertSql, vals.ToArray());

            var id = SQLite3.LastInsertRowid(Handle);

            map.SetAutoIncPK(obj, id);
            map.SetConnection(obj, this);

            return(count);
        }
Пример #4
0
 /// <summary>
 /// Gets the last row identifier.
 /// </summary>
 /// <returns>
 /// The <see cref="long"/>.
 /// </returns>
 public long GetLastRowId() => SQLite3.LastInsertRowid(this.Handle);