1- // EntropyZero dataFresh Copyright (C) 2007 EntropyZero Consulting, LLC.
2- // Please visit us on the web: http://blogs.ent0.com/
3- //
4- // This library is free software; you can redistribute it and/or modify
5- // it under the terms of the GNU Lesser General Public License as
6- // published by the Free Software Foundation; either version 2.1 of the
7- // License, or (at your option) any later version.
8- //
9- // This library is distributed in the hope that it will be useful, but
10- // WITHOUT ANY WARRANTY; without even the implied warranty of
11- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12- // Lesser General Public License for more details.
13- //
14- // You should have received a copy of the GNU Lesser General Public
15- // License along with this library; if not, write to:
16- // Free Software Foundation, Inc.,
17- // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18-
1+ // EntropyZero dataFresh Copyright (C) 2007 EntropyZero Consulting, LLC.
2+ // Please visit us on the web: http://blogs.ent0.com/
3+ //
4+ // This library is free software; you can redistribute it and/or modify
5+ // it under the terms of the GNU Lesser General Public License as
6+ // published by the Free Software Foundation; either version 2.1 of the
7+ // License, or (at your option) any later version.
8+ //
9+ // This library is distributed in the hope that it will be useful, but
10+ // WITHOUT ANY WARRANTY; without even the implied warranty of
11+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+ // Lesser General Public License for more details.
13+ //
14+ // You should have received a copy of the GNU Lesser General Public
15+ // License along with this library; if not, write to:
16+ // Free Software Foundation, Inc.,
17+ // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
1919using System ;
2020using System . Collections . Specialized ;
21- using System . IO ;
2221using System . Text ;
2322
2423namespace DataFresh
2524{
2625 public class DataFreshConsole
2726 {
28- private StringBuilder results = new StringBuilder ( ) ;
29-
30- public NameValueCollection arguments = new NameValueCollection ( ) ;
31- public string connectionString = string . Empty ;
27+ readonly StringBuilder results = new StringBuilder ( ) ;
3228
33- public string Results
29+ public DataFreshConsole ( )
3430 {
35- get { return results . ToString ( ) ; }
31+ ConnectionString = string . Empty ;
32+ Arguments = new NameValueCollection ( ) ;
3633 }
3734
35+ public string Results => results . ToString ( ) ;
36+
37+ public NameValueCollection Arguments { get ; }
38+
39+ public string ConnectionString { get ; set ; }
40+
3841 public void Start ( string [ ] args )
3942 {
4043 ConsoleWrite ( "DataFresh provided by Entropy Zero Consulting" ) ;
@@ -45,55 +48,26 @@ public void Start(string[] args)
4548 return ;
4649 }
4750
48- for ( int i = 0 ; i < args . Length ; i = i + 2 )
49- {
50- this . arguments . Add ( args [ i ] . Replace ( "-" , "" ) , args [ i + 1 ] ) ;
51- //ConsoleWrite(args[i] + ": " +args[i+1]);
52- }
51+ for ( var i = 0 ; i < args . Length ; i += 2 )
52+ Arguments . Add ( args [ i ] . Replace ( "-" , "" ) , args [ i + 1 ] ) ;
5353
54- if ( ! CheckForRequiredArguments ( this . arguments ) )
54+ if ( ! CheckForRequiredArguments ( Arguments ) )
5555 {
5656 return ;
5757 }
5858
59- connectionString = string . Format ( @"user id={0};password={1};Initial Catalog={2};Data Source={3};" ,
60- this . arguments [ "u" ] ,
61- this . arguments [ "p" ] ,
62- this . arguments [ "d" ] ,
63- this . arguments [ "s" ] ) ;
64-
65- SqlDataFresh dataFresh = new SqlDataFresh ( connectionString , true ) ;
59+ ConnectionString =
60+ $@ "user id={ Arguments [ "u" ] } ;password={ Arguments [ "p" ] } ;Initial Catalog={ Arguments [ "d" ] } ;Data Source={ Arguments [ "s" ] } ;";
6661
67- string snapshotPath = this . arguments [ "sp" ] ;
62+ var dataFresh = new SqlDataFresh ( ConnectionString , true ) ;
6863
69- if ( snapshotPath != null )
70- {
71- snapshotPath = snapshotPath . Replace ( "\" " , "" ) ;
72- if ( ! snapshotPath . EndsWith ( @"\" ) )
73- {
74- snapshotPath += @"\" ;
75- }
76-
77- ConsoleWrite ( "snapshotPath = {0}" , snapshotPath ) ;
78- string fullPath = Path . GetFullPath ( snapshotPath ) ;
79- ConsoleWrite ( "fullPath = {0}" , fullPath ) ;
80- dataFresh . SnapshotPath = new DirectoryInfo ( snapshotPath ) ;
81- }
82-
83- string command = this . arguments [ "c" ] . ToUpper ( ) ;
64+ var command = Arguments [ "c" ] . ToUpper ( ) ;
8465 switch ( command )
8566 {
8667 case "PREPARE" :
87- bool ignoreSnapshot = false ;
88- string ignoreSnapshotArgument = arguments [ "ignoresnapshot" ] ;
89- if ( ignoreSnapshotArgument != null )
90- {
91- if ( ignoreSnapshotArgument == "1" )
92- {
93- ignoreSnapshot = true ;
94- }
95- }
96- dataFresh . PrepareDatabaseforDataFresh ( ! ignoreSnapshot ) ;
68+ var ignoreSnapshotArgument = Arguments [ "ignoresnapshot" ] ;
69+ var ignoreSnapshot = ( ignoreSnapshotArgument != null && ignoreSnapshotArgument == "1" ) ;
70+ dataFresh . PrepareDatabaseForDataFresh ( ! ignoreSnapshot ) ;
9771 break ;
9872 case "REFRESH" :
9973 dataFresh . RefreshTheDatabase ( ) ;
@@ -107,44 +81,39 @@ public void Start(string[] args)
10781 case "SNAPSHOT" :
10882 dataFresh . CreateSnapshot ( ) ;
10983 break ;
110- case "FOO" :
111- //no nothing
84+ case "FOO" :
85+ //no nothing
11286 break ;
11387 default :
11488 ConsoleWrite ( "Command '{0}' was not recognized" , command ) ;
11589 break ;
11690 }
11791 }
11892
119- private void ConsoleWrite ( string message , params object [ ] args )
93+ void ConsoleWrite ( string message , params object [ ] args )
12094 {
12195 results . AppendFormat ( message , args ) ;
12296 results . AppendFormat ( Environment . NewLine ) ;
12397
12498 Console . Out . WriteLine ( message , args ) ;
12599 }
126100
127- private bool CheckForRequiredArguments ( NameValueCollection myArgs )
101+ bool CheckForRequiredArguments ( NameValueCollection myArgs )
128102 {
129- if (
130- myArgs == null ||
131- myArgs [ "c" ] == null ||
132- myArgs [ "s" ] == null ||
133- myArgs [ "d" ] == null ||
134- myArgs [ "u" ] == null ||
135- myArgs [ "p" ] == null
136- )
137- {
138- ConsoleWrite ( "Missing required arguments." ) ;
139- WriteUsage ( ) ;
140- return false ;
141- }
142- return true ;
103+ if ( myArgs ? [ "c" ] != null
104+ && myArgs [ "s" ] != null
105+ && myArgs [ "d" ] != null
106+ && myArgs [ "u" ] != null
107+ && myArgs [ "p" ] != null ) return true ;
108+
109+ ConsoleWrite ( "Missing required arguments." ) ;
110+ WriteUsage ( ) ;
111+ return false ;
143112 }
144113
145- private void WriteUsage ( )
114+ void WriteUsage ( )
146115 {
147- string usageText = @"
116+ const string usageText = @"
148117Usage:
149118
150119 DataFreshUtil.exe
@@ -156,7 +125,6 @@ private void WriteUsage()
156125
157126Options:
158127
159- -sp specify path on server where snapshot files are located
160128 -ignoresnapshot 1: ignore snapshot during prepare
161129 0: (default) will create snapshot
162130
@@ -174,7 +142,7 @@ SNAPSHOT create a snapshot of your database
174142
175143 public static DataFreshConsole Execute ( string command , string username , string password , string server , string database , params string [ ] options )
176144 {
177- string [ ] args = new string [ ]
145+ var args = new [ ]
178146 {
179147 "-c" , command ,
180148 "-u" , username ,
@@ -185,12 +153,12 @@ public static DataFreshConsole Execute(string command, string username, string p
185153
186154 if ( options != null && options . Length > 1 )
187155 {
188- string argsString = string . Join ( "|" , args ) ;
189- string optionsString = string . Join ( "|" , options ) ;
156+ var argsString = string . Join ( "|" , args ) ;
157+ var optionsString = string . Join ( "|" , options ) ;
190158 args = string . Format ( argsString + "|" + optionsString ) . Split ( '|' ) ;
191159 }
192160
193- DataFreshConsole console = new DataFreshConsole ( ) ;
161+ var console = new DataFreshConsole ( ) ;
194162 console . Start ( args ) ;
195163 return console ;
196164 }
0 commit comments