protected override void SetParameter(string parameterName, DbParameter value)
        {
            NuoDbParameter param = ImportParameter(value);
            int            index = IndexOf(parameterName);

            if (index == -1)
            {
                collection.Add(param);
            }
            else
            {
                collection[index] = param;
            }
        }
 public override int Add(object value)
 {
     if (value is DbParameter)
     {
         collection.Add(ImportParameter(value as DbParameter));
     }
     else
     {
         NuoDbParameter param = new NuoDbParameter();
         param.Value = value;
         collection.Add(param);
     }
     return collection.Count - 1;
 }
 public override int Add(object value)
 {
     if (value is DbParameter)
     {
         collection.Add(ImportParameter(value as DbParameter));
     }
     else
     {
         NuoDbParameter param = new NuoDbParameter();
         param.Value = value;
         collection.Add(param);
     }
     return(collection.Count - 1);
 }
Пример #4
0
        public object Clone()
        {
            NuoDbParameter param = new NuoDbParameter();

            param.DbType                  = this.DbType;
            param.Direction               = this.Direction;
            param.IsNullable              = this.IsNullable;
            param.ParameterName           = this.ParameterName;
            param.Size                    = this.Size;
            param.SourceColumn            = this.SourceColumn;
            param.SourceColumnNullMapping = this.SourceColumnNullMapping;
            param.SourceVersion           = this.SourceVersion;
            param.Value                   = this.Value;

            return(param);
        }
        private static NuoDbParameter ImportParameter(DbParameter value)
        {
            if (value is NuoDbParameter)
            {
                return(value as NuoDbParameter);
            }
            NuoDbParameter param = new NuoDbParameter();

            param.ParameterName           = value.ParameterName;
            param.Value                   = value.Value;
            param.DbType                  = value.DbType;
            param.Direction               = value.Direction;
            param.Size                    = value.Size;
            param.SourceColumn            = value.SourceColumn;
            param.SourceColumnNullMapping = value.SourceColumnNullMapping;
            param.SourceVersion           = value.SourceVersion;
            return(param);
        }
Пример #6
0
        public override int Add(object value)
        {
            if (value is DbParameter)
            {
                if (!(value is NuoDbParameter))
                {
                    throw new ArgumentException("Parameter is not a NuoDB parameter", "value");
                }

                collection.Add((NuoDbParameter)value);
            }
            else
            {
                NuoDbParameter param = new NuoDbParameter();
                param.Value = value;
                collection.Add(param);
            }
            return(collection.Count - 1);
        }
Пример #7
0
        public void TestNoPrepareNamedParamIn()
        {
            using (NuoDbConnection connection = new NuoDbConnection(TestFixture1.connectionString))
            {
                connection.Open();
                new NuoDbCommand("drop procedure nunit_test if exists", connection).ExecuteNonQuery();
                new NuoDbCommand("create procedure nunit_test(in p1 string, in p2 string) as throw p1; end_procedure", connection).ExecuteNonQuery();

                NuoDbCommand cmd = new NuoDbCommand("nunit_test", connection);
                cmd.CommandType = CommandType.StoredProcedure;
                NuoDbParameter param1 = new NuoDbParameter();
                param1.ParameterName = "p2";
                param1.Direction = ParameterDirection.Input;
                param1.Value = "goodbye";
                cmd.Parameters.Add(param1);
                NuoDbParameter param2 = new NuoDbParameter();
                param2.ParameterName = "p1";
                param2.Direction = ParameterDirection.Input;
                param2.Value = "hello";
                cmd.Parameters.Add(param2);
                try
                {
                    cmd.ExecuteNonQuery();
                    Assert.Fail();
                }
                catch (Exception e)
                {
                    Assert.AreEqual("hello", e.Message);
                }
            }
        }
Пример #8
0
        public void TestNoPrepareNamedParamOut()
        {
            using (NuoDbConnection connection = new NuoDbConnection(TestFixture1.connectionString))
            {
                connection.Open();
                new NuoDbCommand("drop procedure nunit_test if exists", connection).ExecuteNonQuery();
                new NuoDbCommand("create procedure nunit_test(out p1 string, out p2 string) as p1='hello'; end_procedure", connection).ExecuteNonQuery();

                NuoDbCommand cmd = new NuoDbCommand("nunit_test", connection);
                cmd.CommandType = CommandType.StoredProcedure;
                NuoDbParameter param1 = new NuoDbParameter();
                param1.ParameterName = "p2";
                param1.Direction = ParameterDirection.Output;
                cmd.Parameters.Add(param1);
                NuoDbParameter param2 = new NuoDbParameter();
                param2.ParameterName = "p1";
                param2.Direction = ParameterDirection.Output;
                cmd.Parameters.Add(param2);
                cmd.ExecuteNonQuery();
                Assert.AreEqual("hello", cmd.Parameters["p1"].Value);
            }
        }
Пример #9
0
        private void Prepare(bool generatingKeys)
        {
            checkConnection();
            Close();

            StringBuilder sqlString = new StringBuilder(CommandText.Length);
            NuoDbDataParameterCollection newParams = new NuoDbDataParameterCollection();
            int state = 0;
            string curParamName = "";
            bool inSingleQuotes = false, inDoubleQuotes = false, inSmartQuotes = false;
            foreach (char c in CommandText)
            {
                if (c == '\'' && !(inDoubleQuotes || inSmartQuotes))
                {
                    inSingleQuotes = !inSingleQuotes;
                    state = 0;
                    sqlString.Append(c);
                    continue;
                }
                else if (c == '\"' && !(inSingleQuotes || inSmartQuotes))
                {
                    inDoubleQuotes = !inDoubleQuotes;
                    state = 0;
                    sqlString.Append(c);
                    continue;
                }
                else if (c == '`' && !(inSingleQuotes || inDoubleQuotes))
                {
                    inSmartQuotes = !inSmartQuotes;
                    state = 0;
                    sqlString.Append(c);
                    continue;
                }
                if (inSingleQuotes || inDoubleQuotes || inSmartQuotes)
                {
                    sqlString.Append(c);
                    continue;
                }

                if (c == '?')
                    state = 1;
                else if (c == '@')
                    state = 2;
                else if (state == 1)
                {
                    if (c == '.')
                        state = 2;
                    else
                    {
                        // either add a new parameter, or carry over the user-provided one
                        if (parameters.Count > newParams.Count)
                            newParams.Add(parameters[newParams.Count]);
                        else
                            newParams.Add(new NuoDbParameter());
                        state = 0;
                        sqlString.Append("?");
                        sqlString.Append(c);
                    }
                }
                else if (state == 2)
                {
                    if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' ||
                        (curParamName.Length > 0 && ((c >= '0' && c <= '9') || c == '-')))
                    {
                        curParamName += c;
                    }
                    else
                    {
                        // if the user-provided parameters have a value for this name, preserve it
                        if (parameters.Contains(curParamName))
                            newParams.Add(parameters[curParamName]);
                        else if (parameters.Contains("@"+curParamName))
                            newParams.Add(parameters["@" + curParamName]);
                        else
                        {
                            NuoDbParameter p = new NuoDbParameter();
                            p.ParameterName = curParamName;
                            newParams.Add(p);
                        }
                        sqlString.Append("?.");
                        sqlString.Append(curParamName);
                        sqlString.Append(c);

                        curParamName = "";
                        state = 0;
                    }
                }
                else
                {
                    state = 0;
                    sqlString.Append(c);
                }
            }
            // handle the case where the SQL statement ended while parsing a parameter
            if (state == 1)
            {
                // either add a new parameter, or carry over the user-provided one
                if (parameters.Count > newParams.Count)
                    newParams.Add(parameters[newParams.Count]);
                else
                    newParams.Add(new NuoDbParameter());
                sqlString.Append("?");
            }
            else if (state == 2)
            {
                // if the user-provided parameters have a value for this name, preserve it
                if (parameters.Contains(curParamName))
                    newParams.Add(parameters[curParamName]);
                else if (parameters.Contains("@" + curParamName))
                    newParams.Add(parameters["@" + curParamName]);
                else
                {
                    NuoDbParameter p = new NuoDbParameter();
                    p.ParameterName = curParamName;
                    newParams.Add(p);
                }
                sqlString.Append("?.");
                sqlString.Append(curParamName);
            }
            string nuodbSqlString = sqlString.ToString().TrimStart(null);

            // if we are given just the name of the stored procedure, retrieve the number of parameters and generate the full command
            if(CommandType == CommandType.StoredProcedure &&
                !nuodbSqlString.StartsWith("EXECUTE ", StringComparison.InvariantCultureIgnoreCase) &&
                !nuodbSqlString.StartsWith("CALL ", StringComparison.InvariantCultureIgnoreCase))
            {
                char[] quotes = new char[] { '"' };
                string[] parts = nuodbSqlString.Split(new char[] { '.' });
                DataTable paramTable = null;
                if (parts.Length == 2)
                    paramTable = NuoDbConnectionInternal.GetSchemaHelper(connection, "ProcedureParameters", new string[] { null, parts[0].Trim(quotes), parts[1].Trim(quotes), null });
                else
                {
                    NuoDbConnectionStringBuilder builder = new NuoDbConnectionStringBuilder(connection.ConnectionString);
                    string schema = builder.Schema;
                    if(schema.Length==0)
                        schema = "USER";
                    paramTable = NuoDbConnectionInternal.GetSchemaHelper(connection, "ProcedureParameters", new string[] { null, schema, parts[0].Trim(quotes), null });
                }
                int numParams = 0;
                foreach (DataRow row in paramTable.Select("PARAMETER_DIRECTION <> 3", "ORDINAL_POSITION ASC"))
                {
                    int ordinal = row.Field<int>("ORDINAL_POSITION");
                    if (ordinal != ++numParams)
                        throw new NuoDbSqlException(String.Format("Internal error: unexpected ordering of the parameters of the procedure {0}", nuodbSqlString));
                    int direction = row.Field<int>("PARAMETER_DIRECTION");
                    ParameterDirection paramDirection;
                    switch (direction)
                    {
                        case 1: paramDirection = ParameterDirection.Input; break;
                        case 2: paramDirection = ParameterDirection.InputOutput; break;
                        case 4: paramDirection = ParameterDirection.Output; break;
                        default: throw new NuoDbSqlException(String.Format("Internal error: unexpected parameter type for procedure {0}", nuodbSqlString));
                    }
                    // either add a new parameter, or carry over the user-provided one
                    string paramName = row.Field<string>("PARAMETER_NAME");
                    if (parameters.Contains(paramName))
                        newParams.Add(parameters[paramName]);
                    else if (parameters.Contains("@" + paramName))
                        newParams.Add(parameters["@" + paramName]);
                    else if (parameters.Count > newParams.Count)
                    {
                        if (parameters[newParams.Count].ParameterName.Length == 0)
                            parameters[newParams.Count].ParameterName = paramName;
                        newParams.Add(parameters[newParams.Count]);
                    }
                    else
                    {
                        NuoDbParameter p = new NuoDbParameter();
                        p.ParameterName = paramName;
                        newParams.Add(p);
                    }
                    newParams[newParams.Count - 1].DbType = NuoDbConnectionInternal.mapJavaSqlToDbType(row.Field<int>("PARAMETER_DATA_TYPE"));
                    newParams[newParams.Count - 1].Direction = paramDirection;
                }
                StringBuilder strBuilder = new StringBuilder("EXECUTE ");
                strBuilder.Append(nuodbSqlString);
                strBuilder.Append("(");
                for (int i = 0; i < numParams; i++)
                {
                    if (i != 0)
                        strBuilder.Append(",");
                    strBuilder.Append("?");
                }
                strBuilder.Append(")");
                nuodbSqlString = strBuilder.ToString();
            }

            if (nuodbSqlString.StartsWith("EXECUTE ", StringComparison.InvariantCultureIgnoreCase) ||
                nuodbSqlString.StartsWith("CALL ", StringComparison.InvariantCultureIgnoreCase))
            {
                if(connection.InternalConnection.protocolVersion < Protocol.PROTOCOL_VERSION12)
                    throw new NuoDbSqlException(String.Format("server protocol {0} doesn't support prepareCall", connection.InternalConnection.protocolVersion));
                EncodedDataStream dataStream = new RemEncodedStream(connection.InternalConnection.protocolVersion);
                dataStream.startMessage(Protocol.PrepareCall);
                dataStream.encodeString(nuodbSqlString);
                connection.InternalConnection.sendAndReceive(dataStream);
                handle = dataStream.getInt();
                connection.InternalConnection.RegisterCommand(handle);
                int numberParameters = dataStream.getInt();
                for (int i = 0; i < numberParameters; i++) {
                    int     direction = dataStream.getInt();
                    String  name = dataStream.getString();
                    switch(direction)
                    {
                        case 0: newParams[i].Direction = ParameterDirection.Input; break;
                        case 1: newParams[i].Direction = ParameterDirection.InputOutput; break;
                        case 2: newParams[i].Direction = ParameterDirection.Output; break;
                    }
                    if(newParams[i].ParameterName.Length == 0)
                        newParams[i].ParameterName = name;
                }
                parameters = newParams;
                isPrepared = true;
                isPreparedWithKeys = generatingKeys;
            }
            else
            {
                EncodedDataStream dataStream = new RemEncodedStream(connection.InternalConnection.protocolVersion);
                if (generatingKeys)
                {
                    dataStream.startMessage(Protocol.PrepareStatementKeys);
                    dataStream.encodeInt(generatingKeys ? 1 : 0);
                }
                else
                    dataStream.startMessage(Protocol.PrepareStatement);
                dataStream.encodeString(nuodbSqlString);
                connection.InternalConnection.sendAndReceive(dataStream);
                handle = dataStream.getInt();
                connection.InternalConnection.RegisterCommand(handle);
                int numberParameters = dataStream.getInt();
                // a prepared DDL command fails to execute
                if (numberParameters != 0 || nuodbSqlString.StartsWith("SELECT ", StringComparison.InvariantCultureIgnoreCase))
                {
                    parameters = newParams;
                    isPrepared = true;
                    isPreparedWithKeys = generatingKeys;
                }
                else
                {
                    Close();
                    isPrepared = false;
                }
            }
        }
Пример #10
0
        private void Prepare(bool generatingKeys)
        {
            checkConnection();
            Close();

            StringBuilder sqlString = new StringBuilder(CommandText.Length);
            NuoDbDataParameterCollection newParams = new NuoDbDataParameterCollection();
            int    state = 0;
            string curParamName = "";
            bool   inSingleQuotes = false, inDoubleQuotes = false, inSmartQuotes = false;

            foreach (char c in CommandText)
            {
                if (c == '\'' && !(inDoubleQuotes || inSmartQuotes))
                {
                    inSingleQuotes = !inSingleQuotes;
                    state          = 0;
                    sqlString.Append(c);
                    continue;
                }
                else if (c == '\"' && !(inSingleQuotes || inSmartQuotes))
                {
                    inDoubleQuotes = !inDoubleQuotes;
                    state          = 0;
                    sqlString.Append(c);
                    continue;
                }
                else if (c == '`' && !(inSingleQuotes || inDoubleQuotes))
                {
                    inSmartQuotes = !inSmartQuotes;
                    state         = 0;
                    sqlString.Append(c);
                    continue;
                }
                if (inSingleQuotes || inDoubleQuotes || inSmartQuotes)
                {
                    sqlString.Append(c);
                    continue;
                }

                if (c == '?')
                {
                    state = 1;
                }
                else if (c == '@')
                {
                    state = 2;
                }
                else if (state == 1)
                {
                    if (c == '.')
                    {
                        state = 2;
                    }
                    else
                    {
                        // either add a new parameter, or carry over the user-provided one
                        if (parameters.Count > newParams.Count)
                        {
                            newParams.Add(parameters[newParams.Count]);
                        }
                        else
                        {
                            newParams.Add(new NuoDbParameter());
                        }
                        state = 0;
                        sqlString.Append("?");
                        sqlString.Append(c);
                    }
                }
                else if (state == 2)
                {
                    if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' ||
                        (curParamName.Length > 0 && ((c >= '0' && c <= '9') || c == '-')))
                    {
                        curParamName += c;
                    }
                    else
                    {
                        // if the user-provided parameters have a value for this name, preserve it
                        if (parameters.Contains(curParamName))
                        {
                            newParams.Add(parameters[curParamName]);
                        }
                        else if (parameters.Contains("@" + curParamName))
                        {
                            newParams.Add(parameters["@" + curParamName]);
                        }
                        else
                        {
                            NuoDbParameter p = new NuoDbParameter();
                            p.ParameterName = curParamName;
                            newParams.Add(p);
                        }
                        sqlString.Append("?.");
                        sqlString.Append(curParamName);
                        sqlString.Append(c);

                        curParamName = "";
                        state        = 0;
                    }
                }
                else
                {
                    state = 0;
                    sqlString.Append(c);
                }
            }
            // handle the case where the SQL statement ended while parsing a parameter
            if (state == 1)
            {
                // either add a new parameter, or carry over the user-provided one
                if (parameters.Count > newParams.Count)
                {
                    newParams.Add(parameters[newParams.Count]);
                }
                else
                {
                    newParams.Add(new NuoDbParameter());
                }
                sqlString.Append("?");
            }
            else if (state == 2)
            {
                // if the user-provided parameters have a value for this name, preserve it
                if (parameters.Contains(curParamName))
                {
                    newParams.Add(parameters[curParamName]);
                }
                else if (parameters.Contains("@" + curParamName))
                {
                    newParams.Add(parameters["@" + curParamName]);
                }
                else
                {
                    NuoDbParameter p = new NuoDbParameter();
                    p.ParameterName = curParamName;
                    newParams.Add(p);
                }
                sqlString.Append("?.");
                sqlString.Append(curParamName);
            }
            string nuodbSqlString = sqlString.ToString().TrimStart(null);

            // if we are given just the name of the stored procedure, retrieve the number of parameters and generate the full command
            if (CommandType == CommandType.StoredProcedure &&
                !nuodbSqlString.StartsWith("EXECUTE ", StringComparison.InvariantCultureIgnoreCase) &&
                !nuodbSqlString.StartsWith("CALL ", StringComparison.InvariantCultureIgnoreCase))
            {
                char[]    quotes     = new char[] { '"' };
                string[]  parts      = nuodbSqlString.Split(new char[] { '.' });
                DataTable paramTable = null;
                if (parts.Length == 2)
                {
                    paramTable = NuoDbConnectionInternal.GetSchemaHelper(connection, "ProcedureParameters", new string[] { null, parts[0].Trim(quotes), parts[1].Trim(quotes), null });
                }
                else
                {
                    NuoDbConnectionStringBuilder builder = new NuoDbConnectionStringBuilder(connection.ConnectionString);
                    string schema = builder.Schema;
                    if (schema.Length == 0)
                    {
                        schema = "USER";
                    }
                    paramTable = NuoDbConnectionInternal.GetSchemaHelper(connection, "ProcedureParameters", new string[] { null, schema, parts[0].Trim(quotes), null });
                }
                int numParams = 0;
                foreach (DataRow row in paramTable.Select("PARAMETER_DIRECTION <> 3", "ORDINAL_POSITION ASC"))
                {
                    int ordinal = (int)row["ORDINAL_POSITION"];
                    if (ordinal != ++numParams)
                    {
                        throw new NuoDbSqlException(String.Format("Internal error: unexpected ordering of the parameters of the procedure {0}", nuodbSqlString));
                    }
                    int direction = (int)row["PARAMETER_DIRECTION"];
                    ParameterDirection paramDirection;
                    switch (direction)
                    {
                    case 1: paramDirection = ParameterDirection.Input; break;

                    case 2: paramDirection = ParameterDirection.InputOutput; break;

                    case 4: paramDirection = ParameterDirection.Output; break;

                    default: throw new NuoDbSqlException(String.Format("Internal error: unexpected parameter type for procedure {0}", nuodbSqlString));
                    }
                    // either add a new parameter, or carry over the user-provided one
                    string paramName = (string)row["PARAMETER_NAME"];
                    if (parameters.Contains(paramName))
                    {
                        newParams.Add(parameters[paramName]);
                    }
                    else if (parameters.Contains("@" + paramName))
                    {
                        newParams.Add(parameters["@" + paramName]);
                    }
                    else if (parameters.Count > newParams.Count)
                    {
                        if (parameters[newParams.Count].ParameterName.Length == 0)
                        {
                            parameters[newParams.Count].ParameterName = paramName;
                        }
                        newParams.Add(parameters[newParams.Count]);
                    }
                    else
                    {
                        NuoDbParameter p = new NuoDbParameter();
                        p.ParameterName = paramName;
                        newParams.Add(p);
                    }
                    newParams[newParams.Count - 1].DbType    = NuoDbConnectionInternal.mapJavaSqlToDbType((int)row["PARAMETER_DATA_TYPE"]);
                    newParams[newParams.Count - 1].Direction = paramDirection;
                }
                StringBuilder strBuilder = new StringBuilder("EXECUTE ");
                strBuilder.Append(nuodbSqlString);
                strBuilder.Append("(");
                for (int i = 0; i < numParams; i++)
                {
                    if (i != 0)
                    {
                        strBuilder.Append(",");
                    }
                    strBuilder.Append("?");
                }
                strBuilder.Append(")");
                nuodbSqlString = strBuilder.ToString();
            }

            if (nuodbSqlString.StartsWith("EXECUTE ", StringComparison.InvariantCultureIgnoreCase) ||
                nuodbSqlString.StartsWith("CALL ", StringComparison.InvariantCultureIgnoreCase))
            {
                if (connection.InternalConnection.protocolVersion < Protocol.PROTOCOL_VERSION12)
                {
                    throw new NuoDbSqlException(String.Format("server protocol {0} doesn't support prepareCall", connection.InternalConnection.protocolVersion));
                }
                EncodedDataStream dataStream = new RemEncodedStream(connection.InternalConnection.protocolVersion);
                dataStream.startMessage(Protocol.PrepareCall);
                dataStream.encodeString(nuodbSqlString);
                connection.InternalConnection.sendAndReceive(dataStream);
                handle = dataStream.getInt();
                connection.InternalConnection.RegisterCommand(handle);
                int numberParameters = dataStream.getInt();
                for (int i = 0; i < numberParameters; i++)
                {
                    int    direction = dataStream.getInt();
                    String name      = dataStream.getString();
                    switch (direction)
                    {
                    case 0: newParams[i].Direction = ParameterDirection.Input; break;

                    case 1: newParams[i].Direction = ParameterDirection.InputOutput; break;

                    case 2: newParams[i].Direction = ParameterDirection.Output; break;
                    }
                    if (newParams[i].ParameterName.Length == 0)
                    {
                        newParams[i].ParameterName = name;
                    }
                }
                parameters         = newParams;
                isPrepared         = true;
                isPreparedWithKeys = generatingKeys;
            }
            else
            {
                EncodedDataStream dataStream = new RemEncodedStream(connection.InternalConnection.protocolVersion);
                if (generatingKeys)
                {
                    dataStream.startMessage(Protocol.PrepareStatementKeys);
                    dataStream.encodeInt(generatingKeys ? 1 : 0);
                }
                else
                {
                    dataStream.startMessage(Protocol.PrepareStatement);
                }
                dataStream.encodeString(nuodbSqlString);
                connection.InternalConnection.sendAndReceive(dataStream);
                handle = dataStream.getInt();
                connection.InternalConnection.RegisterCommand(handle);
                int numberParameters = dataStream.getInt();
                // a prepared DDL command fails to execute
                if (numberParameters != 0 || nuodbSqlString.StartsWith("SELECT ", StringComparison.InvariantCultureIgnoreCase))
                {
                    parameters         = newParams;
                    isPrepared         = true;
                    isPreparedWithKeys = generatingKeys;
                }
                else
                {
                    Close();
                    isPrepared = false;
                }
            }
        }
Пример #11
0
        public object Clone()
        {
            NuoDbParameter param = new NuoDbParameter();

            param.DbType = this.DbType;
            param.Direction = this.Direction;
            param.IsNullable = this.IsNullable;
            param.ParameterName = this.ParameterName;
            param.Size = this.Size;
            param.SourceColumn = this.SourceColumn;
            param.SourceColumnNullMapping = this.SourceColumnNullMapping;
            param.SourceVersion = this.SourceVersion;
            param.Value = this.Value;

            return param;
        }
Пример #12
0
        private void Prepare(bool generatingKeys)
        {
            checkConnection();
            Close();

            StringBuilder sqlString = new StringBuilder(sqlText.Length);
            NuoDbDataParameterCollection newParams = new NuoDbDataParameterCollection();
            int state = 0;
            string curParamName = "";
            bool inSingleQuotes = false, inDoubleQuotes = false, inSmartQuotes = false;
            foreach (char c in sqlText)
            {
                if (c == '\'' && !(inDoubleQuotes || inSmartQuotes))
                {
                    inSingleQuotes = !inSingleQuotes;
                    state = 0;
                    sqlString.Append(c);
                    continue;
                }
                else if (c == '\"' && !(inSingleQuotes || inSmartQuotes))
                {
                    inDoubleQuotes = !inDoubleQuotes;
                    state = 0;
                    sqlString.Append(c);
                    continue;
                }
                else if (c == '`' && !(inSingleQuotes || inDoubleQuotes))
                {
                    inSmartQuotes = !inSmartQuotes;
                    state = 0;
                    sqlString.Append(c);
                    continue;
                }
                if (inSingleQuotes || inDoubleQuotes || inSmartQuotes)
                {
                    sqlString.Append(c);
                    continue;
                }

                if (c == '?')
                    state = 1;
                else if (c == '@')
                    state = 2;
                else if (state == 1)
                {
                    if (c == '.')
                        state = 2;
                    else
                    {
                        // either add a new parameter, or carry over the user-provided one
                        if (parameters.Count > newParams.Count)
                            newParams.Add(parameters[newParams.Count]);
                        else
                            newParams.Add(new NuoDbParameter());
                        state = 0;
                        sqlString.Append("?");
                        sqlString.Append(c);
                    }
                }
                else if (state == 2)
                {
                    if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' ||
                        (curParamName.Length > 0 && ((c >= '0' && c <= '9') || c == '-')))
                    {
                        curParamName += c;
                    }
                    else
                    {
                        // if the user-provided parameters have a value for this name, preserve it
                        if (parameters.Contains(curParamName))
                            newParams.Add(parameters[curParamName]);
                        else
                        {
                            NuoDbParameter p = new NuoDbParameter();
                            p.ParameterName = curParamName;
                            newParams.Add(p);
                        }
                        sqlString.Append("?.");
                        sqlString.Append(curParamName);
                        sqlString.Append(c);

                        curParamName = "";
                        state = 0;
                    }
                }
                else
                {
                    state = 0;
                    sqlString.Append(c);
                }
            }
            // handle the case where the SQL statement ended while parsing a parameter
            if (state == 1)
            {
                // either add a new parameter, or carry over the user-provided one
                if (parameters.Count > newParams.Count)
                    newParams.Add(parameters[newParams.Count]);
                else
                    newParams.Add(new NuoDbParameter());
                sqlString.Append("?");
            }
            else if (state == 2)
            {
                // if the user-provided parameters have a value for this name, preserve it
                if (parameters.Contains(curParamName))
                    newParams.Add(parameters[curParamName]);
                else
                {
                    NuoDbParameter p = new NuoDbParameter();
                    p.ParameterName = curParamName;
                    newParams.Add(p);
                }
                sqlString.Append("?.");
                sqlString.Append(curParamName);
            }

            EncodedDataStream dataStream = new RemEncodedStream(connection.InternalConnection.protocolVersion);
            if (generatingKeys)
            {
                dataStream.startMessage(Protocol.PrepareStatementKeys);
                dataStream.encodeInt(generatingKeys ? 1 : 0);
            }
            else
                dataStream.startMessage(Protocol.PrepareStatement);
            dataStream.encodeString(sqlString.ToString());
            connection.InternalConnection.sendAndReceive(dataStream);
            handle = dataStream.getInt();
            connection.InternalConnection.RegisterCommand(handle);
            int numberParameters = dataStream.getInt();
            // a prepared DDL command fails to execute
            if (numberParameters != 0 || CommandText.TrimStart(null).Substring(0, 6).ToUpper().Equals("SELECT"))
            {
                parameters = newParams;
                isPrepared = true;
                isPreparedWithKeys = generatingKeys;
            }
            else
            {
                Close();
                isPrepared = false;
            }
        }
Пример #13
0
        public void TestNamedParameters2()
        {
            using (NuoDbConnection connection = new NuoDbConnection(connectionString))
            {
                DbCommand command = connection.CreateCommand();
                connection.Open();

                command.CommandText = "select * from hockey where number = ?.number and team = ?.team";
                NuoDbParameter p1 = new NuoDbParameter();
                p1.ParameterName = "team";
                p1.Value = "Bruins";
                command.Parameters.Add(p1);
                NuoDbParameter p2 = new NuoDbParameter();
                p2.ParameterName = "number";
                p2.Value = 1;
                command.Parameters.Add(p2);
                command.Prepare();

                DbDataReader reader = command.ExecuteReader();
                Assert.IsTrue(reader.Read());
                Assert.AreEqual(1, reader["number"]);
                Assert.IsFalse(reader.Read());
                reader.Close();
            }
        }
Пример #14
0
        public void TestNamedParameters2()
        {
            using (NuoDbConnection connection = new NuoDbConnection(connectionString))
            {
                DbCommand command = connection.CreateCommand();
                connection.Open();

                command.CommandText = "select * from hockey where number = ?.number and team = ?.team";
                NuoDbParameter p1 = new NuoDbParameter();
                p1.ParameterName = "team";
                p1.Value = "Bruins";
                command.Parameters.Add(p1);
                NuoDbParameter p2 = new NuoDbParameter();
                p2.ParameterName = "number";
                p2.Value = 1;
                command.Parameters.Add(p2);
                command.Prepare();

                DbDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", reader[0], reader[1], reader[2], reader["id"]);
                }
                reader.Close();
            }
        }
        public override int Add(object value)
        {
            if (value is DbParameter)
            {
                if (!(value is NuoDbParameter))
                    throw new ArgumentException("Parameter is not a NuoDB parameter", "value");

                collection.Add((NuoDbParameter)value);
            }
            else
            {
                NuoDbParameter param = new NuoDbParameter();
                param.Value = value;
                collection.Add(param);
            }
            return collection.Count - 1;
        }
Пример #16
0
        public void TestNamedParameters3()
        {
            using (NuoDbConnection connection = new NuoDbConnection(connectionString))
            {
                DbCommand command = connection.CreateCommand();
                connection.Open();

                command.CommandText = "select name as \"'?\" from \"hockey\" where name='? ?.fake' or number = ?.number and team = ?.team";
                NuoDbParameter p1 = new NuoDbParameter();
                p1.ParameterName = "TEAM";
                p1.Value = "Bruins";
                command.Parameters.Add(p1);
                NuoDbParameter p2 = new NuoDbParameter();
                p2.ParameterName = "NUMBER";
                p2.Value = 1;
                command.Parameters.Add(p2);
                command.Prepare();

                DbDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine("\t{0}", reader["'?"]);
                }
                reader.Close();
            }
        }
 private static NuoDbParameter ImportParameter(DbParameter value)
 {
     if (value is NuoDbParameter)
         return value as NuoDbParameter;
     NuoDbParameter param = new NuoDbParameter();
     param.ParameterName = value.ParameterName;
     param.Value = value.Value;
     param.DbType = value.DbType;
     param.Direction = value.Direction;
     param.Size = value.Size;
     param.SourceColumn = value.SourceColumn;
     param.SourceColumnNullMapping = value.SourceColumnNullMapping;
     param.SourceVersion = value.SourceVersion;
     return param;
 }