Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MySql.Data.MySqlClient;
  7.  
  8. namespace eddb
  9. {
  10.     public abstract class SQLTable
  11.     {
  12.         abstract public string table_name
  13.         {
  14.             get;
  15.         }
  16.  
  17.         public bool debug_console = false;
  18.        
  19.         public const int INVALID_ID = SQLTypes.SQLId.INVALID;
  20.         public const string ID_STRING = "Id";
  21.  
  22.         protected SQLTypes.SQLId id = INVALID_ID;
  23.  
  24.         /*Create the table, returns true on success*/
  25.         public virtual bool CreateTable(MySqlConnection conn)
  26.         {
  27.             bool retval = false;
  28.             MySqlCommand cmd = new MySqlCommand();
  29.             Entry table_entry = GetNewEntry();
  30.  
  31.             if (conn.State != System.Data.ConnectionState.Open)
  32.                 return false;
  33.  
  34.             cmd.Connection = conn;
  35.  
  36.             cmd.CommandText = "CREATE TABLE IF NOT EXISTS "+ table_name + " (";
  37.             cmd.CommandText += id.GetCreationString(ID_STRING) + " PRIMARY KEY";
  38.             foreach (KeyValuePair<string, SQLTypes.SQLObject> obj in table_entry.Columns)
  39.             {
  40.                 cmd.CommandText += ", " + obj.Value.GetCreationString(obj.Key);
  41.             }
  42.             cmd.CommandText += ") ENGINE=INNODB;";
  43.            
  44.             try
  45.             {
  46.                 cmd.Prepare();              
  47.  
  48.                 cmd.ExecuteNonQuery();
  49.                 retval = true;
  50.             }
  51.             catch
  52.             {
  53.                 retval = false;
  54.             }
  55.              
  56.  
  57.             Console.Write(cmd.CommandText + "\n");
  58.            
  59.             return retval;
  60.         }
  61.  
  62.         public abstract Entry GetNewEntry();
  63.  
  64.         public abstract class Entry
  65.         {
  66.             public Dictionary<string, SQLTypes.SQLObject> Columns;
  67.  
  68.  
  69.         }
  70.  
  71.     }
  72.  
  73.    
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement