Advertisement
Guest User

Untitled

a guest
Nov 21st, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. // Import the core node modules.
  2. var mysql = require( "mysql" );
  3.  
  4. // Create the connection to your database. This time, we're going to use our own custom
  5. // type casting function to manually coerce some of the columns.
  6. var db = mysql.createConnection({
  7. host: "localhost",
  8. user: "root",
  9. password: "",
  10. database: "testing",
  11. typeCast: function castField( field, useDefaultTypeCasting ) {
  12.  
  13. // We only want to cast bit fields that have a single-bit in them. If the field
  14. // has more than one bit, then we cannot assume it is supposed to be a Boolean.
  15. if ( ( field.type === "BIT" ) && ( field.length === 1 ) ) {
  16.  
  17. var bytes = field.buffer();
  18.  
  19. // A Buffer in Node represents a collection of 8-bit unsigned integers.
  20. // Therefore, our single "bit field" comes back as the bits '0000 0001',
  21. // which is equivalent to the number 1.
  22. return( bytes[ 0 ] === 1 );
  23.  
  24. }
  25.  
  26. return( useDefaultTypeCasting() );
  27.  
  28. }
  29. });
  30.  
  31. // Gather records that we know contain a BIT column.
  32. db.query(
  33. `
  34. SELECT
  35. id,
  36. name,
  37. isBFF -- This is a BIT field.
  38. FROM
  39. friend
  40. `,
  41. function handleResults( error, rows ) {
  42.  
  43. console.log( "Results:" );
  44. console.log( rows );
  45.  
  46. }
  47. );
  48.  
  49. // Gracefully close the connection to the database (queued queries will still run).
  50. db.end();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement