Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » SQL » Downloads
 
Add your own
alternative version
Go to top

A Model to Represent Directed Acyclic Graphs (DAG) on SQL Databases

, 14 Jan 2008
Addressing the general problem of representing directed acyclic graphs (DAGs) in SQL databases
#region copyright

// Copyright (C) 2008 Kemal ERDOGAN
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, version 3 of the License.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

namespace ADSync.Utils {
    using System;
    using System.Collections.Generic;
    using System.Transactions;
    using Afcas.Objects;
    using Afcas.Utils;
    using Objects;

    internal static class ADSyncHelper {
        public static void SyncAdToSqlServer( IList< ADPrincipal > pl, string connectionString, string dbSourceName ) {
            Dictionary< string, ADPrincipal > pdic = new Dictionary< string, ADPrincipal >( pl.Count );
            for( int ii = 0; ii < pl.Count; ii++ ) {
                ADPrincipal pr = pl[ ii ];
                pdic[ pr.ADPath ] = pr;
            }

            DBHelper.Init( connectionString );
            using( TransactionScope scope = DBHelper.GetRequiredTransactionScope( ) ) {
                DBHelper.ExecuteNonQuery( "Sync_ClearSyncData", dbSourceName, EdgeSource.Principal );

                // update principals
                for( int ii = 0; ii < pl.Count; ii++ ) {
                    ADPrincipal pr = pl[ ii ];
                    DBHelper.ExecuteNonQuery( "Sync_AddPrincipalToSyncList",
                                              new Guid( pr.Id ),
                                              pr.PrincipalType,
                                              pr.Name,
                                              pr.Email,
                                              pr.DisplayName,
                                              pr.Description,
                                              dbSourceName );
                }
                DBHelper.ExecuteNonQuery( "Sync_SyncPrincipal", dbSourceName );

                // update memberships
                for( int ii = 0; ii < pl.Count; ii++ ) {
                    ADPrincipal pr = pl[ ii ];

                    for( int jj = 0; jj < pr.GroupPaths.Length; jj++ ) {
                        string path = pr.GroupPaths[ jj ];
                        ADPrincipal gr;
                        if( !pdic.TryGetValue( path, out gr ) ) {
                            // this group is out of scope from the path list
                            continue;
                        }
                        DBHelper.ExecuteNonQuery( "Sync_AddEdgeToSyncList", pr.Name, gr.Name, EdgeSource.Principal );
                    }
                }
                DBHelper.ExecuteNonQuery( "Sync_SyncEdge", EdgeSource.Principal, dbSourceName );

                scope.Complete( );
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kemal Erdogan

France France
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 14 Jan 2008
Article Copyright 2008 by Kemal Erdogan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid