public override ColumnSchemaCollection GetTableColumns (TableSchema table)
		{
			ColumnSchemaCollection columns = new ColumnSchemaCollection ();
			
			IPooledDbConnection conn = connectionPool.Request ();
			IDbCommand command = conn.CreateCommand (
				"PRAGMA table_info('" +  table.Name + "')"
			);
			try {
				using (command) {
					using (IDataReader r = command.ExecuteReader()) {
						while (r.Read ()) {
							ColumnSchema column = new ColumnSchema (this, table);

							column.Position = r.GetInt32 (0);
							column.Name = r.GetString (1);
							column.DataTypeName = r.GetString (2);
							column.IsNullable = r.GetInt32 (3) != 0;
							column.DefaultValue = r.IsDBNull (4) ? null : r.GetValue (4).ToString ();
			
							columns.Add (column);
						}
						r.Close ();
					};
				}
			} catch (Exception e) {
				QueryService.RaiseException (e);
			}
			conn.Release ();

			return columns;
		}
		public override TableSchemaCollection GetTables ()
		{
			TableSchemaCollection tables = new TableSchemaCollection ();
			
			IPooledDbConnection conn = connectionPool.Request ();
			IDbCommand command = conn.CreateCommand (
				"SELECT name, sql FROM sqlite_master WHERE type = 'table'"
			);
			try {
				using (command) {
					using (IDataReader r = command.ExecuteReader()) {
						while (r.Read ()) {
							TableSchema table = new TableSchema (this);
		
							table.SchemaName = "main";
							table.Name = r.GetString (0);
							table.IsSystemTable = table.Name.StartsWith ("sqlite_");
							table.Definition = r.GetString (1);
							
							tables.Add (table);
						}
						r.Close ();
					}
				}
			} catch (Exception e) {
				QueryService.RaiseException (e);
			}
			conn.Release ();

			return tables;
		}
		public TableSchemaContainer (TableSchema schema)
		{
			if (schema == null)
				throw new ArgumentNullException ("schema");
			
			this.schema = schema;
		}
		public override ICollection<TableSchema> GetTables ()
		{
			CheckConnectionState ();
			List<TableSchema> tables = new List<TableSchema> ();
			
			IDbCommand command = connectionProvider.CreateCommand (
				"SELECT su.name AS owner, so.name as table_name, so.id as table_id, " +
				" so.crdate as created_date, so.type as table_type " +
				"FROM dbo.sysobjects so, dbo.sysusers su " +
				"WHERE type IN ('S','U') " +
				"AND su.uid = so.uid " +
				"ORDER BY 1, 2"
			);
			using (command) {
				using (IDataReader r = command.ExecuteReader()) {
					while (r.Read ()) {
						TableSchema table = new TableSchema (this);
	
						table.Name = r.GetString(1);
						table.IsSystemTable = r.GetString (4) == "S" ? true : false;
						
						table.SchemaName = r.GetString (0);
						table.OwnerName = r.GetString (0);
						table.Comment = "";
						
						StringBuilder sb = new StringBuilder();
						sb.AppendFormat ("-- Table: {0}\n", table.Name);
						sb.AppendFormat ("-- DROP TABLE {0};\n\n", table.Name);
						sb.AppendFormat ("CREATE TABLE {0} (\n", table.Name);
						
						ICollection<ColumnSchema> columns = table.Columns;
						string[] parts = new string[columns.Count];
						int i = 0;
						foreach (ColumnSchema col in columns)
							parts[i++] = col.Definition;
						sb.Append (String.Join (",\n", parts));
						
						ICollection<ConstraintSchema> constraints = table.Constraints;
						parts = new string[constraints.Count];
						if (constraints.Count > 0)
							sb.Append (",\n");
						i = 0;
						foreach (ConstraintSchema constr in constraints)
							parts[i++] = "\t" + constr.Definition;
						sb.Append (String.Join (",\n", parts));
						
						sb.Append ("\n);\n");
						//sb.AppendFormat ("COMMENT ON TABLE {0} IS '{1}';", table.Name, table.Comment);
						table.Definition = sb.ToString();
						
						tables.Add (table);
					}
					r.Close ();
				}
				connectionProvider.Close (command.Connection);
			}

			return tables;
Beispiel #5
0
		public TableNode (DatabaseConnectionContext context, TableSchema table)
			: base (context)
		{
			if (table == null)
				throw new ArgumentNullException ("table");
			
			this.table = table;
		}
Beispiel #6
0
		public TableSchema (TableSchema table)
			: base (table)
		{
			isSystemTable = table.isSystemTable;
			tableSpaceName = table.tableSpaceName;
			columns = new ColumnSchemaCollection (table.columns);
			constraints = new ConstraintSchemaCollection (table.constraints);
			triggers = new TriggerSchemaCollection (table.triggers);
		}
		public UniqueConstraintEditorWidget (ISchemaProvider schemaProvider, SchemaActions action, TableSchema table, ColumnSchemaCollection columns, ConstraintSchemaCollection constraints)
		{
			if (columns == null)
				throw new ArgumentNullException ("columns");
			if (table == null)
				throw new ArgumentNullException ("table");
			if (constraints == null)
				throw new ArgumentNullException ("constraints");
			if (schemaProvider == null)
				throw new ArgumentNullException ("schemaProvider");
			
			this.schemaProvider = schemaProvider;
			this.table = table;
			this.columns = columns;
			this.constraints = constraints;
			this.action = action;
			
			this.Build();
			
			store = new ListStore (typeof (string), typeof (bool), typeof (string), typeof (object));
			listUnique.Model = store;
			listUnique.Selection.Changed += new EventHandler (SelectionChanged);
			columnSelecter.ColumnToggled += new EventHandler (ColumnToggled);
			
			TreeViewColumn colName = new TreeViewColumn ();
			TreeViewColumn colIsColConstraint = new TreeViewColumn ();

			colName.Title = GettextCatalog.GetString ("Name");
			colIsColConstraint.Title = GettextCatalog.GetString ("Column Constraint");
			
			CellRendererText nameRenderer = new CellRendererText ();
			CellRendererToggle toggleRenderer = new CellRendererToggle ();
			
			nameRenderer.Editable = true;
			nameRenderer.Edited += new EditedHandler (NameEdited);
			
			toggleRenderer.Activatable = true;
			toggleRenderer.Toggled += new ToggledHandler (IsColumnConstraintToggled);
			
			colName.PackStart (nameRenderer, true);
			colIsColConstraint.PackStart (toggleRenderer, true);
			
			colName.AddAttribute (nameRenderer, "text", colNameIndex);
			colIsColConstraint.AddAttribute (toggleRenderer, "active", colIsColumnConstraintIndex);

			listUnique.AppendColumn (colName);
			listUnique.AppendColumn (colIsColConstraint);
			
			columnSelecter.Initialize (columns);
			
			foreach (UniqueConstraintSchema uni in constraints.GetConstraints (ConstraintType.Unique))
				AddConstraint (uni);
			//TODO: also col constraints
			
			ShowAll ();
		}
		public void Initialize (TableSchema table)
		{
			if (table == null)
				throw new ArgumentNullException ("table");
			
			this.originalTable = table;
			this.table = table;
			
			SetWarning (null);
			columnEditor = new ColumnsEditorWidget (schemaProvider, action, settings.ColumnSettings);
			columnEditor.ContentChanged += new EventHandler (OnContentChanged);
			// When primary Key are selected on the "Column Editor", it has to refresh the "Primary Key" Widget.
			columnEditor.PrimaryKeyChanged += delegate(object sender, EventArgs e) {
				if (constraintEditor != null)
					constraintEditor.RefreshConstraints ();
			};
			
			notebook.AppendPage (columnEditor, new Label (AddinCatalog.GetString ("Columns")));
			
			if (settings.ShowConstraints) {
				constraintEditor = new ConstraintsEditorWidget (schemaProvider, action, settings.ConstraintSettings);
				constraintEditor.ContentChanged += new EventHandler (OnContentChanged);
				notebook.AppendPage (constraintEditor, new Label (AddinCatalog.GetString ("Constraints")));
				// If Primary Key are changed on it has to refresh the "Column Editor" Widget to select the correct 
				// columns
				constraintEditor.PrimaryKeyChanged += delegate(object sender, EventArgs e) {
					columnEditor.RefreshConstraints ();
				};
			}

			//TODO: Implement Index
			/*
			if (settings.ShowIndices) {
				indexEditor = new IndicesEditorWidget (schemaProvider, action);
				indexEditor.ContentChanged += OnContentChanged;
				notebook.AppendPage (indexEditor, new Label (AddinCatalog.GetString ("Indexes")));
			}
			*/
						
			if (settings.ShowTriggers) {
				triggerEditor = new TriggersEditorWidget (schemaProvider, action);
				triggerEditor.ContentChanged += new EventHandler (OnContentChanged);
				notebook.AppendPage (triggerEditor, new Label (AddinCatalog.GetString ("Triggers")));
			}
			
			if (settings.ShowComment) {
				commentEditor = new CommentEditorWidget ();
				notebook.AppendPage (commentEditor, new Label (AddinCatalog.GetString ("Comment")));
			}
			notebook.Page = 0;

			entryName.Text = originalTable.Name;

			WaitDialog.ShowDialog ("Loading table data ...");
		}
		public override ICollection<TableSchema> GetTables ()
		{
			CheckConnectionState ();
			List<TableSchema> tables = new List<TableSchema> ();
			
			IDbCommand command = connectionProvider.CreateCommand (
				"SELECT OWNER, TABLE_NAME, TABLESPACE_NAME " +
				"FROM ALL_TABLES " +
				"ORDER BY OWNER, TABLE_NAME"
			);
			using (command) {
				using (IDataReader r = command.ExecuteReader()) {
					while (r.Read ()) {
						TableSchema table = new TableSchema (this);
	
						table.OwnerName = r.GetValue (0).ToString();
						table.SchemaName = r.GetValue (0).ToString();
						table.Name = r.GetString (1).ToString();
						table.IsSystemTable = IsSystem (table.OwnerName);
						table.TableSpaceName = r.GetValue (2).ToString();
						
						StringBuilder sb = new StringBuilder();
						sb.AppendFormat ("-- Table: {0}\n", table.Name);
						sb.AppendFormat ("-- DROP TABLE {0};\n\n", table.Name);
						sb.AppendFormat ("CREATE TABLE {0} (\n", table.Name);
						
						ICollection<ColumnSchema> columns = table.Columns;
						string[] parts = new string[columns.Count];
						int i = 0;
						foreach (ColumnSchema col in columns)
							parts[i++] = col.Definition;
						sb.Append (String.Join (",\n", parts));
						
						ICollection<ConstraintSchema> constraints = table.Constraints;
						parts = new string[constraints.Count];
						if (constraints.Count > 0)
							sb.Append (",\n");
						i = 0;
						foreach (ConstraintSchema constr in constraints)
							parts[i++] = "\t" + constr.Definition;
						sb.Append (String.Join (",\n", parts));
						
						//sb.AppendFormat ("\n) COMMENT '{0}';", table.Comment);
						table.Definition = sb.ToString ();
						
						tables.Add (table);
					}
					r.Close ();
				}
				connectionProvider.Close (command.Connection);
			}

			return tables;
		public PrimaryKeyConstraintEditorWidget (ISchemaProvider schemaProvider, SchemaActions action, TableSchema table, ColumnSchemaCollection columns, ConstraintSchemaCollection constraints)
		{
			if (columns == null)
				throw new ArgumentNullException ("columns");
			if (table == null)
				throw new ArgumentNullException ("table");
			if (constraints == null)
				throw new ArgumentNullException ("constraints");
			if (schemaProvider == null)
				throw new ArgumentNullException ("schemaProvider");
			
			this.schemaProvider = schemaProvider;
			this.table = table;
			this.columns = columns;
			this.constraints = constraints;
			this.action = action;
			
			this.Build();
			
			store = new ListStore (typeof (string), typeof (string), typeof (object));
			listPK.Model = store;
			
			TreeViewColumn colName = new TreeViewColumn ();
			
			colName.Title = GettextCatalog.GetString ("Name");
			CellRendererText nameRenderer = new CellRendererText ();
			
			nameRenderer.Editable = true;
			nameRenderer.Edited += new EditedHandler (NameEdited);
			
			colName.PackStart (nameRenderer, true);
			colName.AddAttribute (nameRenderer, "text", colNameIndex);
			listPK.AppendColumn (colName);
			
			columnSelecter.Initialize (columns);
			
			listPK.Selection.Changed += new EventHandler (SelectionChanged);
			columnSelecter.ColumnToggled += new EventHandler (ColumnToggled);
			
			foreach (PrimaryKeyConstraintSchema pk in constraints.GetConstraints (ConstraintType.PrimaryKey))
				AddConstraint (pk);
			
			ShowAll ();
		}
		public void Initialize (TableSchemaCollection tables, TableSchema table, ColumnSchemaCollection columns, ConstraintSchemaCollection constraints, DataTypeSchemaCollection dataTypes)
		{
			if (columns == null)
				throw new ArgumentNullException ("columns");
			if (constraints == null)
				throw new ArgumentNullException ("constraints");
			if (table == null)
				throw new ArgumentNullException ("table");
			if (tables == null)
				throw new ArgumentNullException ("tables");

			IDbFactory fac = schemaProvider.ConnectionPool.DbFactory;
			if (fac.IsCapabilitySupported ("Table", action, TableCapabilities.PrimaryKeyConstraint)) {
				//not for column constraints, since they are already editable in the column editor
				pkEditor = new PrimaryKeyConstraintEditorWidget (schemaProvider, action, table, columns, constraints);
				pkEditor.ContentChanged += new EventHandler (OnContentChanged);
				notebook.AppendPage (pkEditor, new Label (GettextCatalog.GetString ("Primary Key")));
			}
			
			if (fac.IsCapabilitySupported ("Table", action, TableCapabilities.ForeignKeyConstraint)
				|| fac.IsCapabilitySupported ("TableColumn", action, TableCapabilities.ForeignKeyConstraint)) {
				fkEditor = new ForeignKeyConstraintEditorWidget (schemaProvider, action, tables, table, columns, constraints);
				fkEditor.ContentChanged += new EventHandler (OnContentChanged);
				notebook.AppendPage (fkEditor, new Label (GettextCatalog.GetString ("Foreign Key")));
			}
			
			if (fac.IsCapabilitySupported ("Table", action, TableCapabilities.CheckConstraint)
				|| fac.IsCapabilitySupported ("TableColumn", action, TableCapabilities.CheckConstraint)) {
				checkEditor = new CheckConstraintEditorWidget (schemaProvider, action, table, columns, constraints);
				checkEditor.ContentChanged += new EventHandler (OnContentChanged);
				notebook.AppendPage (checkEditor, new Label (GettextCatalog.GetString ("Check")));
			}
			
			if (fac.IsCapabilitySupported ("Table", action, TableCapabilities.UniqueConstraint)
				|| fac.IsCapabilitySupported ("TableColumn", action, TableCapabilities.CheckConstraint)) {
				uniqueEditor = new UniqueConstraintEditorWidget (schemaProvider, action, table, columns, constraints);
				uniqueEditor.ContentChanged += new EventHandler (OnContentChanged);
				notebook.AppendPage (uniqueEditor, new Label (GettextCatalog.GetString ("Unique")));
			}

			ShowAll ();
		}
 public virtual void RenameTable(TableSchema table, string name)
 {
     throw new NotImplementedException();
 }
		
		public override ICollection<ColumnSchema> GetTableColumns (TableSchema table)
		{
			CheckConnectionState ();
			List<ColumnSchema> columns = new List<ColumnSchema> ();
			
			IDbCommand command = connectionProvider.CreateCommand (
				"SELECT OWNER, TABLE_NAME, COLUMN_NAME, " +
				"       DATA_TYPE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE, " +
				"       NULLABLE, COLUMN_ID, DEFAULT_LENGTH, DATA_DEFAULT " +
				"FROM ALL_TAB_COLUMNS " +
				"WHERE OWNER = '" + table.OwnerName + "' " + 
				"AND TABLE_NAME = '" + table.Name + "' " +
				"ORDER BY OWNER, TABLE_NAME, COLUMN_ID"
			);
			using (command) {
				using (IDataReader r = command.ExecuteReader()) {
					while (r.Read ()) {
						ColumnSchema column = new ColumnSchema (this);
		
						column.Name = GetCheckedString (r, 2);
						column.DataTypeName = GetCheckedString (r, 3);
						column.OwnerName = table.OwnerName;
						column.SchemaName = table.OwnerName;
						column.NotNull = GetCheckedString (r, 7) == "Y";
						column.Length = GetCheckedInt32 (r, 4);
						column.Precision = GetCheckedInt32 (r, 5);
						column.Scale = GetCheckedInt32 (r, 6);
						column.ColumnID = GetCheckedInt32 (r, 8);
						
						StringBuilder sb = new StringBuilder();
						sb.AppendFormat("{0} {1}{2}",
							column.Name,
							column.DataTypeName,
							(column.Length > 0) ? ("(" + column.Length + ")") : "");
						sb.AppendFormat(" {0}", column.NotNull ? "NOT NULL" : "NULL");
						//if (column.Default.Length > 0)
						//	sb.AppendFormat(" DEFAULT {0}", column.Default);
						column.Definition = sb.ToString();
		
						columns.Add (column);
					}
					r.Close ();
				};
				connectionProvider.Close (command.Connection);
			}

			return columns;
Beispiel #14
0
        protected virtual IndexSchema GetTableIndex(DataRow row, TableSchema table)
        {
            IndexSchema schema = new IndexSchema(this);

            return(schema);
        }
		//TODO:
		public override ICollection<ConstraintSchema> GetTableConstraints (TableSchema table)
		{
			CheckConnectionState ();
			List<ConstraintSchema> constraints = new List<ConstraintSchema> ();
			
			IDbCommand command = connectionProvider.CreateCommand ("SHOW TABLE STATUS FROM `" + table.OwnerName + "`;");
			using (command) {
				using (IDataReader r = command.ExecuteReader()) {
//					ConstraintSchema constraint = new ConstraintSchema (this);
//					constraint.PrimaryKey = pkColumn;
//					constraint.ForeignKey = fkColumn;
//							
//					constraints.Add (constraint);
					r.Close ();
				}
				connectionProvider.Close (command.Connection);
			}

			return constraints;
		public TableContainer (TableSchema table)
		{
			if (table == null)
				throw new ArgumentNullException ("table");
			this.table = table;
		}
		public TableEditorDialog (ISchemaProvider schemaProvider, TableSchema table, bool create)
		{
			if (schemaProvider == null)
				throw new ArgumentNullException ("schemaProvider");
			if (table == null)
				throw new ArgumentNullException ("table");
			
			this.schemaProvider = schemaProvider;
			this.originalTable = table;
			this.table = table;
			this.action = create ? SchemaActions.Create : SchemaActions.Alter;
			
			this.Build();
			
			if (create)
				Title = GettextCatalog.GetString ("Create Table");
			else
				Title = GettextCatalog.GetString ("Alter Table");
			
			notebook = new Notebook ();
			vboxContent.PackStart (notebook, true, true, 0);
			
			columnEditor = new ColumnsEditorWidget (schemaProvider, action);
			columnEditor.ContentChanged += new EventHandler (OnContentChanged);
			notebook.AppendPage (columnEditor, new Label (GettextCatalog.GetString ("Columns")));
			
			//TODO: there is a diff between col and table constraints
			IDbFactory fac = schemaProvider.ConnectionPool.DbFactory;
			if (fac.IsCapabilitySupported ("Table", action, TableCapabilities.Constraints)) {
				constraintEditor = new ConstraintsEditorWidget (schemaProvider, action);
				constraintEditor.ContentChanged += new EventHandler (OnContentChanged);
				notebook.AppendPage (constraintEditor, new Label (GettextCatalog.GetString ("Constraints")));
			}

			//TODO:
			//indexEditor = new IndicesEditorWidget (schemaProvider);
			//notebook.AppendPage (indexEditor, new Label (GettextCatalog.GetString ("Indexes")));
			
			if (fac.IsCapabilitySupported ("Table", action, TableCapabilities.Trigger)) {
				triggerEditor = new TriggersEditorWidget (schemaProvider, action);
				triggerEditor.ContentChanged += new EventHandler (OnContentChanged);
				notebook.AppendPage (triggerEditor, new Label (GettextCatalog.GetString ("Triggers")));
			}
			
			if (fac.IsCapabilitySupported ("Table", action, TableCapabilities.Comment)) {
				commentEditor = new CommentEditorWidget ();
				notebook.AppendPage (commentEditor, new Label (GettextCatalog.GetString ("Comment")));
			}

			notebook.Page = 0;

			entryName.Text = originalTable.Name;

			WaitDialog.ShowDialog ("Loading table data ...");

			notebook.Sensitive = false;
			ThreadPool.QueueUserWorkItem (new WaitCallback (InitializeThreaded));
			
			vboxContent.ShowAll ();
			SetWarning (null);
		}
		public void Initialize (TableSchema table, ColumnSchemaCollection columns, ConstraintSchemaCollection constraints)
		{
			if (columns == null)
				throw new ArgumentNullException ("columns");
			if (table == null)
				throw new ArgumentNullException ("table");
			if (constraints == null)
				throw new ArgumentNullException ("constraints");
			
			this.table = table;
			this.columns = columns;
			this.constraints = constraints;

			columnSelecter.Initialize (columns);
			RefreshConstraints ();
		}
        //http://dev.mysql.com/doc/refman/5.1/en/rename-table.html
        public override void RenameTable(TableSchema table, string name)
        {
            ExecuteNonQuery("RENAME TABLE " + table.Name + " TO " + name + ";");

            table.Name = name;
        }
 public bool ShowTableEditorDialog(ISchemaProvider schemaProvider, TableSchema table, bool create)
 {
     return(RunDialog(new TableEditorDialog(schemaProvider, table, create)));
 }
 //http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
 public override void AlterTable(TableSchema table)
 {
     throw new NotImplementedException();
 }
 //http://dev.mysql.com/doc/refman/5.1/en/drop-table.html
 public override void DropTable(TableSchema table)
 {
     ExecuteNonQuery("DROP TABLE IF EXISTS " + table.Name + ";");
 }
        // see: http://dev.mysql.com/doc/refman/5.1/en/tables-table.html
        // // see: http://dev.mysql.com/doc/refman/5.1/en/show-create-table.html
        public override TableSchemaCollection GetTables()
        {
            TableSchemaCollection tables = new TableSchemaCollection();

            IPooledDbConnection conn    = connectionPool.Request();
            IDbCommand          command = conn.CreateCommand("SHOW TABLES;");

            try {
                using (command) {
                    if (GetMainVersion(command) >= 5)
                    {
                        //in mysql 5.x we can use an sql query to provide the comment
                        command.CommandText = "SELECT TABLE_NAME, TABLE_SCHEMA, TABLE_TYPE, TABLE_COMMENT FROM `information_schema`.`TABLES` "
                                              + "WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='"
                                              + command.Connection.Database
                                              + "' ORDER BY TABLE_NAME;";
                        using (IDataReader r = command.ExecuteReader()) {
                            while (r.Read())
                            {
                                TableSchema table = new TableSchema(this);

                                table.Name       = r.GetString(0);
                                table.SchemaName = r.GetString(1);
                                table.Comment    = r.IsDBNull(3) ? null : r.GetString(3);

                                IPooledDbConnection conn2    = connectionPool.Request();
                                IDbCommand          command2 = conn2.CreateCommand("SHOW CREATE TABLE `" + table.Name + "`;");
                                using (IDataReader r2 = command2.ExecuteReader()) {
                                    r2.Read();
                                    table.Definition = r2.GetString(1);
                                }
                                conn2.Release();

                                tables.Add(table);
                            }
                            r.Close();
                        }
                    }
                    else
                    {
                        //use the default command for mysql 4.x and 3.23
                        using (IDataReader r = command.ExecuteReader()) {
                            while (r.Read())
                            {
                                TableSchema table = new TableSchema(this);

                                table.Name       = r.GetString(0);
                                table.SchemaName = command.Connection.Database;

                                IPooledDbConnection conn2    = connectionPool.Request();
                                IDbCommand          command2 = conn2.CreateCommand("SHOW CREATE TABLE `" + table.Name + "`;");
                                using (IDataReader r2 = command2.ExecuteReader()) {
                                    r2.Read();
                                    table.Definition = r2.GetString(1);
                                }
                                conn2.Release();

                                tables.Add(table);
                            }
                            r.Close();
                        }
                    }
                }
            } catch (Exception e) {
                QueryService.RaiseException(e);
            }
            conn.Release();

            return(tables);
        }
        //http://dev.mysql.com/doc/refman/5.1/en/create-table.html
        public override string GetTableCreateStatement(TableSchema table)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("CREATE TABLE ");
            sb.Append(table.Name);
            sb.Append(" (");

            bool first = true;

            foreach (ColumnSchema column in table.Columns)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append("," + Environment.NewLine);
                }

                sb.Append(column.Name);
                sb.Append(' ');
                sb.Append(column.DataType.GetCreateString(column));

                if (!column.IsNullable)
                {
                    sb.Append(" NOT NULL");
                }
                if (column.HasDefaultValue)
                {
                    sb.Append(" DEFAULT ");
                    if (column.DefaultValue == null)
                    {
                        sb.Append("NULL");
                    }
                    else
                    {
                        sb.Append(column.DefaultValue);
                    }
                }
                //TODO: AUTO_INCREMENT

                foreach (ConstraintSchema constraint in column.Constraints)
                {
                    switch (constraint.ConstraintType)
                    {
                    case ConstraintType.Unique:
                        sb.Append(" UNIQUE");
                        break;

                    case ConstraintType.PrimaryKey:
                        sb.Append(" PRIMARY KEY");
                        break;

                    default:
                        throw new NotImplementedException();
                    }
                }

                if (column.Comment != null)
                {
                    sb.Append(" COMMENT '");
                    sb.Append(column.Comment);
                    sb.Append("'");
                }
            }
            //TODO: table comment

            foreach (ConstraintSchema constraint in table.Constraints)
            {
                sb.Append("," + Environment.NewLine);
                sb.Append(GetConstraintString(constraint));
            }

            sb.Append(")");

            if (table.TableSpaceName != null)
            {
                sb.Append(", TABLESPACE ");
                sb.Append(table.TableSpaceName);
                sb.Append(" STORAGE DISK");
            }

            sb.Append(";");

            foreach (TriggerSchema trigger in table.Triggers)
            {
                sb.Append(Environment.NewLine);
                sb.Append(GetTriggerCreateStatement(trigger));
            }

            return(sb.ToString());
        }
        public virtual void CreateTable(TableSchema table)
        {
            string sql = GetTableCreateStatement(table);

            ExecuteNonQuery(sql);
        }
		private void FillReferenceColumnSelector (TreeIter iter, string table)
		{
			if (tables.Contains (table)) {
				refTable = tables.Search (table);
				if (refTable != null) {
					referenceColumnSelecter.Initialize (refTable.Columns);
					referenceColumnSelecter.Sensitive = true;
					store.SetValue (iter, colReferenceTableIndex, table);
					SetSelectionFromIter (iter);
				} else {
					referenceColumnSelecter.Sensitive = false;
					referenceColumnSelecter.Clear ();
				}
				EmitContentChanged ();
				
			} 
		}
Beispiel #27
0
        protected virtual ColumnSchema GetTableIndexColumn(DataRow row, TableSchema table, IndexSchema index)
        {
            ColumnSchema schema = new ColumnSchema(this, table);

            return(schema);
        }
		private void InitializeThreaded (object state)
		{
			tables = schemaProvider.GetTables ();
			dataTypes = schemaProvider.GetDataTypes ();
			columns = originalTable.Columns;
			constraints = originalTable.Constraints;
			triggers = originalTable.Triggers;
			//TODO: indices
			indexes = new IndexSchemaCollection ();
			
			Runtime.LoggingService.Error ("TABLE " + originalTable.Name);
			Runtime.LoggingService.Error ("   columns = " + columns.Count);
			Runtime.LoggingService.Error ("   constraints = " + constraints.Count);

			try {
			foreach (ColumnSchema col in columns) {				
				int dummy = col.Constraints.Count; //get column constraints
				Runtime.LoggingService.Error ("CONSTRAINTS " + col.Name + " " + dummy);
			}
			} catch (Exception ee) {
				Runtime.LoggingService.Error (ee);
				Runtime.LoggingService.Error (ee.StackTrace);
			}

			if (action == SchemaActions.Alter) //make a duplicate if we are going to alter the table
				this.table = originalTable.Clone () as TableSchema;

			DispatchService.GuiDispatch (delegate () {
				InitializeGui ();
			});
		}
		public void Initialize (TableSchema table, ColumnSchemaCollection columns, ConstraintSchemaCollection constraints)
		{
			if (columns == null)
				throw new ArgumentNullException ("columns");
			if (table == null)
				throw new ArgumentNullException ("table");
			if (constraints == null)
				throw new ArgumentNullException ("constraints");
			
			this.table = table;
			this.columns = columns;
			this.constraints = constraints;
			
			columnSelecter.Initialize (columns);
			
			foreach (UniqueConstraintSchema uni in constraints.GetConstraints (ConstraintType.Unique))
				AddConstraint (uni);
			
			//TODO: also col constraints
		}
		public bool ShowTableEditorDialog (ISchemaProvider schemaProvider, TableSchema table, bool create)
		{
			return RunDialog (new TableEditorDialog (schemaProvider, table, create));
Beispiel #31
0
 protected virtual ConstraintSchema GetTableConstraint(DataRow row, TableSchema table)
 {
     return(null);
 }
		public override ICollection<ColumnSchema> GetTableColumns (TableSchema table)
		{
			return GetTableOrViewColumns (table.Name);
		public void Initialize (TableSchemaCollection tables, TableSchema table, ColumnSchemaCollection columns, ConstraintSchemaCollection constraints, DataTypeSchemaCollection dataTypes)
		{
			if (columns == null)
				throw new ArgumentNullException ("columns");
			if (constraints == null)
				throw new ArgumentNullException ("constraints");
			if (table == null)
				throw new ArgumentNullException ("table");
			if (tables == null)
				throw new ArgumentNullException ("tables");

			if (pkEditor != null)
				pkEditor.Initialize (table, columns, constraints);
			if (fkEditor != null)
				fkEditor.Initialize (tables, table, columns, constraints);
			if (checkEditor != null)
				checkEditor.Initialize (table, columns, constraints);
			if (uniqueEditor != null)
				uniqueEditor.Initialize (table, columns, constraints);
		}
		public override ICollection<TableSchema> GetTables ()
		{
			CheckConnectionState ();
			List<TableSchema> tables = new List<TableSchema> ();
			
			IDbCommand command = connectionProvider.CreateCommand (
				"SELECT RDB$RELATION_NAME, RDB$SYSTEM_FLAG, RDB$OWNER_NAME, RDB$DESCRIPTION FROM RDB$RELATIONS "+
				"WHERE RDB$VIEW_BLR IS NULL;"
			);

			using (command) {
				using (IDataReader r = command.ExecuteReader()) {
					while (r.Read ()) {
						TableSchema table = new TableSchema (this);
	
						table.Name = r.GetString (0);
						table.IsSystemTable = (!r.IsDBNull (1) && r.GetInt32 (1) != 0);
						table.OwnerName = r.GetString (2);
						table.Comment = r.GetString (3);
						
						tables.Add (table);
					}
					r.Close ();
				}
				connectionProvider.Close (command.Connection);
			}

			return tables;
Beispiel #35
0
		private void InitializeThreaded (object state)
		{
			tables = schemaProvider.GetTables ();
			dataTypes = schemaProvider.GetDataTypes ();
			columns = originalTable.Columns;
			constraints = originalTable.Constraints;
			triggers = originalTable.Triggers;
			//TODO: indices
			indexes = new IndexSchemaCollection ();
			
			System.Text.StringBuilder builder = new System.Text.StringBuilder ();
			builder.Append ("Loading editor for TABLE ");
			builder.Append (originalTable.Name);
			builder.AppendLine ();
			builder.Append ("    columns = ");
			builder.Append (columns.Count);
			builder.AppendLine ();
			builder.Append ("constraints = ");
			builder.Append (constraints.Count);
			builder.AppendLine ();

			try {
				foreach (ColumnSchema col in columns) {				
					int dummy = col.Constraints.Count; //get column constraints
					builder.Append ("CONSTRAINTS ");
					builder.Append (col.Name);
					builder.Append (" ");
					builder.Append (dummy);
					builder.AppendLine ();
				}
				LoggingService.LogDebug (builder.ToString ());
			} catch (Exception ee) {
				LoggingService.LogDebug (builder.ToString ());
				LoggingService.LogError (ee.ToString ());
			}

			if (action == SchemaActions.Alter) //make a duplicate if we are going to alter the table
				this.table = originalTable.Clone () as TableSchema;

			DispatchService.GuiDispatch (delegate () {
				InitializeGui ();
			});
		}
Beispiel #36
0
 public virtual string GetDeleteQuery(TableSchema table)
 {
     return(string.Concat("DELETE FROM ", table.Name, Environment.NewLine, "WHERE"));
 }
Beispiel #37
0
 public virtual string GetTableCreateStatement(TableSchema table)
 {
     throw new NotImplementedException();
 }
		public override ICollection<ConstraintSchema> GetTableConstraints (TableSchema table)
		{
			CheckConnectionState ();
			List<ConstraintSchema> constraints = new List<ConstraintSchema> ();
			
			IDbCommand command = connectionProvider.CreateCommand (
				"SELECT k.owner, k.table_name, k.constraint_name, " +
				"       k.constraint_type, k.status, k.validated " +
				"FROM all_constraints k " +
				"WHERE k.owner = '" + table.OwnerName + "' " +
				"AND k.table_name = '" + table.Name + "' " +
				"and k.constraint_type = 'P'"
			);
			using (command) {
				using (IDataReader r = command.ExecuteReader()) {
					while (r.Read ()) {
						ConstraintSchema constraint = null;
										
						switch (r.GetString(4)) {
							case "P":
							default:
								constraint = new PrimaryKeyConstraintSchema (this);
								break;
						}
						
						constraint.Name = r.GetString (3);
						constraint.Definition = "";
						
						constraints.Add (constraint);
					}
					r.Close ();
				}
				connectionProvider.Close (command.Connection);
			}

			return constraints;
		public void Initialize (TableSchemaCollection tables, TableSchema table, ColumnSchemaCollection columns, ConstraintSchemaCollection constraints)
		{
			if (columns == null)
				throw new ArgumentNullException ("columns");
			if (table == null)
				throw new ArgumentNullException ("table");
			if (constraints == null)
				throw new ArgumentNullException ("constraints");
			if (tables == null)
				throw new ArgumentNullException ("tables");
			
			this.table = table;
			this.tables = tables;
			this.columns = columns;
			this.constraints = constraints;
			columnSelecter.Initialize (columns);
			foreach (TableSchema tbl in tables)
				if (tbl.Name != table.Name)
					storeTables.AppendValues (tbl.Name, tbl);
		}
Beispiel #40
0
		public void Select (TableSchema table)
		{
			store.Select (table);
		}
 public virtual void DropTable(TableSchema table)
 {
     throw new NotImplementedException();
 }
		public void Initialize (TableSchema table, TriggerSchemaCollection triggers)
		{
			if (table == null)
				throw new ArgumentNullException ("table");
			if (triggers == null)
				throw new ArgumentNullException ("triggers");

			this.table = table;
			this.triggers = triggers;
			
			if (action == SchemaActions.Alter)
				foreach (TriggerSchema trigger in triggers)
					AddTrigger (trigger);
		}
Beispiel #43
0
 public virtual ConstraintSchemaCollection GetColumnConstraints(TableSchema table, ColumnSchema column)
 {
     throw new NotImplementedException();
 }
		//TODO: difference between columns and reference columns + combo events
		public ForeignKeyConstraintEditorWidget (ISchemaProvider schemaProvider, SchemaActions action, TableSchemaCollection tables, TableSchema table, ColumnSchemaCollection columns, ConstraintSchemaCollection constraints)
		{
			if (columns == null)
				throw new ArgumentNullException ("columns");
			if (table == null)
				throw new ArgumentNullException ("table");
			if (constraints == null)
				throw new ArgumentNullException ("constraints");
			if (schemaProvider == null)
				throw new ArgumentNullException ("schemaProvider");
			if (tables == null)
				throw new ArgumentNullException ("tables");
			
			this.schemaProvider = schemaProvider;
			this.table = table;
			this.tables = tables;
			this.columns = columns;
			this.constraints = constraints;
			this.action = action;
			
			this.Build();
			
			store = new ListStore (typeof (string), typeof (string), typeof (bool), typeof (string), typeof (string), typeof (string), typeof (string), typeof (object));
			listFK.Model = store;
			
			storeActions = new ListStore (typeof (string), typeof (int));
			storeTables = new ListStore (typeof (string));
			
			IDbFactory fac = schemaProvider.ConnectionPool.DbFactory;
			if (fac.IsCapabilitySupported ("ForeignKeyConstraint", action,  ForeignKeyConstraintCapabilities.Cascade))
				storeActions.AppendValues ("Cascade", ForeignKeyAction.Cascade);
			if (fac.IsCapabilitySupported ("ForeignKeyConstraint", action,  ForeignKeyConstraintCapabilities.Restrict))
				storeActions.AppendValues ("Restrict", ForeignKeyAction.Restrict);
			if (fac.IsCapabilitySupported ("ForeignKeyConstraint", action,  ForeignKeyConstraintCapabilities.NoAction))
				storeActions.AppendValues ("No Action", ForeignKeyAction.NoAction);
			if (fac.IsCapabilitySupported ("ForeignKeyConstraint", action,  ForeignKeyConstraintCapabilities.SetNull))
				storeActions.AppendValues ("Set Null", ForeignKeyAction.SetNull);
			if (fac.IsCapabilitySupported ("ForeignKeyConstraint", action,  ForeignKeyConstraintCapabilities.SetDefault))
				storeActions.AppendValues ("Set Default", ForeignKeyAction.SetDefault);

			foreach (TableSchema tbl in tables)
				if (tbl.Name != table.Name)
					storeTables.AppendValues (tbl.Name);
			
			TreeViewColumn colName = new TreeViewColumn ();
			TreeViewColumn colRefTable = new TreeViewColumn ();
			TreeViewColumn colIsColumnConstraint = new TreeViewColumn ();
			TreeViewColumn colDeleteAction = new TreeViewColumn ();
			TreeViewColumn colUpdateAction = new TreeViewColumn ();
			
			colName.Title = GettextCatalog.GetString ("Name");
			colRefTable.Title = GettextCatalog.GetString ("Reference Table");
			colIsColumnConstraint.Title = GettextCatalog.GetString ("Column Constraint");
			colDeleteAction.Title = GettextCatalog.GetString ("Delete Action");
			colUpdateAction.Title = GettextCatalog.GetString ("Update Action");
			
			colRefTable.MinWidth = 120;
			
			CellRendererText nameRenderer = new CellRendererText ();
			CellRendererCombo refTableRenderer = new CellRendererCombo ();
			CellRendererToggle isColumnConstraintRenderer = new CellRendererToggle ();
			CellRendererCombo deleteActionRenderer = new CellRendererCombo ();
			CellRendererCombo updateActionRenderer = new CellRendererCombo ();
			
			nameRenderer.Editable = true;
			nameRenderer.Edited += new EditedHandler (NameEdited);
			
			refTableRenderer.Model = storeTables;
			refTableRenderer.TextColumn = 0;
			refTableRenderer.Editable = true;
			refTableRenderer.Edited += new EditedHandler (RefTableEdited);
			
			isColumnConstraintRenderer.Activatable = true;
			isColumnConstraintRenderer.Toggled += new ToggledHandler (IsColumnConstraintToggled);
			
			deleteActionRenderer.Model = storeActions;
			deleteActionRenderer.TextColumn = 0;
			deleteActionRenderer.Editable = true;
			deleteActionRenderer.Edited += new EditedHandler (DeleteActionEdited);
			
			updateActionRenderer.Model = storeActions;
			updateActionRenderer.TextColumn = 0;
			updateActionRenderer.Editable = true;
			updateActionRenderer.Edited += new EditedHandler (UpdateActionEdited);

			colName.PackStart (nameRenderer, true);
			colRefTable.PackStart (refTableRenderer, true);
			colIsColumnConstraint.PackStart (isColumnConstraintRenderer, true);
			colDeleteAction.PackStart (deleteActionRenderer, true);
			colUpdateAction.PackStart (updateActionRenderer, true);

			colName.AddAttribute (nameRenderer, "text", colNameIndex);
			colRefTable.AddAttribute (refTableRenderer, "text", colReferenceTableIndex);
			colIsColumnConstraint.AddAttribute (isColumnConstraintRenderer, "active", colIsColumnConstraintIndex);
			colDeleteAction.AddAttribute (deleteActionRenderer, "text", colDeleteActionIndex);			
			colUpdateAction.AddAttribute (updateActionRenderer, "text", colUpdateActionIndex);
			
			listFK.AppendColumn (colName);
			listFK.AppendColumn (colRefTable);
			listFK.AppendColumn (colIsColumnConstraint);
			listFK.AppendColumn (colDeleteAction);
			listFK.AppendColumn (colUpdateAction);
			
			columnSelecter.ColumnToggled += new EventHandler (ColumnToggled);
			referenceColumnSelecter.ColumnToggled += new EventHandler (ReferenceColumnToggled);
			listFK.Selection.Changed += new EventHandler (SelectionChanged);
			
			ShowAll ();
		}
		public virtual string GetTableAlterStatement (TableSchema table)
		{
			throw new NotImplementedException ();
		}