1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Data ;
4
+ using System . Data . Common ;
4
5
5
6
using PCAxis . Sql . DbClient ;
6
7
using PCAxis . Sql . DbConfig ;
@@ -12,27 +13,27 @@ internal static class GroupingRepositoryStatic
12
13
{
13
14
private static readonly List < string > LanguagesInDbConfig ;
14
15
15
- private static readonly Dictionary < string , List < string > > SqlquerysByLanguage ;
16
+ private static readonly Dictionary < string , string > ValuesQueryByLanguage = new Dictionary < string , string > ( ) ;
17
+ private static readonly string ExistsInLangQuery ;
18
+ private static readonly AbstractQueries Queries ;
19
+
20
+
21
+
16
22
static GroupingRepositoryStatic ( )
17
23
{
18
24
LanguagesInDbConfig = SqlDbConfigsStatic . DefaultDatabase . ListAllLanguages ( ) ;
19
25
20
- //Prepares the sqls. 3 per language.
21
- SqlquerysByLanguage = new Dictionary < string , List < string > > ( ) ;
22
-
23
26
var config = SqlDbConfigsStatic . DefaultDatabase ;
24
- AbstractQueries queries = AbstractQueries . GetSqlqueries ( config ) ;
27
+ Queries = AbstractQueries . GetSqlqueries ( config ) ;
25
28
26
29
InfoForDbConnection info = config . GetInfoForDbConnection ( config . GetDefaultConnString ( ) ) ;
27
30
var sqlCommand = new PxSqlCommandForTempTables ( info . DataBaseType , info . DataProvider , info . ConnectionString ) ;
28
31
32
+ ExistsInLangQuery = GetValuesetExistsInLangSql ( Queries , sqlCommand ) ;
33
+
29
34
foreach ( var language in LanguagesInDbConfig )
30
35
{
31
- string sqlGrouping = queries . GetGroupingQuery ( language , sqlCommand ) ;
32
- string sqlValues = queries . GetGroupingValuesQuery ( language , sqlCommand ) ; ;
33
- string sqlGroupingExistsInLang = GetOtherLanguagesSql ( queries , language , config , sqlCommand ) ;
34
- List < string > sqlquerys = new List < string > { sqlGrouping , sqlValues , sqlGroupingExistsInLang } ;
35
- SqlquerysByLanguage [ language ] = sqlquerys ;
36
+ ValuesQueryByLanguage [ language ] = Queries . GetGroupingValuesQuery ( language , sqlCommand ) ;
36
37
}
37
38
38
39
}
@@ -45,77 +46,64 @@ internal static Models.Grouping GetGrouping(string groupingId, string language)
45
46
return null ;
46
47
}
47
48
48
- string sqlGrouping = SqlquerysByLanguage [ language ] [ 0 ] ;
49
- string sqlValues = SqlquerysByLanguage [ language ] [ 1 ] ;
50
- string sqlGroupingExistsInLang = SqlquerysByLanguage [ language ] [ 2 ] ;
51
-
52
49
var config = SqlDbConfigsStatic . DefaultDatabase ;
53
50
InfoForDbConnection info = config . GetInfoForDbConnection ( config . GetDefaultConnString ( ) ) ;
54
51
var cmd = new PxSqlCommandForTempTables ( info . DataBaseType , info . DataProvider , info . ConnectionString ) ;
55
52
56
- System . Data . Common . DbParameter [ ] parameters = new System . Data . Common . DbParameter [ 1 ] ;
57
- parameters [ 0 ] = cmd . GetStringParameter ( "aGrouping" , groupingId ) ;
53
+ DbParameter [ ] parameters = GetParameters ( groupingId , cmd ) ;
54
+ DataSet ExistsInLangDS = cmd . ExecuteSelect ( ExistsInLangQuery , parameters ) ;
55
+ var availableLanguages = AbstractQueries . ParseLangs ( ExistsInLangDS ) ;
58
56
59
- var groupingDS = cmd . ExecuteSelect ( sqlGrouping , parameters ) ;
60
-
61
- //Oracle works without the 2 next lines, but mssql does not.
62
- parameters = new System . Data . Common . DbParameter [ 1 ] ;
63
- parameters [ 0 ] = cmd . GetStringParameter ( "aGrouping" , groupingId ) ;
57
+ if ( ! availableLanguages . Contains ( language ) )
58
+ {
59
+ throw new ApplicationException ( "Grouping " + groupingId + " not found for language " + language ) ;
60
+ }
64
61
62
+ string sqlValues = ValuesQueryByLanguage [ language ] ;
63
+ parameters = GetParameters ( groupingId , cmd ) ;
65
64
var valuesDS = cmd . ExecuteSelect ( sqlValues , parameters ) ;
66
65
67
- //Oracle works without the 2 next lines, but mssql does not.
68
- parameters = new System . Data . Common . DbParameter [ 1 ] ;
69
- parameters [ 0 ] = cmd . GetStringParameter ( "aGrouping" , groupingId ) ;
70
-
71
- DataSet extraLangsDS = String . IsNullOrEmpty ( sqlGroupingExistsInLang ) ? null : cmd . ExecuteSelect ( sqlGroupingExistsInLang , parameters ) ;
66
+ List < GroupedValue > groupedValuesMaybe = ParseValues ( groupingId , valuesDS ) ;
72
67
68
+ Models . Grouping myOut = Queries . FixGrouping ( language , groupingId , groupedValuesMaybe ) ;
73
69
74
- Models . Grouping grouping = Parse ( groupingId , groupingDS , valuesDS , extraLangsDS ) ;
70
+ myOut . AvailableLanguages . AddRange ( availableLanguages ) ;
75
71
76
- //Adding langs we know exists without checking the DB
77
- grouping . AvailableLanguages . Add ( config . MainLanguage . code ) ;
78
- if ( ! config . MainLanguage . code . Equals ( language ) )
79
- {
80
- grouping . AvailableLanguages . Add ( language ) ;
81
- }
72
+ return myOut ;
73
+ }
82
74
83
- return grouping ;
75
+ private static DbParameter [ ] GetParameters ( string groupingId , PxSqlCommandForTempTables cmd )
76
+ {
77
+ System . Data . Common . DbParameter [ ] parameters = new System . Data . Common . DbParameter [ 1 ] ;
78
+ parameters [ 0 ] = cmd . GetStringParameter ( "aGrouping" , groupingId ) ;
79
+ return parameters ;
84
80
}
85
81
86
- private static string GetOtherLanguagesSql ( AbstractQueries queries , string language , SqlDbConfig config , PxSqlCommand sqlCommand )
82
+ private static string GetValuesetExistsInLangSql ( AbstractQueries queries , PxSqlCommand sqlCommand )
87
83
{
88
84
string sqlGroupingExistsInLang = String . Empty ;
89
85
string glue = String . Empty ;
90
86
foreach ( var lang in LanguagesInDbConfig )
91
87
{
92
- if ( ! lang . Equals ( config . MainLanguage . code ) && ! lang . Equals ( language ) )
93
- {
94
- //skips: config.MainLanguage has to exist and language will fail in GetValueSetQuery if vaulset is not translated
88
+ sqlGroupingExistsInLang += glue + queries . GetGroupingExistsIn ( lang , sqlCommand ) ;
89
+ glue = " UNION " ;
95
90
96
- sqlGroupingExistsInLang += glue + queries . GetGroupingExistsIn ( lang , sqlCommand ) ;
97
- glue = " UNION " ;
98
- }
99
91
}
100
92
return sqlGroupingExistsInLang ;
101
-
102
-
103
93
}
104
94
105
- private static Models . Grouping Parse ( string groupingId , DataSet groupingDS , DataSet valuesDS , DataSet extraLangsDS )
95
+ private static List < GroupedValue > ParseValues ( string groupingId , DataSet valuesDS )
106
96
{
107
97
//Make sure we have a grouping
108
- if ( groupingDS . Tables . Count == 0 || groupingDS . Tables [ 0 ] . Rows . Count < 1 || valuesDS . Tables . Count == 0 )
98
+ if ( valuesDS . Tables . Count == 0 )
109
99
{
110
- throw new ApplicationException ( "Grouping " + groupingId + " not found or empty" ) ;
100
+ throw new ApplicationException ( "Grouping " + groupingId + " empty" ) ;
111
101
}
112
102
113
- var grouping = new PCAxis . Sql . Models . Grouping ( ) ;
114
- grouping . Id = groupingDS . Tables [ 0 ] . Rows [ 0 ] [ 0 ] . ToString ( ) ;
115
- grouping . Label = groupingDS . Tables [ 0 ] . Rows [ 0 ] [ 1 ] . ToString ( ) ;
116
-
103
+ List < GroupedValue > myOut = new List < GroupedValue > ( ) ;
117
104
118
105
var values = new Dictionary < string , GroupedValue > ( ) ;
106
+
119
107
for ( int i = 0 ; i < valuesDS . Tables [ 0 ] . Rows . Count ; i ++ )
120
108
{
121
109
string groupCode = valuesDS . Tables [ 0 ] . Rows [ i ] [ 0 ] . ToString ( ) ;
@@ -130,24 +118,16 @@ private static Models.Grouping Parse(string groupingId, DataSet groupingDS, Data
130
118
gValue = new GroupedValue ( ) ;
131
119
gValue . Code = groupCode ;
132
120
gValue . Text = valuesDS . Tables [ 0 ] . Rows [ i ] [ 2 ] == DBNull . Value ? valuesDS . Tables [ 0 ] . Rows [ i ] [ 3 ] . ToString ( ) : valuesDS . Tables [ 0 ] . Rows [ i ] [ 2 ] . ToString ( ) ;
133
- grouping . Values . Add ( gValue ) ;
121
+ myOut . Add ( gValue ) ;
134
122
values . Add ( groupCode , gValue ) ;
135
123
}
136
124
gValue . Codes . Add ( valuesDS . Tables [ 0 ] . Rows [ i ] [ 1 ] . ToString ( ) ) ;
137
125
}
138
126
139
- if ( extraLangsDS != null )
140
- {
141
127
142
- for ( int i = 0 ; i < extraLangsDS . Tables [ 0 ] . Rows . Count ; i ++ )
143
- {
144
- var lang = extraLangsDS . Tables [ 0 ] . Rows [ i ] [ 0 ] . ToString ( ) ;
145
- grouping . AvailableLanguages . Add ( lang ) ;
146
- }
147
- }
148
128
149
129
150
- return grouping ;
130
+ return myOut ;
151
131
}
152
132
153
133
0 commit comments