private void ProcessColumn(ColumnInfo ci, DbDialect dd)
 {
     string nullDefine = ci.AllowNull ? dd.NullString : dd.NotNullString;
     _sql.Append("\n\t");
     _sql.Append(dd.QuoteForColumnName(ci.Key));
     _sql.Append(" ");
     if (ci.IsDbGenerate && dd.IdentityTypeString != null)
     {
         _sql.Append(dd.IdentityTypeString);
     }
     else
     {
         _sql.Append(dd.GetTypeName(ci));
     }
     if (ci.IsDbGenerate)
     {
         _sql.Append(" ").Append(dd.IdentityColumnString);
     }
     if (ci.IsKey)
     {
         ProcessKeyColumn(ci, dd, nullDefine);
     }
     else
     {
         _sql.Append(nullDefine);
     }
     _sql.Append(",");
 }
 protected override SqlStatement ToSqlStatement(DbDialect dd, List<string> queryRequiredFields)
 {
     var sb = new StringBuilder("ALTER TABLE ");
     sb.Append(dd.QuoteForTableName(_from.MainTableName));
     if(AddColumn != null)
     {
         sb.Append(" ADD ");
         sb.Append(dd.QuoteForColumnName(AddColumn.Key));
         sb.Append(" ");
         sb.Append(dd.GetTypeName(AddColumn));
         if(DefaultValue != null)
         {
             if (DefaultFirst)
             {
                 sb.Append(" DEFAULT(").Append(DefaultValue).Append(")");
                 sb.Append(AddColumn.AllowNull ? " NULL" : " NOT NULL");
             }
             else
             {
                 sb.Append(AddColumn.AllowNull ? " NULL" : " NOT NULL");
                 sb.Append(" DEFAULT(").Append(DefaultValue).Append(")");
             }
         }
         else
         {
             sb.Append(AddColumn.AllowNull ? " NULL" : " NOT NULL");
         }
         sb.Append(";");
     }
     return new SqlStatement(sb.ToString());
 }
        protected override SqlStatement ToSqlStatement(DbDialect dd, List<string> queryRequiredFields)
		{
			var dpc = new DataParameterCollection();
            string sqlString = string.Format(StatementTemplate, From.ToSqlText(dpc, dd), _valuesOptions.ToSqlText(dpc, dd));
			var sql = new SqlStatement(CommandType.Text, sqlString, dpc);
			return sql;
		}
        protected override SqlStatement ToSqlStatement(DbDialect dd, List<string> queryRequiredFields)
		{
			var dpc = new DataParameterCollection();
			string sqlString = string.Format(StatementTemplate, dd.QuoteForTableName(_tableName), Where.ToSqlText(dpc, dd, queryRequiredFields));
			var sql = new SqlStatement(CommandType.Text, sqlString, dpc);
			return sql;
		}
 private void ProcessColumns(DbDialect dd)
 {
     foreach (ColumnInfo ci in Columns)
     {
         ProcessColumn(ci, dd);
     }
 }
        protected override SqlStatement ToSqlStatement(DbDialect dd, List<string> queryRequiredFields)
        {
            CheckIsMutiKey(dd);
            _sql.Append("CREATE TABLE ");
            _sql.Append(dd.QuoteForTableName(TableName));
            _sql.Append(" (");

            ProcessColumns(dd);
            ProcessForeignKeys(dd);
            ProcessPrimaryKey();

            if (Columns.Count > 0)
            {
                _sql.Length--;
            }

            _sql.Append("\n);\n");

            if (HasOneDbGenKey())
            {
                _sql.Append(dd.GetCreateSequenceString(TableName));
            }
            // Create Index
            AddCreateIndexStatement(_sql, dd);
            return new SqlStatement(CommandType.Text, _sql.ToString());
        }
Beispiel #7
0
        public override string ToSqlText(DataParameterCollection dpc, DbDialect dd, List<string> queryRequiredFields)
		{
            if (_ic.SubClauseNotEmpty)
            {
                return string.Format("( NOT ({0}) )", _ic.ToSqlText(dpc, dd, queryRequiredFields));
            }
            return "";
		}
Beispiel #8
0
 protected override string GetValueString(DataParameterCollection dpc, DbDialect dd, KeyValue kv)
 {
     if(kv.Value == null)
     {
         return "NULL";
     }
     return dd.QuoteForColumnName((string)kv.Value);
 }
Beispiel #9
0
 protected DbDriver(DbDialect dialect, string name, string connectionString, string dbProviderFactoryName, AutoScheme autoScheme)
 {
     this.Name = name;
     this.ConnectionString = connectionString;
     this.Dialect = dialect;
     this.AutoScheme = autoScheme;
     ProviderFactory = CreateDbProviderFactory(dbProviderFactoryName);
 }
 public string ToSqlText(DataParameterCollection dpc, DbDialect dd, List<string> queryRequiredFields)
 {
     CheckInput();
     string sqlString = string.Format("SELECT {0} FROM {1}{2}{3}{4}",
         GetColumns(dd),
         From.ToSqlText(dpc, dd),
         Where.ToSqlText(dpc, dd, queryRequiredFields),
         IsGroupBy ? " GROUP BY " + GetFunctionArgs(dd) : "",
         (Order == null || Keys.Count == 0) ? "" : Order.ToSqlText(dpc, dd)
         );
     return sqlString;
 }
Beispiel #11
0
        public string ToSqlText(DataParameterCollection dpc, DbDialect dd, List<string> queryRequiredFields)
		{
			if ( _ic != null )
			{
				string s = _ic.ToSqlText(dpc, dd, queryRequiredFields);
                if (s != null)
                {
                    if (queryRequiredFields != null && !dpc.FindQueryRequiedFieldOrId)
                    {
                        throw new DataException("The QueryRequired fields not found in query.");
                    }
                    return (s.Length > 0) ? " WHERE " + s : "";
                }
			}
			return "";
		}
Beispiel #12
0
        public override string ToSqlText(DataParameterCollection dpc, DbDialect dd, List<string> queryRequiredFields)
		{
			var sb = new StringBuilder();
			foreach ( Condition ic in _list )
			{
                if (ic.SubClauseNotEmpty)
                {
                    sb.Append("(");
                    sb.Append(ic.ToSqlText(dpc, dd, queryRequiredFields));
                    sb.Append(") ");
                    sb.Append(_condition);
                    sb.Append(" ");
                }
			}
			string s = sb.ToString();
			return ( s.Length > 5 ) ? s.Substring(0, s.Length - _condition.Length - 2) : "";
		}
 private void ProcessForeignKeys(DbDialect dd)
 {
     if(!DataSettings.UsingForeignKey)
     {
         return;
     }
     foreach (ColumnInfo ci in Columns)
     {
         if(ci.IsForeignKey)
         {
             _sql.Append("\n\tFOREIGN KEY(");
             _sql.Append(dd.QuoteForColumnName(ci.Key));
             _sql.Append(") REFERENCES ");
             _sql.Append(dd.QuoteForTableName(ci.BelongsToTableName));
             _sql.Append(" ([Id]) ,");
         }
     }
 }
Beispiel #14
0
 public string ToSqlText(DataParameterCollection dpc, Dialect.DbDialect dd)
 {
     if (OrderItems != null && OrderItems.Count > 0)
     {
         var sb = new StringBuilder(" ORDER BY ");
         foreach (ASC oi in OrderItems)
         {
             sb.Append(oi.ToString(dd));
             sb.Append(",");
         }
         if (sb.Length > 10)
         {
             sb.Length--;
         }
         return(sb.ToString());
     }
     return("");
 }
Beispiel #15
0
 public override string ToSqlText(DataParameterCollection dpc, DbDialect dd, List<string> queryRequiredFields)
 {
     if (queryRequiredFields != null && dpc.FindQueryRequiedFieldOrId == false)
     {
         if(_column == "Id" || queryRequiredFields.Contains(_column))
         {
             dpc.FindQueryRequiedFieldOrId = true;
         }
     }
     var sb = new StringBuilder();
     sb.Append(dd.QuoteForColumnName(_column));
     if (_notIn)
     {
         sb.Append(" NOT");
     }
     sb.Append(" IN (");
     if(_args.Length == 1 && _args[0].GetType() == typeof(SqlStatement))
     {
         sb.Append(((SqlStatement)_args[0]).SqlCommandText);
     }
     else if (_args.Length == 1 && _args[0].GetType() == typeof(SelectStatementBuilder))
     {
         sb.Append(((SelectStatementBuilder)_args[0]).ToSqlText(dpc, dd, queryRequiredFields));
     }
     else
     {
         foreach (var o in _args)
         {
             var v = GetValueString(dpc, dd, new KeyValue("in", o));
             sb.Append(v);
             sb.Append(",");
         }
         if (_args.Length > 0)
         {
             sb.Length--;
         }
     }
     sb.Append(")");
     return sb.ToString();
 }
Beispiel #16
0
 public override string ToSqlText(DataParameterCollection dpc, DbDialect dd, List<string> queryRequiredFields)
 {
     if (queryRequiredFields != null && dpc.FindQueryRequiedFieldOrId == false)
     {
         if (KV.Key == "Id" || queryRequiredFields.Contains(KV.Key))
         {
             dpc.FindQueryRequiedFieldOrId = true;
         }
     }
     string dpStr = GetValueString(dpc, dd, KV);
     string dkStr = dd.QuoteForColumnName(KV.Key);
     switch (function)
     {
         case ColumnFunction.ToLower:
             dkStr = string.Format("LOWER({0})", dkStr);
             break;
         case ColumnFunction.ToUpper:
             dkStr = string.Format("UPPER({0})", dkStr);
             break;
     }
     return string.Format("{0} {2} {1}", dkStr, dpStr, Comp);
 }
 protected abstract SqlStatement ToSqlStatement(DbDialect dd, List<string> queryRequiredFields);
        internal string GetColumns(DbDialect dd, bool includeOrigin, bool includeAlias)
		{
			var columns = new StringBuilder();
            if(IsDistinct)
            {
                columns.Append("DISTINCT ");
            }
			foreach ( var k in _keys )
			{
                AddColumn(dd, columns, includeOrigin, includeAlias, k);
			}
            //if(IsGroupBy)
            //{
            //    if (GroupbyArgs.Count != 0)
            //    {
            //        if (GroupbyArgs[0] == "*" || GroupbyArgs.Count > 1)
            //        {
            //        }
            //        else
            //        {
            //            string fa = GroupbyArgs[0];
            //            string fn = fa.StartsWith("DISTINCT ") ? fa : dd.QuoteForColumnName(fa);
            //            string gfn = FunctionName == "COUNT" ? DbEntry.CountColumn : fn;
            //            columns.Append("(")
            //                .Append(fn)
            //                .Append(") AS ").Append(gfn).Append(",");
            //        }
            //    }
            //}
            if (FunctionArgs.Count != 0)
            {
                columns.Append(FunctionName);
                if (FunctionArgs[0] == "*" || FunctionArgs.Count > 1)
                {
                    columns.Append("(*) AS ").Append(DbEntry.CountColumn).Append(",");
                }
                else
                {
                    string fa = FunctionArgs[0];
                    string fn = fa.StartsWith("DISTINCT ") ? fa : dd.QuoteForColumnName(fa);
                    string gfn = FunctionName == "COUNT" ? DbEntry.CountColumn : fn;
                    columns.Append("(")
                        .Append(fn)
                        .Append(") AS ").Append(gfn).Append(",");
                }
            }
            if (columns.Length > 0)
            {
                columns.Length--;
            }
            return columns.ToString();
		}
 private string GetFunctionArgs(DbDialect dd)
 {
     var ret = new StringBuilder();
     foreach (string s in GroupbyArgs)
     {
         ret.Append(dd.QuoteForColumnName(s));
         ret.Append(",");
     }
     if (ret.Length > 1)
     {
         ret.Length--;
     }
     return ret.ToString();
 }
        public SqlStatement ToSqlStatement(DbDialect dd, List<string> queryRequiredFields, bool needLog = true)
		{
		    var sql = ToSqlStatement(dd, queryRequiredFields);
		    sql.NeedLog = needLog;
		    return sql;
		}
Beispiel #21
0
 protected virtual string GetValueString(DataParameterCollection dpc, DbDialect dd, KeyValue kv)
 {
     if(kv.Value == null)
     {
         return "NULL";
     }
     string dpStr;
     if (DataSettings.UsingParameter)
     {
         dpStr = dd.QuoteParameter(string.Format("{0}_{1}", DataParameter.LegalKey(kv.Key), dpc.Count));
         var dp = new DataParameter(dpStr, kv.NullableValue, kv.ValueType);
         dpc.Add(dp);
     }
     else
     {
         dpStr = DataTypeParser.ParseToString(kv.Value, dd);
     }
     return dpStr;
 }
 private void ProcessKeyColumn(ColumnInfo ci, DbDialect dd, string nullDefine)
 {
     if (_isMutiKey)
     {
         _sql.Append(nullDefine);
     }
     else
     {
         if (ci.ValueType == typeof(Guid) || !dd.IdentityIncludePKString || !ci.IsDbGenerate)
         {
             if (!ci.IsDbGenerate)
             {
                 _sql.Append(nullDefine);
             }
             _sql.Append(" PRIMARY KEY");
         }
     }
 }
Beispiel #23
0
 public OracleDriver(DbDialect dialectClass, string name, string connectionString, string dbProviderFactoryName, AutoScheme autoScheme)
     : base(dialectClass, name, connectionString, dbProviderFactoryName, autoScheme)
 {
 }
        protected override SqlStatement ToSqlStatement(DbDialect dd, List<string> queryRequiredFields)
		{
            CheckInput();
            SqlStatement sql = GetSelectSqlStatement(dd, queryRequiredFields);
            if (_limit != null)
            {
                sql.StartIndex = _limit.StartIndex;
                sql.EndIndex = _limit.EndIndex;
            }
            return sql;
		}
 private SqlStatement GetSelectSqlStatement(DbDialect dd, List<string> queryRequiredFields)
 {
     SqlStatement sql = (Range == null) ?
         GetNormalSelectSqlStatement(dd, queryRequiredFields) :
         dd.GetPagedSelectSqlStatement(this, queryRequiredFields);
     sql.SqlCommandText += ";\n";
     return sql;
 }
Beispiel #26
0
 private object GetDefaultValue(MemberHandler member, DbDialect dialect)
 {
     if(member.Is.AllowNull)
     {
         return null;
     }
     var t = member.MemberType;
     if(t == typeof(string) || t == typeof(byte[]))
     {
         return dialect.EmptyString;
     }
     if(t == typeof(DateTime) || t == typeof(Date) || t == typeof(Time))
     {
         return dialect.QuoteDateTimeValue(dialect.DefaultDateTimeString());
     }
     if(t == typeof(Guid))
     {
         return "'" + Guid.NewGuid().ToString() + "'";
     }
     return 0;
 }
 private void CheckIsMutiKey(DbDialect dd)
 {
     int n = 0;
     foreach (ColumnInfo ci in Columns)
     {
         if (ci.IsKey)
         {
             n++;
             _keys += dd.QuoteForColumnName(ci.Key) + ", ";
         }
     }
     _isMutiKey = n > 1;
 }
	    private static void AddColumn(DbDialect dd, StringBuilder columns, bool includeOrigin, bool includeAlias, KeyValuePair<string, string> k)
	    {
	        if (includeOrigin)
	        {
	            columns.Append(dd.QuoteForColumnName(k.Key));
	            if (includeAlias && k.Value != null) { columns.Append(" AS "); }
	        }
	        if (includeAlias)
	        {
	            if (k.Value != null)
	            {
	                columns.Append(dd.QuoteForColumnName(k.Value));
	            }
	            else if (!includeOrigin)
	            {
	                columns.Append(dd.QuoteForColumnName(k.Key));
	            }
	        }
	        columns.Append(",");
	    }
 public SqlStatement GetNormalSelectSqlStatement(DbDialect dd, List<string> queryRequiredFields)
 {
     var dpc = new DataParameterCollection();
     var sqlString = ToSqlText(dpc, dd, queryRequiredFields);
     return new TimeConsumingSqlStatement(CommandType.Text, sqlString, dpc);
 }
	    internal string GetColumns(DbDialect dd)
        {
            return GetColumns(dd, true, true);
        }
 private void AddCreateIndexStatement(StringBuilder sb, DbDialect dd)
 {
     string prefix = "IX_" + TableName.Replace('.', '_') + "_";
     foreach (DbIndex i in Indexes)
     {
         string n = prefix;
         n += i.IndexName ?? i.Columns[0].Key;
         string gn = dd.GenIndexName(n);
         if(gn != null)
         {
             n = "IX_" + gn; 
         }
         sb.Append(i.Unique ? "CREATE UNIQUE " : "CREATE ");
         if (!dd.SupportDirctionOfEachColumnInIndex)
         {
             if (i.Columns[0] is DESC)
             {
                 sb.Append("DESC ");
             }
         }
         sb.Append("INDEX ");
         sb.Append(dd.QuoteForColumnName(n));
         sb.Append(" ON ");
         sb.Append(dd.QuoteForLimitTableName(TableName));
         sb.Append(" (");
         foreach (ASC c in i.Columns)
         {
             sb.Append(dd.SupportDirctionOfEachColumnInIndex ? c.ToString(dd) : dd.QuoteForColumnName(c.Key));
             sb.Append(", ");
         }
         if (i.Columns.Length > 0)
         {
             sb.Length -= 2;
         }
         sb.Append(");\n");
     }
 }