d e v o n

No title available
almost home

Product Placement
ojovivo
taylor price
KIROKAZE
No title available
dirt enthusiast

roma★
"I'm Dorothy Gale from Kansas"

★
sheepfilms
Monterey Bay Aquarium
hello vonnie

JVL
Peter Solarz
Aqua Utopia|海の底で記憶を紡ぐ
Three Goblin Art
trying on a metaphor

seen from Mexico

seen from United States
seen from Canada

seen from Türkiye

seen from Singapore

seen from United States
seen from United States

seen from United States
seen from United States
seen from United States

seen from United Kingdom

seen from United States

seen from United Kingdom

seen from Thailand
seen from United States

seen from United States
seen from United Kingdom
seen from United States

seen from Türkiye

seen from United States
@dave-tech
Scala - spray-json sorted pretty printer
I can't figure out how to get tumblr to embed the gist, but here it is: https://gist.github.com/dedels/6922eb7751e6b3742ded281bb3ece272
GraphQL.NET Delay Loading
On the GraphQL.NET documentation their is some discussion about Query Batching… I thought I’d try a proof of concept out myself! Turns out it is extremely easy. The real trick was that the resolve function need to return a Task. All tasks setup during a run should all have the same awaitable under the hood. In this example (with no actual external system) it is just a TaskCompletionSource.
https://gist.github.com/dedels/c41429e8e14092c459d70519e672d265
Update Windows Environment Variables from Powershell
Powershell makes it extremely easy to update the current session’s environmental variables by just reading and writing from $env:VARIABLE
But updating the systems default environment variables requires you to update the registry. Here’s the snippet to do that:
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' ` -Name VARIABLENAME ` -Value VARIABLEVALUE
SQL Server Stored Procedure Default Parameters and INFORMATION_SCHEMA
I’ve written a few different bits of custom code that look into INFORMATION_SCHEMA to find stored proc parameter names, types etc. This is useful for code-gen or for tracking changes etc. For some integration code generation, I needed to find if the sqlserver stored proc param had a default value or not. If it had a default value and my application tried to send null, then I want to omit the parameter in the generated query.
This piece of info is not in INFORMATION_SCHEMA. There is an intriguing column in sys.parameters called ‘has_default_value’ which looks perfect. However, according to the documentation, (and confirmed in my own application) this is always set to 0 unless the proc is a CLR object.
So, still needing a solution, I decided to parse out the arguments from the stored proc definition signature. Basically, what this snippet will allow you to do is find the position in the argument list of the stored proc and take all the chars up until the next token(,) or token(as). In my application, I only needed to test to see if the parameter contained an equals sign, but this query will actually return you the entire param snippet.
SELECT r.ROUTINE_SCHEMA, r.ROUTINE_NAME, substring(p.PARAMETER_NAME, 2, 100) as PARAMETER_NAME, p.ORDINAL_POSITION, p.DATA_TYPE, cast(substring(r.routine_definition, charindex(p.PARAMETER_NAME, r.ROUTINE_DEFINITION), case when p.ORDINAL_POSITION is null then charindex(p.PARAMETER_NAME, r.ROUTINE_DEFINITION) when p.ORDINAL_POSITION not IN ( select max(pinner.ORDINAL_POSITION) from INFORMATION_SCHEMA.PARAMETERS pinner where pinner.SPECIFIC_NAME=p.SPECIFIC_NAME and pinner.SPECIFIC_SCHEMA=p.SPECIFIC_SCHEMA ) then charindex(',', substring(r.routine_definition, charindex(p.PARAMETER_NAME, r.ROUTINE_DEFINITION), 99)) else patindex('%as%', substring(r.routine_definition, charindex(p.PARAMETER_NAME, r.ROUTINE_DEFINITION), 99)) end ) as varchar(100)) as ParamSnippet from INFORMATION_SCHEMA.ROUTINES r left join INFORMATION_SCHEMA.PARAMETERS p on p.SPECIFIC_NAME=r.ROUTINE_NAME where ROUTINE_TYPE='PROCEDURE' order by r.ROUTINE_NAME, p.ORDINAL_POSITION
Here is a test stored proc and its output:
create procedure dbo.TestDefaultParams @required1 int, @optional1 int=default, @optional2 int=9999, @optional3 varchar(10) = 'hellooooo', @required2 varchar(10) as select @required1, @optional1, @optional2, @optional3, @required2 go
dboTestDefaultParamsrequired11int@required1 int, dboTestDefaultParamsoptional12int@optional1 int=default, dboTestDefaultParamsoptional23int@optional2 int=9999, dboTestDefaultParamsoptional34varchar@optional3 varchar(10) = 'hellooooo', dboTestDefaultParamsrequired25varchar@required2 varchar(10) a
With a bit more script you could clean up the result snippet even more! Or just do any further parsing in your application
Use runas with credentials
As a security feature, the Windows command runas won’t let you include the username and password in the commandline. This means you can’t just create a batch file with your username and password and simply run that in order to do an integrated login across domains. Instead, runas forces you to type in your password at the prompt.
A co-worker of mine found an interesting workaround using a script. Just put the following into a .bat file
@if (@CodeSection == @Batch) @then @echo off start "" runas /netonly /user:DOMAIN\USER "C:\PATH-TO-PROGRAM" CScript //nologo //E:JScript "%~F0" goto :EOF @end WScript.CreateObject("WScript.Shell").SendKeys("PASSWORD{ENTER}");
Replace DOMAIN, USER, PASSWORD and PATH-TO-PROGRAM as appropriate for your application.
Remember! Runas has this security feature for a reason. Doing the above is probably not a good idea.
LINQ to SQL extension for ‘in’ operator
Using LINQ to SQL, if you have a list of ID’s you’d like to retrieve from the db, you could of course write the LINQ query directly
from item in dbtable where item.ID=123 || item.ID=234 || item.ID=345 || .... select item
This is do-able of course, but it requires that you statically know all the items you want to add ahead of time. If you instead have this in a list computed during runtime, you could try to join:
from id in new List<int>{1,2,3}; join item in dbtable on id equals item.ID select item
But this will run the query and then perform the filter on the client side.
Ideally, we'd like to send all of the elements in the list to the server for serverside filtering. If we want this in the where clause, this requires us to dynamically build a where clause. We can do that using Expressions. Basically we'd build an expression that said (item.ID=123 || (item.ID=234 || (item.ID=345 || ... and then set that as the new where clause to our query. This will cause the LINQ to SQL query generator to send all of those items to the server to include in the query.
Here is a sample of how to use this extension as well as the implementation
( from item in dbtable select item ).InList(new List{1,2,3}, itemselect=> itemselect.ID); //and if we have just a vector, we can omit the itemselector ( from item in dbtable select item.ID ).InList(new List<int>{1,2,3});
This will emit SQL similar to:
-- Region Parameters DECLARE @p0 Int = 1 DECLARE @p1 Int = 2 DECLARE @p2 Int = 3 -- EndRegion SELECT [t0].[ID], [t0].[Col1], [t0].[Col2], [t0].[CreateDate] FROM [dbo].[dbtable] AS [t0] WHERE ([t0].[ID] = @p0) OR ([t0].[ID] = @p1) OR ([t0].[ID] = @p2) GO
public static class SQLLinqExtension { //Build a new where clause for this queryable that checks if the column in the compareselector is equal to any item in the list public static IQueryable<QT> InList<QT, ListT>(this IQueryable<QT> source, IEnumerable<ListT> itemlist, Expression<Func<QT, ListT>> compareselector){ var param = Expression.Parameter(source.ElementType, "src"); var filt = null as Expression; foreach(var item in itemlist){ if(filt==null) filt = Expression.Equal( Expression.Invoke(compareselector, param), Expression.Constant(item)); else filt = Expression.OrElse( filt, Expression.Equal( Expression.Invoke(compareselector, param), Expression.Constant(item)) ); } return source.Provider.CreateQuery<QT>( Expression.Call(typeof(Queryable), "Where", new Type[] {source.ElementType}, source.Expression, Expression.Lambda<Func<QT, bool>>(filt, param)) ); } //Helper method when the queryable and the list to check have the same element type. In this case, the item selector returns the item public static IQueryable<ListT> InList<ListT>(this IQueryable<ListT> source, IEnumerable<ListT> itemlist){ return source.InList(itemlist, x=>x); } }
Pega BigDecimal
Pega’s Decimal type maps to a BigDecimal according to the documentation. However, it does not use java.math.BigDecimal
It uses IBM’s implementation - com.ibm.icu.math.BigDecimal
This part is not in the documentation. You need to use the correct full name if you use a parameter of this type, otherwise it will give you a cryptic message that BigDecimal doesn’t match BigDecimal.
Using Graphviz Wrapper from LinqPad
Install Graphviz for Windows and clone the git repository for the wrapper
Then, build the wrapper project.
In a new LinqPad query add the output of the build. And set the import namespaces to:
GraphVizWrapper GraphVizWrapper.Commands GraphVizWrapper.Queries
After all that is setup, you can now run this sample code from LinqPad:
var getStartProcessQuery = new GetStartProcessQuery(); var getProcessStartInfoQuery = new GetProcessStartInfoQuery(); var registerLayoutPluginCommand = new RegisterLayoutPluginCommand(getProcessStartInfoQuery, getStartProcessQuery); var wrapper = new GraphGeneration(getStartProcessQuery, getProcessStartInfoQuery, registerLayoutPluginCommand); wrapper.GraphvizPath = @"C:\Program Files (x86)\Graphviz2.38\bin"; wrapper.RenderingEngine = Enums.RenderingEngine.Dot; var output = wrapper.GenerateGraph( @"digraph{a -> b; b -> c; c -> a;}", Enums.GraphReturnType.Png); Util.RawHtml( String.Format( "<img src=\"data:image/jpg;base64,{0}\" />", Convert.ToBase64String(output))).Dump();
SQL Server Tip - SET NOEXEC ON
https://msdn.microsoft.com/en-us/library/ms188394.aspx
SET NOEXEC ON
Sometimes its easy to work on a stored proc by pasting a bunch of code into the stored proc body and then removing params that are unused as the query compiler complains. However, if your script includes a drop procedure in it, you might lose your procedure before you've figured out how to get the new version to compile.
Setting NOEXEC ON lets you enjoy the warning messages without actually running the drop or create. When you've fixed your stored proc and it stops getting error messages, you can remove the NOEXEC ON line and run the script to completion.
Get currently running SQL statement without VIEW SERVER STATE permission (SQL Server)
Googling this first suggests that you find the current user using sp_who and then checking the results of:
SELECT * FROM sys.dm_exec_requests CROSS APPLY sys.dm_exec_sql_text(sql_handle)
If you do not hold the SQL server VIEW SERVER STATE permission, it will deny this query.
However, you can use this query to get the currently running SQL statement even without that permission!
dbcc inputbuffer(@@SPID) WITH NO_INFOMSGS
This will return a result set that contains a column with the currently running query!
The WITH NO_INFOMSGS part is optional but will stop the statement from printing.
“If you learn only methods, you’ll be tied to your methods. But if you learn principles, you can devise your own methods.” —Ralph Waldo Emerson
Let’s Build A Web Server. Part 3. - Ruslan's Blog
C# - How to delete cookie from .Net - Stack Overflow
To remove a cookie, remove it from the response, then add a new cookie with a blank value that has already expired. I'm not sure why Response.Cookies.Remove doesn't get this to work. But the following does:
HttpCookie currentUserCookie = HttpContext.Current.Request.Cookies["currentUser"]; HttpContext.Current.Response.Cookies.Remove("currentUser"); currentUserCookie.Expires = DateTime.Now.AddDays(-10); currentUserCookie.Value = null; HttpContext.Current.Response.SetCookie(currentUserCookie);
(via c# - How to delete cookie from .Net - Stack Overflow)
MSDN
A complex system that works is invariably found to have evolved from a simple system that worked.
Quote from John Gall as seen on Lessons Learned in Software Development | Henrik Warne's blog
C# Read Lines from a transfer-encoding chunked HTTP stream
Using System.Net.HttpWebRequest to hit a remote server is easy, and typically you want to be notified when the response is ready before processing. However, couchdb has a continuous changes feed available that will leave the connection open. And send the header Transfer-Encoding: chunked
.
To look at the data exchanged in this HTTP response, use CometPeek in fiddler so that the streamed contents are easily viewed. In the response stream, each update encountered by the couchdb server will send one chunked reply prefixed by the content-length and terminted with \r\n\r\n
.Net's HttpWebRequest consumes these messages transparently, so when you read from the stream directly you won't see content length or the extra line breaks. Your code can simply read directly from the stream
.
Now on to a hard lesson learned when reading from this ResponseStream. In the callback to BeginGetResponse you will call EndGetResponse to get the response. Using that your code will GetResponseStream. With this response stream, you can simply ReadBytes while !endOfStream. And this all works with no extra wrestling. However, since couchdb conveniently sends single line messages, I wanted to use System.IO.StreamReader so that I could simply ReadLine each couchdb document. However! if the wrapped response.GetResponseStream() in a StreamReader gets to the end of the stream it actually closes the connection! To get around this, use the infrequently used (as far as I know) constructor for StreamReader that will allow you to ask it to keep the underlying stream open.
msdn
public StreamReader( Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen )
Here is some simple example usage (using still works correctly here, but is possibly unnecessary as we are trying to not close anything):
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8, true, 1024, true))
Use Suave.IO to proxy to another server
let build_proxy_resolver (fwd_to_host : String) fwd_to_port = let heserver = System.Net.Dns.GetHostEntry(fwd_to_host) let ipaddr = heserver.AddressList.[0] fun (request : HttpRequest) -> Some (ipaddr, fwd_to_port) let build_headers ctx = //add and remove headers from the ctx, return the header list ctx.request.headers let proxy_app (ctx:HttpContext) = let new_headers = build_headers ctx let fwd_ctx = {ctx with request={ctx.request with headers=new_headers}} let pxy = proxy (build_proxy_resolver "PROXY_TO.com" 80us) fwd_ctx {ctx with response = { ctx.response with status=Suave.Types.Codes.HTTP_200; content=SocketTask pxy }} |> Some
To use in an actual application, proxy_app can be inserted directly into the routing chain >>=
build_headers should be extended to add authorization headers or rewrite the host header as necessary. Also, build_proxy_resolver should only be called once so the IP lookup can be cached.
I plan on using something like this to serve as a reverse proxy to couchdb and then do my own security model in F# and Suave.
Suave.IO vdir to run choose routes relative to a path
let vdir (s : String) choose_list (ctx : HttpContext) = if ctx.request.url.StartsWith(s) then let us = if ctx.user_state.ContainsKey("original_url") then ctx.user_state else ctx.user_state.Add("original_url", ctx.request.url) let new_req = { ctx with request={ctx.request with url=ctx.request.url.Substring(s.Length)} ; user_state = us } in choose choose_list new_req else None let subapp = vdir "/sub" [ url "/abc" >>= OK "subapp abc route" warbler(fun {request=req ; user_state=us} -> OK ( sprintf "subapp - url %s - original url %A" req.url us)) ]
Now when subapp is loaded inside an application's choose chain, if /sub/xyz is visited, the browser will print: subapp - url /xyz - original url map [("original_url", "/sub/xyz")]
Is this an abuse of the user_state list maintained by Suave?
As an alternative, we can define the vdir function to take a WebPart:
let vdir (s : String) (app : WebPart) (ctx : HttpContext) = if ctx.request.url.StartsWith(s) then let us = if ctx.user_state.ContainsKey("original_url") then ctx.user_state else ctx.user_state.Add("original_url", ctx.request.url) let new_req = { ctx with request={ctx.request with url=ctx.request.url.Substring(s.Length)} ; user_state = us } in app new_req else None let subapp = vdir "/sub" <| choose [ url "/abc" >>= OK "def" warbler(fun {request=req ; user_state=us} -> OK ( sprintf "subapp with choose - url %s - original url %A" req.url us)) ]