Example #1
0
        public static void CreateTable(this CrateConnection conn, Type t)
        {
            var tabledata = AttributeHelpers.GetTableData(t);
            var tablecols = CrateFieldCacheProvider.Instance.Get(t);

            var sb = new StringBuilder();

            sb.Append("CREATE TABLE ").Append(tabledata.Name).Append(" ( ");
            sb.Append(LoopObject(t));

            var pk = tablecols.Where(tc => tc.Value.PrimaryKey).Select(tc => tc.Value.Name).ToArray();

            if (pk.Any())
            {
                sb.Append("primary key (").Append(string.Join(" ,", pk)).Append(" )");
            }
            else
            {
                sb.Remove(sb.Length - 2, 2);
            }
            sb.Append(" ) ");

            sb.Append(string.Format("clustered into {0} shards ", tabledata.NumberOfShards));
            sb.Append(string.Format(" with (number_of_replicas = '{0}') ", EnumHelpers.Replication(tabledata.NumberOfReplicas)));

            var sql = sb.ToString();

            using (var cmd = new CrateCommand(sql, conn))
            {
                cmd.ExecuteNonQuery();
            }
        }
Example #2
0
        public static void DropTable(this CrateConnection conn, string name)
        {
            var sql = "DROP TABLE" + name;

            using (var cmd = new CrateCommand(sql, conn))
            {
                cmd.ExecuteNonQuery();
            }
        }
        public static int Delete <T>(this CrateConnection conn, Expression <Func <T, bool> > exp)
        {
            var table = AttributeHelpers.GetTableData(typeof(T));

            var swhere = "DELETE FROM " + table.Name + " WHERE " + (new WhereTranslater()).Translate(exp);

            using (var cmd = new CrateCommand(swhere, conn))
            {
                return(cmd.ExecuteNonQuery());
            }
        }
Example #4
0
        public static bool CheckIfTableExists(this CrateConnection conn, string name)
        {
            var ret = new List <Table>();

            var sql = "SELECT * FROM information_schema.tables WHERE table_name= '" + name + "'";

            using (var cmd = new CrateCommand(sql, conn))
            {
                var reader = (CrateDataReader)cmd.ExecuteReader();
                ret = reader.ToList <Table>();
            }

            return(ret.Count > 0);
        }
Example #5
0
        public static List <T> Where <T>(this CrateConnection cnn, Expression <Func <T, bool> > exp) where T : class, new()
        {
            List <T> ret;

            var table = AttributeHelpers.GetTableData(typeof(T));

            var swhere = "SELECT * FROM " + table.Name + " WHERE " + (new WhereTranslater()).Translate(exp);

            using (var cmd = new CrateCommand(swhere, cnn))
            {
                var reader = (CrateDataReader)cmd.ExecuteReader();
                ret = reader.ToList <T>();
            }

            return(ret);
        }
Example #6
0
        public static List <TOut> Execute <T, TOut>(this CrateConnection cnn, Expression <Func <List <T>, IEnumerable <TOut> > > exp)
            where T : class, new()
            where TOut : class, new()
        {
            List <TOut> ret;

            var table = AttributeHelpers.GetTableData(typeof(T));

            var sql = (new Translater(table.Name)).Translate(exp);

            using (var cmd = new CrateCommand(sql, cnn))
            {
                var reader = (CrateDataReader)cmd.ExecuteReader();
                ret = reader.ToList <TOut>();
            }

            return(ret);
        }
Example #7
0
        public static long Count <T>(this CrateConnection cnn, Expression <Func <T, bool> > exp = null) where T : class, new()
        {
            long ret   = 0;
            var  table = AttributeHelpers.GetTableData(typeof(T));

            var swhere = "SELECT count(*) FROM " + table.Name;

            if (exp != null)
            {
                swhere += " WHERE " + (new WhereTranslater()).Translate(exp);
            }

            using (var cmd = new CrateCommand(swhere, cnn))
            {
                var reader = (CrateDataReader)cmd.ExecuteReader();
                if (reader.Read())
                {
                    ret = reader.GetLong(0);
                }
            }

            return(ret);
        }