aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Synchronization/ExtensionMethods.cs
blob: 7f16f700644d67391ae0c73c2b96d54c53bebd76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using Tango.Synchronization;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.Synchronization
{
    internal static class SQLExtensions
    {
        /// <summary>
        /// Gets the type of the SQL column.
        /// </summary>
        /// <param name="column">The column.</param>
        /// <returns></returns>
        public static String GetSQLType(this DataColumn column)
        {
            return column.ExtendedProperties[Constants.COLUMN_TYPE].ToString();
        }

        /// <summary>
        /// Gets the information table.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <returns></returns>
        public static DataTable GetInfoTable(this DataTable table)
        {
            return table.ExtendedProperties[Constants.TABLE_INFO] as DataTable;
        }

        /// <summary>
        /// Determines whether column is marked NOT NULL.
        /// </summary>
        /// <param name="column">The column.</param>
        public static bool IsNotNull(this DataColumn column)
        {
            int value = int.Parse(column.ExtendedProperties[Constants.IS_NOT_NULL].ToString());
            return value == 0 ? false : true;
        }

        /// <summary>
        /// Gets the column default value.
        /// </summary>
        /// <param name="column">The column.</param>
        /// <returns></returns>
        public static String GetDefaultValue(this DataColumn column)
        {
            String def = column.ExtendedProperties[Constants.DEFAULT_VALUE].ToString();

            double num = -1;

            if (double.TryParse(def, out num))
            {
                return num.ToString();
            }
            else
            {
                if (def.Contains("randomblob") || def.Contains("localtime"))
                {
                    def = def.Insert(0, "(");
                    def = def.Insert(def.Length - 1, ")");
                }
                return def;
            }
        }

        /// <summary>
        /// Determines whether column definition has default value.
        /// </summary>
        /// <param name="column">The column.</param>
        public static bool HasDefaultValue(this DataColumn column)
        {
            return !String.IsNullOrWhiteSpace(column.ExtendedProperties[Constants.DEFAULT_VALUE].ToString());
        }

        /// <summary>
        /// Converts the DateTime instance to SQLite DateTime string.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static String ToSQLiteDateString(this DateTime date)
        {
            return date.ToString("yyyy-MM-dd HH:mm:ss.fff");
        }
    }
}