Exemplo n.º 1
0
    void databaseWork()
    {
        int rows = 100;

        // create table
        string tableName = Guid.NewGuid().ToString().Replace("-", "");
        var fields = new DataValueType[] { DataValueType.IntegerType, DataValueType.DateTimeType, DataValueType.ShortStringType, DataValueType.LongStringType };
        var createActions = new List<DbAction>();
        createActions.Add(DbAction.EnsureTable(tableName, new string[] { "id" }, new DataValueType[] { DataValueType.IntegerType }));
        createActions.Add(DbAction.AddField(tableName, "f1", DataValueType.BooleanType, true, new BooleanDataValue(false)));
        createActions.Add(DbAction.AddField(tableName, "f2", DataValueType.DateTimeType, false, new DateTimeDataValue(DateTime.Now)));
        createActions.Add(DbAction.AddField(tableName, "f3", DataValueType.FloatType, false, new FloatDataValue(0)));
        createActions.Add(DbAction.AddField(tableName, "f4", DataValueType.ShortStringType, true, new ShortStringDataValue("test")));
        createActions.Add(DbAction.AddField(tableName, "f5", DataValueType.LongStringType, false, new LongStringDataValue("test")));
        WAFRuntime.Engine.Dao.DbProvider.UpdateDatabase(createActions);

        // inserting records
        var table = new SqlTable(tableName);
        var id = new SqlFieldInteger("id", table);
        var f1 = new SqlFieldBoolean("f1", table);
        var f2 = new SqlFieldDateTime("f2", table);
        var f3 = new SqlFieldFloat("f3", table);
        var f4 = new SqlFieldShortString("f4", table);
        var f5 = new SqlFieldLongString("f5", table);
        {
            for (int r = 0; r < rows; r++) {
                SqlQuery q = new SqlQuery(WAFRuntime.Engine.Dao);
                q.Insert(table);
                q.Values(id, r);
                q.Values(f1, false);
                q.Values(f2, DateTime.Now);
                q.Values(f3, (double)r);
                q.Values(f4, "My test string " + r);
                q.Values(f5, "My long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long test string " + r);
                q.ExecuteNonQuery();
            }
        }
        {
            // quering 10 records at the time
            for (int r = 0; r < rows; r++) {
                SqlQuery q = new SqlQuery(WAFRuntime.Engine.Dao);
                q.From(table);
                q.Select(f1);
                q.Select(f2);
                q.Select(f3);
                q.Select(f4);
                q.Where(id > r);
                using (var dr = q.ExecuteReader()) {
                    while (dr.Read()) { }
                }
            }
        }

        // deleting 1/10 of the records
        for (int r = 0; r < rows / 10; r++) {
            SqlQuery q = new SqlQuery(WAFRuntime.Engine.Dao);
            q.Delete(table);
            q.Where(id == r);
            q.ExecuteNonQuery();
        }

        // dropping table
        WAFRuntime.Engine.Dao.DbProvider.UpdateDatabase(new DbAction[] { DbAction.DeleteTable(tableName) }.ToList());
    }
Exemplo n.º 2
0
    void deleteUnusedNodeIdsIn_RelationTable(int relationId, HashSet<int> legalNodeIds)
    {
        var rel = Engine.Definition.Relation[relationId];
        var table = new SqlTable(rel.TableName);
        var nodeIdField1 = new SqlFieldInteger(rel.NameParents, table);
        var nodeIdField2 = new SqlFieldInteger(rel.NameChildren, table);

        var illegalNodeIds = new HashSet<int>();
        {
            var q = new SqlQuery(Engine.Dao);
            q.From(table);
            var rNodeId1 = q.Select(nodeIdField1);
            var rNodeId2 = q.Select(nodeIdField2);
            using (var rs = q.ExecuteReader()) {
                while (rs.Read()) {
                    if (Info.BreakExecution) return;
                    if (!legalNodeIds.Contains(rNodeId1.Value) && !illegalNodeIds.Contains(rNodeId1.Value)) illegalNodeIds.Add(rNodeId1.Value);
                    if (!legalNodeIds.Contains(rNodeId2.Value) && !illegalNodeIds.Contains(rNodeId2.Value)) illegalNodeIds.Add(rNodeId2.Value);
                }
            }
        }
        {
            foreach (var nodeId in illegalNodeIds) {
                if (Info.BreakExecution) return;
                addDeleteCount();
                if (_delete) {
                    var q = new SqlQuery(Engine.Dao);
                    q.Delete(table);
                    q.Where(nodeIdField1 == nodeId | nodeIdField2 == nodeId);
                    q.ExecuteNonQuery();
                }
            }
        }
    }