Created
July 19, 2011 22:08
-
-
Save txdv/1093861 to your computer and use it in GitHub Desktop.
Manos.MySql API proposals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void Proposal1(Context context) | |
{ | |
// Old school C# style! | |
// Has the ability to actually remove those events afterwards!. | |
MySqlConnector.Connect(context, "test", "test", "password", (stream) => { | |
var cmd = stream.Query("SELECT name, surname FROM people where city = 'Aachen'"); | |
cmd.OnRow += (row) => { | |
Console.WriteLine("{0} {1}", row.name, row[1]); | |
}); | |
cmd.OnEnd += () => { | |
Console.WriteLine("End of data!"); | |
} | |
} | |
} | |
public static void Proposal2(Context context) | |
{ | |
// Manos style | |
MySqlConnector.Connect(context, "test", "test", "password", (stream) => { | |
stream.Query("SELECT name, surname FROM people where city = 'Aachen'", (row) => { | |
Console.WriteLine("{0} {1}", row.name, row[1]); | |
}, () => { | |
Console.WriteLine("End of data!"); | |
}); | |
} | |
} | |
public static void Proposal3(Context context) | |
{ | |
// Using a return object and functions to add | |
// delegates, javascript style | |
MySqlConnector.Connect(context, "test", "test", "password", (stream) => { | |
stream.Query("SELECT name, surname FROM people where city = 'Aachen'").OnRow((row) => { | |
Console.WriteLine("{0} {1}", row.name, row[1]); | |
}).OnEnd(() => { | |
Console.WriteLine("End of data!"); | |
}); | |
} | |
} | |
public static void Proposal4(Context context) | |
{ | |
// naming arguments | |
MySqlConnector.Connect(context, "test", "test", "password", (stream) => { | |
stream.Query("SELECT name, surname FROM people where city = 'Aachen'"), | |
onRow: (row) => { | |
Console.WriteLine("{0} {1}", row.name, row[1]); | |
}, | |
onEnd: () => { | |
Console.WriteLine("End of data!"); | |
}); | |
} | |
} | |
public static void Proposal5(Context context) | |
{ | |
// This solution lets one create very imperative looking async style code | |
MySqlConnector.Connect(context, "test", "test", "password", (stream) => { | |
stream.Query("SELECT name, surname FROM people where city = 'Aachen'"), | |
Read: (rows) => { | |
while (!rows.End) { | |
Row row; | |
while (!row.Read(out row))) { | |
// the driver will fire up this event only | |
// if it has some stuff to read again | |
yield return false; | |
} | |
Console.WriteLine("{0} {1}", row.name, row[1]); | |
} | |
Console.WriteLine("End of data!"); | |
// indicate that we finished reading, no need to | |
// call the enumerator again | |
yield return true; | |
}); | |
} | |
} | |
public static void Proposal6(Context context) | |
{ | |
// Don't know if it is possible, since I have not much experience | |
// with await | |
MySqlConnector.Connect(context, "test", "test", "password", (stream) => { | |
stream.Query("SELECT name, surname FROM people where city = 'Aachen'"), | |
Read: (rows) => { | |
Row row; | |
while ((row = await rows.Read()) != null) { | |
Console.WriteLine("{0} {1}", row.name, row[1]); | |
} | |
Console.WriteLine("End of data!"); | |
}); | |
} | |
} | |
public static void AllInOne(Context context) | |
{ | |
// It is possible to use them all! | |
MySqlConnector.Connect(context, "test", "test", "password", (stream) => { | |
var cmd = stream.Query("SELECT name, surname FROM people where city = 'Aachen'", (row) => { | |
Console.WriteLine("{0} {1}", row.name, row[1]); | |
}, | |
onEnd: () => { | |
Console.WriteLine("End of data!"); | |
}).OnEnd(() => { | |
Console.WriteLine("Yeaaahhaa!"); | |
}).OnRow((row) => { | |
Console.WriteLine("Nice, isn't it?"); | |
}); | |
cmd.EndEvent += () => { | |
Console.WriteLine("Another event!"); | |
}; | |
cmd.RowEvent += () => { | |
Console.WriteLine("yeah, another one..."); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment