//******************************************************************************************************
// SqlGenerator.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 06/28/2010 - J. Ritchie Carroll
// Generated original version of source code.
// 08/15/2008 - Mihir Brahmbhatt
// Converted to C# extensions.
// 09/27/2010 - Mihir Brahmbhatt
// Edited code comments.
// 12/20/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections;
using System.Data;
using System.Text;
namespace DataMigrationUtility
{
///
/// Common database functions
///
public static class Common
{
///
/// Converts value to string, null objects (or DBNull objects) will return an empty string ("").
///
/// Value of to convert to string.
/// to return if is null.
/// as a string; if is null, will be returned.
public static string NotNull(object value, string nonNullValue = null)
{
if ((object)nonNullValue == null)
return GSF.Common.ToNonNullString(value);
return GSF.Common.ToNonNullString(value, nonNullValue);
}
}
///
/// Generates a SQL statement for data processing.
///
public class SQLGenerator
{
#region [ Members ]
private Schema m_sourceSchema;
private Table m_currentTable;
private readonly ArrayList m_fieldList = new ArrayList();
private string m_whereSQL;
private bool m_includeNulls;
#endregion
#region [ Constructors ]
public SQLGenerator()
{
m_whereSQL = "";
m_includeNulls = false;
}
public SQLGenerator(Schema SourceSchema)
{
m_sourceSchema = SourceSchema;
}
#endregion
#region [ Properties ]
///
/// Get or set Source object to use for SQL generation.
///
public Schema SourceSchema
{
get
{
return m_sourceSchema;
}
set
{
m_sourceSchema = value;
}
}
///
/// Get or set - WHERE clause SQL to append to generated SQL statements.
///
public string WhereSQL
{
get
{
return m_whereSQL;
}
set
{
m_whereSQL = value;
}
}
///
/// Get or set - to include NULL values in generated SQL statements.
///
public bool IncludeNulls
{
get
{
return m_includeNulls;
}
set
{
m_includeNulls = value;
}
}
///
/// Get setting for source
///
public IDbConnection Connection
{
get
{
return m_sourceSchema.Connection;
}
}
///
/// Get or set source table name.
///
public string TableName
{
get
{
return m_currentTable.Name;
}
set
{
// Lookup table in collection
m_currentTable = m_sourceSchema.Tables[value];
if ((object)m_currentTable != null)
{
// Clear any existing field values
// Field field = default(Field);
foreach (Field field in m_currentTable.Fields)
{
field.Value = null;
}
m_fieldList.Clear();
}
else
{
throw new InvalidOperationException("Table '" + value + "' not found in schema");
}
}
}
#endregion
#region [ Methods ]
///
/// set value to current table column value
///
/// Set to table's field value
/// Set value to of current table
public void SetField(string FieldName, object FieldValue)
{
m_currentTable.Fields[FieldName].Value = FieldValue;
if (m_fieldList.BinarySearch(FieldName, CaseInsensitiveComparer.Default) < 0)
{
m_fieldList.Add(FieldName);
m_fieldList.Sort(CaseInsensitiveComparer.Default);
}
}
///
/// Generate insert SQL statement
///
/// insert SQL statement for current table to process
public string InsertSQL()
{
StringBuilder insertSQL = new StringBuilder();
string fieldName;
bool setComma = false;
insertSQL.Append("INSERT INTO ");
insertSQL.Append(m_currentTable.SQLEscapedName);
insertSQL.Append(" (");
foreach (string currentFieldName in m_fieldList)
{
fieldName = currentFieldName;
if (m_includeNulls || !Convert.IsDBNull(m_currentTable.Fields[fieldName].Value))
{
if (setComma)
insertSQL.Append(", ");
insertSQL.Append(m_currentTable.Fields[fieldName].SQLEscapedName);
setComma = true;
}
}
insertSQL.Append(") VALUES (");
setComma = false;
foreach (string currentFieldName in m_fieldList)
{
fieldName = currentFieldName;
if (m_includeNulls || !Convert.IsDBNull(m_currentTable.Fields[fieldName].Value))
{
if (setComma)
insertSQL.Append(", ");
insertSQL.Append(m_currentTable.Fields[fieldName].SQLEncodedValue);
setComma = true;
}
}
insertSQL.Append(")");
if (!setComma)
throw new InvalidOperationException("No field values were specified in insert SQL");
if (m_whereSQL.Length > 0)
{
insertSQL.Append(" ");
insertSQL.Append(m_whereSQL);
}
return insertSQL.ToString();
}
///
/// Generate update SQL statement
///
/// Update SQL statement to data processing
public string UpdateSQL()
{
StringBuilder updateSQL = new StringBuilder();
string fieldName;
bool setComma = false;
updateSQL.Append("UPDATE ");
updateSQL.Append(m_currentTable.SQLEscapedName);
updateSQL.Append(" SET ");
foreach (string currentFieldName in m_fieldList)
{
fieldName = currentFieldName;
if (m_includeNulls || !Convert.IsDBNull(m_currentTable.Fields[fieldName].Value))
{
if (setComma)
updateSQL.Append(", ");
updateSQL.Append(m_currentTable.Fields[fieldName].SQLEscapedName);
updateSQL.Append(" = ");
updateSQL.Append(m_currentTable.Fields[fieldName].SQLEncodedValue);
setComma = true;
}
}
if (!setComma)
throw new InvalidOperationException("No field values were specified in update SQL");
if (m_whereSQL.Length > 0)
{
updateSQL.Append(" ");
updateSQL.Append(m_whereSQL);
}
return updateSQL.ToString();
}
#endregion
}
}