File: ConnectionInternal.cs

package info (click to toggle)
mono 2.6.7-5.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 327,344 kB
  • ctags: 413,649
  • sloc: cs: 2,471,883; xml: 1,768,594; ansic: 350,665; sh: 13,644; makefile: 8,640; perl: 1,784; asm: 717; cpp: 209; python: 146; sql: 81; sed: 16
file content (117 lines) | stat: -rw-r--r-- 2,445 bytes parent folder | download | duplicates (5)
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
using System;
using ByteFX.Data.Common;

namespace ByteFX.Data.MySqlClient
{
	/// <summary>
	/// 
	/// </summary>
	internal sealed class MySqlInternalConnection
	{
		MySqlConnectionString	settings;
		Driver					driver;
		DateTime				createTime;
		bool					serverVariablesSet;

		public MySqlInternalConnection( MySqlConnectionString connectString )
		{
			settings = connectString;
			serverVariablesSet = false;
		}

		#region Properties
		public MySqlConnectionString Settings 
		{
			get { return settings; }
			set { settings = value; }
		}

		internal Driver Driver 
		{
			get { return driver; }
		}

		#endregion

		#region Methods

		public bool IsAlive() 
		{
			try 
			{
				CommandResult result = driver.Send( DBCmd.PING, (byte[])null );
				// we don't care about the result.  The fact that it responded is enough
				return true;
			}
			catch (Exception)
			{
				return false;
			}
		}

		public bool IsTooOld() 
		{
			TimeSpan ts = DateTime.Now.Subtract( createTime );
			if (ts.Seconds > settings.ConnectionLifetime)
				return true;
			return false;
		}

		/// <summary>
		/// I don't like this setup but can't think of a better way of doing
		/// right now.
		/// </summary>
		/// <param name="connection"></param>
		public void SetServerVariables(MySqlConnection connection)
		{
			if (serverVariablesSet) return;

			// retrieve the encoding that should be used for character data
			MySqlCommand cmd = new MySqlCommand("show variables like 'max_allowed_packet'", connection);
			try 
			{
				MySqlDataReader reader = cmd.ExecuteReader();
				reader.Read();
				driver.MaxPacketSize = reader.GetInt64( 1 );
				reader.Close();
			}
			catch (Exception)
			{
				driver.MaxPacketSize = 1047552;
			}

			cmd.CommandText = "show variables like 'character_set'";
			driver.Encoding = System.Text.Encoding.Default;
		
			try 
			{
				MySqlDataReader reader = cmd.ExecuteReader();
				if (reader.Read())
					driver.Encoding = CharSetMap.GetEncoding( reader.GetString(1) );
				reader.Close();
			}
			catch 
			{ 
				throw new MySqlException("Failure to initialize connection");
			}

			serverVariablesSet = true;
		}

		public void Open() 
		{
			driver = new Driver();
			driver.Open( settings );

			createTime = DateTime.Now;
		}

		public void Close() 
		{
			driver.Close();
		}

		#endregion

	}
}