Beispiel #1
0
        void IProvider.CreateDatabase()
        {
            if (DatabaseExists())
            {
                throw SqlClient.Error.CreateDatabaseFailedBecauseSqlCEDatabaseAlreadyExists(dbName);
            }

            //创建数据库文件
            var connection = new DB2Connection(GetConnectionString(null));

            connection.Open();

            var sqlBuilder = new DB2Builder(this);

            var transaction = connection.BeginTransaction();

            Execute(connection, transaction, sqlBuilder.GetCreateDatabaseCommand(dbName, null));
            try
            {
                MetaModel model = services.Model;
                foreach (MetaTable table in model.GetTables())
                {
                    string createTableCommand = sqlBuilder.GetCreateTableCommand(table);

                    if (!string.IsNullOrEmpty(createTableCommand))
                    {
                        Execute(connection, transaction, createTableCommand);
                    }
                }

                foreach (MetaTable table in model.GetTables())
                {
                    foreach (string commandText in sqlBuilder.GetCreateForeignKeyCommands(table))
                    {
                        if (!string.IsNullOrEmpty(commandText))
                        {
                            Execute(connection, transaction, commandText);
                        }
                    }
                }

                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
            finally
            {
                connection.Close();
            }
        }
Beispiel #2
0
 public override IEnumerable <MetaTable> GetTables()
 {
     foreach (var table in model.GetTables())
     {
         yield return(new CustomMetaTable(table, model));
     }
 }
 public static XElement Adapt(this MetaModel model, IModelAdapter adapter)
 {
     return(new XElement(Namespace + "Database",
                         new XAttribute("Name", adapter.AdaptDatabase(model.DatabaseName)),
                         new XAttribute("Provider", model.ProviderType.AssemblyQualifiedName),
                         model.GetTables().Select(table => table.Adapt(adapter)),
                         model.GetFunctions().Select(function => function.Adapt(adapter))
                         ));
 }
Beispiel #4
0
        void IProvider.CreateDatabase()
        {
            if (File.Exists(dbName))
            {
                throw SqlClient.Error.CreateDatabaseFailedBecauseSqlCEDatabaseAlreadyExists(dbName);
            }

            //创数据库文件
            SQLiteConnection.CreateFile(dbName);

            //var connection = new SQLiteConnection(GetConnectionString(dbName));
            //services.Context.Connection.Open();
            //var transaction = services.Context.Connection.BeginTransaction();
            //services.Context.Transaction = transaction;
            try
            {
                var       SQLiteSqlBuilder = new SQLiteSqlBuilder(this);
                MetaModel model            = services.Model;
                foreach (MetaTable table in model.GetTables())
                {
                    //string createTableCommand = SQLiteSqlBuilder.GetCreateTableCommand(table);

                    //if (!string.IsNullOrEmpty(createTableCommand))
                    //    Execute(connection, transaction, createTableCommand);
                    var commands = SQLiteSqlBuilder.GetCreateTableCommands(table);
                    foreach (var command in commands)
                    {
                        //Execute(connection, transaction, command);
                        services.Context.ExecuteCommand(command);
                    }
                }

                //-----------------SQLite不支持外键--------------------/

                /*
                 * foreach (MetaTable table in model.GetTables())
                 *  foreach (string commandText in SQLiteSqlBuilder.GetCreateForeignKeyCommands(table))
                 *      if (!string.IsNullOrEmpty(commandText))
                 *          Execute(connection, transaction, commandText);
                 */
                //transaction.Commit();
            }
            catch
            {
                //transaction.Rollback();
                throw;
            }
            finally
            {
                //Connection.Close();
            }
        }
        private string DeleteDatabaseSQL(
            MetaModel model)
        {
            StringBuilder commandBuilder = new StringBuilder();

            // Append the SQL for deleting the tables
            foreach (MetaTable table in model.GetTables())
            {
                commandBuilder.Append(m_builder.DeleteTableSQL(table));
                commandBuilder.Append("\r\n");
            }

            return(commandBuilder.ToString());
        }
        /// <summary>
        /// Called to create the entities known to this <see cref="DomainContext"/>
        /// </summary>
        /// <returns>The list of entities</returns>
        protected override IEnumerable <BusinessLogicEntity> CreateEntities()
        {
            MetaModel mm = this.MetaModel;
            IEnumerable <MetaTable>    tables   = mm.GetTables().Where(t => t.RowType.IsEntity);
            List <BusinessLogicEntity> entities = new List <BusinessLogicEntity>();

            foreach (MetaTable table in tables)
            {
                MetaType metaType = table.RowType;
                entities.Add(new LinqToSqlEntity(this, metaType));
            }

            return(entities);
        }
Beispiel #7
0
        public void CreateDatabase()
        {
            if (DatabaseExists())
            {
                throw SqlClient.Error.CreateDatabaseFailedBecauseSqlCEDatabaseAlreadyExists(dbName);
            }
            if (string.IsNullOrEmpty(dbName))
            {
                throw Error.ArgumentNull("Database Name");
            }

            var conn = conManager.UseConnection(this);



            var transaction      = conn.BeginTransaction();
            var OracleSqlBuilder = new OracleSqlBuilder(this);

            try
            {
                Execute(conn, transaction, OracleSqlBuilder.GetCreateDatabaseCommand(dbName, passowrd));
                var commandText = "GRANT DBA TO " + dbName;
                Execute(conn, transaction, commandText);
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }
            finally
            {
                conManager.ReleaseConnection(this);
            }

            var str = string.Format("Data Source={0};User ID={1};Password={2}", conn.DataSource, dbName, passowrd);
            var oracleConnection = new OracleConnection(str);

            oracleConnection.Open();
            var oracleTransaction = oracleConnection.BeginTransaction();

            //var OracleSqlBuilder = new OracleSqlBuilder(this);
            try
            {
                MetaModel model = services.Model;
                foreach (MetaTable table in model.GetTables())
                {
                    var commands = OracleSqlBuilder.GetCreateTableCommands(table);
                    foreach (var command in commands)
                    {
                        Execute(oracleConnection, oracleTransaction, command);
                    }
                }

                foreach (MetaTable table in model.GetTables())
                {
                    foreach (string commandText in OracleSqlBuilder.GetCreateForeignKeyCommands(table))
                    {
                        if (!string.IsNullOrEmpty(commandText))
                        {
                            Execute(oracleConnection, oracleTransaction, commandText);
                        }
                    }
                }

                oracleTransaction.Commit();
            }
            catch
            {
                oracleTransaction.Rollback();
                throw;
            }
            finally
            {
                conManager.ReleaseConnection(this);
            }
        }
Beispiel #8
0
 public override IEnumerable <MetaTable> GetTables()
 {
     return(_orgmodel.GetTables());
 }
Beispiel #9
0
        public void CreateDatabase()
        {
            if (DatabaseExists())
            {
                throw SqlClient.Error.CreateDatabaseFailedBecauseSqlCEDatabaseAlreadyExists(dbName);
            }
            if (string.IsNullOrEmpty(dbName))
            {
                throw Error.ArgumentNull("Database Name");
            }

            //创建数据库文件
            var conn        = conManager.UseConnection(this);
            var transaction = conn.BeginTransaction();

            OracleSqlBuilder = new OracleSqlBuilder(this);
            try
            {
                Execute(conn, transaction, OracleSqlBuilder.GetCreateDatabaseCommand(dbName, passowrd));
                var commandText = "GRANT DBA TO " + dbName;
                Execute(conn, transaction, commandText);
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }
            finally
            {
                conManager.ReleaseConnection(this);
            }

            DbConnectionStringBuilder builder = new OracleConnectionStringBuilder()
            {
                DataSource = conn.DataSource,
                UserID     = dbName,
                Password   = passowrd,
            };

            conn = new OracleConnection(builder.ToString());
            conn.Open();
            transaction = conn.BeginTransaction();
            try
            {
                MetaModel model = services.Model;
                foreach (MetaTable table in model.GetTables())
                {
                    //string createTableCommand = OracleSqlBuilder.GetCreateTableCommand(table);
                    //if (!string.IsNullOrEmpty(createTableCommand))
                    //    Execute(conn, transaction, createTableCommand);

                    //string createPrimaryKey = OracleSqlBuilder.GetPrimaryKeyCommand(table);
                    //if (!string.IsNullOrEmpty(createPrimaryKey))
                    //    Execute(conn, transaction, createPrimaryKey);
                    var commands = OracleSqlBuilder.GetCreateTableCommands(table);
                    foreach (var command in commands)
                    {
                        Execute(conn, transaction, command);
                    }
                }

                //创建外建
                foreach (MetaTable table in model.GetTables())
                {
                    foreach (string commandText in OracleSqlBuilder.GetCreateForeignKeyCommands(table))
                    {
                        if (!string.IsNullOrEmpty(commandText))
                        {
                            Execute(conn, transaction, commandText);
                        }
                    }
                }
                ////创建自动编号列
                //foreach (MetaTable table in model.GetTables())
                //{
                //    var create = OracleSqlBuilder.GetCreateSquenceCommand(table);
                //    if (!string.IsNullOrEmpty(create))
                //        Execute(conn, transaction, create);
                //}
                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
            finally
            {
                conManager.ReleaseConnection(this);
            }
        }
 public override IEnumerable <MetaTable> GetTables()
 {
     return(source.GetTables());
 }
Beispiel #11
0
        public void CreateDatabase()
        {
            if (DatabaseExists())
            {
                throw SqlClient.Error.CreateDatabaseFailedBecauseSqlCEDatabaseAlreadyExists(dbName);
            }

            var connection = new NpgsqlConnection(GetConnectionString(string.Empty));

            connection.Open();
            try
            {
                Execute(connection, null, PgsqlBuilder.GetCreateDatabaseCommand(dbName, null, null));
            }
            finally
            {
                connection.Close();
            }

            connection.ConnectionString = GetConnectionString(dbName);
            connection.Open();
            var transaction = connection.BeginTransaction();

            try
            {
                var       builder = new PgsqlBuilder(this);
                MetaModel model   = services.Model;
                foreach (MetaTable table in model.GetTables())
                {
                    string createTableCommand = builder.GetCreateTableCommand(table);

                    if (!string.IsNullOrEmpty(createTableCommand))
                    {
                        Execute(connection, transaction, createTableCommand);
                    }
                }
                foreach (MetaTable metaTable in model.GetTables())
                {
                    var command = builder.GetPrimaryKeyCommand(metaTable);
                    if (!string.IsNullOrEmpty(command))
                    {
                        Execute(connection, transaction, command);
                    }
                }
                foreach (MetaTable metaTable in model.GetTables())
                {
                    //创建自动编号列
                    var command = SqlBuilder.GetCreateSquenceCommand(metaTable);
                    if (!string.IsNullOrEmpty(command))
                    {
                        services.Context.ExecuteCommand(command);
                    }
                }
                foreach (MetaTable table in model.GetTables())
                {
                    foreach (string commandText in builder.GetCreateForeignKeyCommands(table))
                    {
                        if (!string.IsNullOrEmpty(commandText))
                        {
                            Execute(connection, transaction, commandText);
                        }
                    }
                }

                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
            finally
            {
                connection.Close();
            }
        }