예제 #1
0
        public static void Persist(Model instance)
        {
            string table = Cache.Get(instance).TableName;
            string sql   = string.Empty;

            // Prepare a create table statement if the table doesn't exist. Then make a trigger to write to it after updates.
            if (Sniffer.ON && !Sniffer.TableExists(table, GetConnection()))
            {
                try
                {
                    if (Execute(SQLizer.GetCreateTableStatement(table, Cache.Get(instance).ColumnDictionary)))
                    {
                        Execute(Template.Trigger(table));
                        Execute(Template.StoredProcedure(table));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format(Resources.NoTable, table), ex);
                }
            }

            // If the table exists or was created successfully, check to see if it has already been saved once before.
            if (Sniffer.TableExists(table, GetConnection()))
            {
                // If it's been saved before update the record, otherwise insert a new one.
                if (Sniffer.RecordExists(table, instance.ID, GetConnection()))
                {
                    sql = SQLizer.GetUpdateStatement(table, Reflector.GetPersistenceDictionary(ref instance));
                }
                else
                {
                    sql = SQLizer.GetInsertStatement(table, Reflector.GetPersistenceDictionary(ref instance));
                }
            }

            // Once updated or inserted, check the ID property and then set it accordingly.
            try
            {
                if (Execute(sql))
                {
                    if (instance.ID == 0)
                    {
                        instance.ID = GetLastUpdatedId(table);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(Resources.NoInsert, table), ex);
            }
        }
예제 #2
0
        public static void Drop(Type type)
        {
            string table = Cache.Get(type).TableName;

            try
            {
                if (Sniffer.ON && Sniffer.TableExists(table, GetConnection()))
                {
                    Execute(Template.Drop(table));
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(Resources.NoDrop, table), ex);
            }
        }