Beispiel #1
0
        internal virtual void ExecuteCommand(FileCommand command)
        {
            if (state == ConnectionState.Closed || sw == null)
                Open();

            if (!string.IsNullOrEmpty(command.CommandText))
                sw.WriteLine(TransformToSql(command));

            sw.Flush();
        }
Beispiel #2
0
        /// <summary>
        /// Transforms to SQL by replacing parameters if any.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <returns></returns>
        public string TransformToSql(FileCommand command)
        {
            string query = command.CommandText;

            foreach (FileParameter param in command.Parameters)
            {
                SqlExpressions.Constant c = new SqlExpressions.Constant(param.Value, param.DbType);
                query = query.Replace(param.ParameterName, dialect.Render((SqlExpressions.IDbExpression)c));
            }

            return query;
        }
Beispiel #3
0
        public IDbCommand CreateCommand()
        {
            FileCommand command = new FileCommand(this);
            command.Transaction = currentTransaction;

            return command;
        }
Beispiel #4
0
        internal override void ExecuteCommand(FileCommand command)
        {
            if (command.CommandText.StartsWith("-- TABLE"))
            {
                command.CommandText = command.CommandText.Substring("-- TABLE ".Length);
                StringReader sr = new StringReader(command.CommandText);
                string tableName = sr.ReadLine();
                command.CommandText = sr.ReadToEnd();
                sr.Close();
                FileConnection connection;
                if (!fileConnections.ContainsKey(tableName))
                {
                    connection = new FileConnection();
                    connection.ConnectionString = "filename=" + path + tableName + ".asc";
                    connection.Open();
                    connection.Dialect = Dialect;
                    fileConnections.Add(tableName, connection);
                }
                else
                    connection = fileConnections[tableName];
                connection.ExecuteCommand(command);
            }
            else if (command.CommandText.StartsWith("-- UPDATE"))
            {
                command.CommandText = command.CommandText.Substring("-- UPDATE ".Length);
                TextReader sr = new StringReader(command.CommandText);
                string tableName = sr.ReadLine();
                command.CommandText = sr.ReadToEnd();
                sr.Close();
                command.CommandText = TransformToSql(command);
                FileConnection connection;
                if (fileConnections.ContainsKey(tableName))
                {
                    connection = fileConnections[tableName];
                    connection.Close();
                    fileConnections.Remove(tableName);
                }
                FileStream fs = File.Open(path + tableName + ".asc", FileMode.Open);
                sr = new StreamReader(fs);
                string[] values = command.CommandText.Split('$');
                int position = 0;
                bool found = false;
                while (!found && !((StreamReader)sr).EndOfStream)
                {
                    string line = sr.ReadLine();
                    string[] lineValues = line.Split('$');
                    for (int i = 0; i < values.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(values[i]) && !string.IsNullOrEmpty(lineValues[i]) && values[i] == lineValues[i])
                        {
                            found = true;
                            for (int j = 0; j < values.Length; j++)
                            {
                                if (string.IsNullOrEmpty(values[j]))
                                    values[j] = lineValues[j];
                            }
                            break;
                        }
                    }
                    if (found)
                    {
                        string toEnd = sr.ReadToEnd();
                        fs.Seek(position, SeekOrigin.Begin);
                        StreamWriter sw = new StreamWriter(fs);
                        sw.WriteLine(string.Join("$", values) + "$");
                        sw.Write(toEnd);
                        sw.Close();
                    }
                    else
                        position += line.Length + StringWriter.Null.NewLine.Length;

                }
            }
            else
            {
                command.CommandText = command.CommandText.Substring("-- LOAD ".Length);
                TextReader sr = new StringReader(command.CommandText);
                string tableName = sr.ReadLine();
                command.CommandText = sr.ReadToEnd();
                sr.Close();
                base.Close();
                filename = path + tableName + ".ctl";
                base.Open();
                base.ExecuteCommand(command);
            }
        }