forked from ServiceStack/ServiceStack.OrmLite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgreSQLDialectProvider.cs
More file actions
159 lines (139 loc) · 4.06 KB
/
Copy pathPostgreSQLDialectProvider.cs
File metadata and controls
159 lines (139 loc) · 4.06 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Npgsql;
namespace ServiceStack.OrmLite.PostgreSQL
{
public class PostgreSQLDialectProvider : OrmLiteDialectProviderBase<PostgreSQLDialectProvider>
{
public static PostgreSQLDialectProvider Instance = new PostgreSQLDialectProvider();
private PostgreSQLDialectProvider()
{
base.AutoIncrementDefinition = "";
base.IntColumnDefinition = "integer";
base.BoolColumnDefinition = "boolean";
base.TimeColumnDefinition = "time";
base.DateTimeColumnDefinition = "timestamp";
base.DecimalColumnDefinition = "numeric(38,6)";
base.GuidColumnDefinition = "uuid";
base.ParamString = ":";
base.BlobColumnDefinition = "bytea";
base.RealColumnDefinition = "double precision";
base.StringLengthColumnDefinitionFormat = "text";
base.InitColumnTypeMap();
DbTypeMap.Set<TimeSpan>(DbType.Time, "Interval");
DbTypeMap.Set<TimeSpan?>(DbType.Time, "Interval");
}
public override string GetColumnDefinition(
string fieldName,
Type fieldType,
bool isPrimaryKey,
bool autoIncrement,
bool isNullable,
int? fieldLength,
int? scale,
string defaultValue)
{
string fieldDefinition = null;
if (fieldType == typeof(string))
{
if (fieldLength != null)
{
fieldDefinition = UseUnicode
? string.Format(base.StringLengthUnicodeColumnDefinitionFormat, fieldLength)
: string.Format(base.StringLengthNonUnicodeColumnDefinitionFormat, fieldLength);
}
else
{
fieldDefinition = StringLengthColumnDefinitionFormat;
}
}
else
{
if (autoIncrement)
{
if (fieldType == typeof(long))
fieldDefinition = "bigserial";
else if (fieldType == typeof(int))
fieldDefinition = "serial";
}
else
{
fieldDefinition = GetColumnTypeDefinition(fieldType);
}
}
var sql = new StringBuilder();
sql.AppendFormat("{0} {1}", GetQuotedColumnName(fieldName), fieldDefinition);
if (isPrimaryKey)
{
sql.Append(" PRIMARY KEY");
}
else
{
if (isNullable)
{
sql.Append(" NULL");
}
else
{
sql.Append(" NOT NULL");
}
}
if (!string.IsNullOrEmpty(defaultValue))
{
sql.AppendFormat(DefaultValueFormat, defaultValue);
}
return sql.ToString();
}
public override string EscapeParam(object paramValue)
{
return paramValue.ToString().Replace("'", @"''");
}
public override IDbConnection CreateConnection(string connectionString, Dictionary<string, string> options)
{
return new NpgsqlConnection(connectionString);
}
public override string GetQuotedValue(object value, Type fieldType)
{
if (value == null) return "NULL";
if (fieldType == typeof(DateTime))
{
var dateValue = (DateTime)value;
const string iso8601Format = "yyyy-MM-dd HH:mm:ss.fff";
return base.GetQuotedValue(dateValue.ToString(iso8601Format), typeof(string));
}
if (fieldType == typeof(Guid))
{
var guidValue = (Guid)value;
return base.GetQuotedValue(guidValue.ToString("N"), typeof(string));
}
return base.GetQuotedValue(value, fieldType);
}
public override object ConvertDbValue(object value, Type type)
{
if (value == null || value is DBNull) return null;
return base.ConvertDbValue(value, type);
}
public override long GetLastInsertId(IDbCommand command)
{
command.CommandText = "SELECT LASTVAL()";
var result = command.ExecuteScalar();
if (result is DBNull)
return default(long);
return Convert.ToInt64(result);
}
public override SqlExpressionVisitor<T> ExpressionVisitor<T>()
{
return new PostgreSQLExpressionVisitor<T>();
}
public override bool DoesTableExist(IDbCommand dbCmd, string tableName)
{
var sql = "SELECT COUNT(*) FROM pg_class WHERE relname = {0}"
.SqlFormat(tableName);
dbCmd.CommandText = sql;
var result = dbCmd.GetLongScalar();
return result > 0;
}
}
}