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 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
|
// Copyright (c) 2004-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
//
// MySQL Connector/NET is licensed under the terms of the GPLv2
// <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
// MySQL Connectors. There are special exceptions to the terms and
// conditions of the GPLv2 as it is applied to this software, see the
// FLOSS License Exception
// <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
using System;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using MySql.Data.Common;
using MySql.Data.Types;
using System.Collections.Specialized;
using System.Collections;
using System.Text.RegularExpressions;
using MySql.Data.MySqlClient.Properties;
namespace MySql.Data.MySqlClient
{
internal class SchemaProvider
{
protected MySqlConnection connection;
public static string MetaCollection = "MetaDataCollections";
public SchemaProvider(MySqlConnection connectionToUse)
{
connection = connectionToUse;
}
public virtual DataTable GetSchema(string collection, String[] restrictions)
{
if (connection.State != ConnectionState.Open)
throw new MySqlException("GetSchema can only be called on an open connection.");
collection = collection.ToUpper(CultureInfo.InvariantCulture);
DataTable dt = GetSchemaInternal(collection, restrictions);
if (dt == null)
throw new MySqlException("Invalid collection name");
return dt;
}
public virtual DataTable GetDatabases(string[] restrictions)
{
Regex regex = null;
int caseSetting = Int32.Parse(connection.driver.Property("lower_case_table_names"));
string sql = "SHOW DATABASES";
// if lower_case_table_names is zero, then case lookup should be sensitive
// so we can use LIKE to do the matching.
if (caseSetting == 0)
{
if (restrictions != null && restrictions.Length >= 1)
sql = sql + " LIKE '" + restrictions[0] + "'";
}
else if (restrictions != null && restrictions.Length >= 1 && restrictions[0] != null)
regex = new Regex(restrictions[0], RegexOptions.IgnoreCase);
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
DataTable table = new DataTable("Databases");
table.Columns.Add("CATALOG_NAME", typeof (string));
table.Columns.Add("SCHEMA_NAME", typeof (string));
foreach (DataRow row in dt.Rows)
{
if (caseSetting != 0 && regex != null &&
!regex.Match(row[0].ToString()).Success) continue;
DataRow newRow = table.NewRow();
newRow[1] = row[0];
table.Rows.Add(newRow);
}
return table;
}
public virtual DataTable GetTables(string[] restrictions)
{
DataTable dt = new DataTable("Tables");
dt.Columns.Add("TABLE_CATALOG", typeof (string));
dt.Columns.Add("TABLE_SCHEMA", typeof (string));
dt.Columns.Add("TABLE_NAME", typeof (string));
dt.Columns.Add("TABLE_TYPE", typeof (string));
dt.Columns.Add("ENGINE", typeof (string));
dt.Columns.Add("VERSION", typeof (ulong));
dt.Columns.Add("ROW_FORMAT", typeof (string));
dt.Columns.Add("TABLE_ROWS", typeof (ulong));
dt.Columns.Add("AVG_ROW_LENGTH", typeof (ulong));
dt.Columns.Add("DATA_LENGTH", typeof (ulong));
dt.Columns.Add("MAX_DATA_LENGTH", typeof (ulong));
dt.Columns.Add("INDEX_LENGTH", typeof (ulong));
dt.Columns.Add("DATA_FREE", typeof (ulong));
dt.Columns.Add("AUTO_INCREMENT", typeof (ulong));
dt.Columns.Add("CREATE_TIME", typeof (DateTime));
dt.Columns.Add("UPDATE_TIME", typeof (DateTime));
dt.Columns.Add("CHECK_TIME", typeof (DateTime));
dt.Columns.Add("TABLE_COLLATION", typeof (string));
dt.Columns.Add("CHECKSUM", typeof (ulong));
dt.Columns.Add("CREATE_OPTIONS", typeof (string));
dt.Columns.Add("TABLE_COMMENT", typeof (string));
// we have to new up a new restriction array here since
// GetDatabases takes the database in the first slot
string[] dbRestriction = new string[4];
if (restrictions != null && restrictions.Length >= 2)
dbRestriction[0] = restrictions[1];
DataTable databases = GetDatabases(dbRestriction);
if (restrictions != null)
Array.Copy(restrictions, dbRestriction,
Math.Min(dbRestriction.Length, restrictions.Length));
foreach (DataRow db in databases.Rows)
{
dbRestriction[1] = db["SCHEMA_NAME"].ToString();
FindTables(dt, dbRestriction);
}
return dt;
}
protected void QuoteDefaultValues(DataTable dt)
{
if (dt == null) return;
if (!dt.Columns.Contains("COLUMN_DEFAULT")) return;
foreach (DataRow row in dt.Rows)
{
object defaultValue = row["COLUMN_DEFAULT"];
if (MetaData.IsTextType(row["DATA_TYPE"].ToString()))
row["COLUMN_DEFAULT"] = String.Format("'{0}'", defaultValue);
}
}
public virtual DataTable GetColumns(string[] restrictions)
{
DataTable dt = new DataTable("Columns");
dt.Columns.Add("TABLE_CATALOG", typeof (string));
dt.Columns.Add("TABLE_SCHEMA", typeof (string));
dt.Columns.Add("TABLE_NAME", typeof (string));
dt.Columns.Add("COLUMN_NAME", typeof (string));
dt.Columns.Add("ORDINAL_POSITION", typeof (ulong));
dt.Columns.Add("COLUMN_DEFAULT", typeof (string));
dt.Columns.Add("IS_NULLABLE", typeof (string));
dt.Columns.Add("DATA_TYPE", typeof (string));
dt.Columns.Add("CHARACTER_MAXIMUM_LENGTH", typeof (ulong));
dt.Columns.Add("CHARACTER_OCTET_LENGTH", typeof (ulong));
dt.Columns.Add("NUMERIC_PRECISION", typeof (ulong));
dt.Columns.Add("NUMERIC_SCALE", typeof (ulong));
dt.Columns.Add("CHARACTER_SET_NAME", typeof (string));
dt.Columns.Add("COLLATION_NAME", typeof (string));
dt.Columns.Add("COLUMN_TYPE", typeof (string));
dt.Columns.Add("COLUMN_KEY", typeof (string));
dt.Columns.Add("EXTRA", typeof (string));
dt.Columns.Add("PRIVILEGES", typeof (string));
dt.Columns.Add("COLUMN_COMMENT", typeof (string));
// we don't allow restricting on table type here
string columnName = null;
if (restrictions != null && restrictions.Length == 4)
{
columnName = restrictions[3];
restrictions[3] = null;
}
DataTable tables = GetTables(restrictions);
foreach (DataRow row in tables.Rows)
LoadTableColumns(dt, row["TABLE_SCHEMA"].ToString(),
row["TABLE_NAME"].ToString(), columnName);
QuoteDefaultValues(dt);
return dt;
}
private void LoadTableColumns(DataTable dt, string schema,
string tableName, string columnRestriction)
{
string sql = String.Format("SHOW FULL COLUMNS FROM `{0}`.`{1}`",
schema, tableName);
MySqlCommand cmd = new MySqlCommand(sql, connection);
int pos = 1;
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
string colName = reader.GetString(0);
if (columnRestriction != null && colName != columnRestriction)
continue;
DataRow row = dt.NewRow();
row["TABLE_CATALOG"] = DBNull.Value;
row["TABLE_SCHEMA"] = schema;
row["TABLE_NAME"] = tableName;
row["COLUMN_NAME"] = colName;
row["ORDINAL_POSITION"] = pos++;
row["COLUMN_DEFAULT"] = reader.GetValue(5);
row["IS_NULLABLE"] = reader.GetString(3);
row["DATA_TYPE"] = reader.GetString(1);
row["CHARACTER_MAXIMUM_LENGTH"] = DBNull.Value;
row["CHARACTER_OCTET_LENGTH"] = DBNull.Value;
row["NUMERIC_PRECISION"] = DBNull.Value;
row["NUMERIC_SCALE"] = DBNull.Value;
row["CHARACTER_SET_NAME"] = reader.GetValue(2);
row["COLLATION_NAME"] = row["CHARACTER_SET_NAME"];
row["COLUMN_TYPE"] = reader.GetString(1);
row["COLUMN_KEY"] = reader.GetString(4);
row["EXTRA"] = reader.GetString(6);
row["PRIVILEGES"] = reader.GetString(7);
row["COLUMN_COMMENT"] = reader.GetString(8);
ParseColumnRow(row);
dt.Rows.Add(row);
}
}
}
private static void ParseColumnRow(DataRow row)
{
// first parse the character set name
string charset = row["CHARACTER_SET_NAME"].ToString();
int index = charset.IndexOf('_');
if (index != -1)
row["CHARACTER_SET_NAME"] = charset.Substring(0, index);
// now parse the data type
string dataType = row["DATA_TYPE"].ToString();
index = dataType.IndexOf('(');
if (index == -1)
return;
row["DATA_TYPE"] = dataType.Substring(0, index);
int stop = dataType.IndexOf(')', index);
string dataLen = dataType.Substring(index + 1, stop - (index + 1));
string lowerType = row["DATA_TYPE"].ToString().ToLower();
if (lowerType == "char" || lowerType == "varchar")
row["CHARACTER_MAXIMUM_LENGTH"] = dataLen;
else if (lowerType == "real" || lowerType == "decimal")
{
string[] lenparts = dataLen.Split(new char[] {','});
row["NUMERIC_PRECISION"] = lenparts[0];
if (lenparts.Length == 2)
row["NUMERIC_SCALE"] = lenparts[1];
}
}
public virtual DataTable GetIndexes(string[] restrictions)
{
DataTable dt = new DataTable("Indexes");
dt.Columns.Add("INDEX_CATALOG", typeof (string));
dt.Columns.Add("INDEX_SCHEMA", typeof (string));
dt.Columns.Add("INDEX_NAME", typeof (string));
dt.Columns.Add("TABLE_NAME", typeof (string));
dt.Columns.Add("UNIQUE", typeof (bool));
dt.Columns.Add("PRIMARY", typeof (bool));
dt.Columns.Add("TYPE", typeof(string));
dt.Columns.Add("COMMENT", typeof(string));
// Get the list of tables first
int max = restrictions == null ? 4 : restrictions.Length;
string[] tableRestrictions = new string[Math.Max(max, 4)];
if (restrictions != null)
restrictions.CopyTo(tableRestrictions, 0);
tableRestrictions[3] = "BASE TABLE";
DataTable tables = GetTables(tableRestrictions);
foreach (DataRow table in tables.Rows)
{
string sql = String.Format("SHOW INDEX FROM `{0}`.`{1}`",
MySqlHelper.DoubleQuoteString((string)table["TABLE_SCHEMA"]),
MySqlHelper.DoubleQuoteString((string)table["TABLE_NAME"]));
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
DataTable indexes = new DataTable();
da.Fill(indexes);
foreach (DataRow index in indexes.Rows)
{
long seq_index = (long) index["SEQ_IN_INDEX"];
if (seq_index != 1) continue;
if (restrictions != null && restrictions.Length == 4 &&
restrictions[3] != null &&
!index["KEY_NAME"].Equals(restrictions[3])) continue;
DataRow row = dt.NewRow();
row["INDEX_CATALOG"] = null;
row["INDEX_SCHEMA"] = table["TABLE_SCHEMA"];
row["INDEX_NAME"] = index["KEY_NAME"];
row["TABLE_NAME"] = index["TABLE"];
row["UNIQUE"] = (long) index["NON_UNIQUE"] == 0;
row["PRIMARY"] = index["KEY_NAME"].Equals("PRIMARY");
row["TYPE"] = index["INDEX_TYPE"];
row["COMMENT"] = index["COMMENT"];
dt.Rows.Add(row);
}
}
return dt;
}
public virtual DataTable GetIndexColumns(string[] restrictions)
{
DataTable dt = new DataTable("IndexColumns");
dt.Columns.Add("INDEX_CATALOG", typeof (string));
dt.Columns.Add("INDEX_SCHEMA", typeof (string));
dt.Columns.Add("INDEX_NAME", typeof (string));
dt.Columns.Add("TABLE_NAME", typeof (string));
dt.Columns.Add("COLUMN_NAME", typeof (string));
dt.Columns.Add("ORDINAL_POSITION", typeof (int));
dt.Columns.Add("SORT_ORDER", typeof(string));
int max = restrictions == null ? 4 : restrictions.Length;
string[] tableRestrictions = new string[Math.Max(max, 4)];
if (restrictions != null)
restrictions.CopyTo(tableRestrictions, 0);
tableRestrictions[3] = "BASE TABLE";
DataTable tables = GetTables(tableRestrictions);
foreach (DataRow table in tables.Rows)
{
string sql = String.Format("SHOW INDEX FROM `{0}`.`{1}`",
table["TABLE_SCHEMA"], table["TABLE_NAME"]);
MySqlCommand cmd = new MySqlCommand(sql, connection);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
string key_name = GetString(reader, reader.GetOrdinal("KEY_NAME"));
string col_name = GetString(reader, reader.GetOrdinal("COLUMN_NAME"));
if (restrictions != null)
{
if (restrictions.Length >= 4 && restrictions[3] != null &&
key_name != restrictions[3]) continue;
if (restrictions.Length >= 5 && restrictions[4] != null &&
col_name != restrictions[4]) continue;
}
DataRow row = dt.NewRow();
row["INDEX_CATALOG"] = null;
row["INDEX_SCHEMA"] = table["TABLE_SCHEMA"];
row["INDEX_NAME"] = key_name;
row["TABLE_NAME"] = GetString(reader, reader.GetOrdinal("TABLE"));
row["COLUMN_NAME"] = col_name;
row["ORDINAL_POSITION"] = reader.GetValue(reader.GetOrdinal("SEQ_IN_INDEX"));
row["SORT_ORDER"] = reader.GetString("COLLATION");
dt.Rows.Add(row);
}
}
}
return dt;
}
public virtual DataTable GetForeignKeys(string[] restrictions)
{
DataTable dt = new DataTable("Foreign Keys");
dt.Columns.Add("CONSTRAINT_CATALOG", typeof (string));
dt.Columns.Add("CONSTRAINT_SCHEMA", typeof (string));
dt.Columns.Add("CONSTRAINT_NAME", typeof (string));
dt.Columns.Add("TABLE_CATALOG", typeof(string));
dt.Columns.Add("TABLE_SCHEMA", typeof (string));
dt.Columns.Add("TABLE_NAME", typeof (string));
dt.Columns.Add("MATCH_OPTION", typeof(string));
dt.Columns.Add("UPDATE_RULE", typeof(string));
dt.Columns.Add("DELETE_RULE", typeof(string));
dt.Columns.Add("REFERENCED_TABLE_CATALOG", typeof (string));
dt.Columns.Add("REFERENCED_TABLE_SCHEMA", typeof (string));
dt.Columns.Add("REFERENCED_TABLE_NAME", typeof (string));
// first we use our restrictions to get a list of tables that should be
// consulted. We save the keyname restriction since GetTables doesn't
// understand that.
string keyName = null;
if (restrictions != null && restrictions.Length >= 4)
{
keyName = restrictions[3];
restrictions[3] = null;
}
DataTable tables = GetTables(restrictions);
// now for each table retrieved, we call our helper function to
// parse it's foreign keys
foreach (DataRow table in tables.Rows)
GetForeignKeysOnTable(dt, table, keyName, false);
return dt;
}
public virtual DataTable GetForeignKeyColumns(string[] restrictions)
{
DataTable dt = new DataTable("Foreign Keys");
dt.Columns.Add("CONSTRAINT_CATALOG", typeof(string));
dt.Columns.Add("CONSTRAINT_SCHEMA", typeof(string));
dt.Columns.Add("CONSTRAINT_NAME", typeof(string));
dt.Columns.Add("TABLE_CATALOG", typeof(string));
dt.Columns.Add("TABLE_SCHEMA", typeof(string));
dt.Columns.Add("TABLE_NAME", typeof(string));
dt.Columns.Add("COLUMN_NAME", typeof(string));
dt.Columns.Add("ORDINAL_POSITION", typeof(int));
dt.Columns.Add("REFERENCED_TABLE_CATALOG", typeof(string));
dt.Columns.Add("REFERENCED_TABLE_SCHEMA", typeof(string));
dt.Columns.Add("REFERENCED_TABLE_NAME", typeof(string));
dt.Columns.Add("REFERENCED_COLUMN_NAME", typeof(string));
// first we use our restrictions to get a list of tables that should be
// consulted. We save the keyname restriction since GetTables doesn't
// understand that.
string keyName = null;
if (restrictions != null && restrictions.Length >= 4)
{
keyName = restrictions[3];
restrictions[3] = null;
}
DataTable tables = GetTables(restrictions);
// now for each table retrieved, we call our helper function to
// parse it's foreign keys
foreach (DataRow table in tables.Rows)
GetForeignKeysOnTable(dt, table, keyName, true);
return dt;
}
private string GetSqlMode()
{
MySqlCommand cmd = new MySqlCommand("SELECT @@SQL_MODE", connection);
return cmd.ExecuteScalar().ToString();
}
#region Foreign Key routines
/// <summary>
/// GetForeignKeysOnTable retrieves the foreign keys on the given table.
/// Since MySQL supports foreign keys on versions prior to 5.0, we can't use
/// information schema. MySQL also does not include any type of SHOW command
/// for foreign keys so we have to resort to use SHOW CREATE TABLE and parsing
/// the output.
/// </summary>
/// <param name="fkTable">The table to store the key info in.</param>
/// <param name="tableToParse">The table to get the foeign key info for.</param>
/// <param name="filterName">Only get foreign keys that match this name.</param>
/// <param name="includeColumns">Should column information be included in the table.</param>
private void GetForeignKeysOnTable(DataTable fkTable, DataRow tableToParse,
string filterName, bool includeColumns)
{
string sqlMode = GetSqlMode();
if (filterName != null)
filterName = filterName.ToLower(CultureInfo.InvariantCulture);
string sql = string.Format("SHOW CREATE TABLE `{0}`.`{1}`",
tableToParse["TABLE_SCHEMA"], tableToParse["TABLE_NAME"]);
string lowerBody = null, body = null;
MySqlCommand cmd = new MySqlCommand(sql, connection);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
body = reader.GetString(1);
lowerBody = body.ToLower(CultureInfo.InvariantCulture);
}
MySqlTokenizer tokenizer = new MySqlTokenizer(lowerBody);
tokenizer.AnsiQuotes = sqlMode.IndexOf("ANSI_QUOTES") != -1;
tokenizer.BackslashEscapes = sqlMode.IndexOf("NO_BACKSLASH_ESCAPES") != -1;
while (true)
{
string token = tokenizer.NextToken();
// look for a starting contraint
while (token != null && (token != "constraint" || tokenizer.Quoted))
token = tokenizer.NextToken();
if (token == null) break;
ParseConstraint(fkTable, tableToParse, tokenizer, includeColumns);
}
}
private static void ParseConstraint(DataTable fkTable, DataRow table,
MySqlTokenizer tokenizer, bool includeColumns)
{
string name = tokenizer.NextToken();
DataRow row = fkTable.NewRow();
// make sure this constraint is a FK
string token = tokenizer.NextToken();
if (token != "foreign" || tokenizer.Quoted)
return;
tokenizer.NextToken(); // read off the 'KEY' symbol
tokenizer.NextToken(); // read off the '(' symbol
row["CONSTRAINT_CATALOG"] = table["TABLE_CATALOG"];
row["CONSTRAINT_SCHEMA"] = table["TABLE_SCHEMA"];
row["TABLE_CATALOG"] = table["TABLE_CATALOG"];
row["TABLE_SCHEMA"] = table["TABLE_SCHEMA"];
row["TABLE_NAME"] = table["TABLE_NAME"];
row["REFERENCED_TABLE_CATALOG"] = null;
row["CONSTRAINT_NAME"] = name.Trim(new char[] { '\'', '`' });
ArrayList srcColumns = includeColumns ? ParseColumns(tokenizer) : null;
// now look for the references section
while (token != "references" || tokenizer.Quoted)
token = tokenizer.NextToken();
string target1 = tokenizer.NextToken();
string target2 = tokenizer.NextToken();
if (target2.StartsWith("."))
{
row["REFERENCED_TABLE_SCHEMA"] = target1;
row["REFERENCED_TABLE_NAME"] = target2.Substring(1).Trim(new char[] { '\'', '`' });
tokenizer.NextToken(); // read off the '('
}
else
{
row["REFERENCED_TABLE_SCHEMA"] = table["TABLE_SCHEMA"];
row["REFERENCED_TABLE_NAME"] = target1.Substring(1).Trim(new char[] { '\'', '`' }); ;
}
// if we are supposed to include columns, read the target columns
ArrayList targetColumns = includeColumns ? ParseColumns(tokenizer) : null;
if (includeColumns)
ProcessColumns(fkTable, row, srcColumns, targetColumns);
else
fkTable.Rows.Add(row);
}
private static ArrayList ParseColumns(MySqlTokenizer tokenizer)
{
ArrayList sc = new ArrayList();
string token = tokenizer.NextToken();
while (token != ")")
{
if (token != ",")
sc.Add(token);
token = tokenizer.NextToken();
}
return sc;
}
private static void ProcessColumns(DataTable fkTable, DataRow row,
ArrayList srcColumns, ArrayList targetColumns)
{
for (int i = 0; i < srcColumns.Count; i++)
{
DataRow newRow = fkTable.NewRow();
newRow.ItemArray = row.ItemArray;
newRow["COLUMN_NAME"] = (string)srcColumns[i];
newRow["ORDINAL_POSITION"] = i;
newRow["REFERENCED_COLUMN_NAME"] = (string)targetColumns[i];
fkTable.Rows.Add(newRow);
}
}
#endregion
public virtual DataTable GetUsers(string[] restrictions)
{
StringBuilder sb = new StringBuilder("SELECT Host, User FROM mysql.user");
if (restrictions != null && restrictions.Length > 0)
sb.AppendFormat(CultureInfo.InvariantCulture, " WHERE User LIKE '{0}'", restrictions[0]);
MySqlDataAdapter da = new MySqlDataAdapter(sb.ToString(), connection);
DataTable dt = new DataTable();
da.Fill(dt);
dt.TableName = "Users";
dt.Columns[0].ColumnName = "HOST";
dt.Columns[1].ColumnName = "USERNAME";
return dt;
}
public virtual DataTable GetProcedures(string[] restrictions)
{
DataTable dt = new DataTable("Procedures");
dt.Columns.Add(new DataColumn("SPECIFIC_NAME", typeof(string)));
dt.Columns.Add(new DataColumn("ROUTINE_CATALOG", typeof(string)));
dt.Columns.Add(new DataColumn("ROUTINE_SCHEMA", typeof(string)));
dt.Columns.Add(new DataColumn("ROUTINE_NAME", typeof(string)));
dt.Columns.Add(new DataColumn("ROUTINE_TYPE", typeof(string)));
dt.Columns.Add(new DataColumn("DTD_IDENTIFIER", typeof(string)));
dt.Columns.Add(new DataColumn("ROUTINE_BODY", typeof(string)));
dt.Columns.Add(new DataColumn("ROUTINE_DEFINITION", typeof(string)));
dt.Columns.Add(new DataColumn("EXTERNAL_NAME", typeof(string)));
dt.Columns.Add(new DataColumn("EXTERNAL_LANGUAGE", typeof(string)));
dt.Columns.Add(new DataColumn("PARAMETER_STYLE", typeof(string)));
dt.Columns.Add(new DataColumn("IS_DETERMINISTIC", typeof(string)));
dt.Columns.Add(new DataColumn("SQL_DATA_ACCESS", typeof(string)));
dt.Columns.Add(new DataColumn("SQL_PATH", typeof(string)));
dt.Columns.Add(new DataColumn("SECURITY_TYPE", typeof(string)));
dt.Columns.Add(new DataColumn("CREATED", typeof(DateTime)));
dt.Columns.Add(new DataColumn("LAST_ALTERED", typeof(DateTime)));
dt.Columns.Add(new DataColumn("SQL_MODE", typeof(string)));
dt.Columns.Add(new DataColumn("ROUTINE_COMMENT", typeof(string)));
dt.Columns.Add(new DataColumn("DEFINER", typeof(string)));
StringBuilder sql = new StringBuilder("SELECT * FROM mysql.proc WHERE 1=1");
if (restrictions != null)
{
if (restrictions.Length >= 2 && restrictions[1] != null)
sql.AppendFormat(CultureInfo.InvariantCulture,
" AND db LIKE '{0}'", restrictions[1]);
if (restrictions.Length >= 3 && restrictions[2] != null)
sql.AppendFormat(CultureInfo.InvariantCulture,
" AND name LIKE '{0}'", restrictions[2]);
if (restrictions.Length >= 4 && restrictions[3] != null)
sql.AppendFormat(CultureInfo.InvariantCulture,
" AND type LIKE '{0}'", restrictions[3]);
}
MySqlCommand cmd = new MySqlCommand(sql.ToString(), connection);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
DataRow row = dt.NewRow();
row["SPECIFIC_NAME"] = reader.GetString("specific_name");
row["ROUTINE_CATALOG"] = DBNull.Value;
row["ROUTINE_SCHEMA"] = reader.GetString("db");
row["ROUTINE_NAME"] = reader.GetString("name");
string routineType = reader.GetString("type");
row["ROUTINE_TYPE"] = routineType;
row["DTD_IDENTIFIER"] = routineType.ToLower(CultureInfo.InvariantCulture) == "function" ?
(object)reader.GetString("returns") : DBNull.Value;
row["ROUTINE_BODY"] = "SQL";
row["ROUTINE_DEFINITION"] = reader.GetString("body");
row["EXTERNAL_NAME"] = DBNull.Value;
row["EXTERNAL_LANGUAGE"] = DBNull.Value;
row["PARAMETER_STYLE"] = "SQL";
row["IS_DETERMINISTIC"] = reader.GetString("is_deterministic");
row["SQL_DATA_ACCESS"] = reader.GetString("sql_data_access");
row["SQL_PATH"] = DBNull.Value;
row["SECURITY_TYPE"] = reader.GetString("security_type");
row["CREATED"] = reader.GetDateTime("created");
row["LAST_ALTERED"] = reader.GetDateTime("modified");
row["SQL_MODE"] = reader.GetString("sql_mode");
row["ROUTINE_COMMENT"] = reader.GetString("comment");
row["DEFINER"] = reader.GetString("definer");
dt.Rows.Add(row);
}
}
return dt;
}
protected virtual DataTable GetCollections()
{
object[][] collections = new object[][]
{
new object[] {"MetaDataCollections", 0, 0},
new object[] {"DataSourceInformation", 0, 0},
new object[] {"DataTypes", 0, 0},
new object[] {"Restrictions", 0, 0},
new object[] {"ReservedWords", 0, 0},
new object[] {"Databases", 1, 1},
new object[] {"Tables", 4, 2},
new object[] {"Columns", 4, 4},
new object[] {"Users", 1, 1},
new object[] {"Foreign Keys", 4, 3},
new object[] {"IndexColumns", 5, 4},
new object[] {"Indexes", 4, 3},
new object[] {"Foreign Key Columns", 4, 3},
new object[] {"UDF", 1, 1}
};
DataTable dt = new DataTable("MetaDataCollections");
dt.Columns.Add(new DataColumn("CollectionName", typeof (string)));
dt.Columns.Add(new DataColumn("NumberOfRestrictions", typeof(int)));
dt.Columns.Add(new DataColumn("NumberOfIdentifierParts", typeof (int)));
FillTable(dt, collections);
return dt;
}
private DataTable GetDataSourceInformation()
{
#if CF
throw new NotSupportedException();
#else
DataTable dt = new DataTable("DataSourceInformation");
dt.Columns.Add("CompositeIdentifierSeparatorPattern", typeof (string));
dt.Columns.Add("DataSourceProductName", typeof (string));
dt.Columns.Add("DataSourceProductVersion", typeof (string));
dt.Columns.Add("DataSourceProductVersionNormalized", typeof (string));
dt.Columns.Add("GroupByBehavior", typeof (GroupByBehavior));
dt.Columns.Add("IdentifierPattern", typeof (string));
dt.Columns.Add("IdentifierCase", typeof (IdentifierCase));
dt.Columns.Add("OrderByColumnsInSelect", typeof (bool));
dt.Columns.Add("ParameterMarkerFormat", typeof (string));
dt.Columns.Add("ParameterMarkerPattern", typeof (string));
dt.Columns.Add("ParameterNameMaxLength", typeof (int));
dt.Columns.Add("ParameterNamePattern", typeof (string));
dt.Columns.Add("QuotedIdentifierPattern", typeof (string));
dt.Columns.Add("QuotedIdentifierCase", typeof (IdentifierCase));
dt.Columns.Add("StatementSeparatorPattern", typeof (string));
dt.Columns.Add("StringLiteralPattern", typeof (string));
dt.Columns.Add("SupportedJoinOperators", typeof (SupportedJoinOperators));
DBVersion v = connection.driver.Version;
string ver = String.Format("{0:0}.{1:0}.{2:0}",
v.Major, v.Minor, v.Build);
DataRow row = dt.NewRow();
row["CompositeIdentifierSeparatorPattern"] = "\\.";
row["DataSourceProductName"] = "MySQL";
row["DataSourceProductVersion"] = connection.ServerVersion;
row["DataSourceProductVersionNormalized"] = ver;
row["GroupByBehavior"] = GroupByBehavior.Unrelated;
row["IdentifierPattern"] =
@"(^\`\p{Lo}\p{Lu}\p{Ll}_@#][\p{Lo}\p{Lu}\p{Ll}\p{Nd}@$#_]*$)|(^\`[^\`\0]|\`\`+\`$)|(^\"" + [^\""\0]|\""\""+\""$)";
row["IdentifierCase"] = IdentifierCase.Insensitive;
row["OrderByColumnsInSelect"] = false;
row["ParameterMarkerFormat"] = "{0}";
row["ParameterMarkerPattern"] = "(@[A-Za-z0-9_$#]*)";
row["ParameterNameMaxLength"] = 128;
row["ParameterNamePattern"] =
@"^[\p{Lo}\p{Lu}\p{Ll}\p{Lm}_@#][\p{Lo}\p{Lu}\p{Ll}\p{Lm}\p{Nd}\uff3f_@#\$]*(?=\s+|$)";
row["QuotedIdentifierPattern"] = @"(([^\`]|\`\`)*)";
row["QuotedIdentifierCase"] = IdentifierCase.Sensitive;
row["StatementSeparatorPattern"] = ";";
row["StringLiteralPattern"] = "'(([^']|'')*)'";
row["SupportedJoinOperators"] = 15;
dt.Rows.Add(row);
return dt;
#endif
}
private static DataTable GetDataTypes()
{
DataTable dt = new DataTable("DataTypes");
dt.Columns.Add(new DataColumn("TypeName", typeof (string)));
dt.Columns.Add(new DataColumn("ProviderDbType", typeof (int)));
dt.Columns.Add(new DataColumn("ColumnSize", typeof (long)));
dt.Columns.Add(new DataColumn("CreateFormat", typeof (string)));
dt.Columns.Add(new DataColumn("CreateParameters", typeof (string)));
dt.Columns.Add(new DataColumn("DataType", typeof (string)));
dt.Columns.Add(new DataColumn("IsAutoincrementable", typeof (bool)));
dt.Columns.Add(new DataColumn("IsBestMatch", typeof (bool)));
dt.Columns.Add(new DataColumn("IsCaseSensitive", typeof (bool)));
dt.Columns.Add(new DataColumn("IsFixedLength", typeof (bool)));
dt.Columns.Add(new DataColumn("IsFixedPrecisionScale", typeof (bool)));
dt.Columns.Add(new DataColumn("IsLong", typeof (bool)));
dt.Columns.Add(new DataColumn("IsNullable", typeof (bool)));
dt.Columns.Add(new DataColumn("IsSearchable", typeof (bool)));
dt.Columns.Add(new DataColumn("IsSearchableWithLike", typeof (bool)));
dt.Columns.Add(new DataColumn("IsUnsigned", typeof (bool)));
dt.Columns.Add(new DataColumn("MaximumScale", typeof (short)));
dt.Columns.Add(new DataColumn("MinimumScale", typeof (short)));
dt.Columns.Add(new DataColumn("IsConcurrencyType", typeof (bool)));
dt.Columns.Add(new DataColumn("IsLiteralSupported", typeof (bool)));
dt.Columns.Add(new DataColumn("LiteralPrefix", typeof (string)));
dt.Columns.Add(new DataColumn("LiteralSuffix", typeof (string)));
dt.Columns.Add(new DataColumn("NativeDataType", typeof (string)));
// have each one of the types contribute to the datatypes collection
MySqlBit.SetDSInfo(dt);
MySqlBinary.SetDSInfo(dt);
MySqlDateTime.SetDSInfo(dt);
MySqlTimeSpan.SetDSInfo(dt);
MySqlString.SetDSInfo(dt);
MySqlDouble.SetDSInfo(dt);
MySqlSingle.SetDSInfo(dt);
MySqlByte.SetDSInfo(dt);
MySqlInt16.SetDSInfo(dt);
MySqlInt32.SetDSInfo(dt);
MySqlInt64.SetDSInfo(dt);
MySqlDecimal.SetDSInfo(dt);
MySqlUByte.SetDSInfo(dt);
MySqlUInt16.SetDSInfo(dt);
MySqlUInt32.SetDSInfo(dt);
MySqlUInt64.SetDSInfo(dt);
return dt;
}
protected virtual DataTable GetRestrictions()
{
object[][] restrictions = new object[][]
{
new object[] {"Users", "Name", "", 0},
new object[] {"Databases", "Name", "", 0},
new object[] {"Tables", "Database", "", 0},
new object[] {"Tables", "Schema", "", 1},
new object[] {"Tables", "Table", "", 2},
new object[] {"Tables", "TableType", "", 3},
new object[] {"Columns", "Database", "", 0},
new object[] {"Columns", "Schema", "", 1},
new object[] {"Columns", "Table", "", 2},
new object[] {"Columns", "Column", "", 3},
new object[] {"Indexes", "Database", "", 0},
new object[] {"Indexes", "Schema", "", 1},
new object[] {"Indexes", "Table", "", 2},
new object[] {"Indexes", "Name", "", 3},
new object[] {"IndexColumns", "Database", "", 0},
new object[] {"IndexColumns", "Schema", "", 1},
new object[] {"IndexColumns", "Table", "", 2},
new object[] {"IndexColumns", "ConstraintName", "", 3},
new object[] {"IndexColumns", "Column", "", 4},
new object[] {"Foreign Keys", "Database", "", 0},
new object[] {"Foreign Keys", "Schema", "", 1},
new object[] {"Foreign Keys", "Table", "", 2},
new object[] {"Foreign Keys", "Constraint Name", "", 3},
new object[] {"Foreign Key Columns", "Catalog", "", 0},
new object[] {"Foreign Key Columns", "Schema", "", 1},
new object[] {"Foreign Key Columns", "Table", "", 2},
new object[] {"Foreign Key Columns", "Constraint Name", "", 3},
new object[] {"UDF", "Name", "", 0}
};
DataTable dt = new DataTable("Restrictions");
dt.Columns.Add(new DataColumn("CollectionName", typeof (string)));
dt.Columns.Add(new DataColumn("RestrictionName", typeof (string)));
dt.Columns.Add(new DataColumn("RestrictionDefault", typeof (string)));
dt.Columns.Add(new DataColumn("RestrictionNumber", typeof (int)));
FillTable(dt, restrictions);
return dt;
}
private static DataTable GetReservedWords()
{
DataTable dt = new DataTable("ReservedWords");
dt.Columns.Add(new DataColumn(DbMetaDataColumnNames.ReservedWord, typeof(string)));
Stream str = Assembly.GetExecutingAssembly().GetManifestResourceStream(
"MySql.Data.MySqlClient.Properties.ReservedWords.txt");
StreamReader sr = new StreamReader(str);
string line = sr.ReadLine();
while (line != null)
{
string[] keywords = line.Split(new char[] {' '});
foreach (string s in keywords)
{
if (String.IsNullOrEmpty(s)) continue;
DataRow row = dt.NewRow();
row[0] = s;
dt.Rows.Add(row);
}
line = sr.ReadLine();
}
sr.Close();
str.Close();
return dt;
}
protected static void FillTable(DataTable dt, object[][] data)
{
foreach (object[] dataItem in data)
{
DataRow row = dt.NewRow();
for (int i = 0; i < dataItem.Length; i++)
row[i] = dataItem[i];
dt.Rows.Add(row);
}
}
private void FindTables(DataTable schemaTable, string[] restrictions)
{
StringBuilder sql = new StringBuilder();
StringBuilder where = new StringBuilder();
sql.AppendFormat(CultureInfo.InvariantCulture,
"SHOW TABLE STATUS FROM `{0}`", restrictions[1]);
if (restrictions != null && restrictions.Length >= 3 &&
restrictions[2] != null)
where.AppendFormat(CultureInfo.InvariantCulture,
" LIKE '{0}'", restrictions[2]);
sql.Append(where.ToString());
string table_type = restrictions[1].ToLower() == "information_schema"
?
"SYSTEM VIEW"
: "BASE TABLE";
MySqlCommand cmd = new MySqlCommand(sql.ToString(), connection);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
DataRow row = schemaTable.NewRow();
row["TABLE_CATALOG"] = null;
row["TABLE_SCHEMA"] = restrictions[1];
row["TABLE_NAME"] = reader.GetString(0);
row["TABLE_TYPE"] = table_type;
row["ENGINE"] = GetString(reader, 1);
row["VERSION"] = reader.GetValue(2);
row["ROW_FORMAT"] = GetString(reader, 3);
row["TABLE_ROWS"] = reader.GetValue(4);
row["AVG_ROW_LENGTH"] = reader.GetValue(5);
row["DATA_LENGTH"] = reader.GetValue(6);
row["MAX_DATA_LENGTH"] = reader.GetValue(7);
row["INDEX_LENGTH"] = reader.GetValue(8);
row["DATA_FREE"] = reader.GetValue(9);
row["AUTO_INCREMENT"] = reader.GetValue(10);
row["CREATE_TIME"] = reader.GetValue(11);
row["UPDATE_TIME"] = reader.GetValue(12);
row["CHECK_TIME"] = reader.GetValue(13);
row["TABLE_COLLATION"] = GetString(reader, 14);
row["CHECKSUM"] = reader.GetValue(15);
row["CREATE_OPTIONS"] = GetString(reader, 16);
row["TABLE_COMMENT"] = GetString(reader, 17);
schemaTable.Rows.Add(row);
}
}
}
private static string GetString(MySqlDataReader reader, int index)
{
if (reader.IsDBNull(index))
return null;
return reader.GetString(index);
}
public virtual DataTable GetUDF(string[] restrictions)
{
string sql = "SELECT name,ret,dl FROM mysql.func";
if (restrictions != null)
{
if (restrictions.Length >= 1 && !String.IsNullOrEmpty(restrictions[0]))
sql += String.Format(" WHERE name LIKE '{0}'", restrictions[0]);
}
DataTable dt = new DataTable("User-defined Functions");
dt.Columns.Add("NAME", typeof(string));
dt.Columns.Add("RETURN_TYPE", typeof(int));
dt.Columns.Add("LIBRARY_NAME", typeof(string));
MySqlCommand cmd = new MySqlCommand(sql, connection);
try
{
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
DataRow row = dt.NewRow();
row[0] = reader.GetString(0);
row[1] = reader.GetInt32(1);
row[2] = reader.GetString(2);
dt.Rows.Add(row);
}
}
}
catch (MySqlException ex)
{
if (ex.Number != (int)MySqlErrorCode.TableAccessDenied)
throw;
throw new MySqlException(Resources.UnableToEnumerateUDF, ex);
}
return dt;
}
protected virtual DataTable GetSchemaInternal(string collection, string[] restrictions)
{
switch (collection)
{
// common collections
case "METADATACOLLECTIONS":
return GetCollections();
case "DATASOURCEINFORMATION":
return GetDataSourceInformation();
case "DATATYPES":
return GetDataTypes();
case "RESTRICTIONS":
return GetRestrictions();
case "RESERVEDWORDS":
return GetReservedWords();
// collections specific to our provider
case "USERS":
return GetUsers(restrictions);
case "DATABASES":
return GetDatabases(restrictions);
case "UDF":
return GetUDF(restrictions);
}
// if we have a current database and our users have
// not specified a database, then default to the currently
// selected one.
if (restrictions == null)
restrictions = new string[2];
if (connection != null &&
connection.Database != null &&
connection.Database.Length > 0 &&
restrictions.Length > 1 &&
restrictions[1] == null)
restrictions[1] = connection.Database;
switch (collection)
{
case "TABLES":
return GetTables(restrictions);
case "COLUMNS":
return GetColumns(restrictions);
case "INDEXES":
return GetIndexes(restrictions);
case "INDEXCOLUMNS":
return GetIndexColumns(restrictions);
case "FOREIGN KEYS":
return GetForeignKeys(restrictions);
case "FOREIGN KEY COLUMNS":
return GetForeignKeyColumns(restrictions);
}
return null;
}
internal string[] CleanRestrictions(string[] restrictionValues)
{
string[] restrictions = null;
if (restrictionValues != null)
{
restrictions = (string[])restrictionValues.Clone();
for (int x = 0; x < restrictions.Length; x++)
{
string s = restrictions[x];
if (s == null) continue;
restrictions[x] = s.Trim('`');
}
}
return restrictions;
}
}
}
|