Exemple #1
0
        unsafe bool DoSaveChanges(List <BlockPos> tempCache, char *ptr, string date,
                                  BulkTransaction transaction)
        {
            string template = "INSERT INTO `Block" + name +
                              "` (Username, TimePerformed, X, Y, Z, type, deleted) VALUES (@Name, @Time, @X, @Y, @Z, @Tile, @Del)";
            ushort x, y, z;

            IDbCommand     cmd      = transaction.CreateCommand(template);
            IDataParameter nameP    = transaction.CreateParam("@Name", DbType.AnsiStringFixedLength); cmd.Parameters.Add(nameP);
            IDataParameter timeP    = transaction.CreateParam("@Time", DbType.AnsiStringFixedLength); cmd.Parameters.Add(timeP);
            IDataParameter xP       = transaction.CreateParam("@X", DbType.UInt16); cmd.Parameters.Add(xP);
            IDataParameter yP       = transaction.CreateParam("@Y", DbType.UInt16); cmd.Parameters.Add(yP);
            IDataParameter zP       = transaction.CreateParam("@Z", DbType.UInt16); cmd.Parameters.Add(zP);
            IDataParameter tileP    = transaction.CreateParam("@Tile", DbType.Byte); cmd.Parameters.Add(tileP);
            IDataParameter delP     = transaction.CreateParam("@Del", DbType.Boolean); cmd.Parameters.Add(delP);
            bool           isNative = transaction is NativeBulkTransaction;

            for (int i = 0; i < tempCache.Count; i++)
            {
                BlockPos bP = tempCache[i];
                IntToPos(bP.index, out x, out y, out z);
                DateTime time = Server.StartTimeLocal.AddTicks((bP.flags >> 2) * TimeSpan.TicksPerSecond);
                MakeInt(time.Year, 4, 0, ptr); MakeInt(time.Month, 2, 5, ptr); MakeInt(time.Day, 2, 8, ptr);
                MakeInt(time.Hour, 2, 11, ptr); MakeInt(time.Minute, 2, 14, ptr); MakeInt(time.Second, 2, 17, ptr);

                // For NativeParameter, we make the optimisation of avoiding boxing primitive types.
                if (!isNative)
                {
                    nameP.Value = bP.name;
                    timeP.Value = date;
                    xP.Value    = x; yP.Value = y; zP.Value = z;
                    tileP.Value = (bP.flags & 2) != 0 ? Block.custom_block : bP.rawType;
                    delP.Value  = (bP.flags & 1) != 0;
                }
                else
                {
                    ((NativeParameter)nameP).SetString(bP.name);
                    ((NativeParameter)timeP).SetString(date);
                    ((NativeParameter)xP).U16Value    = x;
                    ((NativeParameter)yP).U16Value    = y;
                    ((NativeParameter)zP).U16Value    = z;
                    ((NativeParameter)tileP).U8Value  = (bP.flags & 2) != 0 ? Block.custom_block : bP.rawType;
                    ((NativeParameter)delP).BoolValue = (bP.flags & 1) != 0;
                }

                if (!BulkTransaction.Execute(template, cmd))
                {
                    cmd.Dispose();
                    cmd.Parameters.Clear();
                    transaction.Rollback(); return(false);
                }
            }
            cmd.Dispose();
            cmd.Parameters.Clear();
            transaction.Commit();
            return(true);
        }
Exemple #2
0
        static void UpgradePlayerTimeSpents()
        {
            using (BulkTransaction bulk = Database.Backend.CreateBulk()) {
                IDataParameter idParam   = bulk.CreateParam("@0", DbType.Int32);
                IDataParameter secsParam = bulk.CreateParam("@1", DbType.Int64);

                for (int i = 0; i < playerIds.Count; i++)
                {
                    idParam.Value   = playerIds[i];
                    secsParam.Value = playerSeconds[i];

                    using (IDbCommand cmd = bulk.CreateCommand("UPDATE Players SET TimeSpent = @1 WHERE ID = @0")) {
                        cmd.Parameters.Add(idParam);
                        cmd.Parameters.Add(secsParam);
                        cmd.ExecuteNonQuery();
                    }
                }

                bulk.Commit();
            }
        }