mirror of
https://github.com/BepInEx/mono.git
synced 2026-11-18 03:51:19 -10:00
* System.Data.SqlClient/ParmUtil.cs: added file - to provide utility for conversion of input parameters * System.Data.SqlClient/PostgresTypes.cs: added file - moved the PostgreHelper class to here. May eventually move the internal class PostgresTypes that's inside the SqlConnection to here as well. Handling of PostgreSQL <-> .NET types need to be though out more. Also, the PostgreHelper has a method to convert from .NET types to a string which can be put into used in an SQL statement to execute against a PostgreSQL database. This is the beginnings of parameters support. It currently only supports input parameters. Still need to do output, input/output, and return parameters. * Test/TestSqlParameters.cs: new test to test the input parameters in System.Data.SqlClient against a PostgreSQL db. * System.Data.SqlClient/PostgresLibrary.cs: moved PostgresHelper class to file PostgresTypes.cs. Also moved struct PostgresType there too. * System.Data.SqlClient/SqlCommand.cs: added input parameters support * System.Data.SqlClient/SqlParameter.cs: got SqlParameter to work * System.Data.SqlClient/SqlParameterCollection.cs: got SqlParameterCollection to work * Test/System.Data_test.build: added files to exclude from test build * System.Data.SqlClient/SqlConnection.cs: release resources no longer used svn path=/trunk/mcs/; revision=4805
177 lines
4.4 KiB
C#
177 lines
4.4 KiB
C#
//
|
|
// ParmUtil.cs - utility to bind variables in a SQL statement to parameters in C# code
|
|
// This is in the PostgreSQL .NET Data provider in Mono
|
|
//
|
|
// Author:
|
|
// Daniel Morgan <[email protected]>
|
|
//
|
|
// (c)copyright 2002 Daniel Morgan
|
|
//
|
|
|
|
// comment DEBUG_ParmUtil for production, for debug messages, uncomment
|
|
//#define DEBUG_ParmUtil
|
|
|
|
using System;
|
|
using System.Data;
|
|
using System.Text;
|
|
|
|
namespace System.Data.SqlClient {
|
|
|
|
enum PostgresBindVariableCharacter {
|
|
Semicolon,
|
|
At,
|
|
QuestionMark
|
|
}
|
|
|
|
public class ParmUtil {
|
|
|
|
private string sql = "";
|
|
private string resultSql = "";
|
|
private SqlParameterCollection parmsCollection = null;
|
|
|
|
static private PostgresBindVariableCharacter PgbindChar = PostgresBindVariableCharacter.Semicolon;
|
|
static char bindChar;
|
|
|
|
// static constructor
|
|
static ParmUtil() {
|
|
switch(PgbindChar) {
|
|
case PostgresBindVariableCharacter.Semicolon:
|
|
bindChar = ':';
|
|
break;
|
|
case PostgresBindVariableCharacter.At:
|
|
bindChar = '@';
|
|
break;
|
|
case PostgresBindVariableCharacter.QuestionMark:
|
|
// this doesn't have named parameters,
|
|
// they must be in order
|
|
bindChar = '?';
|
|
break;
|
|
}
|
|
}
|
|
|
|
public ParmUtil(string query, SqlParameterCollection parms) {
|
|
sql = query;
|
|
parmsCollection = parms;
|
|
}
|
|
|
|
public string ResultSql {
|
|
get {
|
|
return resultSql;
|
|
}
|
|
}
|
|
|
|
// TODO: currently only works for input variables,
|
|
// need to do input/output, output, and return
|
|
public string ReplaceWithParms() {
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
char[] chars = sql.ToCharArray();
|
|
bool bStringConstFound = false;
|
|
|
|
for(int i = 0; i < chars.Length; i++) {
|
|
if(chars[i] == '\'') {
|
|
if(bStringConstFound == true)
|
|
bStringConstFound = false;
|
|
else
|
|
bStringConstFound = true;
|
|
|
|
result.Append(chars[i]);
|
|
}
|
|
else if(chars[i] == bindChar &&
|
|
bStringConstFound == false) {
|
|
#if DEBUG_ParmUtil
|
|
Console.WriteLine("Bind Variable character found...");
|
|
#endif
|
|
StringBuilder parm = new StringBuilder();
|
|
i++;
|
|
while(i <= chars.Length) {
|
|
char ch;
|
|
if(i == chars.Length)
|
|
ch = ' '; // a space
|
|
else
|
|
ch = chars[i];
|
|
|
|
#if DEBUG_ParmUtil
|
|
Console.WriteLine("Is char Letter or digit?");
|
|
#endif
|
|
if(Char.IsLetterOrDigit(ch)) {
|
|
#if DEBUG_ParmUtil
|
|
Console.WriteLine("Char IS letter or digit. " +
|
|
"Now, append char to parm StringBuilder");
|
|
#endif
|
|
parm.Append(ch);
|
|
}
|
|
else {
|
|
#if DEBUG_ParmUtil
|
|
Console.WriteLine("Char is NOT letter or char. " +
|
|
"thus we got rest of bind variable name. ");
|
|
|
|
// replace bind variable placeholder
|
|
// with data value constant
|
|
Console.WriteLine("parm StringBuilder to string p...");
|
|
#endif
|
|
string p = parm.ToString();
|
|
#if DEBUG_ParmUtil
|
|
Console.WriteLine("calling BindReplace...");
|
|
#endif
|
|
bool found = BindReplace(result, p);
|
|
#if DEBUG_ParmUtil
|
|
Console.WriteLine(" Found = " + found);
|
|
#endif
|
|
if(found == true)
|
|
break;
|
|
else {
|
|
// *** Error Handling
|
|
Console.WriteLine("Error: parameter not found: " + p);
|
|
return "";
|
|
}
|
|
}
|
|
i++;
|
|
}
|
|
i--;
|
|
}
|
|
else
|
|
result.Append(chars[i]);
|
|
}
|
|
|
|
resultSql = result.ToString();
|
|
return resultSql;
|
|
}
|
|
|
|
public bool BindReplace (StringBuilder result, string p) {
|
|
// bind variable
|
|
bool found = false;
|
|
|
|
#if DEBUG_ParmUtil
|
|
Console.WriteLine("Does the parmsCollection contain the parameter???: " + p);
|
|
#endif
|
|
if(parmsCollection.Contains(p) == true) {
|
|
// parameter found
|
|
#if DEBUG_ParmUtil
|
|
Console.WriteLine("Parameter Found: " + p);
|
|
#endif
|
|
SqlParameter prm = parmsCollection[p];
|
|
|
|
#if DEBUG_ParmUtil
|
|
// DEBUG
|
|
Console.WriteLine(" Value: " + prm.Value);
|
|
Console.WriteLine(" Direction: " + prm.Direction);
|
|
#endif
|
|
// convert object to string and place
|
|
// into SQL
|
|
if(prm.Direction == ParameterDirection.Input) {
|
|
string strObj = PostgresHelper.
|
|
ObjectToString(prm.DbType,
|
|
prm.Value);
|
|
result.Append(strObj);
|
|
}
|
|
else
|
|
result.Append(bindChar + p);
|
|
|
|
found = true;
|
|
}
|
|
return found;
|
|
}
|
|
}
|
|
}
|