2017年9月25日月曜日

MYSQL カーソルのネスト

DELIMITER //
CREATE PROCEDURE test1()
BEGIN
  DECLARE not_found INT DEFAULT 0;
  DECLARE v_deptno INT;
  DECLARE v_dname VARCHAR(14);
  DECLARE cur1 CURSOR FOR SELECT deptno FROM emp ORDER BY empno;
  DECLARE cur2 CURSOR FOR SELECT dname FROM dept WHERE deptno = v_deptno;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET not_found = 1;
 
  OPEN cur1;
  loop1: LOOP
    FETCH cur1 INTO v_deptno;
    IF not_found THEN
      CLOSE cur1;
      LEAVE loop1;
    END IF;
   
    OPEN cur2;
    loop2: LOOP
      FETCH cur2 INTO v_dname;
      IF not_found THEN
        SET not_found = 0; ← フラグを戻す
        CLOSE cur2;
        LEAVE loop2;
      END IF;
      INSERT INTO work (dname) VALUES (v_dname);
    END LOOP;
  END LOOP;
END
//
DELIMITER ;

2017年9月15日金曜日

ASP.NET MVCのエラーをログファイルに出力する

ASP.NET MVCのエラーをログファイルに出力する。

HandleErrorAttribute.OnException

Controller内で起きた例外を処理する集約例外ハンドラを実装します。
Ajaxリクエストの場合はデフォルトの例外処理は何もせず、
ステータスコードを500、応答本文を例外情報を含んだJSONにして、
$.ajax().fail()で例外処理をしやすくしておきます。

using System;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Mvc;

using System.Web.Optimization;
using System.Web.Routing;
using System.Text;


namespace Jobs {
    public class GlobalHandleErrorAttribute : HandleErrorAttribute {
        public override void OnException(ExceptionContext filterContext) {
            if (filterContext == null) {
                throw new ArgumentNullException("filterContext");
            }

            // ルート・パラメータを取得
            var route = filterContext.RouteData;
          
            // 例外発生時は常にログを取っておく
            StreamWriter objSw = new StreamWriter(HttpContext.Current.Server.MapPath("~/WebApp_Err.log"), true, Encoding.GetEncoding("utf-8"));

            StringBuilder objBld = new StringBuilder();

            objBld.Append(DateTime.Now.ToString());
            objBld.Append("\t");
            objBld.Append(filterContext.HttpContext.Request.RawUrl);
            objBld.Append("\t");
            objBld.Append(route.Values["controller"].ToString());
            objBld.Append("\t");
            objBld.Append(route.Values["action"].ToString());
            objBld.Append("\t");
            objBld.Append(filterContext.Exception.StackTrace);

            objSw.WriteLine(objBld.ToString());
            objSw.Close();

            //LogUtil.LogControllerError(filterContext);

            if (filterContext.HttpContext.Request.IsAjaxRequest()) {
                /// Application_Errorは呼ばれない
                HandleAjaxRequestException(filterContext);
            }
            else {
                /// custom errorが有効でなければ
                /// base.OnException()でExceptionHandledがtrueにならないので
                /// Application_Errorも呼ばれる
                base.OnException(filterContext);
            }
        }

        private void HandleAjaxRequestException(ExceptionContext filterContext) {
            if (filterContext.ExceptionHandled) {
                return;
            }

            filterContext.Result = new JsonResult
            {
                Data = new {
                    Message = filterContext.ToString(),
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                }
            };

            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
        }
    }
}


FilterConfig.csのRegisterGlobalFilters()を編集します。
もともとあるfilters.Add(new HandleErrorAttribute());をコメントアウトして、
代わりにGlobalHandleErrorAttributeを登録します。
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    //filters.Add(new HandleErrorAttribute());
    filters.Add(new GlobalHandleErrorAttribute());
} 
 

Application_Error

Controller外で起きた例外を補足するにはApplication_Error()を定義します。
これはGlobal.asax.csを修正する

using System.IO;
using System.Text;

void Application_Error(object sender, EventArgs e) {
            StreamWriter objSw = new StreamWriter(Server.MapPath("~/WebApp_Err.log"), true, Encoding.GetEncoding("utf-8"));
            Exception objErr = Server.GetLastError();
            StringBuilder objBld = new StringBuilder();
            objBld.Append(DateTime.Now.ToString());
            objBld.Append("\t");
            objBld.Append(objErr.Source);
            objBld.Append("\t");
            objBld.Append(objErr.Message);
            objBld.Append("\t");
            objBld.Append(objErr.TargetSite.ToString());
            objSw.WriteLine(objBld.ToString());
            objSw.Close();
        }

元ネタ http://qiita.com/mocha/items/6928870b2d02d4c1ac37

文字変数のゼロ埋め

文字変数 = string.PadLeft(2, '0');  //右側ゼロ埋め


結果
1 -> 01

月及び日のゼロ埋めで使用

2017年9月14日木曜日

GetFilesで複数の拡張子を取得する方法

GetFilesで複数の拡張子を取得する方法

// C#だけど.
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;

Regex regex = new Regex(@".*\.txt|.*\.log");
IEnumerable<string> p = from file in Directory.GetFiles(@"c:\windows\") where regex.IsMatch(file) select file;
foreach (string matched in p) {
    Console.WriteLine(matched);
}

2017年9月5日火曜日

yaml_dbでエラー

yaml_dbをgemでinstallして rake db:data:dump を実行すると

rake aborted don't know how to build task と表示される。

解決方法
1. install yaml_db
   gem install yaml_db

2. rails アプリのgemfileを編集 
   gem "yaml_db" を追加

3. rakeコマンドでdumpを行う
   rake db:data:dump



2017年9月1日金曜日

excel ゼロ埋め数字

"=text(7100346,""0000000"")"

go-global サーバサイドソリューション(SBS) 仮想アプリケーション

go-global サーバサイドソリューション(SBS) 仮想アプリケーション

https://www.graphon.com/
http://www.mitsuiwa.co.jp/solutions/goglobal/faq3.html
https://www.opensquare.co.jp/seminar/document/46/1_Go-Global.pdf


同時接続 20で250万
これって、remote Appsの5ユーザー10万の方が安いと思うぞ

白地図ぬりぬり

https://n.freemap.jp/

・登録:不要
・料金:無料
・日本語対応:対応

【使い勝手】
日本地図を都道府県や、市区町村別に色分けして地図を作ることができるツールです。
パワポで資料作ったりしてると、この地域のイイ感じの地図が欲しいんだよな〜ということがあるんですが、画像検索をしても見つからなかったりするので、このツールで地域ごとに色分けしたりして地図を作ってます。

f:id:uraweb:20160828191144p:plain
上記は大阪人の僕から見た、大阪の地域別危険度です。
※白←安全  危険→赤

ご旅行の参考にでも。


看板(Kanban) & scrum勉強中

redmineのpluginで backlogsを使用して看板が使えると最近知った。

redmineの実行環境をポータブルにしたいので、rubyの実行環境をjrubyにしていたが
rumix2でポータブル環境を構築出来そうです。

usb memoryで現在、redmine + backlogsの環境を構築中です。


  ※openshiftがver2の上にredmineを構築していたが ver2廃止されるので  

usbなら、どこでも redmineが使える。

openshift ver3のdockerを使って、redmineを構築する方が簡単ぽいのですが

dockerも勉強中、dockerのアプリケーションを仮想化する発送は面白いです。

アプリの仮想をUPすれば良いので、運用は楽になりそう。

C# 全角から半角へ変換

 C# 全角から半角へ変換

元ネタ http://www.atmarkit.co.jp/fdotnet/dotnettips/1051anhankaku/anhankaku.html


using System.Text.RegularExpressions;
using Microsoft.VisualBasic;

    static string abc123ToHankaku(string s) {

            Regex re = new Regex("[0-9A-Za-z:- ]+");
            string output = re.Replace(s, myReplacer);

            return output;
        }

       static string myReplacer(Match m) {
            return Strings.StrConv(m.Value, VbStrConv.Narrow, 0);
        }