1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
|
//
// Authors:
// Christian Hergert <chris@mosaix.net>
// Daniel Morgan <danielmorgan@verizon.net>
// Sureshkumar T <tsureshkumar@novell.com>
// Ben Motmans <ben.motmans@gmail.com>
//
// Copyright (C) 2005 Mosaix Communications, Inc.
// Copyright (c) 2007 Ben Motmans
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEC
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using MonoDevelop.Core;
namespace MonoDevelop.Database.Sql
{
// see:
// http://www.alberton.info/sql_server_meta_info.html + msdn
public class SqlServerSchemaProvider : AbstractSchemaProvider
{
public SqlServerSchemaProvider (IConnectionPool connectionPool)
: base (connectionPool)
{
}
public override DatabaseSchemaCollection GetDatabases ()
{
DatabaseSchemaCollection databases = new DatabaseSchemaCollection ();
IPooledDbConnection conn = connectionPool.Request ();
conn.DbConnection.ChangeDatabase ("master"); //we don't have to change it back afterwards, since the connectionpool will do this for us
IDbCommand command = conn.CreateCommand ("select name from sysdatabases");
try {
using (command) {
using (IDataReader r = command.ExecuteReader()) {
while (r.Read()) {
DatabaseSchema db = new DatabaseSchema (this);
db.Name = r.GetString (0);
databases.Add (db);
}
r.Close ();
}
}
} catch (Exception e) {
QueryService.RaiseException (e);
}
conn.Release ();
return databases;
}
public override TableSchemaCollection GetTables ()
{
TableSchemaCollection tables = new TableSchemaCollection ();
IPooledDbConnection conn = connectionPool.Request ();
IDbCommand command = conn.CreateCommand (
"SELECT su.name AS owner, so.name as table_name, so.id as table_id, " +
" so.crdate as created_date, so.xtype as table_type " +
" FROM dbo.sysobjects so, dbo.sysusers su " +
"WHERE xtype IN ('S','U') " +
"AND su.uid = so.uid " +
"ORDER BY 1, 2"
);
try {
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.OwnerName = r.GetString(0);
table.Definition = GetTableDefinition (table);
tables.Add (table);
}
r.Close ();
}
}
} catch (Exception e) {
QueryService.RaiseException (e);
}
conn.Release ();
return tables;
}
public override ColumnSchemaCollection GetTableColumns (TableSchema table)
{
ColumnSchemaCollection columns = new ColumnSchemaCollection ();
IPooledDbConnection conn = connectionPool.Request ();
IDbCommand command = conn.CreateCommand (
"SELECT su.name as owner, so.name as table_name, " +
" sc.name as column_name, " +
" st.name as date_type, sc.length as column_length, " +
" sc.xprec as data_precision, sc.xscale as data_scale, " +
" sc.isnullable, sc.colid as column_id " +
"FROM dbo.syscolumns sc, dbo.sysobjects so, " +
" dbo.systypes st, dbo.sysusers su " +
"WHERE sc.id = so.id " +
"AND so.xtype in ('U','S') " +
"AND so.name = '" + table.Name + "' " +
"AND su.name = '" + table.OwnerName + "' " +
"AND sc.xusertype = st.xusertype " +
"AND su.uid = so.uid " +
"ORDER BY sc.colid"
);
try {
using (command) {
using (IDataReader r = command.ExecuteReader()) {
while (r.Read()) {
ColumnSchema column = new ColumnSchema (this, table);
column.Name = r.GetString (2);
column.DataTypeName = r.GetString (3);
column.DefaultValue = String.Empty;
column.Comment = String.Empty;
column.OwnerName = table.OwnerName;
column.SchemaName = table.SchemaName;
column.IsNullable = r.GetValue (7).ToString () == "0" ? true : false;
column.DataType.LengthRange.Default = r.GetInt16 (4);
column.DataType.PrecisionRange.Default = r.IsDBNull (5) ? 0 : (int)r.GetByte (5);
column.DataType.ScaleRange.Default = r.IsDBNull (6) ? 0 : (int)r.GetByte (6);
column.Definition = String.Concat (column.Name, " ", column.DataTypeName, " ",
column.DataType.LengthRange.Default > 0 ? "(" + column.DataType.LengthRange.Default + ")" : "",
column.IsNullable ? " NULL" : " NOT NULL");
//TODO: append " DEFAULT ..." if column.Default.Length > 0
columns.Add (column);
}
r.Close ();
}
}
} catch (Exception e) {
QueryService.RaiseException (e);
}
conn.Release ();
return columns;
}
public override ViewSchemaCollection GetViews ()
{
ViewSchemaCollection views = new ViewSchemaCollection ();
IPooledDbConnection conn = connectionPool.Request ();
IDbCommand command = conn.CreateCommand (
"SELECT su.name AS owner, so.name as table_name, so.id as table_id, " +
" so.crdate as created_date, so.xtype as table_type " +
"FROM dbo.sysobjects so, dbo.sysusers su " +
"WHERE xtype = 'V' " +
"AND su.uid = so.uid " +
"ORDER BY 1, 2"
);
try {
using (command) {
using (IDataReader r = command.ExecuteReader()) {
while (r.Read()) {
ViewSchema view = new ViewSchema (this);
view.Name = r.GetString (1);
view.SchemaName = r.GetString (0);
view.OwnerName = r.GetString (0);
StringBuilder sb = new StringBuilder();
sb.AppendFormat ("-- View: {0}\n", view.Name);
sb.AppendFormat ("-- DROP VIEW {0};\n\n", view.Name);
sb.AppendFormat (" {0}\n);", GetSource ("[" + view.OwnerName + "].[" + view.Name + "]"));
view.Definition = sb.ToString ();
views.Add (view);
}
r.Close ();
}
}
} catch (Exception e) {
QueryService.RaiseException (e);
}
conn.Release ();
return views;
}
public override ColumnSchemaCollection GetViewColumns (ViewSchema view)
{
ColumnSchemaCollection columns = new ColumnSchemaCollection ();
IPooledDbConnection conn = connectionPool.Request ();
IDbCommand command = conn.CreateCommand (
"SELECT * FROM \"" + view.Name +
"\" WHERE 1 = 0"
);
try {
using (command) {
using (IDataReader r = command.ExecuteReader()) {
for (int i = 0; i < r.FieldCount; i++) {
ColumnSchema column = new ColumnSchema (this, view);
column.Name = r.GetName(i);
column.DataTypeName = r.GetDataTypeName(i);
column.DefaultValue = "";
column.Definition = "";
column.OwnerName = view.OwnerName;
column.SchemaName = view.OwnerName;
columns.Add (column);
}
r.Close ();
}
}
} catch (Exception e) {
QueryService.RaiseException (e);
}
conn.Release ();
return columns;
}
public override ProcedureSchemaCollection GetProcedures ()
{
ProcedureSchemaCollection procedures = new ProcedureSchemaCollection ();
IPooledDbConnection conn = connectionPool.Request ();
IDbCommand command = conn.CreateCommand (
"SELECT su.name AS owner, so.name as proc_name, so.id as proc_id, " +
" so.crdate as created_date, so.xtype as proc_type " +
"FROM dbo.sysobjects so, dbo.sysusers su " +
"WHERE xtype = 'P' " +
"AND su.uid = so.uid " +
"ORDER BY 1, 2"
);
try {
using (command) {
using (IDataReader r = command.ExecuteReader()) {
while (r.Read ()) {
ProcedureSchema procedure = new ProcedureSchema (this);
procedure.Name = r.GetString (1);
procedure.OwnerName = r.GetString (0);
procedure.LanguageName = "TSQL";
StringBuilder sb = new StringBuilder();
sb.AppendFormat ("-- Procedure: {0}\n", procedure.Name);
sb.AppendFormat (" {0}\n);", GetSource ("[" + procedure.OwnerName + "].[" + procedure.Name + "]"));
procedure.Definition = sb.ToString ();
// FIXME : get sysproc or not
procedures.Add (procedure);
}
r.Close ();
}
}
} catch (Exception e) {
QueryService.RaiseException (e);
}
conn.Release ();
return procedures;
}
public override ConstraintSchemaCollection GetTableConstraints (TableSchema table)
{
ConstraintSchemaCollection constraints = new ConstraintSchemaCollection ();
IPooledDbConnection conn = connectionPool.Request ();
IDbCommand command = conn.CreateCommand ("select name, xtype from sysobjects where xtype in ('F','PK','CK')"); //TODO: unique
try {
using (command) {
using (IDataReader r = command.ExecuteReader()) {
while (r.Read ()) {
ConstraintSchema constraint = null;
switch (r.GetString (1)) {
case "F": //foreign key
constraint = new ForeignKeyConstraintSchema (this);
break;
case "PK": //primary key
constraint = new PrimaryKeyConstraintSchema (this);
break;
case "CK": //check constraint
constraint = new CheckConstraintSchema (this);
break;
default:
break;
}
if (constraint != null) {
constraint.Name = r.GetString (0);
constraints.Add (constraint);
}
}
r.Close ();
}
}
} catch (Exception e) {
QueryService.RaiseException (e);
}
conn.Release ();
return constraints;
}
// see:
// http://www.firebirdsql.org/manual/migration-mssql-data-types.html
// http://webcoder.info/reference/MSSQLDataTypes.html
// http://www.tar.hu/sqlbible/sqlbible0022.html
// http://msdn2.microsoft.com/en-us/library/aa258876(SQL.80).aspx
public override DataTypeSchema GetDataType (string name)
{
if (name == null)
throw new ArgumentNullException ("name");
name = name.ToLower ();
DataTypeSchema dts = new DataTypeSchema (this);
dts.Name = name;
switch (name) {
case "bigint":
dts.LengthRange = new Range (8);
dts.PrecisionRange = new Range (1, 19);
dts.DataTypeCategory = DataTypeCategory.Integer;
break;
case "binary":
dts.LengthRange = new Range (1, 8004);
dts.PrecisionRange = new Range (1, 8000);
dts.DataTypeCategory = DataTypeCategory.Binary;
break;
case "bit":
dts.LengthRange = new Range (1);
dts.DataTypeCategory = DataTypeCategory.Bit;
break;
case "char":
dts.LengthRange = new Range (1, 8000);
dts.PrecisionRange = new Range (1, 8000);
dts.DataTypeCategory = DataTypeCategory.Char;
break;
case "datetime":
dts.LengthRange = new Range (8);
dts.DataTypeCategory = DataTypeCategory.DateTime;
break;
case "decimal":
dts.LengthRange = new Range (5, 17);
dts.PrecisionRange = new Range (1, 38);
dts.ScaleRange = new Range (0, 37);
dts.DataTypeCategory = DataTypeCategory.Float;
break;
case "float":
dts.LengthRange = new Range (8);
dts.ScaleRange = new Range (1, 15);
dts.DataTypeCategory = DataTypeCategory.Float;
break;
case "image":
dts.LengthRange = new Range (0, int.MaxValue);
dts.PrecisionRange = new Range (0, int.MaxValue);
dts.DataTypeCategory = DataTypeCategory.VarBinary;
break;
case "int":
dts.LengthRange = new Range (4);
dts.PrecisionRange = new Range (1, 10);
dts.DataTypeCategory = DataTypeCategory.Integer;
break;
case "money":
dts.LengthRange = new Range (8);
dts.PrecisionRange = new Range (1, 19);
dts.ScaleRange = new Range (4);
dts.DataTypeCategory = DataTypeCategory.Integer;
break;
case "nchar":
dts.LengthRange = new Range (2, 8000);
dts.PrecisionRange = new Range (1, 4000);
dts.DataTypeCategory = DataTypeCategory.NChar;
break;
case "ntext":
dts.LengthRange = new Range (0, int.MaxValue);
dts.PrecisionRange = new Range (0, 1073741823);
dts.DataTypeCategory = DataTypeCategory.NVarChar;
break;
case "numeric":
dts.LengthRange = new Range (5, 17);
dts.PrecisionRange = new Range (1, 38);
dts.ScaleRange = new Range (0, 37);
dts.DataTypeCategory = DataTypeCategory.Float;
break;
case "nvarchar":
dts.LengthRange = new Range (0, 8000);
dts.PrecisionRange = new Range (0, 4000);
dts.DataTypeCategory = DataTypeCategory.NVarChar;
break;
case "real":
dts.LengthRange = new Range (4);
dts.ScaleRange = new Range (7);
dts.DataTypeCategory = DataTypeCategory.Float;
break;
case "smalldatetime":
dts.LengthRange = new Range (4);
dts.DataTypeCategory = DataTypeCategory.DateTime;
break;
case "smallint":
dts.LengthRange = new Range (2);
dts.PrecisionRange = new Range (5);
dts.DataTypeCategory = DataTypeCategory.Integer;
break;
case "smallmoney":
dts.LengthRange = new Range (4);
dts.PrecisionRange = new Range (10);
dts.ScaleRange = new Range (4);
dts.DataTypeCategory = DataTypeCategory.Integer;
break;
case "text":
dts.LengthRange = new Range (0, int.MaxValue);
dts.PrecisionRange = new Range (0, int.MaxValue);
dts.DataTypeCategory = DataTypeCategory.VarChar;
break;
case "timestamp":
dts.LengthRange = new Range (1, 8);
dts.DataTypeCategory = DataTypeCategory.TimeStamp;
break;
case "tinyint":
dts.LengthRange = new Range (1);
dts.PrecisionRange = new Range (1, 3);
dts.DataTypeCategory = DataTypeCategory.Integer;
break;
case "varbinary":
dts.LengthRange = new Range (1, 8004);
dts.PrecisionRange = new Range (0, 8000);
dts.DataTypeCategory = DataTypeCategory.VarBinary;
break;
case "varchar":
dts.LengthRange = new Range (1, 8000);
dts.PrecisionRange = new Range (0, 8000);
dts.DataTypeCategory = DataTypeCategory.VarChar;
break;
case "uniqueidentifier":
dts.LengthRange = new Range (16);
dts.DataTypeCategory = DataTypeCategory.Uid;
break;
case "xml":
dts.LengthRange = new Range (0, int.MaxValue);
dts.PrecisionRange = new Range (0, int.MaxValue);
dts.DataTypeCategory = DataTypeCategory.VarChar;
break;
case "cursor":
case "table":
case "sql_variant":
dts.DataTypeCategory = DataTypeCategory.Other;
break;
default:
break;
}
return dts;
}
//http://msdn2.microsoft.com/en-us/library/aa258257(SQL.80).aspx
public override void CreateDatabase (DatabaseSchema database)
{
IPooledDbConnection conn = connectionPool.Request ();
IDbCommand command = conn.CreateCommand ("CREATE DATABASE " + database.Name);
try {
using (command)
command.ExecuteNonQuery ();
} catch (Exception e) {
QueryService.RaiseException (e);
}
conn.Release ();
}
//http://msdn2.microsoft.com/en-us/library/aa258255(SQL.80).aspx
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.HasDefaultValue) {
sb.Append (" DEFAULT ");
if (column.DefaultValue == null)
sb.Append ("NULL");
else
sb.Append (column.DefaultValue);
}
if (!column.IsNullable)
sb.Append (" NOT NULL");
//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 ();
}
}
//TODO: col comment
}
//TODO: table comment
foreach (ConstraintSchema constraint in table.Constraints) {
sb.Append ("," + Environment.NewLine);
sb.Append (GetConstraintString (constraint));
}
sb.Append (");");
foreach (TriggerSchema trigger in table.Triggers) {
sb.Append (Environment.NewLine);
sb.Append (GetTriggerCreateStatement (trigger));
}
return sb.ToString ();
}
protected virtual string GetConstraintString (ConstraintSchema constraint)
{
StringBuilder sb = new StringBuilder ();
sb.Append ("CONSTRAINT ");
sb.Append (constraint.Name);
sb.Append (' ');
switch (constraint.ConstraintType) {
case ConstraintType.PrimaryKey:
sb.Append ("PRIMARY KEY ");
sb.Append (GetColumnsString (constraint.Columns, true));
break;
case ConstraintType.Unique:
sb.Append ("UNIQUE ");
sb.Append (GetColumnsString (constraint.Columns, true));
break;
case ConstraintType.Check:
sb.Append ("CHECK (");
sb.Append ((constraint as CheckConstraintSchema).Source);
sb.Append (")");
break;
case ConstraintType.ForeignKey:
sb.Append ("FOREIGN KEY ");
sb.Append (GetColumnsString (constraint.Columns, true));
sb.Append (" REFERENCES ");
ForeignKeyConstraintSchema fk = constraint as ForeignKeyConstraintSchema;
sb.Append (fk.ReferenceTable);
sb.Append (' ');
if (fk.ReferenceColumns != null)
sb.Append (GetColumnsString (fk.ReferenceColumns, true));
break;
default:
throw new NotImplementedException ();
}
return sb.ToString ();
}
//http://msdn2.microsoft.com/en-us/library/aa258254(SQL.80).aspx
public override void CreateView (ViewSchema view)
{
throw new NotImplementedException ();
}
//http://msdn2.microsoft.com/en-us/library/aa258259(SQL.80).aspx
public override void CreateProcedure (ProcedureSchema procedure)
{
throw new NotImplementedException ();
}
//http://msdn2.microsoft.com/en-us/library/aa258259(SQL.80).aspx
public override void CreateIndex (IndexSchema index)
{
throw new NotImplementedException ();
}
//http://msdn2.microsoft.com/en-us/library/aa258254(SQL.80).aspx
public override void CreateTrigger (TriggerSchema trigger)
{
string sql = GetTriggerCreateStatement (trigger);
ExecuteNonQuery (sql);
}
protected virtual string GetTriggerCreateStatement (TriggerSchema trigger)
{
StringBuilder sb = new StringBuilder ();
// { FOR | AFTER | INSTEAD OF } { [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] }
// [ WITH APPEND ]
// [ NOT FOR REPLICATION ]
// AS
// [ { IF UPDATE ( column )
// [ { AND | OR } UPDATE ( column ) ]
// [ ...n ]
// | IF ( COLUMNS_UPDATED ( ) { bitwise_operator } updated_bitmask )
// { comparison_operator } column_bitmask [ ...n ]
// } ]
// sql_statement [ ...n ]
sb.Append ("CREATE TRIGGER ");
sb.Append (trigger.Name);
sb.Append (" ON ");
sb.Append (trigger.TableName);
if (trigger.TriggerType == TriggerType.Before)
sb.Append (" FOR ");
else
sb.Append (" AFTER ");
switch (trigger.TriggerEvent) {
case TriggerEvent.Delete:
sb.Append ("DELETE");
break;
case TriggerEvent.Insert:
sb.Append ("INSERT");
break;
case TriggerEvent.Update:
sb.Append ("UPDATE");
break;
default:
throw new NotImplementedException ();
}
sb.Append (" AS ");
sb.Append (Environment.NewLine);
sb.Append (trigger.Source);
sb.Append (";");
return sb.ToString ();
}
//http://msdn2.microsoft.com/en-us/library/aa275464(SQL.80).aspx
public override void AlterDatabase (DatabaseSchema database)
{
throw new NotImplementedException ();
}
//http://msdn2.microsoft.com/en-us/library/aa225939(SQL.80).aspx
public override void AlterTable (TableSchema table)
{
throw new NotImplementedException ();
}
//http://msdn2.microsoft.com/en-us/library/aa225939(SQL.80).aspx
public override void AlterView (ViewSchema view)
{
ExecuteNonQuery (view.Definition);
}
//http://msdn2.microsoft.com/en-us/library/aa225939(SQL.80).aspx
public override void AlterProcedure (ProcedureSchema procedure)
{
ExecuteNonQuery (procedure.Definition);
}
public override void AlterIndex (IndexSchema index)
{
throw new NotImplementedException ();
}
//http://msdn2.microsoft.com/en-us/library/aa225939(SQL.80).aspx
public override void AlterTrigger (TriggerSchema trigger)
{
throw new NotImplementedException ();
}
public override void AlterUser (UserSchema user)
{
throw new NotImplementedException ();
}
//http://msdn2.microsoft.com/en-us/library/aa258843(SQL.80).aspx
public override void DropDatabase (DatabaseSchema database)
{
ExecuteNonQuery ("DROP DATABASE " + database.Name);
}
//http://msdn2.microsoft.com/en-us/library/aa258841(SQL.80).aspx
public override void DropTable (TableSchema table)
{
ExecuteNonQuery ("DROP TABLE " + table.Name);
}
//http://msdn2.microsoft.com/en-us/library/aa258835(SQL.80).aspx
public override void DropView (ViewSchema view)
{
ExecuteNonQuery ("DROP VIEW " + view.Name);
}
//http://msdn2.microsoft.com/en-us/library/aa258830(SQL.80).aspx
public override void DropProcedure (ProcedureSchema procedure)
{
ExecuteNonQuery ("DROP PROCEDURE " + procedure.Name);
}
//http://msdn2.microsoft.com/en-us/library/aa225939(SQL.80).aspx
public override void DropIndex (IndexSchema index)
{
ExecuteNonQuery ("DROP INDEX '" + index.TableName + "." + index.Name + "'");
}
//http://msdn2.microsoft.com/en-us/library/aa258846(SQL.80).aspx
public override void DropTrigger (TriggerSchema trigger)
{
ExecuteNonQuery ("DROP TRIGGER " + trigger.Name);
}
//http://msdn2.microsoft.com/en-US/library/aa238878(SQL.80).aspx
public override void RenameDatabase (DatabaseSchema database, string name)
{
Rename (database.Name, name, "DATABASE");
database.Name = name;
connectionPool.ConnectionContext.ConnectionSettings.Database = name;
}
//http://msdn2.microsoft.com/en-US/library/aa238878(SQL.80).aspx
public override void RenameTable (TableSchema table, string name)
{
Rename (table.Name, name, "OBJECT");
table.Name = name;
}
//http://msdn2.microsoft.com/en-US/library/aa238878(SQL.80).aspx
public override void RenameView (ViewSchema view, string name)
{
Rename (view.Name, name, "OBJECT");
view.Name = name;
}
//http://msdn2.microsoft.com/en-US/library/aa238878(SQL.80).aspx
public override void RenameProcedure (ProcedureSchema procedure, string name)
{
Rename (procedure.Name, name, "OBJECT");
procedure.Name = name;
}
//http://msdn2.microsoft.com/en-US/library/aa238878(SQL.80).aspx
public override void RenameIndex (IndexSchema index, string name)
{
Rename (index.Name, name, "INDEX");
index.Name = name;
}
//http://msdn2.microsoft.com/en-US/library/aa238878(SQL.80).aspx
public override void RenameTrigger (TriggerSchema trigger, string name)
{
Rename (trigger.Name, name, "OBJECT");
trigger.Name = name;
}
public override string GetViewAlterStatement (ViewSchema view)
{
return String.Concat ("DROP VIEW ", view.Name, "; ", Environment.NewLine, view.Definition);
}
public override string GetProcedureAlterStatement (ProcedureSchema procedure)
{
return String.Concat ("DROP PROCEDURE ", procedure.Name, "; ", Environment.NewLine, procedure.Definition);
}
protected string GetTableDefinition (TableSchema table)
{
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);
ColumnSchemaCollection 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));
ConstraintSchemaCollection 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);
return sb.ToString ();
}
private string GetSource (string objectName)
{
LoggingService.LogDebug ("GetSource: " + objectName);
IPooledDbConnection conn = connectionPool.Request ();
IDbCommand command = conn.CreateCommand (
String.Format ("EXEC sp_helptext '{0}', null", objectName)
);
StringBuilder sb = new StringBuilder ();
try {
using (command) {
using (IDataReader r = command.ExecuteReader()) {
while (r.Read ())
sb.Append (r.GetString (0));
r.Close ();
}
}
} catch (Exception e) {
QueryService.RaiseException (e);
}
conn.Release ();
return sb.ToString ();
}
//http://msdn2.microsoft.com/en-US/library/aa238878(SQL.80).aspx
private void Rename (string oldName, string newName, string type)
{
IPooledDbConnection conn = connectionPool.Request ();
IDbCommand command = conn.CreateStoredProcedure (
String.Format ("EXEC sp_rename '{0}', '{1}', '{2}'", oldName, newName, type)
);
try {
using (command)
command.ExecuteNonQuery ();
} catch (Exception e) {
QueryService.RaiseException (e);
}
conn.Release ();
}
}
}
|