Example #1
0
        /// <summary>
        /// <para>Edits the connection string (See <see cref="Builder"/>) to open connections to the <paramref name="newDatabase"/>.</para>
        ///
        /// <para>NOTE: Generally it is better to use <see cref="ExpectDatabase"/> instead and interact with the new object</para>
        /// </summary>
        /// <param name="newDatabase"></param>
        public void ChangeDatabase(string newDatabase)
        {
            //change the connection string to point to the newDatabase
            Builder = Helper.ChangeDatabase(Builder, newDatabase);

            //for DBMS that do not persist database in connection string (Oracle), we must persist this change
            _currentDatabase = ExpectDatabase(newDatabase);
        }
Example #2
0
        /// <summary>
        /// Internal API constructor intended for Implementation classes, instead use <see cref="DiscoveredDatabase.ExpectTable"/> instead.
        /// </summary>
        /// <param name="database"></param>
        /// <param name="table"></param>
        /// <param name="querySyntaxHelper"></param>
        /// <param name="schema"></param>
        /// <param name="tableType"></param>
        public DiscoveredTable(DiscoveredDatabase database, string table, IQuerySyntaxHelper querySyntaxHelper, string schema = null, TableType tableType = TableType.Table)
        {
            _table    = table;
            Helper    = database.Helper.GetTableHelper();
            Database  = database;
            Schema    = schema;
            TableType = tableType;

            QuerySyntaxHelper = querySyntaxHelper;
        }
Example #3
0
        public virtual string GetCreateTableSql(DiscoveredDatabase database, string tableName, DatabaseColumnRequest[] columns, Dictionary <DatabaseColumnRequest, DiscoveredColumn> foreignKeyPairs, bool cascadeDelete, string schema)
        {
            if (string.IsNullOrWhiteSpace(tableName))
            {
                throw new ArgumentNullException(FAnsiStrings.DiscoveredDatabaseHelper_GetCreateTableSql_Table_name_cannot_be_null, "tableName");
            }

            StringBuilder bodySql = new StringBuilder();

            var server       = database.Server;
            var syntaxHelper = server.GetQuerySyntaxHelper();

            syntaxHelper.ValidateTableName(tableName);

            foreach (DatabaseColumnRequest c in columns)
            {
                syntaxHelper.ValidateColumnName(c.ColumnName);
            }

            //the name sans brackets (hopefully they didn't pass any brackets)
            tableName = syntaxHelper.GetRuntimeName(tableName);

            //the name uflly specified e.g. [db]..[tbl] or `db`.`tbl` - See Test HorribleColumnNames
            var fullyQualifiedName = syntaxHelper.EnsureFullyQualified(database.GetRuntimeName(), schema, tableName);

            bodySql.AppendLine("CREATE TABLE " + fullyQualifiedName + "(");

            foreach (var col in columns)
            {
                var datatype = col.GetSQLDbType(syntaxHelper.TypeTranslater);

                //add the column name and accompanying datatype
                bodySql.AppendLine(GetCreateTableSqlLineForColumn(col, datatype, syntaxHelper) + ",");
            }

            var pks = columns.Where(c => c.IsPrimaryKey).ToArray();

            if (pks.Any())
            {
                bodySql.Append(GetPrimaryKeyDeclarationSql(tableName, pks, syntaxHelper));
            }

            if (foreignKeyPairs != null)
            {
                bodySql.AppendLine();
                bodySql.AppendLine(GetForeignKeyConstraintSql(tableName, syntaxHelper,
                                                              foreignKeyPairs.ToDictionary(k => (IHasRuntimeName)k.Key, v => v.Value), cascadeDelete, null));
            }

            var toReturn = bodySql.ToString().TrimEnd('\r', '\n', ',');

            toReturn += ")" + Environment.NewLine;

            return(toReturn);
        }
Example #4
0
        /// <summary>
        /// <para>Creates a new server pointed at the <paramref name="server"/> which should be a server of DBMS <paramref name="databaseType"/></para>
        ///
        /// <para><see cref="ImplementationManager"/> must have a loaded implementation for the DBMS type</para>
        /// </summary>
        /// <param name="server">The server to connect to e.g. "localhost\sqlexpress"</param>
        /// <param name="database">The default database to connect into/query (see <see cref="GetCurrentDatabase"/>)</param>
        /// <param name="databaseType">The DBMS provider type</param>
        /// <param name="usernameIfAny">Optional username to set in the connection string</param>
        /// <param name="passwordIfAny">Optional password to set in the connection string</param>
        /// <exception cref="ImplementationNotFoundException"></exception>
        public DiscoveredServer(string server, string database, DatabaseType databaseType, string usernameIfAny, string passwordIfAny)
        {
            Helper = ImplementationManager.GetImplementation(databaseType).GetServerHelper();

            Builder = Helper.GetConnectionStringBuilder(server, database, usernameIfAny, passwordIfAny);

            if (!string.IsNullOrWhiteSpace(database))
            {
                _currentDatabase = ExpectDatabase(database);
            }
        }
Example #5
0
        /// <summary>
        /// <para>Creates a new database with the given <paramref name="newDatabaseName"/>.</para>
        ///
        /// <para>In the case of Oracle this is a user+schema (See https://stackoverflow.com/questions/880230/difference-between-a-user-and-a-schema-in-oracle) </para>
        /// </summary>
        /// <param name="newDatabaseName"></param>
        /// <returns></returns>
        public DiscoveredDatabase CreateDatabase(string newDatabaseName)
        {
            //the database we will create - it's ok DiscoveredDatabase is IMightNotExist
            DiscoveredDatabase db = ExpectDatabase(newDatabaseName);

            Helper.CreateDatabase(Builder, db);

            if (!db.Exists())
            {
                throw new Exception(string.Format(FAnsiStrings.DiscoveredServer_CreateDatabase_Helper___0___tried_to_create_database___1___but_the_database_didn_t_exist_after_the_creation_attempt, Helper.GetType().Name, newDatabaseName));
            }

            return(db);
        }
Example #6
0
        /// <summary>
        /// <para>Creates a new database with the given <paramref name="newDatabaseName"/>.</para>
        ///
        /// <para>In the case of Oracle this is a user+schema (See https://stackoverflow.com/questions/880230/difference-between-a-user-and-a-schema-in-oracle) </para>
        /// </summary>
        /// <param name="newDatabaseName"></param>
        /// <returns></returns>
        public DiscoveredDatabase CreateDatabase(string newDatabaseName)
        {
            //the database we will create - it's ok DiscoveredDatabase is IMightNotExist
            DiscoveredDatabase db = ExpectDatabase(newDatabaseName);

            Helper.CreateDatabase(Builder, db);

            if (!db.Exists())
            {
                throw new Exception("Helper tried to create database " + newDatabaseName + " but the database didn't exist after the creation attempt");
            }

            return(db);
        }
Example #7
0
        public virtual string GetCreateTableSql(DiscoveredDatabase database, string tableName, DatabaseColumnRequest[] columns, Dictionary <DatabaseColumnRequest, DiscoveredColumn> foreignKeyPairs, bool cascadeDelete, string schema)
        {
            if (string.IsNullOrWhiteSpace(tableName))
            {
                throw new ArgumentNullException("Table name cannot be null", "tableName");
            }

            string bodySql = "";

            var server       = database.Server;
            var syntaxHelper = server.GetQuerySyntaxHelper();

            //the name sans brackets (hopefully they didn't pass any brackets)
            tableName = syntaxHelper.GetRuntimeName(tableName);

            //the name uflly specified e.g. [db]..[tbl] or `db`.`tbl` - See Test HorribleColumnNames
            var fullyQualifiedName = syntaxHelper.EnsureFullyQualified(database.GetRuntimeName(), schema, tableName);

            bodySql += "CREATE TABLE " + fullyQualifiedName + "(" + Environment.NewLine;

            foreach (var col in columns)
            {
                var datatype = col.GetSQLDbType(syntaxHelper.TypeTranslater);

                //add the column name and accompanying datatype
                bodySql += GetCreateTableSqlLineForColumn(col, datatype, syntaxHelper) + "," + Environment.NewLine;
            }

            var pks = columns.Where(c => c.IsPrimaryKey).ToArray();

            if (pks.Any())
            {
                bodySql += GetPrimaryKeyDeclarationSql(tableName, pks, syntaxHelper);
            }

            if (foreignKeyPairs != null)
            {
                bodySql += Environment.NewLine + GetForeignKeyConstraintSql(tableName, syntaxHelper, foreignKeyPairs, cascadeDelete) + Environment.NewLine;
            }

            bodySql = bodySql.TrimEnd('\r', '\n', ',');

            bodySql += ")" + Environment.NewLine;

            return(bodySql);
        }
 /// <summary>
 /// Equality based on Server and database name
 /// </summary>
 /// <param name="other"></param>
 /// <returns></returns>
 protected bool Equals(DiscoveredDatabase other)
 {
     return(Equals(Server, other.Server) && string.Equals(_database, other._database, StringComparison.OrdinalIgnoreCase));
 }
Example #9
0
 public abstract void DropDatabase(DiscoveredDatabase database);
Example #10
0
 public abstract void CreateBackup(DiscoveredDatabase discoveredDatabase, string backupName);
Example #11
0
 public abstract DirectoryInfo Detach(DiscoveredDatabase database);
Example #12
0
 public abstract IEnumerable <DiscoveredTableValuedFunction> ListTableValuedFunctions(DiscoveredDatabase parent, IQuerySyntaxHelper querySyntaxHelper,
                                                                                      DbConnection connection, string database, DbTransaction transaction = null);
Example #13
0
 public abstract IEnumerable <DiscoveredTable> ListTables(DiscoveredDatabase parent, IQuerySyntaxHelper querySyntaxHelper, DbConnection connection,
                                                          string database, bool includeViews, DbTransaction transaction = null);
Example #14
0
 /// <inheritdoc/>
 public abstract void CreateSchema(DiscoveredDatabase discoveredDatabase, string name);
 //constructor
 public DiscoveredTableValuedFunction(DiscoveredDatabase database, string functionName, IQuerySyntaxHelper querySyntaxHelper, string schema = null) : base(database, functionName, querySyntaxHelper, schema, TableType.TableValuedFunction)
 {
     _functionName = functionName;
 }