Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
//
// AssemblyInfo.cs
//
// Author:
// Andreas Nahr (ClassDevelopment@A-SoftTech.com)
//
// (C) 2003 Ximian, Inc. http://www.ximian.com
// (C) 2004 Novell (http://www.novell.com)
//
using System;
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyVersion (Consts.FxVersion)]
[assembly: AssemblyDelaySign (true)]
[assembly: AssemblyKeyFile ("../mono.pub")]

View File

@@ -0,0 +1,10 @@
thisdir = class/WebMatrix.Data
SUBDIRS =
include ../../build/rules.make
LIBRARY = WebMatrix.Data.dll
include ../../build/library.make
LIB_MCS_FLAGS = -r:$(corlib) -r:System.dll -r:System.Data.dll -r:System.Core.dll -r:System.Configuration.dll
TEST_MCS_FLAGS = -r:System.dll -r:System.Core.dll -r:System.Data.dll -r:Mono.Data.Sqlite.dll -r:Microsoft.CSharp.dll

View File

@@ -0,0 +1,54 @@
//
// ConnectionEventArgsTests.cs
//
// Author:
// Jérémie "garuma" Laval <jeremie.laval@gmail.com>
//
// Copyright (c) 2011 Novell
//
// 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 THE
// 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.
#if NET_4_0
using System;
using System.Data.Common;
using System.Data.SqlClient;
using WebMatrix.Data;
using NUnit.Framework;
namespace MonoTests.WebMatrix.Data
{
[TestFixtureAttribute]
public class ConnectionEventArgsTests
{
[Test]
public void InstantiateTest ()
{
var evtArgs = new ConnectionEventArgs (null);
Assert.IsNull (evtArgs.Connection, "#1");
var conn = new SqlConnection ();
evtArgs = new ConnectionEventArgs (conn);
Assert.AreEqual (conn, evtArgs.Connection, "#2");
}
}
}
#endif

View File

@@ -0,0 +1,117 @@
//
// DatabaseTests.cs
//
// Author:
// Jérémie "garuma" Laval <jeremie.laval@gmail.com>
//
// Copyright (c) 2011 Novell
//
// 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 THE
// 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.
#if NET_4_0
using System;
using System.IO;
using System.Linq;
using System.Data.Common;
using System.Collections.Generic;
using WebMatrix.Data;
using NUnit.Framework;
namespace MonoTests.WebMatrix.Data
{
[TestFixtureAttribute]
public class DatabaseTests
{
Database database;
string dbPath;
[SetUp]
public void Setup ()
{
dbPath = Path.Combine ("Test", "testsqlite.db");
database = Database.OpenConnectionString ("Data Source="+dbPath+";Version=3;", "Mono.Data.Sqlite");
}
[Test]
public void QuerySingleTest ()
{
var result = database.QuerySingle ("select * from memos where Text=@0 limit 1", "Grendel");
Assert.IsNotNull (result);
Assert.AreEqual ("Grendel", result.Text);
Assert.AreEqual (5, result.Priority);
}
[Test]
public void SimpleQueryTest ()
{
var result = database.Query ("select * from memos");
Assert.IsNotNull (result);
Assert.AreEqual (5, result.Count ());
var col1 = new string[] { "Webmatrix", "Grendel", "Garuma", "jpobst", "Gonzalo" };
var col2 = new object[] { 10, 5, -1, 6, 4 };
int index = 0;
foreach (var row in result) {
Assert.AreEqual (col1[index], row.Text);
Assert.AreEqual (col2[index], row.Priority);
index++;
}
}
[Test]
public void InsertTest ()
{
string newPath = dbPath + ".tmp";
File.Copy (dbPath, newPath, true);
database = Database.OpenConnectionString ("Data Source="+newPath+";Version=3;", "Mono.Data.Sqlite");
database.Execute ("insert into memos values ('foo', @0);", 42);
Assert.AreEqual (42, database.QueryValue ("select Priority from memos where Text='foo'"));
Assert.AreEqual (6, database.GetLastInsertId ());
File.Delete (newPath);
}
[Test]
public void QueryValueTest ()
{
var res = database.QueryValue ("select Priority from memos where Text='Webmatrix'");
Assert.AreEqual (10, res);
}
[Test]
public void ConnectionOpenedTest ()
{
bool opened = false;
Database.ConnectionOpened += (sender, e) => opened = sender == database;
var result = database.QuerySingle ("select * from memos where Text=@0 limit 1", "Grendel");
Assert.IsTrue (opened);
}
}
}
#endif

View File

@@ -0,0 +1,99 @@
//
// DynamicRecordTests.cs
//
// Author:
// Jérémie "garuma" Laval <jeremie.laval@gmail.com>
//
// Copyright (c) 2011 Novell
//
// 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 THE
// 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.
#if NET_4_0
using System;
using System.Collections.Generic;
using WebMatrix.Data;
using NUnit.Framework;
namespace MonoTests.WebMatrix.Data
{
[TestFixtureAttribute]
public class DynamicRecordTests
{
DynamicRecord record;
[SetUp]
public void Setup ()
{
var fields = new Dictionary<string, object> () {
{ "foo", 1 },
{ "bar", 4.1f },
{ "foobar", "foobar" }
};
record = new DynamicRecord (fields);
}
[Test]
public void ColumnsTest ()
{
var columns = record.Columns;
Assert.AreEqual (3, columns.Count);
Assert.AreEqual ("foo", columns[0]);
Assert.AreEqual ("bar", columns[1]);
Assert.AreEqual ("foobar", columns[2]);
}
[Test]
public void AccessByNameTest ()
{
Assert.AreEqual (1, record["foo"]);
Assert.AreEqual (4.1f, record["bar"]);
Assert.AreEqual ("foobar", record["foobar"]);
}
[Test]
public void AccessByIndexTest ()
{
Assert.AreEqual (1, record[0]);
Assert.AreEqual (4.1f, record[1]);
Assert.AreEqual ("foobar", record[2]);
}
[Test]
public void AccesByDynamicTest ()
{
dynamic r = record;
Assert.AreEqual (1, r.foo);
Assert.AreEqual (4.1f, r.bar);
Assert.AreEqual ("foobar", r.foobar);
}
[Test]
public void GetDynamicMemberNamesTest ()
{
var expected = new string[] { "foo", "bar", "foobar" };
CollectionAssert.AreEquivalent (expected, record.GetDynamicMemberNames ());
}
}
}
#endif

View File

@@ -0,0 +1,6 @@
../../build/common/Consts.cs
../../build/common/Locale.cs
Assembly/AssemblyInfo.cs
WebMatrix.Data/ConnectionEventArgs.cs
WebMatrix.Data/DynamicRecord.cs
WebMatrix.Data/Database.cs

View File

@@ -0,0 +1,49 @@
//
// ConnectionEventArgs.cs
//
// Copyright (c) 2011 Novell
//
// Authors:
// Jérémie "garuma" Laval
//
// 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 THE
// 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.
//
#if NET_4_0
using System;
using System.Data.Common;
namespace WebMatrix.Data
{
public class ConnectionEventArgs : EventArgs
{
public ConnectionEventArgs (DbConnection connection)
{
Connection = connection;
}
public DbConnection Connection {
get;
private set;
}
}
}
#endif

View File

@@ -0,0 +1,242 @@
//
// Database.cs
//
// Copyright (c) 2011 Novell
//
// Authors:
// Jérémie "garuma" Laval
//
// 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 THE
// 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.
//
#if NET_4_0
using System;
using System.Linq;
using System.Threading;
using System.Dynamic;
using System.Data.Common;
using System.Configuration;
using System.ComponentModel;
using System.Collections.Generic;
namespace WebMatrix.Data
{
public class Database : IDisposable
{
public static event EventHandler<ConnectionEventArgs> ConnectionOpened;
bool opened;
DbConnection connection;
readonly string providerName;
static readonly Dictionary<string, string> lastInsertedIdStmts = new Dictionary<string, string> ();
static Database ()
{
// Add your own DB-specific way to do so
lastInsertedIdStmts.Add ("System.Data.SqlClient", "select @@IDENTITY;");
lastInsertedIdStmts.Add ("Mono.Data.Sqlite", "select last_insert_rowid ();");
lastInsertedIdStmts.Add ("MySql.Data", "SELECT LAST_INSERT_ID();");
lastInsertedIdStmts.Add ("Npgsql", "SELECT lastval();");
}
private Database (DbConnection connection, string providerName)
{
this.connection = connection;
this.providerName = providerName;
}
public static Database Open (string name)
{
var config = ConfigurationManager.ConnectionStrings[name];
if (config == null)
throw new ArgumentException ("name", string.Format ("Database with name {0} doesn't exist", name));
return OpenConnectionString (config.ConnectionString, config.ProviderName);
}
public static Database OpenConnectionString (string connectionString)
{
return OpenConnectionString (connectionString, "System.Data.SqlClient");
}
public static Database OpenConnectionString (string connectionString, string providerName)
{
var factory = DbProviderFactories.GetFactory (providerName);
var conn = factory.CreateConnection ();
conn.ConnectionString = connectionString;
return new Database (conn, providerName);
}
public void Close ()
{
opened = false;
connection.Close ();
}
public void Dispose ()
{
Dispose (true);
}
protected virtual void Dispose (bool disposing)
{
if (disposing) {
if (opened)
connection.Close ();
connection.Dispose ();
}
}
public int Execute (string commandText, params object[] args)
{
var command = PrepareCommand (commandText);
PrepareCommandParameters (command, args);
EnsureConnectionOpened ();
var result = command.ExecuteNonQuery ();
command.Dispose ();
return result;
}
public IEnumerable<dynamic> Query (string commandText, params object[] args)
{
var result = QueryInternal (commandText, args, false);
return result != null ? result.Select (r => new DynamicRecord (r)) : null;
}
public dynamic QuerySingle (string commandText, params object[] args)
{
var result = QueryInternal (commandText, args, true);
return result != null ? new DynamicRecord (result[0]) : null;
}
List<Dictionary<string, object>> QueryInternal (string commandText, object[] args, bool unique)
{
EnsureConnectionOpened ();
var command = PrepareCommand (commandText);
PrepareCommandParameters (command, args);
string[] columnsNames;
var rows = new List<Dictionary<string, object>> ();
using (var reader = command.ExecuteReader ()) {
if (!reader.Read () || !reader.HasRows)
return null;
columnsNames = new string [reader.FieldCount];
do {
var fields = new Dictionary<string, object> ();
for (int i = 0; i < reader.FieldCount; ++i) {
if (columnsNames[i] == null)
columnsNames[i] = reader.GetName (i);
fields[columnsNames[i]] = reader[i];
}
rows.Add (fields);
} while (!unique && reader.Read ());
}
command.Dispose ();
return rows;
}
public object QueryValue (string commandText, params object[] args)
{
EnsureConnectionOpened ();
var command = PrepareCommand (commandText);
PrepareCommandParameters (command, args);
var result = command.ExecuteScalar ();
command.Dispose ();
return result;
}
public object GetLastInsertId ()
{
string sql;
if (!lastInsertedIdStmts.TryGetValue (providerName, out sql))
throw new NotSupportedException ("This operation is not available for your database");
return QueryValue (sql);
}
DbCommand PrepareCommand (string commandText)
{
var command = connection.CreateCommand ();
command.CommandText = commandText;
return command;
}
static void PrepareCommandParameters (DbCommand command, object[] args)
{
if (args.Length == 0)
return;
int index = 0;
foreach (var arg in args) {
var param = command.CreateParameter ();
param.ParameterName = "@" + index;
param.Value = args[index++];
command.Parameters.Add (param);
}
}
static void TriggerConnectionOpened (Database self, DbConnection connection)
{
EventHandler<ConnectionEventArgs> evt = ConnectionOpened;
if (evt != null)
evt (self, new ConnectionEventArgs (connection));
}
void EnsureConnectionOpened ()
{
if (opened)
return;
connection.Open ();
opened = true;
TriggerConnectionOpened (this, connection);
}
public DbConnection Connection {
get {
return connection;
}
}
}
}
#endif

View File

@@ -0,0 +1,152 @@
//
// DynamicRecord.cs
//
// Copyright (c) 2011 Novell
//
// Authors:
// Jérémie "garuma" Laval
//
// 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 THE
// 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.
//
#if NET_4_0
using System;
using System.Dynamic;
using System.Data.Common;
using System.Collections.Generic;
using System.ComponentModel;
namespace WebMatrix.Data
{
public sealed class DynamicRecord : DynamicObject, ICustomTypeDescriptor
{
readonly Dictionary<string, object> fields;
internal DynamicRecord (Dictionary<string, object> fields)
{
this.fields = fields;
Columns = new List<string> (fields.Keys).AsReadOnly ();
}
public IList<string> Columns {
get;
private set;
}
public object this[string name] {
get {
var retval = fields[name];
if (retval == DBNull.Value)
return null;
return retval;
}
}
public object this[int index] {
get {
var retval = fields[Columns[index]];
if (retval == DBNull.Value)
return null;
return retval;
}
}
public override IEnumerable<string> GetDynamicMemberNames ()
{
return fields.Keys;
}
public override bool TryGetMember (GetMemberBinder binder, out object result)
{
bool success = fields.TryGetValue (binder.Name, out result);
if (result == DBNull.Value)
result = null;
return success;
}
AttributeCollection ICustomTypeDescriptor.GetAttributes ()
{
return null;
}
string ICustomTypeDescriptor.GetClassName ()
{
return null;
}
string ICustomTypeDescriptor.GetComponentName ()
{
return null;
}
TypeConverter ICustomTypeDescriptor.GetConverter ()
{
return null;
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
{
return null;
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
{
return null;
}
Object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
{
return null;
}
Object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
{
return null;
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
{
return null;
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute[] attributes)
{
return null;
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
{
return null;
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute[] attributes)
{
return null;
}
}
}
#endif

View File

@@ -0,0 +1,4 @@
WebMatrix.Data/ConnectionEventArgsTests.cs
WebMatrix.Data/DynamicRecordTests.cs
../WebMatrix.Data/DynamicRecord.cs
WebMatrix.Data/DatabaseTests.cs